Skip to content

fix: finite jax.grad through vmapped_deflections_from for masked slots and cNFWSph - #738

Merged
Jammy2211 merged 1 commit into
mainfrom
feature/substructure-padded-slot-grad
Nov 20, 2026
Merged

Jammy2211 merged 1 commit into
mainfrom
feature/substructure-padded-slot-grad

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Summary

Fixes NaN jax.grad through MassProfile.vmapped_deflections_from, the batched hook the JAX line-of-sight / subhalo scan uses (PyAutoLabs/PyAutoLens#871).

  • Masked slots. Callers pad empty slots with zeros. A zero scale radius has infinite partial derivatives, and where turns zero × ∞ into NaN in the backward pass. Masked rows are now replaced by a benign in-domain row before evaluation: a zero centre followed by vmapped_padding_params, which defaults to ones. A masked slot's value and gradient are therefore exactly zero.
  • cNFWSph. F_func_from / dev_F_func_from evaluated each closed form outside its domain (sqrt of a negative) and relied on where to discard the result, which leaks NaN gradients. Each branch is now evaluated on an in-domain value. The theta == radius branch keeps its old value and now carries the exact first-order slope (F = r f(θ/r) with f'(1) = 1/2; dF/2 = r g(θ/r) with g'(1) = 1/3). cNFWSph.vmapped_padding_params = (0, 1, 0.1) overrides the default, because ones would make scale_radius == core_radius and divide by zero.
  • Deflection values are unchanged: the independent review compared old and new F/dF on 2,000+ points, numpy and jax, and found them bit-identical.
  • nfw_truncated.py is untouched; ones are a valid tNFW row (τ = 1).

Tests

  • New test_autogalaxy/profiles/mass/abstract/test_vmapped_deflections.py (jax tests skip without jax), covering tNFW and cNFWSph:
    • With zero-padded masked slots, the value, d/dgrid and d/dparams equal the no-padding results to 1e-10, and the masked slots' gradients are exactly 0.
    • With every slot masked, the deflection and all gradients are 0.
    • cNFWSph vmapped gradients are finite and match finite differences, at grid points inside the core, between the core and the scale radius, outside it, and exactly on it.
    • F and dF are continuous at the radius.
  • Control: 5 of the 7 tests fail with the source change reverted.
  • test_autogalaxy/profiles: 924 passed.

Smoke

Targeted (pyauto-heart smoke --targeted): autogalaxy guides/using_jax.py PASS; autolens guides/using_jax.py, guides/misc/witt_wynne.py PASS.

Review

Independent review (Opus 5.5): CLEAN.

Companion: the PyAutoLens PR pads galaxies_to_halo_arrays with the same benign row and adds the witness test. Each PR is sufficient on its own for the witness, so they can merge in either order.

Refs PyAutoLabs/PyAutoLens#871

🤖 Generated with Claude Code

…s and cNFWSph

A masked-out slot of MassProfile.vmapped_deflections_from (callers pad these
with zeros) put a zero scale radius into the profile; `where` discards its
value but its infinite partial derivatives turned every gradient through the
plane into NaN. Masked rows are now swapped for a benign in-domain row
(`vmapped_padding_params`, default ones) before evaluation.

The cNFW F / dF closed forms evaluated each branch outside its domain (sqrt
of a negative), which also leaked NaN gradients through `where`. Each branch
is now evaluated on an in-domain value, and the theta == radius branch
carries the exact first-order slope. cNFWSph pads with (0, 1, 0.1).

Refs PyAutoLabs/PyAutoLens#871

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant