Neural networks, deep learning, NLP · insight-0430
Unscale gradients before clipping when training with a gradient scaler
- Evidence type
- documented fact
- Difficulty
- medium
- Roles
- ML engineer
- Industries
- cross-industry
Summary: situation, action, reason, boundary
Situation
You add gradient clipping to a training loop that already uses
GradScalerfor mixed precision.Action
Call
scaler.unscale_(optimizer)afterbackwardand beforeclip_grad_norm_, thenscaler.stepandscaler.update. Log the unscaled norm, not the scaled one.Reason
The PyTorch AMP examples state that clipping without unscaling uses scaled gradients and invalidates the threshold.
Boundary
Any loop combining a gradient scaler with clipping or with gradient-norm logging.
Symptom or risk
Clipping runs on scaled gradients, so the threshold means something different on every step as the scale changes; clipping either never fires or fires constantly.
Exceptions
Loops without a scaler; reduced-precision formats that you run without scaling.
Trade-offs
None beyond one extra call; it must happen once per optimizer per step.
Pitfalls
Calling unscale twice in one step, or unscaling only one of several optimizers.
Lessons and concepts
Claims and their sources
PyTorch AMP examples state that clipping without unscaling invalidates the threshold because gradients are scaled, and show scaler.unscale_(optimizer) before clipping.