Fix GPU-only NaN gradient in form factor pole integral#103
Merged
Conversation
The rationally-centered integrand in ratcen() selects between a regular form (rf) and a near-pole form (rfn) via jnp.where. The unused rf branch divides by gav (= average of the denominator grid), which goes to ~0 near a pole. jnp.where returns the correct value, but reverse-mode autodiff differentiates both branches and propagates 0*inf from the dead rf branch. On CPU gav lands at a tiny non-zero so the rf gradient is finite (0*huge=0); on GPU FMA/rounding lands gav at exactly 0, giving 0*inf=nan. The nan flows back through vTe=sqrt(Te/Me) into grad.electron.normed_Te and blows up fits. Guard the unused branch's denominator with the double-where idiom so its gradient stays finite. The selected value is unchanged, so the fix is backend-independent and numerically identical in the forward pass. Also pre-initialize best_weights in _1d_optax_loop_ so a never-improving (e.g. NaN) batch returns valid params instead of raising UnboundLocalError. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
almilder
approved these changes
Jun 4, 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.
Problem
Running `run_tsadar.py --mode fit` on a GPU produces a NaN gradient in `grad.electron.normed_Te` after the first epoch. The NaN feeds back into the optimizer (adam), making every subsequent epoch's loss NaN, and the run eventually crashes. The same fit runs fine on CPU.
Reproduced off `main` on a Perlmutter A100 (bundled shot 111411; the defect is shot-independent).
Root cause
A `jnp.where` NaN-gradient leak in the form factor's principal-value integral, `ratcen()` in tsadar/core/physics/ratintn.py:
```python
rf = fav / gav + tmp * gdif / (12.0 * gav3) # selected far from a pole
rfn = fdif / gdif + tmp * jnp.log(...) / gdif2 # selected near a pole
out = jnp.where(|gdif| < 1e-4*|gav|, rf, rfn)
```
Near a pole, `gav = ½(g[i] + g[i-1]) → 0`, so the unselected `rf` branch is `inf`. `jnp.where` returns the correct value (`rfn`), but reverse-mode autodiff differentiates both branches and propagates `0 × (inf gradient of rf)`:
The NaN flows back through `vTe = sqrt(Te/Me)` → `grad.electron.normed_Te`.
Fix
Apply the double-where safe-divide idiom: guard `gav` in the `rf` branch (`gav_safe = jnp.where(use_rf, gav, 1.0)`) so the dead branch's gradient stays finite. The selected value is unchanged — the forward pass is numerically identical and the fix is backend-independent.
Also pre-initialize `best_weights` in `1d_optax_loop` so a never-improving (e.g. NaN) batch returns valid params instead of raising `UnboundLocalError` (a secondary crash that masked the real bug). Defensive; can be dropped if a minimal diff is preferred.
Verification
Full GPU fit run on Perlmutter A100 completes with `EXIT=0`, zero NaN/errors, finite decreasing losses across all batches, through postprocessing. Previously NaN at epoch 2.
🤖 Generated with Claude Code