Skip to content

perf: reuse the JAX compile in ImagingSparseOperator curvature blocks - #679

Closed
Jammy2211 wants to merge 1 commit into
mainfrom
ccr-eef5e2b2-t0z1tp
Closed

Jammy2211 wants to merge 1 commit into
mainfrom
ccr-eef5e2b2-t0z1tp

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Closes #678.

Why

ImagingSparseOperator.curvature_matrix_diag_from / curvature_matrix_off_diag_from ran an eager lax.fori_loop whose body was a new closure on every call, so JAX retraced and recompiled every time. HowToLens chapter_3 tutorial_6_adaptive_reconstruction.py, the slowest smoke script on the Heart board, makes 118 calls and triggered 132 backend compiles of about 0.1s each. That is below JAX's persistent-cache threshold, so the CI compile cache never helped.

Change

  • Both block loops are now module-level jax.jit kernels with their sizes static (S/S0/S1, batch, grid and FFT shapes). They are built lazily behind lru_cache, so importing autoarray still never imports JAX. Same-size calls, such as a regularization scan over a fixed mesh, reuse one compile, and under an outer jit the kernel is inlined.
  • Method signatures are unchanged. Array operands are unwrapped with .array, because inverse_variances_native can be an Array2D in the NumPy path.
  • There is no cross-fit curvature reuse or preloads (see autoarray/preloads/abstract.py for why that stays explicit).

Verification

  • Numerics: old and new outputs are bit-identical (diag 300×300 and 129×129, off-diag 129×77, random triplets on a 150×150 grid).

  • test_autoarray: 2297 passed, 4 xfailed. New tests:

    • both blocks match a dense AᵀWA reference, including a padded tail;
    • repeated same-shape calls add no compile, with the test asserting at least one real compile so the check can't pass vacuously.
  • JAX parity (autolens_workspace_test): all pass, including jit(fit_from) matching NumPy:

    • misc/jax_assertions/fit_imaging_sparse_operator.py and sparse_operators.py;
    • imaging/jax_likelihood/ rectangular, _rtu, _dspl (off-diag), _dspl_rtu, _mge and delaunay.
  • Timing: HowToLens tutorial_6 (py3.12, smoke env plus full_datasets), interleaved A/B on one machine:

    run 1 run 2 curvature calls
    main 93.2s 90.9s 63.4s / 62.3s
    this branch 78.2s 76.1s 48.3s / 46.1s

    Every printed log evidence is unchanged (Overlay 15907.596, Hilbert 16432.066, best 16687.265538008818).

Coordination

PyAutoArray is also claimed by the active magnification-maps-wiring task (PyAutoLens#818, a branch local to the dev box). The maintainer approved proceeding for this single-file change on 2026-11-08. Merge order is a human /prm call.


Generated by Claude Code

curvature_matrix_diag_from and curvature_matrix_off_diag_from ran an
eager lax.fori_loop whose body was a fresh closure on every call, so JAX
retraced and recompiled every time. HowToLens chapter_3 tutorial_6, the
slowest smoke script, makes 118 calls and triggered 132 backend compiles
of about 0.1s each. That is below JAX's persistent-cache threshold, so the
CI compile cache did not help.

Both block loops are now module-level jax.jit kernels with their sizes
static, built lazily behind lru_cache, so importing autoarray still never
imports JAX. Same-size calls, such as a regularization scan over a fixed
mesh, reuse one compile, and under an outer jit the kernel is inlined.
The method signatures are unchanged. Array operands are unwrapped with
.array, since inverse_variances_native can be an Array2D in the NumPy
path.

Numerics: old and new outputs are bit-identical for diag 300x300 and
129x129 and off-diag 129x77 with random triplets on a 150x150 grid.

Verification:
- test_autoarray: 2297 passed, 4 xfailed. New tests check both blocks
  against a dense A^T W A reference (padded tail exercised) and that
  repeated same-shape calls add no compile.
- autolens_workspace_test JAX parity: fit_imaging_sparse_operator,
  sparse_operators, imaging/jax_likelihood rectangular, _rtu, _dspl
  (off-diag), _dspl_rtu, _mge and delaunay all pass.
- HowToLens tutorial_6 (py3.12, smoke env plus full_datasets),
  interleaved A/B: old 93.2s / 90.9s, new 78.2s / 76.1s. Curvature-call
  time went from 63.4 / 62.3s to 48.3 / 46.1s. All printed log
  evidences are unchanged.

Closes #678

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016nhQ8McPUvDThp9s9JAiZz

Copy link
Copy Markdown
Collaborator Author

Closing without merging, at the maintainer's request (2026-11-08). The CI motivation went away: the HowTo chapter-3 tutorials whose repeated curvature calls drove this now run on small datasets in smoke (HowToLens#131, HowToGalaxy#105). The measurements and the bit-identical check stay here and on #678 for reference. The branch is kept, so it can be reopened if wanted.


Generated by Claude Code

@Jammy2211 Jammy2211 closed this Nov 8, 2026
@Jammy2211
Jammy2211 deleted the ccr-eef5e2b2-t0z1tp branch November 25, 2026 20:10
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: reuse the JAX compile in ImagingSparseOperator curvature blocks

2 participants