Transformer Training — Presentation 07

Stability: Spikes and Cures

Large runs fail in two ways small ones rarely show: the loss jumps, or part of the model quietly saturates. Both reproduced on a tiny model: attention-logit growth and QK-norm, z-loss, one bad batch, clipping and skipping, and what large runs report doing.

Loss spike Attention-logit growth QK-norm z-loss Gradient clipping Batch skipping Adam β2
Spike → Diagnose → Guard → Restart
00

Topics We’ll Cover

Live companion and sources

This deck follows chapter 8, stability, where a run at ten times the default learning rate shows one attention head collapse onto single characters. Measured numbers: results.md §8a–8b.

01

Two Ways Large Runs Fail

Loud: a loss spike

The loss suddenly jumps, and either recovers or never comes back. Often one step with a huge gradient throws the weights somewhere bad.

Quiet: saturation

The loss stops improving because part of the model has saturated: attention heads that put all their weight on one position pass no gradient back, so they stop learning.

Both can be reproduced on the Decoder’s 5,376-parameter model, three seeds each, and both have standard cures.

02

Attention-Logit Growth and Entropy Collapse

At a high learning rate the query and key weights grow, the scores \(q^\top k/\sqrt{d_k}\) grow with their product, and the softmax becomes a hard maximum: each position attends to exactly one other, the attention entropy falls towards zero, and the gradient through the softmax vanishes (deck 03). Dehghani et al. (2023) saw exactly this diverge an 8-billion-parameter vision transformer; Wortsman et al. (2023) showed the same instability in small models at high learning rates.

Peak lrRunFinal validation lossLargest scoreEntropy (nats)
0.01baseline2.277 ± 0.09042.20.62
0.01QK-norm2.272 ± 0.03311.00.60
0.01z-loss 1e-32.310 ± 0.11367.50.38
0.1baseline2.571 ± 0.10216,104.70.05
0.1QK-norm2.102 ± 0.03924.40.34
0.1z-loss 1e-32.589 ± 0.10219,648.60.05

AdamW, warmup 50 + cosine, 2,000 steps, clip 1.0, three seeds; largest attention score and mean attention entropy on the validation windows at the end (results.md §8b). At 0.1 the baseline does not blow up; it learns less.

03

A Saturating Softmax, Live

Eight attention scores for one query, multiplied by a growing scale (as the \(W_Q, W_K\) weights grow). Watch the weights, the entropy \(H = -\sum_j p_j \log p_j\) and the size of the gradient the softmax passes back.

The upstream gradient \(\delta P\) is fixed in this demo; only the scale changes. Toy numbers, chosen for the picture; the measured run is on the previous slide.

04

The Cures: QK-Norm and z-Loss

QK-norm

Apply a LayerNorm to each head’s queries and keys before the dot product, so the scores cannot grow just because the weights do (only the norm’s gain can scale them). At learning rate 0.1 the largest score stays at 24.4 and the run reaches 2.102 ± 0.039: the best of every run here, because the higher learning rate is now usable.

z-loss

PaLM added \(10^{-4}\log^2 Z\) to the loss (Chowdhery et al. 2022; here \(10^{-3}\)), pulling the output softmax’s normaliser \(\log Z\) towards 0. It targets a different instability, output logits drifting, and does nothing for attention: the largest score still reaches 19,649.

$$\begin{gathered}\hat q = \operatorname{LN}(q),\quad \hat k = \operatorname{LN}(k) \\ L = L_{\text{CE}} + c\,\overline{(\log Z)^2}\end{gathered}$$

The plainest cure is a lower learning rate, which costs speed.

05

A Loss Spike: One Bad Batch

Train with momentum (no adaptive scaling) at learning rate 0.5, and at step 1,000 feed one batch that is nothing like the book: every window the character “@” repeated. Its gradient is huge. 1,400 steps, constant learning rate, three seeds (results.md §8a):

RunlrValidation loss beforeJust afterAt the end
momentum, no clipping0.52.334, 2.424, 2.29617.138, 8.175, 14.494diverged, diverged, diverged
momentum, clip at 1.00.52.424, 2.454, 2.3592.477, 2.598, 2.4482.325, 2.351, 2.318
momentum, skip the batch0.52.334, 2.424, 2.2962.334, 2.424, 2.2962.322, 2.276, 2.306
AdamW (lr 0.01), no clipping0.012.367, 2.329, 2.4352.377, 2.342, 2.4412.229, 2.250, 2.282

Without protection the step throws the weights far off and every seed diverges. AdamW at its usual learning rate barely notices the batch even unclipped: Adam divides each step by the running size of past gradients, so one large gradient cannot make one large step.

06

Clipping and Skipping

$$g \leftarrow g \cdot \min\Big(1, \frac{c}{\lVert g\rVert + 10^{-6}}\Big)$$

Gradient clipping

Rescale any gradient whose global norm exceeds a threshold \(c\) (Pascanu et al. 2013; PyTorch’s clip_grad_norm_; \(c = 1\) here and in the default run). The bad batch now moves the loss only a little, and the run carries on.

Skipping

Skip a batch whose gradient norm is far above the recent average (here 4×): the step never happens, so the loss does not move at all.

Why momentum is fragile: its step is \(\eta\) times a running sum of gradients, so one gradient \(k\) times too large moves the weights \(k\) times too far. Adam’s step \(\eta\,\hat m/\sqrt{\hat v}\) is bounded by about \(\eta\) per coordinate.

07

What Large Runs Report

So the toolkit is several guards at once

Warmup, clipping, QK-norm or z-loss, a lower \(\beta_2\), monitoring the gradient norm, and checkpoints to restart from (deck 13). The site’s notebook 08 repeats the QK-norm / z-loss comparison on a width-512 model on a GPU: GPU run pending.

08

What to Take Away