Repository navigation
Description
Overview
Reported on Slack by @qiuhan96 (working with an undergraduate at the University of Groningen) on the public pip release of autolens. Setting a subhalo's redshift as a free parameter (af.UniformPrior) raises jax.errors.TracerBoolConversionError during fitting. The only workaround today is use_jax=False, which is much slower. The cause is that autolens/lens/tracer_util.py performs Python-level <, <=, ==, float() on the redshift values, which are traced arrays under jax.jit when the subhalo redshift is free.
Plan
- Add a unit test that fits a model with a free-parameter subhalo redshift under JAX and asserts no
TracerBoolConversionError. - Reformulate
grid_2d_at_redshift_fromso the redshift comparisons used to choose where to insert the subhalo plane are JAX-friendly (e.g. compute candidate traced grids and select withjnp.where/jax.lax.switch, rather than branching on Python booleans). - Stop calling
float()and pairwise<on potentially-traced redshifts inplane_redshifts_from/planes_from— sort by the concrete redshifts of the non-subhalo galaxies (which are Python floats) and carry the subhalo redshift through as a traced scalar. - Stop forcing the traced subhalo centre back through
tuple(...)inAnalysisLens.tracer_via_instance_from(autolens/analysis/analysis/lens.py:116). - Cover the edge cases in tests: subhalo before lens plane, between lens and source, equal to lens redshift, equal to source redshift.
- Verify the full Nautilus/Dynesty fit from the reporter's script runs end-to-end with
use_jax=True.
Detailed implementation plan
Affected Repositories
Jammy2211/PyAutoLens(primary)
Branch Survey
| Repository | Current Branch | Dirty? |
|---|---|---|
| ./PyAutoLens | main | clean |
Suggested branch: feature/subhalo-redshift-jax-fix
Implementation Steps
-
Reproduce. Add a regression test in
test_autolens/lens/test_tracer_util.py(or a newtest_autolens/analysis/test_analysis_lens_jax.py) that builds a 3-galaxy model (lens at z=0.5, subhalo withaf.UniformPrior(0.2, 0.9), source at z=1.0) and callsjax.jit(analysis.fit_from)(instance), asserting it returns without raising. -
Refactor
autolens/lens/tracer_util.py:plane_redshifts_from. The list comprehension[float(galaxy.redshift) for galaxy in galaxies_ascending_redshift](line 49) and thesorted(galaxies, key=lambda g: g.redshift)call (line 46) both fail on traced redshifts. Either:- Split inputs into "concrete-redshift galaxies" (sorted with Python
sorted) and "traced-redshift galaxies" (the subhalo), and recombine without callingfloat(); or - Accept that this helper only ever sees Python-float redshifts and move the subhalo handling out before the call.
- Split inputs into "concrete-redshift galaxies" (sorted with Python
-
Refactor
autolens/lens/tracer_util.py:grid_2d_at_redshift_from(line 199). The three branches that fail under JAX are:- Line 249:
if redshift <= plane_redshifts[0]: return grid.copy() - Line 257:
[... if galaxies[0].redshift == redshift] - Line 267-268:
for plane_index, plane_redshift in enumerate(plane_redshifts): if redshift > plane_redshift: plane_index_insert = plane_index + 1
Replace with a structured selection: compute the traced grid for every candidate insertion position (before plane 0, between each pair, after the last plane), then pick the right one via
jax.lax.switch(orjnp.whereover a stacked result) using a comparison vector built withjnp.less/jnp.less_equal. The numpy path keeps the existing implementation behindif xp is np:. - Line 249:
-
Update
autolens/analysis/analysis/lens.py:99-116:AnalysisLens.tracer_via_instance_from. The lineinstance.galaxies.subhalo.mass.centre = tuple(subhalo_centre.in_list[0])forces a Pythontuple(...)of traced scalars. Either keep the centre as a traced 2-vector or skip the round-trip and pass the traced centre directly into the downstreamTracerbuild. -
Tests. Cover four scenarios in
test_autolens/lens/test_tracer_util.py:- subhalo redshift
< lens.redshift - subhalo redshift
== lens.redshift - lens.redshift
<subhalo redshift<source.redshift (the typical case) - subhalo redshift
== source.redshift
Run each under both
xp=npandxp=jnp(the latter insidejax.jit). - subhalo redshift
-
End-to-end check. Run the reporter's reproduction script (the model defined above plus a Nautilus/Dynesty search with a tiny
nlive) withuse_jax=Trueand confirm the fit completes.
Key Files
autolens/lens/tracer_util.py—plane_redshifts_from,planes_from,grid_2d_at_redshift_from.autolens/analysis/analysis/lens.py—AnalysisLens.tracer_via_instance_from(subhalo branch at line 99-116).test_autolens/lens/test_tracer_util.py— new JAX regression tests.
Out of scope
- Changes to
autogalaxyorautoarray. - Anything related to the
instance.perturbshortcut atlens.py:99— that path is unaffected by the bug (subhalo redshift is taken from the model, not perturbed).
Original Prompt
Click to expand starting prompt
Free-parameter subhalo redshift breaks under JAX (TracerBoolConversionError)
Reporter
Reported on Slack by @qiuhan96 (with an undergraduate at the University of Groningen). Running the public pip release of autolens on Python 3.13.
Symptom
Setting the subhalo's redshift as a free parameter (a af.UniformPrior) raises a jax.errors.TracerBoolConversionError during model fitting. The fit only runs if use_jax=False is set, which is much slower.
Reproduction
import autofit as af
import autolens as al
bulge = al.model_util.mge_model_from(
mask_radius=mask_radius,
total_gaussians=20,
gaussian_per_basis=2,
centre_prior_is_uniform=True,
)
mass = af.Model(al.mp.Isothermal)
shear = af.Model(al.mp.ExternalShear)
lens = af.Model(al.Galaxy, redshift=0.5, bulge=bulge, mass=mass, shear=shear)
# Subhalo
subhalo_mass = af.Model(al.mp.IsothermalSph)
subhalo_mass.centre_0 = af.UniformPrior(lower_limit=-0.1, upper_limit=0.1)
subhalo_mass.centre_1 = af.UniformPrior(lower_limit=1.2, upper_limit=1.8)
subhalo_mass.einstein_radius = af.UniformPrior(lower_limit=0.01, upper_limit=0.4)
# Trigger: free-parameter redshift
redshift_subhalo = af.UniformPrior(lower_limit=0.2, upper_limit=0.9)
# redshift_subhalo = 0.6 # <-- works fine
subhalo_galaxy = af.Model(al.Galaxy, redshift=redshift_subhalo, mass=subhalo_mass)
# Source
bulge = al.model_util.mge_model_from(
mask_radius=mask_radius, total_gaussians=20, centre_prior_is_uniform=False
)
source = af.Model(al.Galaxy, redshift=1.0, bulge=bulge)
model = af.Collection(galaxies=af.Collection(lens=lens, subhalo=subhalo_galaxy, source=source))When redshift_subhalo is a UniformPrior, the fit raises:
File autolens/analysis/analysis/lens.py:99, in AnalysisLens.tracer_via_instance_from
subhalo_centre = tracer_util.grid_2d_at_redshift_from(
galaxies=instance.galaxies,
redshift=instance.galaxies.subhalo.redshift,
...
)
File autolens/lens/tracer_util.py:247, in grid_2d_at_redshift_from
plane_redshifts = plane_redshifts_from(galaxies=galaxies)
File autolens/lens/tracer_util.py:46, in plane_redshifts_from
galaxies_ascending_redshift = sorted(galaxies, key=lambda galaxy: galaxy.redshift)
...
jax._src.core.TracerBoolConversionError: Attempted boolean conversion of traced array with shape bool[].
Workaround: setting use_jax=False lets the fit run, but is much slower.
Root cause
autolens/lens/tracer_util.py performs several Python-level operations on the redshift values that fail when one of them is a JAX traced scalar:
- Line 46 —
sorted(galaxies, key=lambda g: g.redshift)does pairwise<comparisons on Python objects holding traced redshifts. - Line 49 —
[float(g.redshift) for g in ...]callsfloat()on a traced scalar. - Line 249 —
if redshift <= plane_redshifts[0]:is a Python branch on a traced boolean. - Line 257 —
[plane_index for ... if galaxies[0].redshift == redshift]filters on a traced boolean. - Line 267-268 —
for ...: if redshift > plane_redshift: plane_index_insert = plane_index + 1again branches on a traced boolean and uses a Python integer to index the inserted plane.
grid_2d_at_redshift_from is called from autolens/analysis/analysis/lens.py:99 whenever instance.galaxies.subhalo exists, with redshift=instance.galaxies.subhalo.redshift. When that redshift is free, the value passed in is a traced array and every comparison above is illegal under jax.jit.
Activity
Jammy2211 commentedon May 8, 2026
Hey @qiuhan96 — picking this up now 👋
First step: a clean reproducer in our integration-test repo (
autolens_workspace_test) that mirrors the model you shared but driven through our JAX likelihood-function harness, so we can see exactly what JAX is choking on and have a regression test for the fix to land against. I'll do a sanity-check run with the redshift fixed (yourredshift_subhalo = 0.6case) alongside the free-UniformPriorcase so we can compare the two side-by-side.Quick sidebar from the dig-in: I checked whether the SLaM subhalo helpers ever exposed a
free_redshift-style API — they don't, the existing pipelines all pinsubhalo.redshiftto the lens redshift. So youraf.UniformPrior(...)approach is the right one; the bug is on our side, not your model composition.Will post back as soon as the reproducer is running. Thanks for the very thorough writeup — the traceback you included pointed straight at the broken line.
Jammy2211 commentedon May 8, 2026
Quick update @qiuhan96 — reproducer is up and we've got it pinned down 🎯
Built a script that mirrors your model but goes through our JAX integration-test harness, with two scenarios side-by-side:
log_likelihood ≈ -3.52e5, JIT matches NumPy withinrtol=1e-4.af.UniformPrior(0.2, 0.9)→ 💥 raisesTracerBoolConversionErrorexactly as you reported.The interesting bit: the failure is even earlier in the chain than the line your traceback flagged. It's not the
if redshift <= plane_redshifts[0]:attracer_util.py:249— that line never gets reached. Python's built-insorted()at line 46 already breaks because the key function returns traced scalars, andsorteddoes pairwise<to order them:```
File autolens/lens/tracer_util.py:46, in plane_redshifts_from
galaxies_ascending_redshift = sorted(galaxies, key=lambda galaxy: galaxy.redshift)
jax.errors.TracerBoolConversionError: Attempted boolean conversion of traced array with shape bool[].
```
So the fix has to handle the sort itself, not just the explicit
<=/==branches further down. That actually simplifies things — we know the lens (z=0.5) and source (z=1.0) redshifts are concrete Python floats, and only the subhalo's redshift is traced, so we can sort the concrete-redshift galaxies once and merge in the traced subhalo with a JAX-friendly insertion that usesjnp.searchsorted/jax.lax.condrather than a Python sort.The reproducer is at
autolens_workspace_test/scripts/jax_likelihood_functions/imaging/subhalo.pyon branchfeature/subhalo-redshift-jax-repro— once the fix lands, this script will turn into a regression test (Scenario B will be expected to PASS too).In the meantime, your
use_jax=Falseworkaround is the right move. We'll let you know as soon as the fix is up. 🛠️Jammy2211 commentedon May 8, 2026
Smoke Test Results — 2026-05-08
All 11 smoke tests passed. 1 skipped (database/scrape/general — NEEDS_FIX 2026-04-27).
PR: PyAutoLabs/autolens_workspace_test#78
Jammy2211 commentedon May 8, 2026
Smoke Test Results — 2026-05-08
All 11 smoke tests passed. (1 skipped:
database/scrape/general.py— NEEDS_FIX 2026-04-27, pre-existing)Tested against worktree
feature/subhalo-redshift-jax-repro— newsubhalo.pyreproducer does not affect existing smoke suite.Jammy2211 commentedon May 8, 2026
Reproducer PR is up: PyAutoLabs/autolens_workspace_test#79 ✅
Once that lands on
mainit'll be visible to anyone running our integration tests, and the same script becomes the regression check the moment the underlying tracer_util fix goes in (Scenario B will start passing, the script will exit 1, and we'll flip the assertion polarity in a follow-up commit).Starting on the actual fix in
PyAutoLensnext — likely on afeature/subhalo-redshift-jax-fixbranch. Will keep this thread updated.Jammy2211 commentedon May 8, 2026
Reproducer is on
main🟢 — PyAutoLabs/autolens_workspace_test#79 merged.So now we have a permanent, automated check that exercises this bug under our JAX integration-test runner. Anyone running
run_all_scripts.shwill see it fail loudly the moment the upstreamtracer_utilfix accidentally regresses, and Scenario A'srtol=1e-4jit-vs-numpy comparison guards against any drift in the multi-plane likelihood path on the way.Now switching to the actual fix in
PyAutoLens🛠️Jammy2211 commentedon May 8, 2026
Fix is in flight 🛠️
Plan we're going with:
plane_redshifts_from,planes_from, andgrid_2d_at_redshift_fromJAX-aware. Each one keeps its existing numpy-fast-path when every galaxy redshift is a Python number (so concrete-redshift fits stay byte-for-byte identical), and gets a new JAX path when one or more redshifts are traced.jnp.searchsorted(method='scan_unrolled')to find where the traced subhalo redshift slots into the concrete-plane sequence.grid_2d_at_redshift_fromthen runs the multi-plane ray trace at every candidate insertion position and selects the right one withjax.lax.switch.tuple(subhalo_centre.in_list[0])line inanalysis/lens.py:116— that round-trip forces a traced 2-vector through a Pythontuple(...), which would still break under JIT even after thetracer_utilfix.There's one open question we'll find out once the
tracer_utilblock is past: whether the cosmology distance computations downstream are JAX-friendly with a traced redshift. They're called fromtraced_grid_2d_list_fromand pull from astropy. If they're not, we'll file a follow-up — won't try to bundle that into this PR.Library unit tests stay numpy-only; the JAX regression check stays in the reproducer script we just merged in
autolens_workspace_test. Once the fix lands, we'll flip Scenario B's polarity from "expected to raise" to "expected to pass" in a small follow-up PR.Will keep this thread updated as the PR goes up.
Jammy2211 commentedon May 8, 2026
PR is up: #499 ✅
Scenario B of our reproducer now passes — same
log_likelihood = -3.523166e+05as the fixed-redshift baseline, with the JIT path matching NumPy withinrtol=1e-4. Existing JAX likelihood-function tests (lp.py,mge.py, etc.) all still produce their original regression literals, and the full PyAutoLens unit suite (273 tests) is green.A few notes worth surfacing:
No public API changes. Every signature is unchanged. Existing scripts with concrete-float redshifts hit a fast-path that's byte-for-byte identical to the previous code, so no production fits change behaviour.
One small constraint added. When you make a
Galaxy.redshifta free parameter under JAX, the galaxies must be declared in ascending-redshift order insideaf.Collection(galaxies=...). Your bug-report code already does this (lens=, subhalo=, source=), and that's also the natural declaration order, so this should never bite anyone in practice. Documented in the PR body's "Migration" section.Cosmology turned out to be fine. I'd flagged a risk that the astropy distance functions might also break on traced redshifts — they don't, at least for Planck15 (they accept traced scalars and return finite values). If a non-Planck cosmology turns out to need a JAX-friendly shim, we can file a follow-up, but it's not blocking you.
tuple(subhalo_centre.in_list[0])inlens.py:116works fine under JIT — turns out wrapping a(traced_y, traced_x)pair intuple(...)is a no-op and the downstreamxp.array(centre)builds a traced 1D array as expected. Initially flagged as suspect, but no fix needed there.Once #499 is reviewed + merged we'll cut a release. After that, please retry your script with
use_jax=True— it should run end-to-end. We'll also flip the polarity of the integration-test reproducer inautolens_workspace_test(small follow-up PR) so it serves as the regression test going forward.Jammy2211 commentedon May 8, 2026
Smoke Test Results — 2026-05-08
All 11 active smoke tests passed.
database/scrape/general.pyskipped (NEEDS_FIX per no_run.yaml).This run was performed from worktree
feature/multi-viz-imaging-small-datasets-overrideto verify themulti/visualization_imagingenv_vars.yaml override fix before opening the PR.Jammy2211 commentedon May 8, 2026
Smoke Test Results — 2026-05-08
Run against PR #499's
feature/subhalo-redshift-jax-fixbranch with PYTHONPATH pointing at the patched library. Scripts covered: 7 JAX likelihood-function scripts (imaging/lp, mge, rectangular; interferometer/mge, rectangular; point_source/point; multi/mge), 3 aggregator scripts, andmodel_composition/multi_galaxy_mge.py. The single skip (database/scrape/general) is a pre-existingNEEDS_FIXunrelated to this change.No regressions — every existing JAX likelihood-function script still produces its hardcoded regression literal within
rtol=1e-4. The_any_tracedguard at the top of eachtracer_utilfunction correctly routes concrete-redshift inputs to the unchanged numpy fast-path. ✅Merge pull request #499 from PyAutoLabs/feature/subhalo-redshift-jax-fix
Jammy2211 commentedon May 8, 2026
PR #499 is on
main🟢 — merge commitb790632.Next step from our side: a tiny follow-up PR in
autolens_workspace_testto flip the polarity of Scenario B in the reproducer script — right now it expectsTracerBoolConversionErrorand exits 1 if Scenario B accidentally passes (i.e. now). Flipping it makes Scenario B's PASS the asserted outcome, turning the same script into the permanent JAX regression check for this bug. Will get that up shortly.Once a release is cut from
mainand you've upgraded, please retry your model withuse_jax=Trueand let us know how it goes. Thanks again for the great bug report — the traceback you pasted made this fix straightforward to scope. 🙏Jammy2211 commentedon May 8, 2026
Shipped — 2026-05-08
PRs
d827d1cb79063261b0b4fSummary
@qiuhan96 — all done from our side! 🎉 Free-parameter subhalo redshift now works under
jax.jit. The fix added a JAX-aware fast-path guard totracer_util.plane_redshifts_from,tracer_util.planes_from,tracer_util.grid_2d_at_redshift_from, andTracer.galaxies_ascending_redshift— when every galaxy redshift is concrete the existing numpy code runs unchanged (so no production fits are affected); when any redshift is traced, the function trusts input galaxy order and uses Python identity to match the subhalo to its plane. Samesubhalo.pyscript that reproduced the bug now serves as the permanent JAX regression check, with both Scenario A (fixed) and Scenario B (freeUniformPrior) producing matchinglog_likelihoodvalues withinrtol=1e-4.Once the next PyAutoLens release is cut from
mainand you've upgraded, please retry your model withuse_jax=True. Closing this issue — feel free to re-open if anything still feels off when you hit it on the upgraded version.Thanks again for the very thorough bug report — the traceback, the model-composition snippet, and the workaround note all made this fix straightforward to scope and ship in a single session. 🙏
fix: make tracer_util JAX-safe for traced subhalo redshifts (PyAutoLa…