↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Loading the recording…

Why depth is fragile

A transformer is a stack of blocks that each read the residual stream, a vector per token, and add something back to it. With the token and position embeddings and the -th block,

Going up, every block multiplies the size of the stream by something; coming back down, backpropagation multiplies the gradient by the transpose of each block's Jacobian. Over 32 blocks a factor a little above one explodes and a factor a little below one vanishes. Two choices decide those factors before any data is seen: how large the random weights start (initialization) and where, if anywhere, the stream is rescaled (normalization).

This tab trains one deep, narrow GPT, 32 blocks of width 128 with four heads, 12.76M parameters, on the same TinyStories batches as the other Pretraining tabs, once for each of four initializations and four layouts: sixteen runs of 400 AdamW steps at a learning rate of 1e-3, with no warmup and no gradient clipping, so nothing hides what the start does. At steps 0, 1, 2, 3, 5, 10, 20, 50, 100, 200 and 400 each run stops for one forward and backward pass on eight fixed validation windows and records, at every depth, the root mean square (RMS) of the stream and of the loss's gradient with respect to it.

Four initializations, four layouts

Every weight matrix, the embeddings included, starts from a normal distribution :

  • , the value GPT-2 uses;
  • too small, ;
  • too large, ;
  • scaled residual, GPT-2's full rule: , except that the two projections that write into the stream in each block (attention's output and the MLP's second layer) start at , so that the writes add up to about one.

The four layouts place the normalization differently:

  • no norm: , nothing rescales anything;
  • Post-LN, the original Transformer's: , the stream itself is normalized after every block;
  • Pre-LN, GPT-2 onwards: , only each block's input is normalized, plus one LayerNorm before the output;
  • Pre-RMSNorm, the same with RMSNorm, which divides by the root mean square without first subtracting the mean.

The All sixteen grid shows every combination at the chosen step: each cell is two strips of 33 depths, the stream coloured by its RMS against depth 0 and the gradient against the top block, red for larger and blue for smaller, ten times per colour band.

Explosion: no norm, large weights

With and no normalization, each linear layer of width 128 multiplies a vector's size by about , and each block stacks several of them and adds the result to the stream. The Residual stream across depth panel shows what that does at step 0: the stream's RMS is 0.28 after the embedding and 3.05 after the first block, passes by depth 4 and by depth 8, and overflows float32 to infinity at depth 12. Everything above is NaN, the loss is NaN, and the run stops before its first step. Nothing was trained; the initialization alone did it.

The other three initializations do not explode without normalization: at or below, each block writes far less than the stream already holds and the stream stays at the embedding's size, 0.028, all the way up. The danger runs the other way for the gradient over training, as the next section shows.

Vanishing: deep Post-LN

Post-LN never gets off the ground here. All four of its runs fall to about 6.0 nats within 80 steps, the plateau where a model predicts how common each token is but not which one comes next, and are still there at step 400 (5.99 to 6.02), while every other run that did not overflow has left it. Their attention barely moves: averaged over blocks its entropy ends at 3.77 to 3.87 nats, against 3.88 for spreading evenly over the visible tokens.

The Gradient across depth panel shows why, and the Depth × step map shows it happening. At step 0 Post-LN's gradient is nearly flat across the blocks. Each LayerNorm divides the stream by its size, so as the blocks start writing more, every block's backward pass shrinks the gradient that reaches the blocks below, and 32 of them compound. By step 400 the gradient at the embedding has fallen from about 0.7 (1.4 with ) to with , with the scaled init, with and with , while the top block's stays between 0.04 and 0.19. Xiong et al. showed that Post-LN's gradients are largest near the output at initialization and that it needs a learning-rate warmup; Liu et al. traced the instability to each block's growing dependence on its residual branch. Without warmup, at this depth, the lower blocks simply stop learning.

Pre-norm: a growing stream that still trains

Pre-LN and Pre-RMSNorm leave the stream unnormalized, so it grows with depth: each block writes something of size one or so, computed from a normalized input. At step 0 with the stream goes from 0.028 at the embedding to 0.37 at the top; with from 0.28 to 48, and to about 200 by step 400. Because the identity path carries the gradient straight down, it does not vanish; it is largest at the bottom, where the embedding's small size makes the first normalization amplify it (5.3 at the embedding against 0.004 at the top for ).

GPT-2's scaled residual initialization is aimed at that growth. With it, Pre-LN's stream at step 0 grows only from 0.028 to 0.049 over 32 blocks instead of to 0.37, and the gradient at the top is 0.42 instead of 0.054. Large weights show up in the Attention entropy panel instead: with , Pre-LN's and Pre-RMSNorm's attention ends at 1.5 and 1.4 nats on average, far sharper than the 3.0 to 3.6 of the other initializations.

How far each of these runs gets by step 400 is less telling than the internal picture. Every run passes the 6.0 plateau and leaves it at a different step, so 400 steps rank them noisily: with , LayerNorm ends at 4.59 and RMSNorm at 5.28, while with the scaled init it is the other way round, 5.02 against 4.41. The model without normalization and ends lowest of all, at 4.22, though its gradient norm jumped to 329 at step 3 and the scaled run without normalization spiked to 1,618 at step 201. The Training loss strip shows each run's path.

Where it came from

Glorot and Bengio, and then He et al. for ReLU networks, chose the initial variance so that each layer keeps its input's size, which is what does. Ba et al. introduced layer normalization, and the original Transformer placed it after every residual addition. GPT-2 moved it to each block's input and scaled the residual projections by ; Xiong et al. explained why Pre-LN trains without warmup. Zhang and Sennrich's RMSNorm drops the centring, and most large models now use it in the pre-norm position. DeepNet (Wang et al.) went the other way, keeping Post-LN and scaling the residual so it trains 1,000 layers deep. The Arch ladder tab swaps LayerNorm for RMSNorm in a shallow GPT; μP handles the related question of how initialization and learning rates should change with width.

Papers

  1. On Layer Normalization in the Transformer Architecture Ruibin Xiong et al. · ICML 2020 Compares Post-LN and Pre-LN at initialization: Post-LN's large gradients near the output need a warmup, Pre-LN trains without one.
  2. Understanding the Difficulty of Training Transformers Liyuan Liu et al. · EMNLP 2020 Traces Post-LN's instability to each block's growing dependence on its residual branch, the effect the Post-LN runs' gradients show.
  3. Layer Normalization Jimmy Lei Ba et al. · arXiv 2016 Introduces normalizing each example's features by their own mean and standard deviation.
  4. Root Mean Square Layer Normalization Biao Zhang et al. · NeurIPS 2019 RMSNorm: rescaling by the root mean square alone, without centring, as the Pre-RMSNorm runs do.
  5. Language Models are Unsupervised Multitask Learners Alec Radford et al. · 2019 GPT-2: LayerNorm moved to each block's input and the residual projections initialized at 1/√N, the scaled residual init here.
  6. Delving Deep into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification Kaiming He et al. · ICCV 2015 Chooses each layer's initial variance so signals keep their size through depth.
  7. DeepNet: Scaling Transformers to 1,000 Layers Hongyu Wang et al. · arXiv 2022 Keeps Post-LN and scales the residual branch and its initialization so a 1,000-layer Transformer trains.

See also