Back to ML Engineer Mind Map
中文·English
💻 ML EngineerID: mle-gradient-clipping-nan-loss

Gradient Clipping & NaN Loss Debug

梯度爆炸排查与 NaN 损失定位
🎯Core Definition
Gradient Clipping & NaN Loss Debugging provides the systematic troubleshooting framework to detect, isolate, and remediate training instabilities, exploding gradients, and numerical divergence in deep neural networks and LLMs; gradient explosion causes parameters to take catastrophic updates into steep non-convex walls, producing arithmetic overflows that cascade into `Loss = NaN`; standard remedies include: 1) Global Norm Gradient Clipping (ggmax_normg2g \leftarrow g \cdot \frac{\text{max\_norm}}{\|g\|_2} whenever g2>max_norm\|g\|_2 > \text{max\_norm}, preserving directional vectors); 2) Dynamic Loss Scaling in mixed-precision FP16 (scaling loss by S=216S=2^{16} to shift tiny gradients into representable float exponents while discarding NaN steps); 3) Anomaly tracing with PyTorch `detect_anomaly` and backward hooks.
💡Use Cases
Large-scale LLM pre-training loss spike debugging, deep recurrent/transformer network stabilization, and FP16/BF16 numerical audits.
Key Problems Solved
Mid-training loss divergence in distributed clusters burns tens of thousands of compute dollars per incident; gradient clipping and loss scaling guarantee resilient, continuous convergence.
🎯5 High-Frequency Exam Points
1
Derive Global Norm Gradient Clipping and explain why it preserves gradient direction vectors unlike element-wise value clipping?
2
Explain the state-machine rules of Dynamic Loss Scaling in FP16 mixed-precision training?
3
Why does BF16's 8-bit exponent match FP32's dynamic range, eliminating the need for Loss Scalers required by FP16's 5-bit exponent?
4
List the 5 most common mathematical operations that trigger NaNs in PyTorch computation graphs and their specific fixes?
5
How to monitor the Grad-Norm-to-Weight-Norm ratio as a vital diagnostic metric for optimizer learning rate health?
🔗Foundational Prerequisite Cards (Click to Review)
Updated 2026-08-14
🎯
Test Your Knowledge: Practice Questions for "Gradient Clipping & NaN Loss Debug"
Single choice pitfall questions with instant feedback and mistake tracking.
🚀 Start Card Practice
Previous CardProduction PSI Drift MonitoringNext Card5 CV Splitting Strategies & Leak Defense

🔗 More ML Engineer Knowledge Cards

Bias-Variance Tradeoff & OverfittingLoss Function Taxonomy & GradientsOptimizer Convergence & MomentumEnsemble Stacking & Blending