Skip to content

feat: replace Ludlow16 colossus pure_callback with JAX-native impl (#403) - #406

Merged
Jammy2211 merged 2 commits into
mainfrom
feature/ludlow16-jax-native
May 14, 2026
Merged

Jammy2211 merged 2 commits into
mainfrom
feature/ludlow16-jax-native

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Summary

Closes #403. Phase 2 of the Ludlow16 JAX-native work (Phase 1 was the feasibility report + prototype in #402).

The previous autogalaxy/profiles/mass/dark/mcr_util.py wrapped a colossus.halo.concentration call in jax.pure_callback to compute the Ludlow et al. 2016 mass-concentration relation. The callback couldn't be JIT-traced or differentiated and pinned colossus as a hard runtime dependency. This PR replaces it with a JAX-native implementation.

After this PR:

  • import colossus and jax.pure_callback are gone from production code (grep autogalaxy/ confirms).
  • colossus is moved from required to test/dev extras in pyproject.toml — production no longer needs it installed.
  • The previous JAX/NumPy branching in kappa_s_and_scale_radius_for_ludlow and kappa_s_scale_radius_and_core_radius_for_ludlow collapses to a single xp-aware call.

Cross-implementation verification

This PR is the second half of an equivalence check that was set up in advance: autolens_workspace_test PR #92 locked in a fitness._vmap regression literal (-1.349200e+09) computed via the colossus pure_callback path. Running that same script against this PR's code gives bit-identical -1.349200e+09 to rtol=1e-4. The pure_callback is gone but the downstream science is unchanged.

Numbers

Carried over from Phase 1 (#402), reverified against this PR's production code:

Quantity Max rel error vs colossus
c200c (lensing grid log M ∈ [10,14] Msun/h, z ∈ [0.1,2.5]) 7.5 × 10⁻⁴
kappa_s end-to-end 1.07 × 10⁻³
NFW convergence per-pixel 8.21 × 10⁻⁴
NFW deflection per-pixel 8.21 × 10⁻⁴
cNFW deflection per-pixel 7.60 × 10⁻⁴

The intrinsic scatter of the Ludlow16 relation itself is σ_log10(c) = 0.13 dex ≈ 35% — about 350× larger than the JAX-vs-colossus offset. The scatter_sigma parameter on mp.NFWMCRScatterLudlow exists specifically to marginalise over that dispersion.

API Changes

The colossus pure_callback chain is replaced by a JAX-native port. Production no longer imports colossus. The internal mcr_util helpers (_ludlow16_cosmology_callback, ludlow16_cosmology_jax) are removed; new public entry points live in autogalaxy/profiles/mass/dark/ludlow16.py. The xp branches in the two callers (kappa_s_and_scale_radius_for_ludlow, kappa_s_scale_radius_and_core_radius_for_ludlow) collapse to single xp-aware calls. colossus moves from required to test/dev extras. See full details below.

Test Plan

  • python -m pytest test_autogalaxy/ — 868 passed (was 858 before; +10 from new test_ludlow16.py).
  • Cross-implementation: from ~/Code/PyAutoLabs-wt/ludlow16-jax-native with activate.sh sourced, run python autolens_workspace_test/scripts/jax_likelihood_functions/imaging/subhalo.py — all four scenarios pass; C and D produce vmap = -1.349200e+09 (the colossus-locked literal from PR Updated critical curve and caustic functions #92) to rtol=1e-4.
  • Visual inspect ludlow16.py algorithm follows colossus' modelLudlow16 / modelEisenstein98 line-for-line.
Full API Changes (for automation & release notes)

Added

  • autogalaxy/profiles/mass/dark/ludlow16.py — JAX-native module:
    • ludlow16_concentration(M200c_Msun_per_h, z, h, Om0, Ob0, Tcmb0, sigma8, ns, xp=np, Ode0=None, c_array_size=200, sigma_nk=256, growth_nz=256) — the main entry point.
    • transfer_eh98(k, h, Om0, Ob0, Tcmb0, xp=np) — Eisenstein-Hu '98 transfer function.
    • sigma_R(R, h, Om0, Ob0, Tcmb0, sigma8, ns, xp=np, ...) — RMS top-hat mass variance, σ₈-normalised.
    • growth_factor(z, Om0, Ode0, xp=np, ...) — flat-ΛCDM linear growth factor D(z)/D(0).
    • einasto_mass_ratio(c, xp=np, alpha=0.18) — Einasto enclosed-mass ratio M(<r_s)/M(<c r_s).
    • PLANCK15_COSMOLOGY module-level constant matching colossus' built-in planck15 preset.
  • autogalaxy/profiles/mass/dark/mcr_util.ludlow16_cosmology(mass_at_200, redshift_object, redshift_source, xp=np) — new xp-aware top-level helper returning (concentration, cosmic_average_density, critical_surface_density, kpc_per_arcsec).
  • test_autogalaxy/profiles/mass/dark/test_ludlow16.py — 10 numpy-path tests (cross-check vs colossus, skipped if colossus unavailable).

Removed

  • mcr_util._ludlow16_cosmology_callback — internal _-prefixed function, no external callers.
  • mcr_util.ludlow16_cosmology_jax — was the jax.pure_callback wrapper; no external callers.

Changed

  • mcr_util.kappa_s_and_scale_radius_for_ludlow — if xp is np: ... else: ... branching collapsed to a single ludlow16_cosmology(..., xp=xp) call. Signature unchanged.
  • mcr_util.kappa_s_scale_radius_and_core_radius_for_ludlow — same collapse. Signature unchanged.
  • pyproject.toml — colossus==1.3.1 moved from dependencies to [project.optional-dependencies] test and dev.
  • Test tolerances loosened from 1.0e-4 to 1.0e-3 (still 0.1%) on NFW-MCR pytest.approx literals in test_nfw_mcr.py, test_nfw_scatter.py, test_nfw_truncated_mcr.py, test_nfw_truncated_mcr_scatter.py. The old 1e-4 was implicitly claiming colossus-level precision; the new code differs from colossus by ~2e-4 in these places (well below the Ludlow16 intrinsic scatter the scatter_sigma parameter marginalises over).

Migration

None — all removed names were internal (_-prefixed) or had no external callers (verified by grep across PyAutoArray, PyAutoFit, PyAutoLens, all workspaces).

References

  • Phase 1 issue: #397 (closed)
  • Phase 1 PR: #402 (merged — adds the feasibility report and prototype to docs/research/)
  • Workspace regression PR: autolens_workspace_test#92 (merged — locked in the -1.349200e+09 colossus literal that this PR's code reproduces)

🤖 Generated with Claude Code

Phase 2 swap-in for #403. The previous mcr_util.py wrapped a colossus
call in jax.pure_callback to compute the Ludlow et al. 2016
mass-concentration relation. The callback couldn't be JIT-traced or
differentiated and pinned colossus as a hard runtime dependency.

This PR:
  - Adds autogalaxy/profiles/mass/dark/ludlow16.py — a JAX-native port
    of colossus.halo.concentration.modelLudlow16 (~400 lines: EH98
    transfer + Heath '77 growth factor + Einasto gammainc + 200-point
    Ludlow c-solver, all xp-aware so the same function runs under both
    numpy and JAX).
  - Replaces _ludlow16_cosmology_callback and ludlow16_cosmology_jax
    in mcr_util.py with a single ludlow16_cosmology(..., xp=np).
    Removes the jax.pure_callback and the colossus imports.
  - Drops the if-xp-is-np branching in kappa_s_and_scale_radius_for_ludlow
    and kappa_s_scale_radius_and_core_radius_for_ludlow.
  - Moves colossus from required to test/dev extras in pyproject.toml.
  - Adds 10-test unit suite test_ludlow16.py (numpy-path regression
    vs colossus, skipped if colossus unavailable).

Numbers (matching the Phase 1 prototype, PR #402):
  c200c max rel error vs colossus      : 7.5e-4
  end-to-end kappa_s max rel error     : 1.07e-3
  end-to-end conv/defl per-pixel max   : 8.21e-4
Intrinsic Ludlow16 scatter (~0.13 dex = ~35%) is ~350x larger.

Cross-implementation verification: re-running autolens_workspace_test
subhalo.py against this PR's code (Scenarios C and D, the regression
literals locked in via PR #92 from the colossus path) gives
vmap = -1.349200e+09 — bit-identical match to rtol=1e-4. The JAX-native
implementation reproduces the colossus pure_callback's downstream
log-likelihood exactly at the precision we care about.

Test tolerance updates: four existing NFW-MCR test files had
pytest.approx tolerances of 1.0e-4 on scale_radius / truncation_radius
literals. These were implicitly claiming colossus-level precision (the
literals were generated with colossus). The JAX implementation differs
from colossus by ~2e-4 at these points (sub-Ludlow-scatter), so the
tolerances are loosened to 1.0e-3 (still 0.1%, well below the
intrinsic Ludlow scatter the scatter_sigma parameter marginalises over).
Affected: test_nfw_mcr.py, test_nfw_scatter.py, test_nfw_truncated_mcr.py,
test_nfw_truncated_mcr_scatter.py.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@Jammy2211 Jammy2211 added the pending-release PR queued for the next release build label May 14, 2026
CI on Python 3.12 fails with AttributeError: module 'numpy' has no
attribute 'trapezoid' — numpy.trapezoid was added in 1.26 and CI's
3.12 environment uses an older numpy. The 3.13 job, which gets a
newer numpy, passes.

Replace the two xp.trapezoid calls with a manual trapezoidal-rule
helper that works across all numpy versions (and JAX). Math is
identical, the C/D regression literal -1.349200e+09 from
autolens_workspace_test/subhalo.py is unchanged.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@Jammy2211
Jammy2211 merged commit 277e0c5 into main May 14, 2026
5 checks passed
@Jammy2211
Jammy2211 deleted the feature/ludlow16-jax-native branch May 14, 2026 14:24
@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.

feat: replace Ludlow16 colossus pure_callback with JAX-native implementation

1 participant