Skip to content
mlmentorship

Gradient clipping

Cap the norm of the gradient before each optimizer step. The simplest and most reliable defense against training instability.

Published · 4 min read ·Core ·Intermediate

Visual quick review

Visual first · depth when needed

Explain geometrically why global L2-norm clipping shortens an over-threshold gradient while preserving its direction.

Preparing the visual…

Summary

Gradient clipping rescales the gradient vector before the optimizer step so that its global norm does not exceed a fixed threshold . If , replace with ; otherwise leave it unchanged.

Learning objective

See why global-norm clipping shortens a gradient without turning it

Global-norm clipping as a radial projection A dashed original gradient g equals 6 comma 8 and extends from the origin beyond a circular norm limit. Its norm is 10 while the threshold c is 5. A solid clipped gradient follows the same ray and stops halfway at the circle, at 3 comma 4 with norm 5. Direct labels, different endpoint shapes, and solid versus dashed lines distinguish the vectors without color. origin norm limit c = 5 clipped g′ original g ORIGINAL g = (6, 8) ‖g‖₂ = 10 SCALE EVERY COMPONENT c / ‖g‖₂ = 5 / 10 RESULT g′ = (3, 4) ‖g′‖₂ = 5 · same ray
Read it this way: begin with the dashed gradient g, whose norm 10 lies outside the radius-5 limit. Multiplying every component by the same factor, 5/10, moves its endpoint radially to the circle at (3, 4). The solid clipped vector is shorter but stays on the same ray, so global-norm clipping preserves direction; clipping components independently need not. The dashed line, square and triangle endpoints, positions, and direct labels carry the explanation without color. Original schematic based on Pascanu, Mikolov, and Bengio (2013) and the PyTorch global-norm definition.

Training instabilities. Loss spikes, NaN gradients, exploding updates. Are usually caused by a single batch with anomalous gradients. Without clipping, that one bad step can drive parameters into a region from which training never recovers. Clipping bounds the worst-case update and turns a divergence into a recoverable hiccup.

Standard in: transformer pretraining (always), most RL training (always), RNN training (originally proposed for RNNs by Pascanu et al., 2013, where exploding gradients are intrinsic).

Two flavors

Global-norm clipping (the standard)

Compute the L2 norm of the concatenated gradient vector across all parameters:

If , scale every parameter’s gradient by . This preserves the direction of the gradient (just shrinks magnitude). is the dominant default for transformer training.

Per-parameter clipping

Clip each parameter’s gradient norm independently. Simpler but distorts the gradient direction; rarely used.

Value clipping

Clip individual elements of to a range . Distorts direction even more; mostly historical.

How to pick the threshold

  • Transformers: is the universal default. Llama, Mistral, Qwen, GPT all use 1.0.
  • RNNs / LSTMs: between 0.25 and 5; needs tuning.
  • RL: depends on reward scale and policy parametrization; often .
  • Diagnostic: log over training. If it almost never exceeds , the clip is inactive (try lower); if it always does, the clip is destroying signal (try higher).

Combined with mixed precision

In FP16/BF16 mixed precision, the loss is scaled before the backward pass to keep small gradients representable. Clipping must be applied on the unscaled gradients (after the scaler unscales them). PyTorch’s GradScaler and similar tooling enforce this ordering.

Common pitfalls

  • Clipping per parameter group instead of globally. Gives a different effective clip for each parameter; rarely intended.
  • Forgetting to unscale before clipping under AMP. The clip threshold is meaningless if applied to scaled gradients.
  • Setting clipping too aggressive. for a transformer cripples training; you’ll see flat loss curves with the clip always active.
  • Treating clipping as a fix for a buggy data pipeline. A consistent stream of large gradients usually indicates a data or initialization problem, not a clipping problem.