Gradient Clipping
Gradient clipping limits gradient size before an optimizer update. It is used to reduce instability from exploding gradients, especially in sequence models, reinforcement learning, and very deep networks.
For a gradient vector , norm clipping with threshold is:
If , the gradient is unchanged. If it is larger, the gradient is rescaled to have norm .
Where It Fits
A common training step is:
Gradient clipping changes optimization dynamics. It should be recorded as part of the training configuration.
Clipping Types
| Type | Formula Shape | Use |
|---|---|---|
| global norm | rescale all gradients by one factor | common default for deep networks |
| per-parameter norm | clip each tensor separately | can distort relative layer scales |
| value clipping | simple but axis-blind | |
| adaptive clipping | threshold depends on parameter or layer norm | useful when scales vary strongly |
Global norm clipping uses all trainable parameters:
where is the gradient tensor for parameter group or tensor .
Order in a Training Step
A public training recipe should say where clipping happens:
The exact order matters. Clipping before gradient accumulation is not the same as clipping the accumulated gradient. In distributed training, clipping local gradients before synchronization is not the same as clipping the global synchronized gradient.
Clipping With Accumulation
If micro-batches form one optimizer step:
then the threshold should usually apply to , not each independently, unless the method explicitly defines micro-batch clipping.
Diagnostics
Track both the raw norm and clipped norm:
If for many steps, clipping is a major part of the optimizer dynamics. That may be intended, but it should not be invisible in the run record.
When Clipping Is a Symptom
| Pattern | Possible Cause |
|---|---|
| every step clips heavily | learning rate, loss scale, or batch construction may be wrong |
| spikes after resume | optimizer/scheduler/scaler state may not be restored |
| spikes only for some batches | data preprocessing or label outliers |
| spikes in sequence/graph tasks | long examples, bad masking, or variable-size reduction |
| NaN after clipping | numerical overflow happened before the clip point |
Checks
- Is clipping by global norm, per-parameter norm, or value?
- Is clipping applied before or after gradient scaling and accumulation?
- Are distributed gradients clipped consistently across workers?
- Is clipping hiding a deeper instability such as bad learning rate or loss scaling?
- Are gradient norms logged enough to know whether clipping is active?
- Is the threshold defined on raw, accumulated, synchronized, or unscaled gradients?
- Is the clipping ratio logged, not only the threshold?