Foundation reference

Layer Normalization & RMSNorm

LayerNorm and RMSNorm normalize each activation vector with its own statistics rather than statistics over the batch, so they do exactly the same computation at training and test time (Ba, Kiros & Hinton, 2016).

A newer notebook covers this concept. This older page keeps its summary, math and sources.

Concept 54 of 100Core TrainingStudy phase 2
Key equation
μ(x)=1d∑i=1dxi,σϵ(x)=1d∑i=1d(xi−μ(x))2+ϵ,LNϵ(x)=γ⊙x−μ(x)σϵ(x)+β,RMSϵ(x)=1d∑i=1dxi2+ϵ,RMSNormϵ(x)=γ⊙xRMSϵ(x)\begin{aligned}\mu(x)&=\frac{1}{d}\sum_{i=1}^d x_i,\\ \sigma_\epsilon(x)&=\sqrt{\frac{1}{d}\sum_{i=1}^d\bigl(x_i-\mu(x)\bigr)^2+\epsilon},\\ \mathrm{LN}_\epsilon(x)&=\gamma\odot\frac{x-\mu(x)}{\sigma_\epsilon(x)}+\beta,\\ \mathrm{RMS}_\epsilon(x)&=\sqrt{\frac{1}{d}\sum_{i=1}^d x_i^2+\epsilon},\\ \mathrm{RMSNorm}_\epsilon(x)&=\gamma\odot\frac{x}{\mathrm{RMS}_\epsilon(x)}\end{aligned}
Reading map and next steps

Study guide

Where to start

Concept 54 of 100Layer NormalizationCore Training · Study phase 2: Architecture fundamentals
Question

Multiplying the input by a positive constant a leaves LayerNorm's output almost unchanged when ε is small next to a² times the variance of x; why is the cancellation only approximate when ε > 0, and what happens when a is negative?

How to study this pagePredict · Check · Generalize · Continue
PredictSay what you expect.

Before you use the demo in the newer notebook, say what you expect to change.

CheckCheck against the math and the papers.

Compare what the notebook demo shows with the key equation here and with the papers cited below.

GeneralizeName what stays the same.

Identify the part of the idea that survives a change of notation or model, so you can recognize it in another paper or system.

ContinueContinue in the newer notebook.

The newer notebook covers the same concept and has an interactive demo.

Why It Matters for Modern Models

  • LayerNorm and RMSNorm normalize each activation vector with its own statistics rather than statistics over the batch, so they do exactly the same computation at training and test time (Ba, Kiros & Hinton, 2016).
  • The original Transformer applied LayerNorm after each residual addition (post-norm); GPT-3 and LLaMA instead normalize the input of each sublayer (pre-norm), and LLaMA uses RMSNorm (Brown et al., 2020; Touvron et al., 2023).
  • Where the normalization sits changes how training behaves: Xiong et al. (2020) showed that post-norm Transformers have large gradients near the output at initialization and need a learning-rate warmup, while pre-norm Transformers can train without one.
  • RMSNorm skips the mean; Zhang & Sennrich (2019) found comparable quality to LayerNorm with running time reduced by 7% to 64% across their models, and the actual saving depends on the implementation.

Questions to Think About

  • What do the learned γ and β restore after normalization, and why can they not recover the mean and scale that each input vector had before it was normalized?
  • How does moving the normalization from after the residual addition (post-norm) to the start of each sublayer (pre-norm) change the path that gradients take through a deep stack of layers?

Interactive Demo

The interactive demo for this concept is in the newer notebook.

Open the notebook demo

Core Math

If you prefer intuition first, try the demo in the newer notebook, then come back for the math.

Key Equation
μ(x)=1d∑i=1dxi,σϵ(x)=1d∑i=1d(xi−μ(x))2+ϵ,LNϵ(x)=γ⊙x−μ(x)σϵ(x)+β,RMSϵ(x)=1d∑i=1dxi2+ϵ,RMSNormϵ(x)=γ⊙xRMSϵ(x)\begin{aligned}\mu(x)&=\frac{1}{d}\sum_{i=1}^d x_i,\\ \sigma_\epsilon(x)&=\sqrt{\frac{1}{d}\sum_{i=1}^d\bigl(x_i-\mu(x)\bigr)^2+\epsilon},\\ \mathrm{LN}_\epsilon(x)&=\gamma\odot\frac{x-\mu(x)}{\sigma_\epsilon(x)}+\beta,\\ \mathrm{RMS}_\epsilon(x)&=\sqrt{\frac{1}{d}\sum_{i=1}^d x_i^2+\epsilon},\\ \mathrm{RMSNorm}_\epsilon(x)&=\gamma\odot\frac{x}{\mathrm{RMS}_\epsilon(x)}\end{aligned}

For one activation vector x∈Rdx\in\mathbb R^d, with integer d≥1d\ge 1, let γ,β∈Rd\gamma,\beta\in\mathbb R^d and set ϵ=10−5\epsilon=10^{-5} for this page. Write xix_i for coordinate ii of xx, and let ⊙\odot denote elementwise multiplication.

LayerNorm centers the dd features and then applies an elementwise affine map:

μ(x)=1d∑i=1dxi,σϵ(x)=1d∑i=1d(xi−μ(x))2+ϵ,LNϵ(x)=γ⊙x−μ(x)σϵ(x)+β.\begin{aligned} \mu(x) &= \frac{1}{d}\sum_{i=1}^d x_i,\\ \sigma_\epsilon(x) &= \sqrt{\frac{1}{d}\sum_{i=1}^d\bigl(x_i-\mu(x)\bigr)^2+\epsilon},\\ \mathrm{LN}_\epsilon(x) &= \gamma\odot\frac{x-\mu(x)}{\sigma_\epsilon(x)}+\beta. \end{aligned}

RMSNorm omits mean centering and uses the stabilized root mean square:

RMSϵ(x)=1d∑i=1dxi2+ϵ,RMSNormϵ(x)=γ⊙xRMSϵ(x).\begin{aligned} \mathrm{RMS}_\epsilon(x) &= \sqrt{\frac{1}{d}\sum_{i=1}^d x_i^2+\epsilon},\\ \mathrm{RMSNorm}_\epsilon(x) &= \gamma\odot\frac{x}{\mathrm{RMS}_\epsilon(x)}. \end{aligned}

The demo in the linked notebook uses one scalar γ\gamma and one scalar β\beta for all dd features; the equations above allow a separate value for each feature. RMSNorm has no β\beta term under this page's convention.

LayerNorm centres and rescales each vector (Ba, Kiros & Hinton, 2016); RMSNorm only rescales it (Zhang & Sennrich, 2019). This page and the linked notebook demo place ϵ=10−5\epsilon=10^{-5} inside each square root as an explicit numerical convention; library defaults, normalized axes, and affine variants may differ.

Key Papers

Layer Normalization

Ba, Kiros & Hinton2016arXiv
Read paper →

Root Mean Square Layer Normalization

Zhang & Sennrich2019NeurIPS
Read paper →

On Layer Normalization in the Transformer Architecture

Xiong et al.2020ICML
Read paper →

Connections

Where to go next

A newer notebook covers Layer Normalization, with an interactive demo.

Continue in the newer notebook