Gradient clipping and loss spikes
Clip the global gradient norm to a threshold and explain how this bounds the size of each update. Describe loss spikes in large runs and common responses: skipping batches or rewinding to a checkpoint.
Builds on: Vanishing and exploding gradients, The LLM training loop
What the animation says
One bad step
A big training run: loss falls smoothly for days. Then, within a few steps, it jumps up. That's a loss spike. Sometimes loss recovers. Sometimes it diverges, and every step since the last good checkpoint is wasted. Watch the lower chart: the gradient norm often surges first. So how do we stop one huge gradient from throwing the model off course?
A speed limit
Gradient descent moves the parameters against the gradient, scaled by the learning rate. Normally the gradient is short, so the step is small. But a bad batch, or exploding gradients, can return a gradient a thousand times longer. One step lands far away, where loss is high. Gradient clipping is a speed limit. If the gradient is longer than a threshold c, shrink it to length c. Same direction, shorter arrow. Now a gradient of length 0.4 arrives, and c is 1. What does clipping do?
One norm over everything
Right: short gradients pass untouched. A model has many tensors. Take two: a bias gradient, three and zero, and a weight gradient, zero and four. Treat all entries as one vector. Its length: square root of nine plus sixteen, which is five. With c equal to one, every entry is multiplied by one fifth. That gives 0.6 and 0.8, total length one. Now suppose each tensor is clipped to one separately. What comes out?
Global keeps direction
Per tensor, you get one-zero and zero-one. The three-to-four ratio becomes one-to-one, so the arrow bends. Global clipping uses one scale for all tensors, so the direction is kept exactly. And the step is bounded. With plain gradient descent and learning rate 0.1, this update has size 0.1 instead of 0.5. In general, each step is at most learning rate times c. A typical c is 1. Optimizers covered later rescale each parameter, so they don't inherit this bound exactly.
Where it runs
In code, it's a few short lines. These two lines sum the squares of every tensor, then take one square root: the global norm. This line picks the scale: one, or c over the norm, whichever is smaller. The max guards against dividing by zero. Then every tensor is multiplied by that same scale. Now place it in the training loop. Drag clip into the right slot.
When a spike hits anyway
Backward, clip, then step. Clipping caps each step, but a spike can still build up over many steps, and the cause may be a bad batch. First response: skip the batch. If the norm is extreme, say over a hundred, drop that update entirely. Second: rewind. Reload a checkpoint from before the spike, and resume on different batches, skipping the ones seen around it. Sometimes with a lower learning rate, too. Remember: one global norm, one shared scale. And when a spike hits, skip or rewind.
Cap the step's length, keep its direction
Global-norm clipping and the update bound
Clipping a two-tensor gradient
Clip between backward and step; skip extremes
import numpy as np
def clip_global(grads, c):
norm = np.sqrt(sum((g**2).sum() for g in grads))
scale = min(1.0, c / max(norm, 1e-12))
return [g * scale for g in grads], norm
# in the loop: backward pass -> clip_global -> optimizer step
grads = [np.array([3.0, 0.0]), np.array([[0.0], [4.0]])]
clipped, norm = clip_global(grads, c=1.0)
print(norm, *[g.ravel() for g in clipped]) # 5.0 [0.6 0. ] [0. 0.8]
skip = norm > 100.0 # extreme norm: drop this update entirely
print(skip) # False
What to remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses