Skip to content

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

Merged
Jammy2211 merged 3 commits into
mainfrom
feature/convolver-fft-frame-from-mask
Nov 17, 2026
Merged

Jammy2211 merged 3 commits into
mainfrom
feature/convolver-fft-frame-from-mask

Conversation

@Jammy2211

@Jammy2211 Jammy2211 commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

ConvolverState padded 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's trimmed_shape_for_kernel from #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_mask native arrays and changed 6 workspace smoke results (including a Hilbert image-mesh frame dependence). Only trimmed_shape_for_kernel and its tests come over from #675; its Imaging.apply_mask and dataset:trim_to_mask changes 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_shape and the internal ConvolverState.mask / blurring_mask can now be smaller than the native array. Convolution results are unchanged up to FFT rounding (around 1e-12).
See full details below.

Test Plan

  • PyAutoArray unit suite: 2458 passed (36 new frame tests, 4 new trimmed_shape_for_kernel tests)
  • PyAutoGalaxy unit suite against this branch: 1486 passed. PyAutoLens: 1021 passed, 1 xfailed
  • New tests: a small mask on a large stamp convolves the same as the same mask on a tight stamp, and the same as the old frame. Covers image and mapping matrix; numpy, JAX FFT and JAX real space; odd, even and mixed-parity frames, including frames whose parity differs from the native array; over-sampled (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.
  • Likelihood parity, autolens_workspace_test/scripts/imaging/jax_likelihood/delaunay.py (full dataset, JAX): vmap log likelihood −532.29584351 on both main and the branch. NumPy fit.log_likelihood −44.32005541738977 (main) vs −44.32005541739147 (branch), a 1.7e-12 difference.
  • Smoke (pyauto-heart smoke, task worktree root): 140 passed, 0 failed. autolens_workspace 42 scripts + 2 notebooks; autogalaxy_workspace 14 + 2; autolens_workspace_test 39, including imaging/jax_likelihood/delaunay.py and delaunay_mge.py with 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 (only delaunay.py is named on the thread); none fail here.
  • CPU benchmark on Sam's geometry (400×400 stamp, 0.03"/px, 1.5" circular mask, 37×37 PSF, 7860 image + 8496 blurring pixels, 900-column mapping matrix, JAX FFT, jitted, fp64): fft_shape (400, 400) → (180, 180); convolved_mapping_matrix_from 2309 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 plus buffer from the edge.

Changed Behaviour

  • ConvolverState.fft_shape is now next_fast_len(max(full_shape, min(trimmed_shape, padded_shape))) instead of next_fast_len(max(full_shape, padded_shape)), where padded_shape = required_shape_for_kernel(...). It is never larger than before.
  • ConvolverState.mask and ConvolverState.blurring_mask are now resized (padded or cropped, centred) to fft_shape, so they can be smaller than the native array. ConvolverState.source_mask is unchanged.
  • Convolved images and mapping matrices are unchanged up to FFT rounding and are still returned on the input mask.

Migration

  • None.

Generated by the PyAutoLabs agent workflow.

🤖 Generated with Claude Code

Jammy2211 and others added 2 commits November 17, 2026 14:12
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>
@Jammy2211
Jammy2211 merged commit e81c6e1 into main Nov 17, 2026
4 checks passed
@Jammy2211
Jammy2211 deleted the feature/convolver-fft-frame-from-mask branch November 17, 2026 15:18
@pyautolabs-bot pyautolabs-bot Bot removed the pending-release PR queued for the next release build label Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant