Repository navigation
Description
Overview
jax.grad through the batched substructure ray-tracing path is NaN as soon as any plane has a zero-padded halo slot, which is always the case when planes hold different halo counts. cNFWSph gradients through the same hook are NaN even with no padding. The deflection values are correct; only the gradients break. Gradient-based line-of-sight / subhalo fits (HMC, gradient optimisers, Fisher forecasts) therefore get NaN.
Found by the independent adversary review of shift 2026-11-19-pm (notes on PyAutoLens#870, finding F2 on PyAutoGalaxy#737). Reproduced on fresh main (PyAutoLens@aa02375f, PyAutoGalaxy@5b85fbf0): 2 + 5 NFWTruncatedSph halos packed at max_n=5 or 8 → d/dgrid and d/dparams NaN; cNFWSph single halo d/dparams [nan, nan, -1.64, nan, nan].
Plan
- Make a masked-out slot of
MassProfile.vmapped_deflections_fromgradient-safe: swap its parameters for a benign in-domain row before evaluation (double-where), so its value and gradient are exactly zero. - Let a profile declare its padding row (
vmapped_padding_params, default ones);cNFWSphoverrides it because ones would putscale_radius == core_radiusand divide by zero. - Fix the
cNFWSphNaN gradient: the cNFWF/dFclosed forms evaluate each branch outside its domain (sqrt of a negative), andwherepasses that NaN gradient through. Evaluate each branch on an in-domain value; give thetheta == radiusbranch the correct slope. - In PyAutoLens, pad empty slots of
galaxies_to_halo_arrayswith the same benign row instead of zeros, so gradients are finite even against an autogalaxy whose hook predates the fix. - Add the witness tests (jax-only, skip without jax).
- Tier: glance — effective level: supervised; merge mode: auto-merge on green if the Witness passes after a merge slot.
Detailed implementation plan
Affected Repositories
- PyAutoGalaxy (library, ships first)
- PyAutoLens (library, primary)
Work Classification
Library (bug)
Branch Survey
| Repository | Current Branch | Dirty? |
|---|---|---|
| PyAutoGalaxy | main | clean |
| PyAutoLens | main | clean |
Suggested branch: feature/substructure-padded-slot-grad
Worktree root: ~/Code/PyAutoLabs-wt/substructure-padded-slot-grad/
Implementation Steps
autogalaxy/profiles/mass/abstract/abstract.py:vmapped_padding_paramsclass attribute;vmapped_deflections_fromreplaces masked rows with[0, 0, *padding]beforevmap.autogalaxy/profiles/mass/dark/cnfw.py:F_func_from/dev_F_func_fromevaluate inner/outer branches on safe substitutes; edge branchtheta == radiuscarries the exact first-order slope (1/2 for F/r, 1/3 for dF/(2r) in x);cNFWSph.vmapped_padding_params = (0, 1, 0.1).autolens/lens/substructure_util.py:_padding_params_from(profile_cls, n_params);galaxies_to_halo_arraystiles it into empty slots.- Tests:
test_autogalaxy/profiles/mass/abstract/test_vmapped_deflections.py(padded == unpadded value/grad to 1e-10, padded-slot grads exactly 0, all-masked, cNFW grads finite and match finite differences, F/dF continuity at the radius);test_autolens/lens/test_substructure_util.py(witness: 2+5 tNFW atmax_n=8, multi-plane summed deflection gradient wrt grid and real halo params finite and equal to the no-padding gradient to 1e-10 in float64).
Key Files
PyAutoGalaxy/autogalaxy/profiles/mass/abstract/abstract.pyPyAutoGalaxy/autogalaxy/profiles/mass/dark/cnfw.pyPyAutoLens/autolens/lens/substructure_util.py
Original Prompt
Click to expand starting prompt
PyAutoMind draft/bug/autolens/substructure_jax_grad_is_nan_when_a.md (Witness: a test builds two planes with 2 and 5 NFWTruncatedSph halos, packs them with galaxies_to_halo_arrays(max_n=8), and asserts jax.grad of the summed multi-plane deflection with respect to the grid and every real halo's params is finite and matches the no-padding gradient to 1e-10 in float64; a second test asserts finite jax.grad through cNFWSph.vmapped_deflections_from.)
Activity
Jammy2211 commentedon Nov 20, 2026
Shipped
Merged 2026-11-20 via the human's
/prm(shift 2026-11-19-pm2):vmapped_deflections_fromkeepsjax.gradfinite for masked slots, andcNFWSphgradients are finite.jax.gradis finite.Witness: padded == unpadded gradients to 1e-10, padded-slot gradients exactly 0 (NaN reproduced on main; the lens witness fails with neither fix and passes with either). Tests: ag profiles 924 passed, al lens 228 passed; targeted smoke using_jax.py x2 + witt_wynne.py PASS; independent Opus review CLEAN. Follow-up nits (exact padding-row assert; clearer error on a
vmapped_padding_paramslength mismatch) are noted in the Mind record.Jammy2211 commentedon Nov 24, 2026
The 24 November shift rechecked the report that padded halos produce NaN gradients. Fresh main contains the fixes from PyAutoGalaxy#738 and PyAutoLens#872 (Galaxy
1ba51f32, Lensdbc63e65).I packed one halo on the first plane, two on the second, and an empty source plane with
galaxies_to_halo_arrays(max_n=None). In float64 I differentiatedsum(traced_grids_via_scan(...)[-1] ** 2)with respect to all halo parameters on 100 grid points. I repeated the run after explicitly zeroing every masked row. Both runs gave the same results.The reported failure did not reproduce. Should the shift retire the duplicate draft
substructure_util_zero_padded_halo_slots_make, or is there a different failing population to test?Jammy2211 commentedon Nov 24, 2026
retire