Missing GradScaler Update in AMP Loop
When using `torch.cuda.amp.autocast()`, gradients are computed in float16. The dynamic range of float16 is narrow, causing gradients to overflow to infinity. If a `GradScaler` is not used, or if `scaler.update()` is omitted, the scale factor isn't adjusted to prevent these overflows, leading to NaN gradients and subsequently NaN loss.
When using `torch.
- Symptom
RuntimeError: Function 'LogSoftmaxBackward' returned nan values in its 0th output- Root cause
- When using `torch.cuda.amp.
- Recommended fix
- Wrap optimizer step and loss backward with GradScaler scaler = torch.cuda.amp.GradScaler() scaler.scale(loss).backward() scaler.step(optimizer) scaler.update() GradScaler multiplies the loss by a scale factor to prevent float16 underflow/overflow, then un-scales gradients before applying updates.
- How Denpex helps
- Denpex investigates Missing GradScaler Update in AMP Loop using the evidence you provide or your connected workload collects. Earlier rank, host or application evidence is needed to distinguish an initiating failure from a downstream report.
What this failure is
Missing GradScaler Update in AMP Loop is a Precision failure seen during ML training runs. When using `torch.cuda.amp.autocast()`, gradients are computed in float16. The dynamic range of float16 is narrow, causing gradients to overflow to infinity. If a `GradScaler` is not used, or if `scaler.update()` is omitted, the scale factor isn't adjusted to prevent these overflows, leading to NaN gradients and subsequently NaN loss. Common tags: Mixed Precision (AMP).
Is this what broke your run? Paste your log.
You're reading about Missing GradScaler Update in AMP Loop. Paste your own traceback and relevant evidence for an investigation of your workload, with a next action or a specific missing fact. A reference entry does not establish your cause. No account or card for the free diagnosis. Review data handling before submitting sensitive logs.
Before uploading, review cloud data handling and local options.
Upload your log file. Large files are processed in your browser. Only selected evidence is submitted when you run diagnosis.
Why it happens (the mechanism)
The crash often happens randomly during training, making it look like a data issue or a learning rate explosion, when it's purely a numerical precision limitation of FP16.
What you'll observe
- Loss goes to NaN
- RuntimeError: Function 'LogSoftmaxBackward' returned nan values in its 0th output
Common symptoms and what they mean
| Symptom | Why it happens |
|---|---|
| Model trains normally for a few steps and then loss suddenly jumps to NaN. | When using `torch.cuda.amp.autocast()`, gradients are computed in float16. The dynamic range of float16 is narrow, causing gradients to overflow to infinity. If a `GradScaler` is not used, or if `scaler.update()` is omitted, the scale factor isn't adjusted to prevent these overflows, leading to NaN gradients and subsequently NaN loss. |
| Weights become filled with NaN. | When using `torch.cuda.amp.autocast()`, gradients are computed in float16. The dynamic range of float16 is narrow, causing gradients to overflow to infinity. If a `GradScaler` is not used, or if `scaler.update()` is omitted, the scale factor isn't adjusted to prevent these overflows, leading to NaN gradients and subsequently NaN loss. |
Which systems are affected
- PyTorch
- CUDA
How to confirm this is the problem
Use this checklist to test the hypothesis against a small reproduction. No single line proves the root cause, so preserve the preceding events and compare one variable at a time.
- ✓Check if `torch.cuda.amp.autocast()` is used without `torch.cuda.amp.GradScaler()`.
- ✓Print `scaler.get_scale()` to see if it drops to 0 or explodes.
Root cause
- When using `torch.cuda.amp.autocast()`, gradients are computed in float16. The dynamic range of float16 is narrow, causing gradients to overflow to infinity. If a `GradScaler` is not used, or if `scaler.update()` is omitted, the scale factor isn't adjusted to prevent these overflows, leading to NaN gradients and subsequently NaN loss.
The fix and how to prevent it
Searchable error signature
RuntimeError: Function 'LogSoftmaxBackward' returned nan values in its 0th outputUse this text as a lookup key in logs and upstream issue trackers. It is not presented as a captured customer log. Confirm the cause from your own preceding events, versions, configuration and the cited references.
The fix and the prevention pattern
The root cause is on this page and stays free. A free account adds the exact remediation steps, saved history, and the fix on every entry in the encyclopedia.
Sign up free. Unlock the full analysisNo credit card. Daily allowance follows verified trust tier. Instant access.
Diagnose this failure in VS Code
Select the traceback or open the failed terminal, then run Denpex locally to see the initiating rank, collateral failures, exact fix, and verification command without uploading the log.
Install the free VS Code extensionReferences
Don't just read the fix, diagnose your run
The encyclopedia tells you what went wrong. Denpex tells you what went wrong in YOUR training run. With your logs, your config, and your stack.