We first compute the mean μx and variance σx2 of the activation vector as follows:
μx=n1i=1∑nxi,σx2=n1i=1∑n(xi−μx)2.
The LayerNorm operation[1]Layer normalization[link] Jimmy Lei Ba, Jamie Ryan Kiros, and Geoffrey E. Hinton. arXiv preprint. 2016. is then defined as follows:
LayerNorm(x)=g⊙σx2+ϵx−μx+b
where g,b∈Rn are learnable parameters, ϵ is a small constant, and ⊙ denotes the element-wise multiplication.
Geometric interpretation
We can break down the LayerNorm operation into three distinct steps.
1. Mean-centering
We first subtract the mean μx from the activation vector x:
x(1)=x−μx.
This can equivalently be thought of as projecting the activation vector onto the all-ones direction 1^, and subtracting this component away:
Thus, mean-centering can be thought of as projecting the n-dimensional activation onto an (n−1)-dimensional subspace, namely the orthogonal complement of 1^.
2. Variance normalization
After mean-centering, we normalize the variance of the activation vector:
x(2)=σx2+ϵx(1).
Note that ϵ is a small constant (≈10−5) to prevent division by zero, and to improve numerical stability.
Variance normalization can equivalently be thought of as projecting the activation onto the n-radius sphere.
To see this, first note that the variance of the original activation vector x is equal to the variance of the mean-centered activation vector x(1), since variance is invariant under translation:
σx2=σx(1)2.
Next, notice that the variance of a mean-centered vector can be expressed in terms of its squared norm:
Finally, we apply an affine transformation, scaling by a learned gain g∈Rn, and shifting by a learned bias vector b∈Rn:
x(3)=g⊙x(2)+b.
Root mean squared layer normalization
Root Mean Square Layer Normalization[2]Root mean square layer normalization[link] Biao Zhang and Rico Sennrich. Advances in Neural Information Processing Systems. 2019. is a simplification of Layer Normalization that skips mean-centering, and normalizes by the root mean square of the activations.
We first compute the root mean square of the activation vector as follows:
RMS(x)=n1∑i=1nxi2.
The RMSNorm operation is defined as follows:
RMSNorm(x)=g⊙RMS(x)x+b.
References
References cited in the text are listed first, in order of citation; additional references follow, ordered alphabetically.
Layer normalization[link] Jimmy Lei Ba, Jamie Ryan Kiros, and Geoffrey E. Hinton. arXiv preprint. 2016.
Root mean square layer normalization[link] Biao Zhang and Rico Sennrich. Advances in Neural Information Processing Systems. 2019.
What is LayerNorm folding?[link] Neel Nanda. 2022.
Geometry and dynamics of LayerNorm[link] Paul M. Riechers. arXiv preprint. 2024.