Skip to content

fix: pad empty halo slots with benign params so substructure jax.grad is finite - #872

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

galaxies_to_halo_arrays padded empty halo slots with zeros. A padded NFWTruncatedSph slot therefore had scale_radius = 0, and jax.grad through the multi-plane substructure deflections was NaN whenever planes held different halo counts, which they always do. max_n=None (#870) did not help, because every plane below the fullest is still padded.

Empty slots now hold a benign row: a zero centre followed by the profile's vmapped_padding_params, or ones when the profile has none. The mask still zeroes the slot's deflection, and its gradient is now finite (and zero). The companion PyAutoGalaxy PR (PyAutoLabs/PyAutoGalaxy#738) makes the hook itself swap masked rows, and also fixes the cNFWSph NaN gradient. Either PR alone makes this PR's witness pass, so they can merge in either order.

Closes #871

Tests (witness)

  • test__galaxies_to_halo_arrays__padded_slots_keep_multi_plane_gradients_finite: two planes with 2 and 5 NFWTruncatedSph halos are packed at max_n=8. jax.grad of the summed multi-plane deflection, with respect to the grid and to every real halo's params, is finite and equals the no-padding gradient (each plane packed at max_n equal to its own count) to 1e-10 in float64. The padded slots' gradients are exactly 0.
  • test__galaxies_to_halo_arrays__empty_slots_hold_benign_padding.
  • Control runs of the witness test:
    • passes with this PR alone (autogalaxy main);
    • passes with the autogalaxy fix alone;
    • fails with neither fix.
  • test_autolens/lens: 228 passed. The finite cNFWSph gradient half of the witness is tested in PyAutoGalaxy#738.

Smoke

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

Review

Independent review (Opus 5.5): CLEAN. It noted that, because this PR pads with the benign row, the hook's own handling of zero padding is tested only in PyAutoGalaxy#738.

🤖 Generated with Claude Code

… is finite

galaxies_to_halo_arrays padded empty slots with zeros, so a padded
NFWTruncatedSph slot had scale_radius = 0 and jax.grad through the
multi-plane deflections was NaN whenever planes held different halo counts.
Empty slots now hold the profile's `vmapped_padding_params` row (ones when it
has none) behind a zero centre. Adds the witness test: 2 + 5 halos at
max_n=8 give the same finite gradient as no padding, to 1e-10.

Closes #871

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Jammy2211
Jammy2211 merged commit 666741c into main Nov 20, 2026
5 checks passed
@Jammy2211
Jammy2211 deleted the feature/substructure-padded-slot-grad branch November 20, 2026 08:24
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.

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

1 participant