What is gradient clipping?
Gradient clipping is a technique that keeps neural network training stable by scaling gradients down to a set threshold whenever their size exceeds it, so no single update step changes parameters excessively.
Unlike gradient accumulation, which sums gradients over several small batches to grow effective batch size, gradient clipping caps gradient size. The two can be used in the same run, with clipping applied to the averaged accumulated gradient before the optimiser step.
- Topic
- Machine learning, models and training
- Also known as
- Gradient norm clipping
How gradient clipping works
- After backpropagation, one total norm is computed from the gradients of all parameters.
- If the norm exceeds the threshold, the gradient vector is scaled to it with its direction kept; below the threshold it is left alone.
- A simpler variant keeps each gradient component within a range, which can change direction.
- The optimiser updates parameters with the clipped gradient; the threshold is chosen as a hyperparameter.
In more detail
Interaction with mixed precision: FP16 scaling and BF16
In mixed-precision FP16 training with dynamic loss scaling (`GradScaler`), gradients are multiplied by a large scale factor to prevent underflow. Gradients need to be unscaled (`scaler.unscale_`) before computing and clipping their norm; otherwise, the inflated norm triggers constant clipping against the wrong scale. (BF16 typically avoids loss scaling altogether.)
Computing the global norm in distributed training
In distributed setups such as FSDP, Tensor Parallelism or Pipeline Parallelism, parameters are sharded across devices. Computing the exact global L2 norm requires summing squared norms across all shard groups (without double-counting replicated or tied weights) before scaling, making framework-level helpers (like `FSDP.clip_grad_norm_`) necessary.
Why it is still used in LLM training
In modern LLM pre-training and fine-tuning (such as LLaMA-class models with AdamW), global L2 norm clipping at a threshold like `max_norm = 1.0` remains a common default. Even though AdamW normalises steps via second moments, occasional outlier gradients can still corrupt those running moment estimates and trigger loss spikes or NaN divergence.
Commonly confused terms
| Term | How it differs |
|---|---|
| Per-sample gradient clipping (DP-SGD) | Standard mini-batch gradient clipping bounds the norm of the aggregate batch gradient to stabilise optimisation, whereas per-sample gradient clipping in DP-SGD clips each individual example's gradient before aggregation to bound privacy sensitivity. |
| Weight clipping | Gradient clipping scales down excessive gradients to prevent exploding updates, whereas weight clipping directly clamps model parameters to a fixed interval (used in architectures like early Wasserstein GANs). |
Why gradient clipping matters for on-premises AI
In long training runs, rare but very large gradients can cause sudden loss spikes and collapse; gradient clipping is a standard safeguard against this. A collapse in training that may take days on in-house GPUs is a direct cost. Logging gradient norm throughout training reveals problem batches and the onset of instability. Too low a threshold slows learning, so it is set by looking at normal gradient sizes.
During continued pre-training of a language model on its in-house servers, a bank turns on gradient clipping and logs gradient norm each step; at steps where it spikes, the team checks which batch was processed and removes corrupt documents from the data.
Common questions
Why is gradient clipping needed?
At some steps gradients can grow huge enough to make updates that damage the model. Clipping limits the effect of those steps.
Does gradient clipping slow learning?
With a suitable threshold most steps are unaffected. Too low a threshold clips gradients constantly, slowing learning.
Related terms
- Gradient descent
- Gradient accumulation
- Backpropagation
- Stochastic gradient descent
- Learning rate
- Optimiser
Sources
- Pascanu, Mikolov and Bengio, On the difficulty of training Recurrent Neural Networks, 2012
- Goodfellow, Bengio and Courville, Deep Learning, 2016
In the glossary: Glossary, D to L