On Layer Normalization in the Transformer Architecture
- Publication
- ICML 2020Conference
- First submitted to arXiv
- arXiv:2002.04745
Cite this paper · BibTeX
The citation identifies the linked source version. It cites the original paper and its authors.
@misc{transformerspreln2020,
title = {{On Layer Normalization in the Transformer Architecture}},
author = {Ruibin Xiong and Yunchang Yang and Di He and Kai Zheng and Shuxin Zheng and Chen Xing and Huishuai Zhang and Yanyan Lan and Liwei Wang and Tie-Yan Liu},
year = {2020},
eprint = {2002.04745},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2002.04745}
}
One odd habit of training Transformers is that you can't set the learning rate where you want it right from the start.
For the first 4000 steps, the original Transformer paper has the learning rate climb slowly from near 0 to its maximum, then gradually decay. This climb is called learning-rate warm-up. Convolutional networks usually start with a fairly large learning rate and lower it over time; only special cases such as very-large-batch training need warm-up. Transformers, though, need it almost every time.
Warm-up is a nuisance. It slows training down and adds two more hyperparameters to tune, the maximum learning rate and the number of warm-up steps, and the final results are sensitive to both. When training a large model like BERT, every setting you try is expensive.
Why do Transformers insist on warm-up? In February 2020, Ruibin Xiong, Yunchang Yang, Di He, and colleagues from the Chinese Academy of Sciences, Peking University, Microsoft Research Asia, and Nankai University released this paper. They argue that the problem lies in where LayerNorm is placed.
Defining the Problem
Two Arrangements
Each Transformer layer has two sub-layers, self-attention and a feed-forward network (FFN). Each sub-layer has a residual connection, and each has a LayerNorm. These three pieces can be arranged in different orders:

On the left is what the original Transformer and BERT do, running the sub-layer, residual addition, and LayerNorm in that order. LayerNorm sits between the residual blocks, and the authors call this Post-LN. Taking the FFN sub-layer as an example:
On the right, LayerNorm moves inside the residual branch, before the sub-layer; this is called Pre-LN:
Pre-LN's main path has nothing but additions; since the main path is never normalized along the way, one more LayerNorm has to be added before the final output.
Pre-LN was already in use before this paper. The language models of Baevski and Auli and OpenAI's Sparse Transformer both adopted it, and translation toolkits such as tensor2tensor and OpenNMT implemented it; Wang et al. even found that Pre-LN beats Post-LN as the number of layers grows. Still, all of these works kept warm-up in their training pipelines.
How Much Does Warm-Up Really Matter?
The authors first confirm the effect of warm-up with a Post-LN Transformer on the IWSLT14 German-to-English translation task. The warm-up schedule is:
followed by inverse square root decay. They train with Adam and with plain SGD, comparing runs with and without warm-up and with different warm-up lengths:

Warm-up has to be there, and its length has to be right too.
- Without warm-up, it won't train. With Adam and no warm-up, BLEU reaches only 8.45; models with warm-up get about 34. SGD does much worse than Adam to begin with, but the trend is the same, with BLEU staying barely above 0 over 15 epochs without warm-up.
- The warm-up length is a sensitive setting too. With only 500 warm-up steps, maximum learning rates of 5e-4 and 1e-3 give BLEU scores of just 31.16 and 2.77, respectively.
Others had explained what warm-up does before. The authors of RAdam argued that warm-up reduces the variance of Adam's adaptive learning rate early in training. But here SGD needs warm-up too, so the authors conclude that the benefit of warm-up isn't tied to any particular optimizer, and the cause has to be sought in the model itself.
Solving the Problem
Looking at the Moment of Initialization
Warm-up only happens during the first few thousand steps, so the authors focus on initialization, examining the gradients when the network has just been built and all its parameters are random.
They analyze it with mean field theory and make a few simplifications. Attention uses a single head; every weight matrix is with Xavier initialization; the attention matrices and are initialized to 0, so attention starts out uniform; and the input vectors are treated as Gaussian as well.
The analysis has three steps, looking first at the size of the hidden states, then at how LayerNorm treats gradients, and finally at the two put together.
Step 1: How Big Is the Signal on the Main Path?
Post-LN ends every layer with LayerNorm, so each layer's output norm is fixed at around . The authors look instead at , just before the last normalization: the vector after the FFN output is added to the residual. Pre-LN's main path isn't normalized, and every layer adds something on top. The authors prove:
Post-LN stays the same size at every layer; Pre-LN grows linearly with depth.
Where does the come from? has just gone through LayerNorm, so its squared norm is . Then it passes through the FFN. After multiplying by the Xavier-initialized , each element has variance 1; ReLU cuts out half, leaving a squared norm of ; multiplying by keeps it at . Add the two together and you get . Each Pre-LN layer likewise adds at least the FFN's , which is where the in the lower bound comes from.
Step 2: LayerNorm Divides the Gradient by the Size of Its Input
LayerNorm subtracts the mean from its input and divides by the standard deviation. Scale the input up by , and the output stays the same; so, conversely, the derivative of the output with respect to the input shrinks by a factor of . The authors write this as:
is LayerNorm's Jacobian matrix. Each time the input's length doubles, its spectral norm halves. When the gradient propagates back through LayerNorm, it's multiplied by this Jacobian, so the larger the input, the smaller the gradient passed back.
Step 3: Putting It Together
Putting the first two steps together gives the paper's main theorem. The authors analyze the second weight matrix of the last FFN layer, , and conclude (with high probability):
Here's the reasoning. In Post-LN, the input to the last LayerNorm is , whose size is fixed at around regardless of the number of layers, so the last layer's gradient doesn't depend on the number of layers either. In Pre-LN, the input to the final LayerNorm is the main path, accumulated all the way up; its squared norm grows linearly with the number of layers , so its norm is on the order of , and the gradient passing through it is divided by .
The authors also give a preliminary extension of the analysis, concluding that in Post-LN, the closer parameters are to the output layer, the larger their gradients, shrinking toward the input; in Pre-LN, gradients are roughly the same across layers.
Measuring It for Real
The theory makes quite a few simplifications, so the authors check it on real models. They first confirm that the concentration property of the hidden-state norms holds, then measure the gradients:

The measurements match the theory.
- The two plots on the right: Post-LN's last-layer gradient is around 1.6 no matter how big the model, while Pre-LN's falls as the number of layers grows.
- The two plots on the left: Post-LN's gradients grow steadily from layer 1 to layer 6, largest in the layer closest to the output; Pre-LN's vary much less across layers.
So at initialization, Post-LN has large gradients on the parameters near the output. Using a large learning rate right away makes the update steps too big, and training becomes unstable. Warm-up sidesteps exactly this stretch by waiting until the gradients have shrunk before turning up the learning rate. The green bars in the figure (Post-LN after warm-up) are almost all 0; the gradients really have become much smaller.
The authors also tried training Post-LN without warm-up, using a fixed small learning rate of 1e-4 throughout. That does train the model to some extent, but convergence is much slower.
Looking at it the other way around, Pre-LN's gradients are well-behaved right from initialization, so it should be able to do without warm-up.
Discussion
Translation: No Warm-Up, and Faster Too
The authors test this on two translation tasks:
- IWSLT14 German-to-English: a 6-layer encoder and a 6-layer decoder, embedding dimension 512, FFN 1024, 4 attention heads.
- WMT14 English-to-German: the Transformer base setting, with about 4.5 million parallel sentence pairs.
Post-LN follows the original paper with 4000 warm-up steps, then inverse square root decay. Pre-LN has no warm-up at all. On IWSLT14 it starts at 5e-4 and is multiplied by 0.1 at epoch 8; on WMT14, two runs start at 7e-4 and 1.5e-3, decay at epoch 6, and then follow inverse square root decay. The two models' maximum learning rates are set to be the same. Since RAdam claims to remove the need for warm-up, the authors include it in the IWSLT14 comparison as well.

Reading the figure, we can sum it up in three points.
- Warm-up is no longer necessary. On IWSLT14, Pre-LN without warm-up ends at a BLEU of about 34 and a validation loss of about 4, on par with Post-LN with warm-up.
- Faster convergence. On IWSLT14, Pre-LN at epoch 9 performs about as well as Post-LN at epoch 15; WMT14 shows something similar.
- Where LayerNorm goes matters more than the optimizer. RAdam does let Post-LN train without warm-up; but once you switch to Pre-LN, whether you use RAdam or Adam makes almost no difference.
The authors also mention that with well-tuned hyperparameters (an initial learning rate of 7.5e-4, dropout of 0.3, and so on), Pre-LN can reach a test BLEU of 35.5 on IWSLT14.
BERT: 40% Saved
Pre-training uses BERT base (12 layers, hidden dimension 768, 12 heads), on English Wikipedia plus a BookCorpus the authors collected themselves, about 3.4 billion words in total.
Post-LN BERT follows the original paper's settings, with 10,000 warm-up steps and a maximum learning rate of 1e-4. The authors tried raising Post-LN's learning rate to 3e-4, and training simply diverged. Pre-LN BERT has no warm-up and decays linearly from 3e-4.

Post-LN has to train until step 700k for its validation loss to reach 1.69; Pre-LN gets there by step 500k, about 40% faster. The 200k steps saved far exceed the 10k steps of warm-up itself, so the speed-up comes from more than skipping warm-up: above all, Pre-LN can handle a much larger learning rate.
Fine-tuning models from different stages on MRPC and RTE shows that Pre-LN also converges faster on downstream tasks.
Conclusion
This paper traces an engineering habit back to an architectural cause. At initialization, Post-LN has large gradients on the parameters near the output, which warm-up is needed to avoid. Move LayerNorm inside the residual branch, before the sub-layer, and the size of the main path shrinks the last layer's gradient by , the gradients become more even across layers, warm-up can be dropped, and the learning rate can be turned up higher.
That said, the theoretical analysis looks only at the moment of initialization and makes simplifications such as uniform attention and a single head; the part about Post-LN gradients growing larger closer to the output is labeled by the authors themselves as a preliminary result, supported mainly by the measurements in Figure 3.
The idea of “move normalization into the branch and keep the shortcut clean” had also appeared in convolutional networks; GPT-2 likewise moved LayerNorm to the input of each sub-layer:
If you're training a Transformer from scratch, put LayerNorm before each sub-layer, and remember to add one more LayerNorm before the output. At the scale of the paper's experiments, this trains without warm-up, and you can use a somewhat larger learning rate.
Pre-LN later became the default arrangement for many large language models, but it comes at a cost, which the result of step 1 already shows. The main path's squared norm grows linearly with the number of layers, while each layer adds roughly the same amount, so the later the layer, the smaller its relative influence on the main path. In 2021, NormFormer pointed out that during Pre-LN pre-training, the early layers' gradients are much larger than the later layers', and added a few extra normalizations to each layer; in 2022, DeepNet went back and reworked Post-LN, aiming to keep both Post-LN's performance and Pre-LN's stability, and pushed Transformers to a thousand layers.
Where LayerNorm should go is a question that isn't settled yet; we'll come back to it when we get the chance.