Sam Babichenko

Reading notes / NanoGPT speedrun

Small-scale proxies for large-scale Transformer training instabilities

Wortsman et al., 2023

Context. Very large transformers showed failures never seen at small scale: attention logits that grow until the softmax collapses (ViT-22B) and output logits that drift away from the log-probabilities they represent (PaLM). Wortsman et al. reproduce both in models of a few million parameters at high enough learning rates, and the large-scale fixes work there too. Large-scale instabilities are high-learning-rate instabilities whose threshold moves down as the model grows, so a sweep of final loss against learning rate across small sizes is a cheap proxy, and an intervention that flattens that curve is one that will matter at scale. The speedrun trains a small model at very high learning rates, which is exactly this regime, and several of its changes are these fixes.

Setting: LR sensitivity as the proxy. Pre-norm transformers on C4 from 2.4M to 1.2B non-embedding parameters (plus a few 4.8B runs), trained with AdamW, are summarized by Eη[min⁡(ℓ(A(η)),ℓ0)−ℓ⋆],η∈{3⋅10−4,10−3,…,3⋅10−1},\mathbb E_{\eta}\big[\min(\ell(A(\eta)),\ell_0)-\ell^\star\big],\qquad\eta\in\{3\cdot10^{-4},10^{-3},\dots,3\cdot10^{-1}\}, the average excess of final loss over the best learning rate across three decades, with diverged runs capped at the initial loss ℓ0\ell_0. Everything is empirical, in one training setup.

Step 1

Attention logits blow up first, and QK-norm stops them. Without a fix, the learning rate at which runs diverge falls with size, and the maximum attention logit climbs by orders of magnitude before the loss blows up. The growth is in the norms of queries and keys, while their cosine similarity does not rise; the authors' explanation is that attention logits are the only quantity quadratic in the parameter scale, ⟨XW1,XW2⟩\langle XW_1,XW_2\rangle, so they are the first to grow large as the weights grow. Forcing a 10M model's logits to a fixed scale of 10410^4 makes it worse than a bigram model, so the saturated softmax is the cause. Every run whose maximum logit at step 2000 exceeded 10410^4 diverged, and a fit of that maximum against log size predicted the 4.8B divergence at LR 0.01 before that model was trained. Normalizing queries and keys per head before the dot product removes the failure, and a 1.2B model then trains at LR 0.3. The paper uses LayerNorm with learned gains; the speedrun uses a parameter-free RMS form, under which every query and key has norm dh\sqrt{d_h} and Cauchy-Schwarz bounds each logit by the scale times dhd_h.

Step 2

Output logits drift unless something holds log⁡Z\log Z near zero. The cross-entropy is invariant to adding a constant to every output logit, so the mean logit lies along a flat direction and nothing pulls it back. Without weight decay, at high learning rates and every size tested, the logits drift strongly negative late in training and the loss degrades. PaLM's z-loss adds a penalty that pulls log⁡Z\log Z back toward zero, L=CE(y,target)+10−4 (log⁡Z)2,Z=∑jeyj,\mathcal L=\mathrm{CE}(y,\text{target})+10^{-4}\,(\log Z)^2,\qquad Z=\textstyle\sum_je^{y_j}, and removes the instability; weight decay also removes it for the larger models. A logit softcap blocks the same drift by bounding the logits (my connection; the paper does not test caps).

Step 3

Gradients shrink until AdamW's ϵ\epsilon dominates the denominator. The gradient RMS falls with model size and learning rate, and at 4.8B and LR 0.3 it reaches about 10−810^{-8}, AdamW's default ϵ\epsilon. In m/(v+ϵ)m/(\sqrt v+\epsilon), once v≈ϵ\sqrt v\approx\epsilon the update shrinks well below its intended scale and the layer stops learning, and the measured update RMS drops when the gradient RMS crosses ϵ\epsilon. Lowering ϵ\epsilon to 10−1510^{-15} improves the 4.8B loss; raising it to 10−610^{-6} makes the run diverge. The proposed mechanism is output-norm growth: the residual stream's RMS grows with depth and learning rate, and the final layernorm divides the gradient it passes back by that RMS.

Step 4

How standard hyperparameter choices change LR sensitivity. With qk-layernorm and z-loss in place, other interventions barely move the range of stable learning rates but change the loss inside it. Longer warmup lowers LR sensitivity, most for the larger models; weight decay applied independently of the learning rate lowers it relative to the PyTorch default, where a learning-rate sweep also sweeps the decay; and depth raises it faster than width. μ\muParam keeps the best learning rate fixed across width but neither lowers sensitivity nor improves loss, and it does not remove the need for qk-layernorm. More steps or a larger batch leave sensitivity unchanged.

Step 5

Which of these fixes the speedrun adopted. QK-norm came in at record 5, alongside other changes. Because it fixes the query and key norms, the logit scale (the softmax temperature) becomes a hyperparameter, and record 20 set it to 0.12 instead of 1/dh1/\sqrt{d_h} (about 3 s; with dh=128d_h=128 that caps logits near 15 instead of 11). For the output logits the speedrun uses a cap: 30tanh⁡(z/30)30\tanh(z/30) at record 9, cap 15 at record 18 (about 10 s, 1490 to 1390 steps), now an asymmetric 23 σ((z+5)/7.5)23\,\sigma((z+5)/7.5); these gains are steps saved, so the cap does more than prevent divergence, which is outside this paper. Adam's ϵ\epsilon went from 10−810^{-8} to 10−1010^{-10} at record 20 (about 10 steps), by the same mechanism for a different cause: with a zero-initialized head, the gradients reaching the Adam-trained embeddings start near zero, below ϵ\epsilon. Muon normalizes whole matrices and has no per-coordinate ϵ\epsilon in this role.

Limits. The paper studies only slow divergences (fast loss spikes are out of scope), in one codebase with one data set and optimizer; LR sensitivity does not reflect the overall loss level and meaningless for interventions that change what the learning rate means; and the mechanisms (quadratic dependence, output-norm growth) are hypotheses supported by measurements.

Terms

LR sensitivity
Eη[min⁡(ℓ(A(η)),ℓ0)−ℓ⋆]\mathbb E_\eta[\min(\ell(A(\eta)),\ell_0)-\ell^\star] over three decades of learning rate
attention-logit growth
max⁡∣zij∣\max|z_{ij}| grows with q,kq,k norms; past about 10410^4 the softmax collapses and training fails
qk-layernorm
normalize queries and keys per head before the dot product; logits bounded by scale × dh\times\,d_h
output-logit divergence
output logits drift along the flat shift direction of the cross-entropy, late in training
z-loss / softcap
10−4(log⁡Z)210^{-4}(\log Z)^2 keeps the normalizer near 1; a cap bounds the logits and blocks the same drift (my connection)
AdamW ϵ\epsilon problem
gradient RMS shrinks to about ϵ\epsilon at scale, so m/(v+ϵ)m/(\sqrt v+\epsilon) shrinks
independent weight decay
θ←θ−st(ηΔ+λθ)\theta\leftarrow\theta-s_t(\eta\Delta+\lambda\theta): decay not scaled by the peak learning rate
logit-max prediction
quadratic fit in log size forecast the 4.8B divergence at LR 0.01
speedrun fixes
QK-norm (record 5), scale 0.12 (20), softcap (9, 18), Adam ϵ=10−10\epsilon=10^{-10} (20)

Quiz

  1. Why does the paper study instabilities by sweeping the learning rate in small models instead of training large ones?
  2. With parameter-free RMS normalization of queries and keys in a head of dimension dhd_h, what limits the attention logits?
  3. What did μ\muParam change in the paper's learning-rate sweeps?

Sources