Skip to content
mlmentorship

Gradient clipping

Project a finite global gradient onto a norm ball, then distinguish that bound from the optimizer's final parameter displacement.

Published · 5 min read ·Core ·Intermediate
Reading depth

Summary

Global-norm clipping shortens a finite gradient by one common factor, preserving its direction when the factor is positive. For threshold , use , leaving a zero vector unchanged. This caps the gradient norm, not every optimizer’s eventual update.

The fixture has , norm ten, and threshold five. Scaling by one-half produces with norm five. Ordinary SGD with learning rate moves by , norm .

Learning objective

Shorten the whole gradient vector without changing its direction

Shorten the vector without turning itThe vector (3,4) has norm 5. Clipping to norm 2 multiplies every component by 2/5, giving (1.2,1.6).Pull inward along the same ray(3,4) becomes (1.2,1.6)Length 5 becomes 2. Direction stays.
Derivation
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: the dashed gradient (6, 8) has norm ten. A common factor one-half places (3, 4) on the radius-five circle. The solid vector retains its direction. The optimizer still transforms it before changing parameters. Original schematic follows Pascanu, Mikolov, and Bengio (2013) and the PyTorch global-norm definition.
Check: Why does global clipping produce (3,4), rather than independently truncating both coordinates to 5?
Read it this way: The vector (3,4) has norm 5. Clipping to norm 2 multiplies every component by 2/5, giving (1.2,1.6).
Does clipping rotate this gradient toward an axis?

Among vectors inside the radius- Euclidean ball, radial projection minimizes distance to . Cauchy-Schwarz makes the nearest nonzero candidate parallel to ; its permitted length is . This geometric guarantee neither diagnoses a loss spike nor makes a nonfinite gradient usable. Check finiteness before stepping; infinity multiplied by zero does not produce a repaired direction.

Two flavors

Global-norm clipping (the standard)

Compute the norm over the declared optimizer parameter set after its intended accumulation and synchronization:

Use that same factor for every included tensor. For sharded gradients, aggregate squared norms over unique shards; replicated data-parallel copies are not additional parameter coordinates.

Per-parameter clipping

Separate tensor caps change relative scaling and generally rotate the concatenated gradient.

Value clipping

Coordinate clipping to turns into , with norm and a different direction. That is a coordinate bound, not the radius-five constraint.

How to pick the threshold

Log the preclip norm, nonfinite frequency, clipping frequency, and actual optimizer update norm. Threshold units depend on loss reduction, parameterization, and batch accumulation. Rare clipping may be an intentional safety limit; frequent clipping warrants checking the intended learning-rate and loss-scale interaction. Validate a threshold against the training objective rather than treating an architecture-wide number as universal.

Combined with mixed precision

If loss scaling is used, finish accumulation and unscale once before clipping. Otherwise the threshold applies to scaled units; dividing afterward changes the effective cap. BF16 frequently runs without a loss scaler; format choice alone does not imply one.

Change the Requirement

Bound Parameter Movement

New task: Cap the final optimizer displacement rather than its input gradient.

Picture and change: Inspect momentum, preconditioning, learning rate, and decay before choosing an update-level constraint.

Check: With velocity and rule , the fixture gives , not a norm-five update.

Use when: The requirement concerns actual parameter movement. The bound applies to ordinary SGD without those additional transformations.

Accumulate Before Clipping

New task: Combine several microbatches into one optimizer step under a declared summed-loss convention.

Picture and change: Sum contributions first, then clip the combined gradient.

Check: Scalar contributions and sum to ; clipping each to five before adding instead gives zero.

Use when: Multiple backward calls represent one update. For mean loss, apply the correct sample-count normalization; averaging unequal batch means defines a different gradient.

Common pitfalls

  • Wrong constrained object. Tensor, coordinate, example, gradient, and update clipping solve different problems.
  • Wrong units. Unscale before measuring the threshold when using AMP scaling.
  • Invalid directions. Skip or diagnose nonfinite gradients; do not expect a norm projection to repair them.
  • Missing diagnosis. Repeated large gradients can reflect data, initialization, objective, or numerical issues; clipping alone identifies none.

Reconstruct

Does threshold one bound every AdamW step by its learning rate?

No. Saved moments, coordinate scaling, and decoupled decay also contribute to parameter movement. The ordinary-SGD bound cannot be transferred without inspecting the actual update rule.