Skip to content

perf(lens_calc): cache (f, ZeroSolver) for zero_contour critical curves - #434

Merged
Jammy2211 merged 1 commit into
mainfrom
feature/fast-viz-zero-contour-perf
May 21, 2026
Merged

Jammy2211 merged 1 commit into
mainfrom
feature/fast-viz-zero-contour-perf

Conversation

@Jammy2211

@Jammy2211 Jammy2211 commented May 21, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes the closure cache-busting bug that made zero_contour-based critical curves and Einstein radii ~150× slower than they should be, blocking them from being a viable default for JIT'd likelihood functions.

LensCalc._critical_curve_list_via_zero_contour rebuilt f = self._make_eigen_fn(...) and solver = ZeroSolver(...) on every invocation. Since JAX caches compiled functions by callable identity, a fresh f each call meant the ZeroSolver.zero_contour_finder JIT-compile fired anew every time — ~10 s per call instead of the expected ~66 ms warm. With (f, ZeroSolver) cached on the LensCalc instance keyed on (kind, pixel_scales, tol, max_newton), repeat calls reuse JAX's compile cache as expected.

This is the perf prerequisite for any future flip of the plotter default to zero_contour and for the planned Euclid latent migration off the marching-squares path. See PyAutoPrompt/z_features/fast_visualization.md, Phase A′.

Companion PR in PyAutoLens (Jammy2211/PyAutoLens#527) tightens the broad except that silently swallowed this class of regression on production runs.

Closes #433 (alongside the PyAutoLens companion).

API Changes

LensCalc instances now carry a private _zero_contour_cache dict. No public API surface changes — same method names, same signatures, same return values. Behavioural change: _critical_curve_list_via_zero_contour reuses its closure and ZeroSolver across repeat calls with the same parameters.

See full details below.

Test Plan

  • New unit test test_autogalaxy/operate/test_deflections.py::test__zero_contour_cache__starts_empty_and_is_per_instance — confirms each LensCalc gets a fresh cache and that mutations don't leak across instances.
  • pytest test_autogalaxy/operate/ — 41 passed locally.
  • CPU benchmark on SIE + circular source: tangential warm 10300 ms → 67 ms, Einstein radius warm 10300 ms → 68 ms, radial warm 10303 ms → 380 ms.
  • euclid_strong_lens_modeling_pipeline smoke (6 scripts) — all PASS, posted at perf: cache zero_contour solver + viz sanity net (fast_viz A') #433 (comment).
  • autolens_workspace_test/scripts/imaging/modeling_visualization_jit.py __Visualization Sanity__ block — passes (correctness + warm call 79 ms).
Full API Changes (for automation & release notes)

Removed

  • None.

Added

  • LensCalc._zero_contour_cache: dict — private instance attribute, lazy-populated by _critical_curve_list_via_zero_contour. Maps (kind, pixel_scales, tol, max_newton) → (f, ZeroSolver).

Changed Signature

  • None.

Changed Behaviour

  • LensCalc._critical_curve_list_via_zero_contour(...) — reuses cached (f, ZeroSolver) across calls with the same (kind, pixel_scales, tol, max_newton) instead of constructing fresh ones each invocation. Identical return values; warm-call latency drops from ~10 s to ~66 ms on CPU.

Migration

  • None required — transparent perf fix.

🤖 Generated with Claude Code

Maps (kind, pixel_scales, tol, max_newton) -> (f, ZeroSolver) on each
LensCalc instance so repeat calls reuse JAX's compiled function cache.
Without this, _critical_curve_list_via_zero_contour built a fresh
closure and solver on every invocation, busting the JIT cache and
paying the full ~10s compile cost every time.

CPU benchmark on an SIE + circular source:
- tangential warm: 10300 ms -> 67 ms
- einstein_radius warm: 10300 ms -> 68 ms
- radial warm: 10303 ms -> 380 ms

This is the perf prerequisite for any future flip of the plotter
default to zero_contour and for migrating Euclid-style latents off
the marching_squares path. Closes #433 alongside the PyAutoLens
companion PR.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@Jammy2211

Copy link
Copy Markdown
Collaborator Author

Workspace PR: PyAutoLabs/autolens_workspace_test#111

@Jammy2211
Jammy2211 merged commit a9a887a into main May 21, 2026
6 checks passed
@Jammy2211
Jammy2211 deleted the feature/fast-viz-zero-contour-perf branch May 21, 2026 17:46
@Jammy2211 Jammy2211 removed the pending-release PR queued for the next release build label Sep 4, 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: cache zero_contour solver + viz sanity net (fast_viz A')

1 participant