Repository navigation
fix: finite jax.grad through vmapped_deflections_from for masked slots and cNFWSph - #738
Merged
Merged
Conversation
…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>
This was referenced Nov 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes NaN
jax.gradthroughMassProfile.vmapped_deflections_from, the batched hook the JAX line-of-sight / subhalo scan uses (PyAutoLabs/PyAutoLens#871).whereturns zero × ∞ into NaN in the backward pass. Masked rows are now replaced by a benign in-domain row before evaluation: a zero centre followed byvmapped_padding_params, which defaults to ones. A masked slot's value and gradient are therefore exactly zero.F_func_from/dev_F_func_fromevaluated each closed form outside its domain (sqrt of a negative) and relied onwhereto discard the result, which leaks NaN gradients. Each branch is now evaluated on an in-domain value. Thetheta == radiusbranch 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.nfw_truncated.pyis untouched; ones are a valid tNFW row (τ = 1).Tests
test_autogalaxy/profiles/mass/abstract/test_vmapped_deflections.py(jax tests skip without jax), covering tNFW and cNFWSph:test_autogalaxy/profiles: 924 passed.Smoke
Targeted (
pyauto-heart smoke --targeted): autogalaxyguides/using_jax.pyPASS; autolensguides/using_jax.py,guides/misc/witt_wynne.pyPASS.Review
Independent review (Opus 5.5): CLEAN.
Companion: the PyAutoLens PR pads
galaxies_to_halo_arrayswith 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