Gradient Clipping
Gradient clipping limits large gradients before optimizer updates. Norm and value clipping reduce the impact of spikes during neural network training.
Gradient clipping limits how large gradients can get before an optimizer uses them to update a model. A gradient tells the optimizer how sensitive the loss is to each parameter. When a batch produces an unusually large gradient, clipping reduces its influence on the next update. Global norm clipping shortens the gradient vector without turning it, so its components keep the same proportions. When the norm is already below the threshold, the gradient stays unchanged.
Why Gradient Clipping Stabilizes Training#
Backpropagation multiplies derivatives along paths through a network. In a recurrent neural network, this multiplication repeats across time steps. When the derivatives amplify one another, the resulting exploding gradients can cause oversized parameter updates, loss spikes, and training divergence. Razvan Pascanu, Tomas Mikolov, and Yoshua Bengio proposed gradient norm clipping for this problem in a 2012 preprint that appeared at ICML in 2013. Clipping leaves the chain of derivatives intact, so it cannot restore a vanishing gradient that has already shrunk toward zero.
With plain stochastic gradient descent, a gradient norm capped at 1.0 and a learning rate of 0.01 give a parameter update with a norm of at most 0.01. With momentum or an adaptive optimizer such as Adam, the threshold bounds the incoming gradient, and the final update also depends on the optimizer's stored state.
Clipping by Norm vs. Clipping by Value#
- Global norm clipping treats the gradients of the selected parameters as one vector. If its L2 norm (Euclidean length) exceeds the threshold, every component is multiplied by the threshold divided by the norm. A gradient of
(3, 4)has length 5, so a threshold of 2 scales it to(1.2, 1.6). Clipping layers separately lets each layer have its own scale factor, which can change the direction of the combined gradient. - Value clipping clamps each component to a fixed interval. With limits of −2 and 2,
(3, 4)becomes(2, 2). The vector changes direction and its L2 norm is about 2.83, because the threshold applies to individual components.
Where Clipping Fits in the Training Loop#
In PyTorch, clip_grad_norm_ modifies gradients in place and returns their norm before clipping. The quadratic loss below produces the gradient (3, 4) used in the comparison.
import torch
weights = torch.nn.Parameter(torch.tensor([3.0, 4.0]))
optimizer = torch.optim.SGD([weights], lr=0.1)
loss = 0.5 * weights.square().sum()
loss.backward()
# Clip the gradient before the optimizer reads it.
before = torch.nn.utils.clip_grad_norm_([weights], max_norm=2.0)
after = weights.grad.norm()
optimizer.step()
print(f"Gradient norm: {before.item():.1f} → {after.item():.1f}")The output, Gradient norm: 5.0 → 2.0, shows how clipping reduced the gradient to the chosen limit. With gradient accumulation, clipping runs once on the combined gradient at the end of the accumulation window. Clipping each micro-batch separately rescales contributions before they can reinforce or cancel one another. When mixed precision training uses loss scaling, unscale the accumulated gradients before clipping so the threshold applies to their actual magnitude. The enabled gradient scaler skips the optimizer update when it detects NaN or inf during unscaling.
Choosing a Threshold for Language and Vision Models#
In large language model (LLM) training, global norm clipping reduces the influence of gradient spikes on the optimizer. The PaLM team used a global threshold of 1.0 across its models. When the 540-billion-parameter model still hit loss spikes, the team resumed from a checkpoint roughly 100 steps before a spike and skipped about 200–500 batches spanning its onset.
The threshold is a training hyperparameter. The fraction of pre-clip norms above it shows whether clipping affects a few outlier steps or most of the run. A low threshold can shrink typical gradients and slow learning. An ablation study can isolate that effect by holding the optimizer, learning rate schedule, and batch size fixed while comparing thresholds on validation data.
In Ultralytics YOLO26 training, the default trainer unscales gradients and caps their global L2 norm at 10.0 before the optimizer step. A custom trainer can change that limit by overriding optimizer_step().










