Repository navigation
perf: size the ConvolverState FFT frame from the mask, not the native array - #699
Merged
Merged
Conversation
The smallest centred, parity-preserving shape that holds every unmasked pixel a kernel half-width plus a buffer from the edge. Taken from PyAutoArray#675 (commits a7f9fbc..bbd126c) with its unit tests; the Imaging.apply_mask and dataset:trim_to_mask changes from that PR are not brought over. Co-authored-by: samlange04 <80762641+samlange04@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The frame was padded up from the native array, so a small mask on a large stamp ran its FFTs over the whole stamp. It is now the larger of full_shape and trimmed_shape_for_kernel (the unmasked and blurring region plus a kernel half-width and a one-pixel buffer), capped at the frame the native array would be padded to, so it is never larger than before. The internal mask and blurring mask are cropped to it about their central pixel, which only removes masked pixels and keeps the slim ordering; if a crop ever lost a pixel the state falls back to the padded frame. Outputs stay on the caller's mask, so dataset shapes, fit images, image meshes and results are unchanged. Tests: a small mask on a large stamp convolves (image and mapping matrix; numpy, JAX FFT and JAX real space; odd, even and mixed-parity frames; over sampled) identically to the same mask on a tight stamp and to the old frame, and the frame shrinks and never grows. Co-authored-by: samlange04 <80762641+samlange04@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The unittest-nojax leg failed with ModuleNotFoundError: the new tests imported jax.numpy unconditionally. The numpy modes still run there; the jax modes skip. Co-Authored-By: Claude Opus 5.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.
Summary
ConvolverStatepadded its FFT frame up from the native array, so a small mask on a large stamp ran every FFT over the whole stamp. The frame is now sized from the mask, using @samlange04'strimmed_shape_for_kernelfrom #675: the unmasked (and blurring) region plus a kernel half-width and a one-pixel buffer, centred on the mask's central pixel, capped so it is never larger than before. The internal mask and blurring mask are cropped to that frame; cropping only removes masked pixels, so the slim ordering of image and blurring pixels is unchanged. If a crop ever lost a pixel, the state falls back to the old frame.Every output stays on the caller's mask, so dataset shapes, fit images, image meshes and saved results do not change. There is no config key; this is always on.
This replaces #675's approach, which trimmed the
Imaging.apply_masknative arrays and changed 6 workspace smoke results (including a Hilbert image-mesh frame dependence). Onlytrimmed_shape_for_kerneland its tests come over from #675; itsImaging.apply_maskanddataset:trim_to_maskchanges do not. Co-authored with @samlange04 (discussion: PyAutoLabs/.github#43).Closes #698. Supersedes #675.
API Changes
Adds one utility,
mask_2d_util.trimmed_shape_for_kernel.ConvolverState.fft_shapeand the internalConvolverState.mask/blurring_maskcan now be smaller than the native array. Convolution results are unchanged up to FFT rounding (around 1e-12).See full details below.
Test Plan
trimmed_shape_for_kerneltests)convolve_over_sample_size=2). The frame shrinks and never grows; a mask near the edge pads as before; a crop that would lose pixels falls back.autolens_workspace_test/scripts/imaging/jax_likelihood/delaunay.py(full dataset, JAX): vmap log likelihood −532.29584351 on both main and the branch. NumPyfit.log_likelihood−44.32005541738977 (main) vs −44.32005541739147 (branch), a 1.7e-12 difference.pyauto-heart smoke, task worktree root): 140 passed, 0 failed. autolens_workspace 42 scripts + 2 notebooks; autogalaxy_workspace 14 + 2; autolens_workspace_test 39, includingimaging/jax_likelihood/delaunay.pyanddelaunay_mge.pywith their pinned likelihoods; HowToLens 41. These are the suites where Imaging.apply_mask: trim the native arrays to the mask plus a PSF half-width (trim_to_mask) #675 had 6 failures (onlydelaunay.pyis named on the thread); none fail here.fft_shape(400, 400) → (180, 180);convolved_mapping_matrix_from2309 ms → 531 ms median per call (4.3×); max |new − old| = 5e-13.Heart dev-ship gate (
readiness --dev --repos PyAutoArray): GREEN, no blocking reasons. Shown, non-blocking: canonical checkouts behind origin (Nerves, Fit, Array, Galaxy, Lens, CTI), one uncommitted PyAutoLens source change in the canonical checkout, and manifest drift in generated hooks, the tenant firewall and workspace checkouts.Full API Changes (for automation & release notes)
Added
autoarray.mask.mask_2d_util.trimmed_shape_for_kernel(mask_2d, kernel_shape_native, buffer=1): the smallest centred, parity-preserving shape that keeps every unmasked pixel a kernel half-width plusbufferfrom the edge.Changed Behaviour
ConvolverState.fft_shapeis nownext_fast_len(max(full_shape, min(trimmed_shape, padded_shape)))instead ofnext_fast_len(max(full_shape, padded_shape)), wherepadded_shape = required_shape_for_kernel(...). It is never larger than before.ConvolverState.maskandConvolverState.blurring_maskare now resized (padded or cropped, centred) tofft_shape, so they can be smaller than the native array.ConvolverState.source_maskis unchanged.Migration
Generated by the PyAutoLabs agent workflow.
🤖 Generated with Claude Code