Concepts / Linear Warmup in LLM Training

Linear Warmup in LLM Training

Training a large language model means repeatedly nudging its parameters so that its predictions improve.

  • Programming

One Outlier Gradient Can Derail a Run

Training a large language model means repeatedly nudging its parameters so that its predictions improve. Each nudge is driven by gradients, and most of the time those gradients are well behaved. Occasionally, however, a gradient becomes far larger than the rest, and a single update built from it can push the parameters much further than intended. The result is an unstable training run.

This article focuses on one remedy, gradient clipping, and on what it does to the parameters that training is trying to adjust. Clipping is a technique used to prevent exploding gradients by limiting the gradient norm to a maximum value.

Direction, Magnitude, and the Norm

Definition

Gradient clipping: a technique used to prevent exploding gradients by limiting the gradient norm to a maximum value.

A gradient tells training how each parameter should change to reduce the error. It has a direction, which says whether a parameter should go up or down, and a magnitude, which says how strongly. The overall size of the gradient is often described by its norm, a single number that summarizes how large the gradient is taken as a whole. Written compactly, the norm of a gradient vector $g$ is $\|g\|$.

The key idea behind clipping is to put a ceiling on that overall size. Call the ceiling the maximum norm. If the gradient is already within the ceiling, nothing is done to it. If it exceeds the ceiling, it is scaled back so that its size no longer goes past the maximum. Clipping therefore acts on the size of the whole gradient, not on each parameter separately.

Where Clipping Sits in One Training Step

Gradient clipping is not used alone in the improved version of the training loop. The source describes an updated training function that takes the earlier train_model_simple function and adds three concepts: linear warmup, cosine decay, and gradient clipping. Clipping is the piece that directly limits the size of gradients, while linear warmup and cosine decay are the other two additions introduced alongside it.

The flow below traces one iteration. The backward pass produces gradients, clipping limits their overall size, and the optimizer then uses the clipped gradients to update the parameters. The learning-rate schedule, which is where warmup and cosine decay live, also feeds into the update.

Tracing a Gradient Above and Below the Ceiling

The clearest way to see clipping is to follow one gradient through it. Suppose the gradient has two components, 3 and 4, and the maximum norm is 1. The norm of this gradient is 5, which is well above the ceiling, so clipping applies. The example below uses illustrative numbers that are not from the source; they are chosen so the arithmetic is easy to check.

Clipping scales the whole gradient by the same factor, here 1/5, so that the new norm equals the maximum of 1. Both components shrink, and the ratio between them stays the same: 3 and 4 become 0.6 and 0.8. The largest entry went from 4 to 0.8, which is substantially smaller, matching what the source describes for the largest gradient value after clipping with a maximum norm of 1.

Worked Check on the Largest Value

Worked example

Largest gradient value after clipping

A gradient has components 3 and 4 and a maximum norm of 1. Find the largest component after clipping and compare it with the largest component before clipping.

  1. 1
    Find the norm

    The norm is the square root of 3 squared plus 4 squared, which is the square root of 25, so the norm is 5.

  2. 2
    Compare with the ceiling

    The norm 5 is greater than the maximum of 1, so the gradient exceeds the ceiling and must be scaled back.

  3. 3
    Apply the scale

    Multiply every component by 1 divided by 5. Component 3 becomes 0.6 and component 4 becomes 0.8.

  4. 4
    Check the new norm

    The new components give a norm of the square root of 0.36 plus 0.64, which is 1. The new norm equals the maximum.

Result

The largest component fell from 4 to 0.8, and the clipped gradient has norm exactly 1.

Mistakes Beginners Make with Clipping

  • Common mistake

    Believing clipping shrinks every gradient

    What it looks like

    A gradient with norm 0.5 and maximum norm 1 is left exactly as it is.

    Why it fails

    Clipping only acts on gradients that exceed the maximum. Smaller gradients are left unchanged.

    Do this instead

    Check the norm against the maximum first. Clipping is applied only when the norm is above the ceiling.

  • Common mistake

    Thinking clipping caps each component separately

    Why it fails

    The source describes limiting the overall gradient norm, so the scaling applies to the whole vector at once.

    Do this instead

    Think of clipping as one scale factor applied to the entire gradient, not as a separate cap on each entry.

  • Common mistake

    Assuming clipping changes the update's direction

    What it looks like

    Components 3 and 4 become 0.6 and 0.8, keeping their ratio.

    Why it fails

    Scaling every component by the same factor keeps the proportions between them.

    Do this instead

    Clipping changes how large the update is, not which way the parameters are pushed.

  • Common mistake

    Treating clipping as a replacement for warmup or cosine decay, when the source presents all three as additions in one training function

Predict Before You Check

What do you think happens?

A gradient has norm 0.5, and the maximum norm is 1. After clipping, is the gradient changed?

  • Yes, it is scaled down to norm 1

  • No, it is left unchanged

  • Yes, it is scaled up to norm 1

Reveal answer
Answer:

No, it is left unchanged

Clipping acts only on gradients that exceed the maximum. A norm of 0.5 is below 1, so nothing is done to it.

EASY

A gradient has norm 8 and the maximum norm is 2. Using the same scaling idea as the worked check, what is the norm of the clipped gradient, and by what factor is every component multiplied?

Hints
  • Compare the norm 8 with the maximum 2 first.
  • The clipped norm should equal the maximum.
  • The factor is the maximum divided by the original norm.

Key Takeaways

  • Gradient clipping limits the gradient norm, which is the overall size of the gradient, to a maximum value, which helps prevent exploding gradients from producing very large parameter updates.
  • Clipping acts only when the norm exceeds the maximum; a gradient already within the ceiling is left unchanged.
  • With a maximum norm of 1, the largest gradient value after clipping is substantially smaller than before, because the whole gradient is scaled back together.
  • Within the modified training function, clipping sits between the backward pass and the optimizer update, alongside linear warmup and cosine decay as the three additions that help stabilize LLM training.

Related concepts

More concepts starting with L