Repository navigation
perf: size the ConvolverState FFT frame from the mask, not the native array #698
Description
Overview
ConvolverState sizes its internal FFT frame by padding up from the native array shape, so a small mask on a large stamp pays for an FFT over the whole stamp (Sam's JWST case: a 1.5" mask on a 400 px stamp runs 400x400 FFTs). This sizes the frame from the mask instead, using @samlange04's trimmed_shape_for_kernel from PyAutoArray#675, while every output stays on the caller's mask, so dataset shapes, fit images, image meshes and saved results do not change.
Community thread: PyAutoLabs/.github#43. Supersedes PyAutoArray#675 (whose approach trimmed Imaging.apply_mask native arrays and changed 6 workspace smoke results, including a Hilbert image-mesh frame dependence).
Plan
- Bring over
mask_2d_util.trimmed_shape_for_kerneland its unit tests from Imaging.apply_mask: trim the native arrays to the mask plus a PSF half-width (trim_to_mask) #675 (co-authored by @samlange04). Not theImaging.apply_mask/dataset:trim_to_maskchanges. - In
ConvolverState, size the FFT frame from the trimmed shape of the mask (or mask plus explicit blurring mask), capped so it is never larger than today's frame, and crop the internal mask and blurring mask to it (centred). - Keep linear (no wrap-around) convolution, the slim ordering of image and blurring pixels, and all outputs on the caller's mask. No config key; always on.
- Audit every consumer of the state's frame (FFT, real space, numpy, oversampled, mapping-matrix, JAX static shapes,
is_for_maskcaching). - Tests: small mask on a large stamp vs the same mask on a tight stamp give the same convolved image and mapping matrix (FFT, real space, odd/even, oversampled, blurring region); the frame shrinks and never grows.
- Witness: PyAutoArray/PyAutoGalaxy/PyAutoLens unit suites; autolens, autogalaxy, autolens_workspace_test (incl.
scripts/imaging/jax_likelihood/delaunay.py) and HowToLens smoke with pinned likelihoods unchanged; a short CPU benchmark on Sam's geometry.
Detailed implementation plan
Affected Repositories
- PyAutoArray (primary)
Branch Survey
| Repository | Current Branch | Dirty? |
|---|---|---|
| ./PyAutoArray | main | shared with galaxy-duplicate-pytree-registration (touches only autoarray/abstract_ndarray.py; human OK 2026-11-17) |
Suggested branch: feature/convolver-fft-frame-from-mask
Implementation Steps
autoarray/mask/mask_2d_util.py: addtrimmed_shape_for_kernel(mask_2d, kernel_shape_native, buffer=1)as in Imaging.apply_mask: trim the native arrays to the mask plus a PSF half-width (trim_to_mask) #675 (commits a7f9fbc..bbd126c); tests intest_autoarray/mask/test_mask_2d_util.py.autoarray/operators/convolver.pyConvolverState.__init__: replacemin_blur_shape = required_shape_for_kernel(...)(never below native) withtrimmed_shape_for_kernel(same mask, kernel, buffer=1)capped element-wise at the oldrequired_shape_for_kernelvalue;fft_shape = next_fast_len(max(full_shape, capped), real=True); cropself.mask/self.blurring_masktofft_shapewithresized_from(centred).full_shapestill guarantees linear convolution. Guard: if the crop would drop any unmasked or blurring pixel, fall back to today's frame.- Audit consumers:
convolved_image_from(blurring image, mixed precision),convolved_mapping_matrix_from, real-space JAX and numpy variants, oversampled paths (sub_slim_to_fine_slim,blurring_sub_slim_to_fine_slim,image_mask),reversed_kernelpreloaded state,is_for_mask, and the Galaxy batched numpy fast path. The sparse operator (inversion_imaging_util.py) sizes its own frame and is untouched. - Unit tests in
test_autoarray/operators/test_convolver.py. - Smoke + likelihood parity vs main; CPU benchmark (400 px stamp, 0.03"/px, 1.5" mask, 37x37 PSF, 900-column mapping matrix).
Key Files
autoarray/operators/convolver.py—ConvolverStateframe sizingautoarray/mask/mask_2d_util.py—trimmed_shape_for_kerneltest_autoarray/operators/test_convolver.py,test_autoarray/mask/test_mask_2d_util.py
Original Prompt
Click to expand starting prompt
ConvolverState: size the FFT frame from the mask, not the native array (Sam's trim, internal)
Type: feature
Target: autoarray
Repos:
- PyAutoArray
Difficulty: medium
Autonomy: supervised
Priority: high
Consequence: judge
Community: Imaging.apply_mask: trim the native arrays to the mask plus a PSF half-width, so the FFT frame follows the mask rather than the stamp .github#43
Filed: 2026-11-17
Agreed by the human in the /community chat on 2026-11-17, and promised to @samlange04 on
#43 (PyAutoLabs/.github#43 (comment)):
we implement it on our side, building on his PR, credit him as co-author
(Co-authored-by: samlange04), ask him to re-run his GPU benchmark, then close
PyAutoArray#675 in favour of ours.
What
ConvolverState (autoarray/operators/convolver.py, ~lines 97-155) sets
full_shape = mask_shape + kernel - 1 from the native mask shape and only ever pads
(required_shape_for_kernel). Size it instead from
mask_2d_util.trimmed_shape_for_kernel(mask, kernel_shape_native, buffer=1) (take that
function and its tests from PR #675 branch samlange04:..., commits a7f9fbc..bbd126c)
and crop/pad the internal self.mask / self.blurring_mask to that frame
(resized_from is centred and parity-preserving, so slim ordering is unchanged). Never make
the frame larger than today's. All outputs stay on the caller's mask
(Array2D(values=..., mask=image.mask)), so dataset shapes, fit images, image meshes and
saved results do not change: no config key, always on.
Check every path that uses state.mask / fft_shape: convolved_image_from (incl. the
blurring image), convolved_mapping_matrix_from, the real-space and numpy variants, the
over-sampled convolve paths (convolve_over_sample_size > 1), the sparse operator setup,
is_for_mask caching, and JAX jit/vmap static shapes.
Witness
- Unit: convolved image / mapping matrix identical (to FFT rounding) between a small mask on a
large stamp and the same mask on a tight stamp; blurring region preserved; even/odd frames. - Smoke: autolens, autogalaxy, autolens_test, howtolens all green, and pinned likelihoods
unchanged (the PR Imaging.apply_mask: trim the native arrays to the mask plus a PSF half-width (trim_to_mask) #675 approach broke 6 scripts; this one must break none). - Benchmark: Sam's JWST geometry (400 px stamp, 0.03"/px, 1.5" mask, 37x37 PSF, 30x30 mesh)
FFT frame 400x400 -> ~180x180, ~4x per call on GPU (his A6000/4090 table on Feature/typing #43).
Activity
Jammy2211 commentedon Nov 17, 2026
Library PR created: #699
Branch
feature/convolver-fft-frame-from-mask(head e5068e5). Tier: judge, so the merge is the human's (/prm).Smoke results (branch PyAutoArray,
pyauto-heart smoke)delaunay.pylikelihood: −532.29584351 on both main and the branch.fft_shape(400, 400) → (180, 180); 900-columnconvolved_mapping_matrix_from2309 ms → 531 ms median.Jammy2211 commentedon Nov 17, 2026
Shipped
Merged in #699 (merge commit e81c6e1, 2026-11-17). Not yet on PyPI: the PR carries the
pending-releaselabel until the next release.What changed
ConvolverStatenow sizes its FFT frame from the mask, not the native array: the unmasked and blurring region plus a kernel half-width and a one-pixel buffer, centred, and never larger than before. The internalmask/blurring_maskare cropped to that frame; if a crop would lose a pixel it falls back to the old frame.mask_2d_util.trimmed_shape_for_kernel, taken with its tests from @samlange04's Imaging.apply_mask: trim the native arrays to the mask plus a PSF half-width (trim_to_mask) #675 (co-authored).Evidence
autolens_workspace_test/scripts/imaging/jax_likelihood/delaunay.py: −532.29584351 on main and on the branch.fft_shape400×400 → 180×180;convolved_mapping_matrix_from2309 ms → 531 ms median per call (4.3×).Supersedes #675. @samlange04 has been asked on PyAutoLabs/.github#43 to re-run his GPU benchmark once this is released.