fix(thermo): stable gradients for exp_m_kappa and g visibility splines - #22
Open
MinhMPA wants to merge 6 commits into
Open
fix(thermo): stable gradients for exp_m_kappa and g visibility splines#22MinhMPA wants to merge 6 commits into
MinhMPA wants to merge 6 commits into
Conversation
…ling The Friedmann-scan accumulates a spurious d(a_grid[i])/d(omega_b) eigenvalue product (~10^14 at loga=-8). The spline derivative's 1/h factor amplifies this by ~1200x, causing AD to return ~10^17 instead of the physical ~-335. Fix: stop all accumulated gradient in kappa_dot_grid, then restore only the n_H_0 ∝ omega_b path (exact, since kappa_dot ∝ n_H_0 linearly): _kappa_dot_for_deriv = sg(kappa_dot_grid) * n_H_0 / sg(n_H_0) Also adds dkappa_dot_dloga_of_loga to ThermoResult and the regression test test_opacity_logderivative_gradient_matches_fd_for_omega_b (<1% vs FD). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…s (RED) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Applies n_H_0 rescaling to kappa_dot_of_loga, exp_m_kappa_of_loga, and g_of_loga to stop the accumulated Friedmann-scan gradient (~10^8x FD blowup). Unifies the _kappa_dot_for_deriv helper into _kd_safe, reused for the kappa_dot spline itself and for g_prime_grid computation. Gradient accuracy: exact where x_e~const (loga<-8); 10-30% residual near recombination (loga~-7) from d(xe)/d(omega_b) — still finite vs the prior blowup. GREEN: test_kappa_dot_gradient_matches_fd_for_omega_b GREEN: test_exp_m_kappa_gradient_matches_fd_for_omega_b GREEN: test_g_gradient_matches_fd_for_omega_b 10/10 thermodynamics tests pass. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Documents forward-mode AD fixes (z_reio, shoot_fn, kappa/g splines) and notes PR URLs for manual opening (gh CLI unavailable on compute node). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…_kappa, g Three new xfail tests in TestThermoForwardModeAD confirm the intent: once PR-B (_find_z_reio custom_jvp) and the background DirectAdjoint wiring are merged, jax.jvp through the n_H_0-rescaled splines should be finite and match centred FD to <1% at loga=-8. Marked xfail(raises=TypeError) on this branch because two custom_vjp barriers block full-pipeline forward-mode: 1. background_solve uses RecursiveCheckpointAdjoint by default 2. _find_z_reio still uses custom_vjp (fixed in feat/forward-mode-ad) Also adds _thermo_jvp_fd_pair helper analogous to _thermo_ad_fd_pair. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Open
3 tasks
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
Stacked on PR-A (#20, `fix/kd-dloga-gradient`). Merge this PR after #20 lands — the commits from #20 are included in this diff and will vanish automatically once #20 is merged.
Extends the n_H_0 rescaling fix from PR-A to the two remaining visibility-function splines: `exp_m_kappa` and `g`. Without this, `jax.grad` through any quantity touching the visibility function (source functions, C_l) produces `nan`/`inf`.
Fix: Same rescaling pattern as PR-A (#20):
```python
_emk_safe = stop_gradient(exp_m_kappa) * (n_H_0 / stop_gradient(n_H_0))
_g_safe = stop_gradient(g) * (n_H_0 / stop_gradient(n_H_0))
```
Changes (on top of PR-A)
Merge order (4-PR AD-correctness series)
Test plan