Skip to content

Fix GPU-only NaN gradient in form factor pole integral#103

Merged
almilder merged 1 commit into
mainfrom
fix/gpu-nan-gradient-ratintn
Jun 4, 2026
Merged

Fix GPU-only NaN gradient in form factor pole integral#103
almilder merged 1 commit into
mainfrom
fix/gpu-nan-gradient-ratintn

Conversation

@joglekara

Copy link
Copy Markdown
Member

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(...) / gdif
2 # 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)`:

  • CPU: `gav` lands at a tiny non-zero (~1e-16) → `1/gav³` is huge but finite → `0 × huge = 0`. Harmless.
  • GPU: FMA/rounding lands `gav` at exactly `0` → `1/gav³ = inf` → `0 × inf = NaN`.

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

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]>
@joglekara joglekara requested a review from almilder June 4, 2026 17:49
@almilder almilder merged commit 3ad51cc into main Jun 4, 2026
2 checks passed
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.

2 participants