Skip to content

fix: substructure jax.grad is NaN with padded halo slots and through cNFWSph #871

Closed

Description

@Jammy2211

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_from gradient-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); cNFWSph overrides it because ones would put scale_radius == core_radius and divide by zero.
  • Fix the cNFWSph NaN gradient: the cNFW F / dF closed forms evaluate each branch outside its domain (sqrt of a negative), and where passes that NaN gradient through. Evaluate each branch on an in-domain value; give the theta == radius branch the correct slope.
  • In PyAutoLens, pad empty slots of galaxies_to_halo_arrays with 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

  1. autogalaxy/profiles/mass/abstract/abstract.py: vmapped_padding_params class attribute; vmapped_deflections_from replaces masked rows with [0, 0, *padding] before vmap.
  2. autogalaxy/profiles/mass/dark/cnfw.py: F_func_from / dev_F_func_from evaluate inner/outer branches on safe substitutes; edge branch theta == radius carries 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).
  3. autolens/lens/substructure_util.py: _padding_params_from(profile_cls, n_params); galaxies_to_halo_arrays tiles it into empty slots.
  4. 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 at max_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.py
  • PyAutoGalaxy/autogalaxy/profiles/mass/dark/cnfw.py
  • PyAutoLens/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

  1. Jammy2211 commented on Nov 20, 2026

    @Jammy2211
    CollaboratorAuthor

    Shipped

    Merged 2026-11-20 via the human's /prm (shift 2026-11-19-pm2):

    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_params length mismatch) are noted in the Mind record.

  2. Jammy2211 commented on Nov 24, 2026

    @Jammy2211
    CollaboratorAuthor

    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, Lens dbc63e65).

    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 differentiated sum(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.

    Profile Finite real gradient entries Exactly zero masked entries Max absolute grid difference from unpadded tracing
    NFWTruncatedSph 15/15 15/15 2.22e-16
    cNFWSph 15/15 15/15 3.67e-15 or less
    KaplinghatCoredNFWSph 21/21 21/21 2.22e-16
    YangSIDMSph 15/15 15/15 2.22e-16

    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?

  3. Jammy2211 commented on Nov 24, 2026

    @Jammy2211
    CollaboratorAuthor

    retire

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions