Gradient clipping bounds the gradient norm before the optimizer step to prevent exploding gradients. Two approaches: ① per-parameter (value) clipping gi←clip(gi,−c,c), i.e. gi←gi⋅min(1,∣gi∣c) — it only bounds individual component magnitudes, scales different components by different factors (distorting the direction), and cannot bound the global norm; ② global-norm clipping: compute the total norm G=∥g∥2=∑igi2 (per-layer normalized variant G=∑l∥gl∥22/nl with nl = parameter count of layer l, so large layers don't dominate), and when G>c scale uniformly g←g⋅Gc — direction preserved, step norm hard-bounded at c (equivalent to projecting onto a ball of radius c; PyTorch clip_grad_norm_ commonly uses c=1.0). When to clip: after gradient synchronization (DDP All-Reduce) and before the optimizer step; in mixed precision, clip the gradients of the FP32 master weights (clipping low-precision gradients loses precision). Loss-spike (NaN/Inf) handling: standard procedure when loss suddenly jumps to NaN/Inf — ① diagnose: LR too large? anomalous data samples (corrupted images / missing labels)? gradient overflow? ② if the gradient contains NaN/Inf, skip this optimizer step — NaN gradients corrupt the FP32 master weights and poison all subsequent training; ③ in AMP, check Loss Scaling first: FP16 forward/backward overflow → Inf gradients → halve the dynamic loss scale (S←S/2) and skip/replay the step; after several overflow-free steps, double S; ④ roll back to the last good checkpoint, lower the LR, inspect the data pipeline, tighten clipping if needed; ⑤ monitor long-term: track the grad-norm curve (a loss explosion is preceded by a persistently rising gradient norm) and the loss curve, and auto-pause training on NaN detection.
💡Use Cases
standard protection for RNN/LSTM and long-sequence Transformer training (chain-multiplied long-range gradients explode most easily), and a routine component of large-batch and mixed-precision training; interview favorites: why global norm instead of per-parameter clipping, how to handle loss spikes, and the ordering of clipping vs AMP/DDP.
⚡Key Problems Solved
exploding gradients can throw parameters out of the normal region in one step (loss → NaN, training ruined); clipping bounds the worst-case step so training continues through pathological moments. Compared with per-parameter clipping, global-norm clipping preserves direction and bounds the norm, making it the engineering default. Note clipping treats the symptom: if it triggers frequently (the scale c/G is often much less than 1), the LR is too large or the model/data have real problems — lower the LR or fix the data instead of relying on the clip.
🎯5 High-Frequency Exam Points
1
Write the global-norm clipping formula: compute G=∥g∥2, and when G>c apply g←g⋅Gc; why does uniform scaling preserve the direction and bound the step norm at c?
2
Per-parameter clipping gi←gi⋅min(1,c/∣gi∣) vs global-norm clipping: drawbacks of each (direction distortion, uncontrollable global norm), and when to use which.
3
When does clipping happen in DDP (after gradient All-Reduce, before the step)? Why clip only after synchronization? Why clip the FP32 gradients in mixed precision?
4
Full loss-spike (NaN/Inf) handling: why must steps with NaN gradients be skipped, halving the AMP loss scale (S←S/2) with step replay, and the ordering of checkpoint rollback and LR reduction.
5
Effects of c being too small / too large? How can monitoring tell you that clipping is too frequent (the ratio c/G persistently far below 1) and the LR is too large?