The interview problem
A training run occasionally produces large gradients. Compare global-norm clipping with coordinate clipping, calculate their effects on a two-parameter update, and explain the correct ordering with accumulation, mixed precision and an adaptive optimizer. Does clipping guarantee a small parameter update?
Original fundamentals practice, not a reported company interview. Numerical examples are hypothetical. PyTorch primary documentation retrieved September 27, 2026; publication date not established. Interview date: not applicable.
1. Define the operation and its boundary
For a finite gradient vector g and positive threshold c, global L2 clipping produces g′ = g × min(1, c/||g||₂), with zero gradients left unchanged. Treat all included parameter gradients as one concatenated vector. Coordinate clipping instead clamps each component into [−c, c]. A threshold number without its norm and parameter scope is incomplete.
Clipping changes the gradient supplied to the optimizer. It does not alter the forward loss, repair mislabeled data or prove that training is stable. Decide whether the norm includes the entire model or separate parameter groups. Clipping each layer independently is a different operation from clipping the whole model.
2. Calculate direction and update size
Let g = [3, 4], whose L2 norm is five, and c = 2. Global clipping multiplies both coordinates by 2/5, giving [1.2, 1.6] with norm two. Coordinate clipping gives [2, 2], whose norm is √8 ≈ 2.828. It satisfies the per-coordinate limit but not an L2 limit of two.
The original direction has coordinate ratio 3/4; global clipping preserves that ratio, while coordinate clipping changes it to one. For plain SGD with learning rate 0.1, no momentum and no other update terms, the globally clipped step is [−0.12, −0.16], norm 0.2. The coordinate-clipped step is [−0.2, −0.2], norm about 0.2828.
Now split [3, 4] into two single-parameter groups and clip each group's norm to two. The result is again [2, 2]. More generally, clipping K disjoint groups each to c permits a combined norm up to c√K. Group-level bounds are not a global bound.
3. Clipping and averaging do not commute
Consider two equally weighted scalar microbatch gradients, 10 and −8, with c = 1. The desired mean gradient is (10 − 8)/2 = 1; clipping that mean leaves one. Clipping each raw microbatch first produces 1 and −1, whose mean is zero. These lead to different optimizer updates.
For a large-batch objective, accumulate correctly normalized contributions at unchanged parameters, then clip the final gradient once. Account for unequal microbatch sizes or valid-token counts before clipping. If the loss changes from a mean to a sum, the gradient scale changes; a previously chosen threshold no longer has the same meaning.
Similarly, for replicated distributed training, clipping each rank's local gradient before averaging generally differs from clipping the averaged gradient. With sharded parameters, a true global norm requires combining squared norms over the unique shards with the correct process groups. Do not count replicated copies multiple times or assume a local norm is the model-wide norm.
4. Unscale before measuring the threshold
backward over the effective batch
↓
complete required gradient synchronization
↓
unscale gradients and apply intended normalization
↓
check finite values → compute norm → clip
↓
optimizer step → scaler update → clear gradientsPyTorch's mixed-precision guidance calls for unscaling before clipping and after gradients for the optimizer have accumulated. Its scaler tracks unscaling so the step does not repeat it. Follow the implementation's supported ordering, particularly with multiple optimizers or sharded training.
For g = [3, 4], suppose loss scaling multiplies gradients by 1,024. Clipping the scaled gradient to norm two and then dividing by 1,024 yields an unscaled norm of 2/1,024 = 0.001953125. The intended norm was two: clipping at the wrong stage makes the effective threshold 1,024 times too small.
Nonfinite values require explicit handling. Compute the norm with a numerically stable reduction and suitable accumulation precision: even finite components can overflow a naïve sum of squares. Multiplying infinity by a zero clipping factor can yield NaN rather than a repaired gradient. Skip or fail the update according to the training policy, record the event and investigate the source. Do not silently convert invalid gradients into apparent successful optimization.
5. A gradient bound is not every optimizer's step bound
The plain-SGD bound ||Δθ||₂ ≤ ηc holds only when the entire update is −ηg′. With momentum mt = 0.9mt−1 + g′, clipping the current gradient does not clip accumulated momentum. In one dimension, let previous momentum be ten and current clipped gradient one. New momentum is ten; at learning rate 0.1, the step magnitude is one, not 0.1.
For an Adam-style first update with zero initial moments, bias correction and negligible epsilon, the per-coordinate update is approximately −η sign(g′). For example, clipping [3, 4] to norm 0.01 gives [0.006, 0.008]. With η = 0.1 and epsilon much smaller than these components, the first update is approximately [−0.1, −0.1], with norm 0.1414. That exceeds the plain-SGD bound ηc = 0.001. Adaptive normalization changes the relation between gradient magnitude and step size. Later updates also depend on moment history.
Weight decay can add another change not covered by a clipped data-gradient bound. If a strict update-size limit is required, specify an update-clipping algorithm and its interaction with optimizer state and decay. That is a different algorithm; do not promise it from ordinary gradient clipping.
6. Use clipping as a measured intervention
Log the norm before clipping, the applied multiplier, fraction of clipped steps, nonfinite events and actual parameter-update norm. A norm measured only after clipping can conceal how often the threshold activates. Compare validation quality and convergence under a controlled threshold sweep rather than selecting the smallest threshold that prevents visible spikes.
If clipping activates on nearly every step, investigate loss normalization, learning rate, sequence lengths and objective weighting. A rare large gradient might reflect an informative hard example or a corrupted batch. Inspect batch metadata and representative loss terms without assuming either explanation. Clipping suppresses magnitude from both.
Check task imbalance too: one large-gradient objective can trigger global rescaling of every objective's contribution. Per-task clipping may be an intentional alternative, but changes the combined optimization rule. Document the decision and compare it against the original objective on real validation criteria.
7. Follow-ups and self-review
Follow-ups: Does norm clipping change direction? It preserves the included finite gradient vector's direction, but the optimizer may transform that direction. Does coordinate clipping bound L2 norm by c? No. Can a threshold transfer unchanged between summed and averaged loss? Generally not. Does no overflow mean correct training? No. Is clipping every microbatch equivalent to clipping the effective batch? The counterexample shows it is not.
Score 0–2 each for norm/scope definition, numerical direction comparison, accumulation ordering, mixed-precision handling and optimizer-step caveats. A strong answer states precisely which quantity is bounded.
Sources and next steps
- PyTorch: Automatic Mixed Precision examples — unscaling, clipping and accumulation ordering.
The numerical counterexamples and update-bound analysis are original. Continue with gradient accumulation and optimizer momentum.
Continue your preparation
Choose a connected learning path or browse more ML fundamentals questions. Read our editorial method and attribution policy.