Training deep Transformers stably
You have built the block, counted it, and stacked it. Now train it. With 12 layers, almost anything works. With 48 layers and a learning rate that worked at 12, the loss falls for a few hundred steps, then shoots up and never comes back. Or it trains for a week and then, at step 80,000, spikes by a full nat for no visible reason. Large-model logbooks are full of such events. PaLM's authors report around 20 loss spikes while training their largest model, each handled by rewinding to an earlier checkpoint and skipping the batches that followed, and in DeepNet's translation experiments an ordinary post-LN Transformer with 6 encoder and 6 decoder layers trained fine while the same model with 18 and 18 diverged.
This section is about why deep Transformers become unstable and what keeps them stable. Some causes we have met already, gradients that grow with depth (§5.1) and the post-LN gradient profile (§5.2). Here we add three more, an initialization that does not respect depth, the first steps of Adam, and attention that collapses onto single tokens. We finish with the practical question of how deep a model should be at all. The full operational toolkit for pretraining runs, including loss-spike recovery, z-loss, and monitoring, is the subject of §8.8. This section explains the mechanisms that toolkit is built on.
Initialization that respects depth
GPT-2 initializes every weight matrix from (the value in OpenAI's released code, inherited from the first GPT paper), with one exception. The paper scales the weights of residual layers at initialization by one over the square root of the number of residual layers. In code (nanoGPT and many others) that means the output projection of each residual branch, attention's and the FFN's , gets standard deviation
since a model with blocks has residual branches.
Here is the derivation, which is §5.1's argument with numbers. In a pre-LN model each branch reads a normalized input, so its write at initialization is roughly a random vector whose per-coordinate variance is proportional to , about for a constant set by the branch's internal width and activation. The writes are close to independent, so variances add,
With a fixed the stream's variance grows linearly with depth. With , the total added variance is no matter how deep the model is.
In plain words, if branches each add a random push, make each push times smaller so that their sum has the same size for every depth.
In real numbers, GPT-2 small () starts its residual projections at , and GPT-2 XL () at . The rule touches only the two matrices whose output is added to the stream (both are called c_proj in GPT-2's code). Why those, rather than every matrix in the branch? Shrinking the last matrix shrinks the write in proportion and changes nothing else. Shrinking the input projections instead would also shrink the attention logits, which depend on quadratically, and would move the FFN's pre-activations toward the nearly linear part of GELU, changing what the branch computes rather than only how loudly it writes. Section 0.7 previewed this rule, and §6.2 applies it when it initializes a whole GPT.
The same scaling keeps the backward pass bounded. In §5.1 we showed that a stack of residual branches with Jacobian gain amplifies gradients by about , and that keeps this near . Shrinking each branch's output matrix by shrinks its gain the same way.
Unscaled, the stream ends up 1.9 times its starting size at 12 layers and about 4.55 times at 96. Scaled, it stays at 1.05 for every depth, matching the formula. Why is the base standard deviation 0.02? It is a fixed choice that GPT-2 inherited from the first GPT, close to for widths in the low thousands. A fixed number cannot be right for every width, and principled width-dependent choices (including different scalings for the embedding, hidden, and output layers) are what μP derives in §9.5.
GPT-2 initialization with scaled residual projections
Write gpt2_init_block(d, n_layer, rng) returning one block's parameters as a dictionary with the keys of §5.6 (ln1_g, ln1_b, W_qkv, b_qkv, W_o, b_o, ln2_g, ln2_b, W_fc, b_fc, W_proj, b_proj) and shapes , , , for the matrices. Every matrix is except the two residual output projections W_o and W_proj, which use . Biases start at 0, LayerNorm gains at 1 and shifts at 0.
The family of depth-aware schemes
GPT-2's rule is one of several ways to keep a deep residual stack near the identity at the start. They all shrink, gate, or rebalance the branches relative to the skip path.
| scheme | what it does | idea |
|---|---|---|
| GPT-2 scaled init (Radford et al., 2019) | output projections at | total added variance independent of depth |
| ReZero (Bachlechner et al., 2020) | with a learned scalar initialized to 0 | every block starts as the identity, no normalization needed |
| T-Fixup (Huang et al., 2020) | depth-dependent scaling of initial weights, no LayerNorm | removes the need for warmup and norms in translation models |
| Admin (Liu et al., 2020) | rescales the skip path in post-LN using statistics from a forward pass | controls the amplification effect of §5.2 |
| DeepNorm (Wang et al., 2022) | post-LN with , , and some weights initialized smaller by (decoder-only) | bounds the size of updates, trained 1,000-layer models |
DeepNorm's constants come from requiring that a single optimizer step changes the model's output by a bounded amount, whatever the depth. That is the right way to think about every entry in the table. Stability is about how much the function changes per update, and depth multiplies small per-layer changes into large ones unless something compensates.
Warmup, and why Adam needs it here
Almost every Transformer recipe starts with a learning-rate warmup. GPT-3 ramped linearly over its first 375 million tokens, and LLaMA 2 over 2,000 steps. Section 5.2 showed one reason, post-LN's large top-layer gradients. There is a second reason that applies to every architecture trained with Adam (§0.6), and it is visible in the very first step.
Adam updates each coordinate by , where and are bias-corrected moving averages of the gradient and the squared gradient, and is a small constant. As §0.6 showed, at step 1 bias correction makes and exactly, so
In plain words, Adam's first step moves every parameter by the full learning rate, in the direction of its gradient's sign, whether that gradient is meaningful or pure noise.
For the next few dozen steps is an average over very few squared gradients, so it is a noisy estimate, and dividing by its square root keeps the steps large and erratic. Liu et al. (2019) quantified this, showing that the variance of Adam's adaptive step size is large early in training, and proposed RAdam to correct it. Warmup does the same job more crudely. It keeps small until has seen enough gradients to be trusted, and it lets the model move away from its fragile initial state gently. The derivation of warmup length from Adam's variance is in §8.7.
A gradient of and a gradient of produce the same first step, . On a mostly-noise gradient stream, the first ten steps are on average more than twice as large as steps later in training. Warmup exists to tame exactly those early steps.
Attention entropy collapse
The third failure lives inside attention. Each row of an attention matrix is a probability distribution over the keys, and its entropy,
measures how spread out it is. Uniform attention has entropy , and attention on a single key has entropy 0. Zhai et al. (2023) tracked the entropy of every head during training across vision, speech, translation, and language models, and found a consistent pattern. Training instability, whether oscillating loss or outright divergence, was accompanied by pathologically low attention entropy. They called it entropy collapse.
Why would low entropy hurt? Look at what the softmax passes back. The Jacobian of with respect to the logits is
which you derived in §4.3. When approaches a one-hot vector , this becomes . The gradient with respect to the queries and keys vanishes, so the head cannot learn its way out, while a small change in the weights can still flip which key wins and change the output abruptly. A collapsed head is both stuck and brittle.
Where collapse comes from, and a bound you can derive
Attention logits are . Nothing in a pre-LN Transformer stops and from growing during training, and if they grow, the logits grow in proportion to the product of their norms. Sharp logits mean low entropy. Here is a bound that makes the dependence exact.
Suppose every logit in a row lies in . The largest attention weight is at most what it would be with one logit at and all others at ,
Shannon entropy is never smaller than , because . Therefore
In plain words, if the logits are confined to a range of size , attention cannot collapse much. As the range grows, the guaranteed entropy falls off like , exponentially fast.
Zhai et al. prove a bound of the same exponential form stated through the weights, which falls exponentially fast as the spectral norm of the attention logits grows (they control that norm through and the size of the inputs), and show it is tight. Both bounds say the same thing. Keep the effective scale of the logits under control and entropy cannot collapse.
The table tells the story. As the logit scale grows, entropy falls from near toward zero and the average top weight rises past 0.9. The softmax Jacobian first grows, since attention becomes selective, and then shrinks as the rows harden into one-hot vectors and stop passing gradient back. The bound is loose for any single row, but it is always below the true entropy.
Two cures, σReparam and QK-norm
Zhai et al.'s fix, σReparam, reparameterizes every linear layer's weight as
where is the largest singular value of (its spectral norm) and is a learned scalar (the code below starts it at 1). The spectral norm is estimated cheaply by the power iteration of §0.10, run on without forming it, one step per training step with a persistent vector, as in spectral normalization (Miyato et al., 2018). Now the scale of every layer is controlled by a single number that the optimizer moves deliberately, rather than by the drift of thousands of weights. With it, Zhai et al. trained Vision Transformers to competitive accuracy without warmup, weight decay, LayerNorm, or adaptive optimizers.
The more widely used cure is QK-norm, which normalizes each head's query and key vectors (with LayerNorm or RMSNorm) before the dot product. The logit then has a magnitude fixed by the norms' gains, so it cannot grow just because and did. Dehghani et al. (2023) found that vision Transformers of around 8 billion parameters diverged because their attention logits grew so large that attention became almost one-hot, with near-zero entropy, which is entropy collapse in the wild. QK-norm fixed it for their 22-billion-parameter model. Wortsman et al. (2023) reproduced this "attention logit growth" in small models trained at high learning rates and showed the same fix works. QK-norm is now standard in many recent models, and §8.8 covers it with its relatives.
Attention entropy, stable and masked
Write attention_entropy(scores) that takes raw attention logits of shape (..., T) (masked positions are -np.inf), applies a softmax over the last axis, and returns the entropy of each row, shape (...,). It must be numerically stable for huge logits and must treat masked entries as contributing exactly zero, since .
The softmax Jacobian and how it vanishes
Write softmax_jacobian(z) for logits of shape (..., T), returning the Jacobians with shape (..., T, T), where . Use a stable softmax. Then convince yourself (the tests check it) that the Jacobian vanishes as the logits are scaled up.
Spectral norm by power iteration, and σReparam
§0.10 derived power iteration and its convergence rate (Exercise 0.10c builds it for a symmetric matrix). Here you adapt it to what σReparam needs, the spectral norm of a rectangular weight with a warm-started vector that survives between training steps. Implement two functions.
spectral_norm(W, n_iter=50, u=None)estimates by power iteration and returns(sigma, u), whereuis the final unit vector in the input space ofW(lengthW.shape[1]). Start fromuif given, else from a vector of ones. Each iteration computes , then , and the estimate is .sigma_reparam(W, gamma, u, n_iter=1)runsn_iterpower-iteration steps fromuand returns(W_hat, u_new)with .
Prove the entropy bound is tight
Write entropy_floor(s, T) returning the lower bound computed stably for large (use np.log1p), and worst_logits(s, T) returning a length-T logit vector inside whose softmax entropy is as small as possible. The tests check that your bound is never violated by random logits in the range, and how close your worst case comes to it. (Ask the AI tutor why the bound cannot be achieved exactly for most , and what the true minimum looks like.)
Logit growth at the output
The final softmax over the vocabulary has its own version of the problem. Softmax is unchanged when a constant is added to every logit, so nothing in the cross-entropy loss pins down the overall level of the output logits. During long runs their normalizer can drift far from zero, and in 16-bit arithmetic that drift eventually costs precision and stability. PaLM added an auxiliary z-loss, , which gently pulls toward zero, and Wortsman et al. (2023) showed that this output-logit divergence also appears in small models at high learning rates and that z-loss fixes it. Gemma 2 took a blunter approach, soft-capping both attention and final logits with . Both are covered in §8.8. The thread connecting them to attention entropy collapse is that a Transformer has several places where a scale is left free by the loss, and every free scale is a potential runaway.
Depth versus width
Stability is one reason not to make a model arbitrarily deep. It is worth knowing the others, because "how should I shape a model with parameters?" is a common design question.
Quality depends only weakly on shape. Kaplan et al. (2020) found that at a fixed non-embedding parameter count, loss varies by only a few percent across a wide range of shapes. The aspect ratio could change by a factor of 40 with little effect, and a 6-layer model with came within 3% of the loss of GPT-2's 48-layer, configuration. This is an empirical finding for the range they studied, not a law, and other work finds that very small models benefit from being deep and thin (Liu et al., 2024, MobileLLM, for models under a billion parameters).
Depth costs latency, width costs nothing extra in sequence. The blocks run one after another, so time per generated token grows with , while a wider matrix multiply keeps a GPU busier for the same wall-clock time. Wide layers also split cleanly across GPUs with tensor parallelism (§10.4), and deep stacks split into pipeline stages that must be kept full (§10.5).
Depth is harder to train. Every mechanism in this section gets worse with depth, and so does the underuse of late layers in pre-LN models (§5.2).
In practice, large dense models settle on aspect ratios of roughly 100 to 130. GPT-3 175B has , LLaMA-2 7B has , and LLaMA-2 70B has . GPT-2 small, at , is comparatively deep for its size.
A stability checklist
| symptom | likely cause | first things to try |
|---|---|---|
| diverges in the first few hundred steps | learning rate too high for the init, no or short warmup, post-LN | longer warmup, lower peak learning rate, pre-LN, scaled residual init |
| stream or activation norms grow with depth at init | unscaled residual branches | output-projection init |
| attention entropy of some heads falls toward 0, then loss spikes | attention logit growth | QK-norm, σReparam, lower learning rate for |
| output drifts, late-training instability in 16-bit | free output-logit scale | z-loss, logit soft-capping |
| loss spike that recovers slowly, gradient-norm spike | a bad batch, too-large step, Adam or issues | gradient clipping, rewind and skip data (§8.8) |
| deep layers barely change the output | pre-LN stream dominance | output norms, depth-scaled norms, shallower and wider shape |
In PyTorch
Two pieces you will use in real code. GPT-2's initialization as a function applied over a module, and a σReparam linear layer with a persistent power-iteration buffer.
Read more
- Zhai et al. (2023). Stabilizing Transformer Training by Preventing Attention Entropy Collapse. Entropy collapse, the entropy lower bound, and σReparam.
- Wortsman et al. (2023). Small-scale proxies for large-scale Transformer training instabilities. Attention logit growth and output logit divergence, reproduced cheaply.
- Dehghani et al. (2023). Scaling Vision Transformers to 22 Billion Parameters. The QK-norm fix for attention logit growth at scale.
- Wang et al. (2022). DeepNet, Scaling Transformers to 1,000 Layers. DeepNorm, derived from bounding the size of model updates.
- Liu et al. (2019). On the Variance of the Adaptive Learning Rate and Beyond. Why Adam's early steps are unreliable, and RAdam.
- Bachlechner et al. (2020). ReZero is All You Need. Zero-initialized residual gates.
- Kaplan et al. (2020). Scaling Laws for Neural Language Models. Section 3.1 shows how little shape matters at fixed size.