Skip to content

test: JAX finite-difference gradient correctness suite (jax_grad)#157

Merged
Jammy2211 merged 2 commits into
mainfrom
feature/jax-autodiff-gradients-audit
Jul 9, 2026
Merged

test: JAX finite-difference gradient correctness suite (jax_grad)#157
Jammy2211 merged 2 commits into
mainfrom
feature/jax-autodiff-gradients-audit

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Part of the JAX autodiff gradients audit — PyAutoLabs/autolens_workspace_developer#87.

Upgrades scripts/jax_grad/ from finiteness-only checks to autodiff-vs-finite-difference correctness, and extends coverage to every differentiable PyAutoLens likelihood.

Scripts Changed

  • scripts/jax_grad/util.py (new) — shared FD comparison helpers: central differences with per-parameter scaled steps, autodiff on the eager likelihood + FD sweep on a jitted one guarded by an eager-vs-jit base-point check (anti-pure_callback-const-folding), explicit skip-list semantics.
  • scripts/jax_grad/imaging_lp.py — standard lp.Sersic + lp_linear.Sersic variants, FD-validated (rel err ≤ 1e-5 / ≤ 3e-9). Evaluation point anchored near simulator truth so the positive-only NNLS keeps the source live; asserts the source block is live. Fixes the stale imaging/simple dataset pointer that broke the script on clean checkouts.
  • scripts/jax_grad/imaging_mge.py — MGE source FD-validated through the linear inversion (rel err ≤ 5e-7). Fixes the stale imaging/source_complex pointer.
  • scripts/jax_grad/imaging_pixelization.py (new) — RectangularUniform: strict FD assertions on all 14 params (AD = FD to 7 s.f.). RectangularAdaptDensity (os_pix=1): lens-light FD assertions + staircase-invariance assertions (LL bit-identical under ≤1e-6 mass shifts — autodiff's zero mass gradients are the correct a.e. derivative; fails loudly if mesh differentiability ever changes).
  • scripts/jax_grad/point_source.py (new) — FitPositionsSource source-plane χ² FD-validated (rel err ≤ 5e-6, incl. magnification-via-Hessian); eager path (forward jit blocked upstream by the Grid2DIrregular xp gap).
  • scripts/jax_grad/weak.py (new) — FitWeak FD-validated (rel err ≤ 3e-9), plain + per-galaxy redshift-scaled.

Validation

All six scripts run green from the repo root on CPU JAX (float64), 2026-07-09, against current mains. Heart YELLOW acknowledged by the user at ship (pre-existing mcmc-smoke failures / worktree drift / assistant version skew — unrelated to this change).

🤖 Generated with Claude Code

https://claude.ai/code/session_01XGDp54jKUd3kUziTF77pNu

Upgrades the jax_grad scripts from finiteness-only checks to autodiff-vs-
central-finite-difference correctness, per the JAX gradient audit
(PyAutoLabs/autolens_workspace_developer#87):

- util.py: shared FD comparison helpers (per-parameter scaled steps, eager
  autodiff + jitted FD sweep guarded by an eager-vs-jit consistency check,
  explicit skip-list semantics for knowingly-approximate parameters).
- imaging_lp.py: standard lp.Sersic + lp_linear.Sersic variants, FD-validated;
  evaluation point anchored near the simulator truth so the positive-only NNLS
  keeps the source component live (prior medians zero it and legitimately kill
  all source/mass gradients).
- imaging_mge.py: MGE source FD-validated through the linear inversion.
- imaging_pixelization.py (new): RectangularUniform strictly FD-validated on
  all parameters; RectangularAdaptDensity asserts lens-light FD-correctness
  plus the staircase invariance of the likelihood in mass/shear directions
  (autodiff zero is the correct a.e. derivative; the rank-space CDF transform
  is invariant under order-preserving deformations at os_pix=1).
- point_source.py (new): FitPositionsSource source-plane chi-squared
  FD-validated incl. the magnification-via-Hessian term (eager path; forward
  jit remains blocked upstream).
- weak.py (new): FitWeak likelihood FD-validated, plain + redshift-scaled.

Also fixes the stale dataset pointers (imaging/simple, imaging/source_complex
-> imaging/jax_test) that broke both pre-existing scripts on a clean checkout.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XGDp54jKUd3kUziTF77pNu
…ling

Extends imaging_pixelization.py with two production-shaped variants at
over_sample_size_pixelization=4:

- RectangularAdaptImage + reg.Adapt + al.AdaptImages + border relocator (the
  production configuration): all 14 gradients live, AD vs FD(h=1e-7) <= ~1%
  on mass/shear and 6 digits on lens light.
- RectangularAdaptDensity: all 14 gradients live, <= ~3.3% (worst:
  einstein_radius).

FD uses rel_step=1e-7 (below the rank-reordering scale) with a 5% tolerance
that reflects micro-staircase contamination of the finite differences — FD
drifts toward autodiff as h shrinks, so autodiff is the h-consistent
reference. A liveness assertion guards every parameter at os_pix=4, since
this is the configuration gradient-based inference will use.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XGDp54jKUd3kUziTF77pNu
@Jammy2211 Jammy2211 merged commit 1f34925 into main Jul 9, 2026
4 checks passed
@Jammy2211 Jammy2211 deleted the feature/jax-autodiff-gradients-audit branch July 9, 2026 21:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pending-release PR queued for the next release build

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant