refactor: one kernels module instead of five copies - #61
Open
Hendrik-code wants to merge 1 commit into
Open
Conversation
Four independent 3D Gaussian implementations, three bias fields and two copies of the Laplace/Scharr constant tables lived in five modules: gpu/contrast.py, gpu/fromSeg.py, gpu/domain_transfer.py, synthseg/functional.py, cpu/contrast.py. Each carried some version of a comment saying it was kept local so its module would stay self-contained. They were not equivalent, and that is what let two of them go wrong unnoticed -- the uncentred Gaussian and the malformed 2D Scharr x-kernel, both corrected earlier in this series. Each had to be fixed on its own because there was no shared implementation to fix instead. smauglab/transforms/kernels.py is that implementation now. The consolidation is not bit-for-bit at the two blur call sites, deliberately: * Radius is ceil(3*sigma). domain_transfer and fromSeg used round(3*sigma), which is never wider, so their kernels may be one tap larger. * Padding is reflect everywhere. domain_transfer used replicate; fromSeg relied on conv3d's implicit zero padding, which pulls the volume border towards zero. That is the one difference here that was wrong rather than merely different. Everything else is the same arithmetic as the copy it replaces: the dense Gaussian, both derivative tables, and the bias field, whose synthseg and domain_transfer copies were already line-for-line identical. The local wrappers stay where callers expect them -- _gaussian_blur_3d in fromSeg.py still clamps to [0, 1], _random_bias_field3d in domain_transfer.py still returns a bare volume rather than [B, C, D, H, W] -- so this is a change of implementation, not of interface. unit_tests/test_kernels.py covers the merged behaviour and the equivalences that make it one implementation: that synthseg and domain_transfer now produce the same bias field from the same seed, and that the CPU transform hands out the shared tables. test_kernel_correctness.py follows the Gaussian to its new home. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #60. First of the three deduplication PRs; no new behaviour, but see the caveat below — it is not bit-for-bit everywhere.
What was duplicated
Four independent 3-D Gaussian implementations, three bias fields and two copies of the Laplace/Scharr constant tables, across five modules:
gpu/contrast.pygpu/fromSeg.pygpu/domain_transfer.pysynthseg/functional.pycpu/contrast.pyEach carried some version of a comment saying it was kept local so its module would stay self-contained. The
synthseganddomain_transferbias fields were line-for-line identical.That divergence is the point. It is what let two of the copies go wrong unnoticed — the uncentred Gaussian (#58) and the malformed 2-D Scharr x-kernel (#58) — and why each had to be fixed on its own rather than in one place.
smauglab/transforms/kernels.pyis that one place now.Not bit-for-bit, deliberately
Two blur call sites change slightly:
ceil(3*sigma).domain_transferandfromSegusedround(3*sigma), which is never wider, so their kernels may now be one tap larger.reflecteverywhere.domain_transferusedreplicate;fromSegrelied onconv3d's implicit zero padding, which pulls the volume border towards 0. That is the one difference here that was wrong rather than merely different.Everything else is the same arithmetic as the copy it replaces.
Interfaces are unchanged
The local wrappers stay where their callers expect them —
_gaussian_blur_3dinfromSeg.pystill clamps to[0, 1],_random_bias_field3dindomain_transfer.pystill returns a bare[D, H, W]volume rather than[B, C, D, H, W]. This is a change of implementation, not of interface.Testing
New
unit_tests/test_kernels.py(12 tests) covers the merged behaviour and, more usefully, the equivalences that make this one implementation rather than a sixth copy:test_synthseg_and_domain_transfer_now_share_one_implementation— same seed, same fieldtest_the_kernels_are_shared_by_both_backends— the CPU transform hands out the shared tablestest_a_constant_volume_survives_the_blur— the reflect-padding property that the zero-padded copy failedtest_kernel_correctness.pyfrom #58 follows the Gaussian to its new home; its 1-D assertions move onto the sigma-derivedgaussian_kernel1d, since the fixed-size helper is now internal togaussian_kernel3d.Net: −209 / +56 across the five modules, plus the 265-line shared module.
Full suite: 82 passed, 169 subtests.
ruffandmypyclean.