Skip to content

perf: size the ConvolverState FFT frame from the mask, not the native array #698

Closed

Description

@Jammy2211

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_kernel and 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 the Imaging.apply_mask / dataset:trim_to_mask changes.
  • 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_mask caching).
  • 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

  1. autoarray/mask/mask_2d_util.py: add trimmed_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 in test_autoarray/mask/test_mask_2d_util.py.
  2. autoarray/operators/convolver.py ConvolverState.__init__: replace min_blur_shape = required_shape_for_kernel(...) (never below native) with trimmed_shape_for_kernel(same mask, kernel, buffer=1) capped element-wise at the old required_shape_for_kernel value; fft_shape = next_fast_len(max(full_shape, capped), real=True); crop self.mask / self.blurring_mask to fft_shape with resized_from (centred). full_shape still guarantees linear convolution. Guard: if the crop would drop any unmasked or blurring pixel, fall back to today's frame.
  3. 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_kernel preloaded 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.
  4. Unit tests in test_autoarray/operators/test_convolver.py.
  5. 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 — ConvolverState frame sizing
  • autoarray/mask/mask_2d_util.py — trimmed_shape_for_kernel
  • test_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:

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

  1. Jammy2211 commented on Nov 17, 2026

    @Jammy2211
    CollaboratorAuthor

    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)

    Workspace Passed Failed Total
    autolens_workspace (scripts + notebooks) 44 0 44
    autogalaxy_workspace (scripts + notebooks) 16 0 16
    autolens_workspace_test 39 0 39
    HowToLens 41 0 41
    • Unit tests: PyAutoArray 2458 passed; PyAutoGalaxy 1486 passed; PyAutoLens 1021 passed, 1 xfailed.
    • delaunay.py likelihood: −532.29584351 on both main and the branch.
    • CPU benchmark on Sam's geometry: fft_shape (400, 400) → (180, 180); 900-column convolved_mapping_matrix_from 2309 ms → 531 ms median.
  2. Jammy2211 commented on Nov 17, 2026

    @Jammy2211
    CollaboratorAuthor

    Shipped

    Merged in #699 (merge commit e81c6e1, 2026-11-17). Not yet on PyPI: the PR carries the pending-release label until the next release.

    What changed

    • ConvolverState now 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 internal mask / blurring_mask are cropped to that frame; if a crop would lose a pixel it falls back to the old frame.
    • New utility 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).
    • All outputs stay on the caller's mask, so dataset shapes, fit images, image meshes and saved results do not change. No config key; always on.
    • Final commit 88b391d is test-only: the JAX modes of the new frame-equality tests are skipped when JAX is not installed.

    Evidence

    • PyAutoArray unit suite: 2458 passed. PyAutoGalaxy 1486 passed and PyAutoLens 1021 passed (1 xfailed) against the branch.
    • Smoke: 140 passed, 0 failed (autolens_workspace, autogalaxy_workspace, autolens_workspace_test, HowToLens).
    • Likelihood parity, autolens_workspace_test/scripts/imaging/jax_likelihood/delaunay.py: −532.29584351 on main and on the branch.
    • CPU benchmark on Sam's geometry: fft_shape 400×400 → 180×180; convolved_mapping_matrix_from 2309 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions