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

TypeFormula ShapeUse
global normrescale all gradients by one factorcommon default for deep networks
per-parameter normclip each tensor separatelycan distort relative layer scales
value clippingsimple but axis-blind
adaptive clippingthreshold depends on parameter or layer normuseful 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

PatternPossible Cause
every step clips heavilylearning rate, loss scale, or batch construction may be wrong
spikes after resumeoptimizer/scheduler/scaler state may not be restored
spikes only for some batchesdata preprocessing or label outliers
spikes in sequence/graph taskslong examples, bad masking, or variable-size reduction
NaN after clippingnumerical 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?