Skip to main content

nantrace

Your loss went NaN at step 30,000. nantrace tells you which op created it, on which line, and why, and suggests the fix.

It started with a NaN in TorchRL's MPPI planner (pytorch/rl#4717) that took a long manual search to pin down. PyTorch tells you that something is NaN. It rarely tells you where it began.

Install

pip install nantrace

It needs Python 3.10 or newer and PyTorch 2.4 or newer.

Use it with a coding agent

nantrace ships an MCP server and an agent skill, so Claude Code, Cursor, Codex and other agents can trace a NaN, read a short JSON answer (about 1 KB), and see the exact line where the NaN started instead of guessing or wrapping the loss in nan_to_num.

pip install "nantrace[mcp]"

# Claude Code: use the python of the environment that has your project's torch
claude mcp add nantrace -- /path/to/venv/bin/python -m nantrace mcp
nantrace install-skill            # adds .claude/skills/nantrace/SKILL.md to this project

For Cursor, add this to .cursor/mcp.json (other MCP clients take the same command):

{"mcpServers": {"nantrace": {"command": "/path/to/venv/bin/python", "args": ["-m", "nantrace", "mcp"]}}}

The server has two tools. trace_nan(script) runs a script with every op traced and returns the cause: op, line, line inside the library, input ranges, why, and a fix. fuzz(path) tests the functions in a fuzz file on edge case inputs. Each call runs in its own process with a timeout, so a crash or a hang in your code cannot take the server down. Without MCP, the agent can call the same thing from the shell: nantrace run --brief repro.py.

nantrace: step 137: loss is nan and 6 parameter gradients are not finite (q.weight, q.bias, k.weight ...)

  1. origin forward  aten.exp.default  ->  float32 [16, 8, 8], range [9.345e-42, 4.671e+35], 3 +inf
     at       guard_training.py:21 in forward
              w = scores.exp()
     module   Attention
     input 0  float32 [16, 8, 8], range [-94.47, 2063], all finite
     why      exp overflow: in float32, exp(x) is inf for x above 88.7, and the largest input here is 2063
     fix      subtract the max first (x - x.amax(dim, keepdim=True)), or use torch.softmax, torch.logsumexp or F.log_softmax instead of exp followed by sum

  2. then   forward  aten.div.Tensor  ->  float32 [16, 8, 8], range [0, 1], 3 nan, 13 zeros
     at       guard_training.py:22 in forward
              attn = w / w.sum(-1, keepdim=True)  # softmax without the max shift
     why      inf / inf (or 0 / 0) gives NaN

Why another NaN tool

detect_anomaly forward hooks (most NaN tools) nantrace
Finds NaN created in the forward pass no, only backward layer level op level
Finds NaN created in the backward pass yes, but reports the first bad grad in reverse order, often far from the cause no yes, mapped to the forward line
Follows inf → NaN back to the op that started it no no yes
Explains the cause (overflow, log(0), 0/0, fully masked row, float16 range, the torch.where gradient trap) no no yes
Cost while training 1.4x to 2x every step small, but paid every step about 0 to 4% (Guard), full tracing only for the bad step

These are the problems people report about detect_anomaly in pytorch/pytorch#160016, and the reason JAX users have jax_debug_nans while PyTorch users get a backward traceback.

Use it

1. In a training loop: Guard

import nantrace

guard = nantrace.Guard(model)
for batch in loader:
    loss = guard.step(compute_loss, batch)   # runs forward and backward
    optimizer.step()
    optimizer.zero_grad()

After each backward pass Guard checks the loss and the gradient norms in one fused reduction. When that check fails, it restores the RNG state and module buffers from the start of the step and runs the same step again under the op level tracer. It raises NonFiniteError with the report. Use on_error="skip" to print the report, clear the gradients so optimizer.step() does nothing, and keep training.

The check runs before optimizer.step(), so the weights are still the ones that produced the NaN and nothing needs to be copied.

2. On a script: nantrace run

nantrace run train.py --steps 50       # stops at the first NaN, exit code 3
nantrace run --brief train.py          # short JSON, sized for agents
nantrace run --json train.py           # full JSON report for CI
nantrace run --stop any train.py       # also stop at the first inf

No code changes. Every op is checked, so use it on a short reproduction. Exit codes: 0 nothing found, 3 NaN, 4 an Inf that is still alive at the end (such as an infinite loss), 1 the script failed for another reason.

3. Around any block: trace

with nantrace.trace(model) as tr:
    loss = model(x).logsumexp(-1).mean()
    loss.backward()
print(tr.explain(loss))           # or tr.explain(param.grad)

4. Before training, in tests: fuzz

findings = nantrace.fuzz(my_loss, logits, targets)
assert not findings, "\n\n".join(map(str, findings))

Or from the command line, with a file of fuzz_ functions that each return the function and a valid example input (see examples/fuzz_checks.py):

nantrace fuzz examples/fuzz_checks.py

Starting from one valid example input, fuzz tries edge cases on each argument: zeros, constant values, huge and tiny values, negative values, a batch of one, float16 and bfloat16, masks with a fully masked row, and integer targets that are all one value (such as ignore_index=-100). Each try runs forward and backward under the tracer, and every NaN or Inf is reported with its root cause. Put it in a unit test to catch the edge cases users would otherwise report as issues.

From valid inputs, fuzz found the bug behind 7 real issues (benchmarks/realworld/fuzz_rediscover.py), for example:

pytorch#201658 linear_cross_entropy: when arg 2 (integer) is all -100
   origin  inside the custom op: where writes the constant nan, at torch/nn/modules/linear_cross_entropy.py:87
pytorch#201627 gumbel_softmax: when arg 0 is float16 with values up to 25% of the float16 limit
   origin  aten.div at torch/nn/functional.py:2277, float16 overflow (the exact result reaches 1.638e+05)

and reported nothing for 5 numerically safe functions, including the fixed version of the TorchRL weighting.

5. PyTorch Lightning and Hugging Face

from nantrace.integrations.lightning import NantraceCallback
trainer = L.Trainer(callbacks=[NantraceCallback()])

from nantrace.integrations.hf import NantraceTrainer
trainer = NantraceTrainer(model=model, args=args, train_dataset=ds)   # same arguments as Trainer

Both check the gradients after every backward pass (the Trainer also checks the loss) and, when something is NaN or Inf, replay that batch under the tracer and raise NonFiniteError with the report. With fp16 and a GradScaler the gradient check is off by default, because the scaler expects inf gradients now and then.

What it diagnoses

  • exp overflow, and exp underflow that later becomes 0 / 0 (for example softmax weights that all round to 0)
  • log of 0 or negative values, log(softmax) and log(sigmoid) underflow
  • division by zero, 0 / 0, inf / inf, inf - inf, 0 * inf
  • gradients that are infinite at a point: sqrt, log, pow, acos, asin, atanh, norms and distances at 0
  • the torch.where(mask, log(x), 0) trap, where the forward pass is finite but the gradient is NaN
  • softmax over a fully masked row
  • float16 and bfloat16 overflow, with the exact value computed again in float64
  • NaN that was already in the data or a parameter, named by parameter

Tested on real bugs

benchmarks/realworld/ holds 9 NaN bugs: the reproducers from 8 public issues (PyTorch, DeepChem and a GNN research repo) and one common pattern (a hand written binary cross entropy), plus 4 controls whose bugs are already fixed upstream. nantrace run gives the stated root cause for all 9 and reports nothing for the 4 controls. See benchmarks/realworld/RESULTS.md.

The bug that started it

Run unchanged on the TorchRL reproduction script, nantrace points to torchrl/modules/planners/mppi.py:256 and reports that exp underflowed to 0 a few lines earlier, so the softmax weights summed to 0. That is exactly the cause found by hand in pytorch/rl#4717.

Overhead

benchmarks/overhead.py: 6 layer MLP, width 1024, batch 256, AdamW. CPU and MPS on a MacBook Air M2, CUDA on an NVIDIA T4 (Kaggle).

CPU MPS CUDA (T4)
plain training 1.00x 1.00x 1.00x
nantrace.Guard 1.04x about 1.0x (within noise) about 1.0x (within noise)
torch.autograd.detect_anomaly 1.36x 1.7x to 2.1x 2.43x

Limits

  • Tested on PyTorch 2.15 with Python 3.13 on CPU and Apple MPS, and on PyTorch 2.11 with CUDA 13 on an NVIDIA T4, with Lightning 2.6 and Transformers 5.22. Not yet tested with multiple GPUs or distributed training. examples/cuda_check.ipynb runs the test suite on a Colab GPU in one click, and reports are very welcome.
  • The replay needs the step to be repeatable. Nondeterministic GPU kernels or random data augmentation inside the step can make the NaN disappear on replay; nantrace says so when that happens.
  • Code inside torch.compile is not traced op by op yet.
  • With GradScaler, pass check_grads=False; inf gradients are expected there and the scaler skips those steps.
  • torch.autograd.detect_anomaly is built in. It checks the backward pass only.
  • sherlorch also builds an op level provenance graph, through TorchFunctionMode. It focuses on the forward pass and does not explain causes.
  • Hugging Face DebugUnderflowOverflow records the absolute min and max of every module, which is great for watching values climb in float16.
  • trainscope is for loss spikes in long LLM runs rather than NaN.

For coding agents

skills/nantrace/SKILL.md (installed by nantrace install-skill) teaches an agent when to use nantrace, how to read the JSON, and to prefer stable builtins over hiding the NaN. See the agent section at the top for MCP setup.

License

MIT

Metadata

Release files for nantrace 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nantrace 0.1.1
File Size Uploaded Provenance Actions
nantrace-0.1.1.tar.gz 42.8 kB Without provenance Details for nantrace-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for nantrace 0.1.1
File Interpreter ABI Platform Actions
nantrace-0.1.1-py3-none-any.whl Python 3 none any Details for nantrace-0.1.1-py3-none-any.whl

Total release size: 80.1 kB

Release files / nantrace-0.1.1.tar.gz

Download URL nantrace-0.1.1.tar.gz
Size 42.8 kB
Tags Source
SHA-256 checksum
How to use checksums
25ddee2794d7cfe4689452a0d75665f2a107447f24c39f27c777aeffe2354db8
BLAKE2b-256 checksum
How to use checksums
343d9e888cca6a0c170cfdb05fd60d769bf08ba8f438e3ade2596dd18bff58a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.7

Release files / nantrace-0.1.1-py3-none-any.whl

Download URL nantrace-0.1.1-py3-none-any.whl
Size 37.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fbd0f370e57b5f32261da4727886d18cf76bb3d2de7bf60ca5f57265c820da34
BLAKE2b-256 checksum
How to use checksums
66d24c01918f89da77efc3afc6dc815426fb5f2a9c23161cdd45c3a22049c5af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.7

Security for nantrace 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page