Dropping the Mean: RMSNorm Versus LayerNorm in Transformers
Machine LearningModel answerLast updated
Contrast LayerNorm and RMSNorm formulas, what removing centering changes, and why many large language models adopt the simpler normalization.
Many recent large language models replace LayerNorm with RMSNorm inside their transformer blocks. Explain the difference between the two.
Your answer should:
- Write out both operations for the hidden vector of a single token, using the usual definitions: LayerNorm with a learned scale and bias, RMSNorm with a learned scale only.
- Describe what is lost, and what is kept, when the mean-subtraction step is removed.
- Argue why RMSNorm can be an appealing choice in practice, and where that argument is weaker than it first sounds.
Assumptions
- Normalization runs across the hidden (feature) dimension of one token, not across the batch or the sequence.
- Details such as whether a bias is present, where epsilon goes, and the precision used for reductions differ between codebases. State the choices you make.
Points worth clarifying up front
- Which axis is being normalized?
- Is there a learned bias?
- Is normalization applied before the sublayer (pre-norm) or after it (post-norm)?
- In what precision are the reductions computed?
Hints
Hint 1
Write both formulas side by side for a vector x of dimension d. Which statistics does each need: mean, variance, or only a mean of squares?
Hint 2
Think about which transformations of the input each layer is invariant to. Rescaling x by a constant affects both similarly; adding a constant to every feature affects them differently.
Hint 3
Consider cost versus benefit: how much of a transformer block's time is spent in normalization, and is it compute-bound or memory-bound? Also consider reduction precision in bf16/fp16.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Definitions
Let x ∈ ℝ^d be the hidden vector of one token, and let ε be a small constant for numerical stability.
LayerNorm
- μ = (1/d) Σ xᵢ
- σ² = (1/d) Σ (xᵢ − μ)²
- yᵢ = γᵢ · (xᵢ − μ) / √(σ² + ε) + βᵢ
RMSNorm
- RMS(x) = √((1/d) Σ xᵢ² + ε)
- yᵢ = γᵢ · xᵢ / RMS(x)
(The bias β is usually dropped as well. Some implementations place ε differently or keep a bias; I assume the forms above.)
Note that if the mean μ happens to be zero, the two normalizers coincide: variance equals mean-square. RMSNorm is exactly "LayerNorm without centering (and without the bias)".
What removing the mean subtraction changes
Invariances
- Both are invariant to rescaling the input: multiplying x by a positive constant leaves the output unchanged (up to ε).
- LayerNorm is additionally invariant to a shift: adding the same constant c to every feature of x leaves the output unchanged, because the mean is removed first.
- RMSNorm is not shift invariant. Adding a constant changes both the direction of the vector and its RMS.
Geometric view
LayerNorm projects x onto the hyperplane orthogonal to the all-ones vector, then rescales to a fixed norm, so outputs lie on a sphere of radius √d in a (d−1)-dimensional subspace. RMSNorm just rescales x onto a sphere of radius √d in the full d-dimensional space, keeping the direction exactly.
Why this loss is often tolerable
- The original argument for LayerNorm's success (and the argument made by the RMSNorm authors) is that re-scaling invariance matters most for stabilizing activations and gradients; re-centering contributes little.
- The following linear layer can learn to ignore or compensate for a mean component if that component is unhelpful.
- Residual-stream features in trained LLMs tend to have small or structured means, so centering removes little information.
- RMSNorm keeps the full direction of x, including any informative common-mode component, which LayerNorm discards.
Why RMSNorm is attractive
- Fewer operations. One reduction (sum of squares) instead of two (mean, then variance, or a Welford-style combined pass). No subtraction pass. No bias parameters, saving d parameters per norm.
- Fused-kernel friendliness. A single reduction followed by an elementwise scale fuses easily into neighboring ops and needs less register and shared-memory traffic.
- Reported speedups. Papers report roughly 7–64% faster normalization depending on model and hardware. In a full transformer step this is only a small share of wall time, since matmuls and attention dominate, so the end-to-end gain is typically a few percent.
- Comparable quality. Across many language-model setups, RMSNorm matches LayerNorm within noise, which is why it became the default in several open model families.
- Simpler parameterization. Fewer knobs, no bias to initialize or decay.
Caveats and trade-offs
- The speed gain is modest. Normalization is memory-bandwidth-bound, so what is saved is mostly memory traffic and kernel launches, not FLOPs. Without fusion, the gain can be negligible.
- No guarantee of better models. Results are empirical. Some tasks, architectures, or small models can slightly prefer LayerNorm. Do not claim RMSNorm is universally better.
- Loss of shift invariance can matter. If activations develop a large common offset (a few massive-activation dimensions are known to appear in LLMs), RMS is dominated by that offset, and the other features get squashed. LayerNorm would remove the shared offset first.
- Placement matters more than type. Pre-norm versus post-norm affects stability and gradient flow more than the choice between LN and RMSNorm. Pre-norm with RMSNorm is the common modern recipe, often with extra norms on queries and keys for stability.
- Gradient behavior differs slightly. LayerNorm's backward pass projects out the mean direction of the gradient; RMSNorm's only projects out the component along x. This is a minor difference but can matter in analysis.
Numerical precision
- Compute the reduction (sum of squares or mean/variance) in fp32 even when activations are bf16 or fp16. Squaring in fp16 can overflow for values above about 256, and low-precision accumulation over thousands of features loses accuracy.
- Place ε inside the square root (as above) to avoid division by zero. Typical values are 1e-5 to 1e-6; with fp16 a larger ε may be needed.
- LayerNorm has the extra hazard of catastrophic cancellation in x − μ when the mean is large relative to the spread, and the naive E[x²] − μ² variance formula is unstable. Two-pass or Welford formulations fix this, at some extra cost. RMSNorm avoids this particular issue because it never subtracts a mean.
- Cast the normalized result back to the activation dtype before applying γ, or apply γ in fp32 and then cast; be consistent with the reference implementation, since mismatches show up as small checkpoint-conversion differences.
Reference implementation (PyTorch)
import torch
import torch.nn as nn
class RMSNorm(nn.Module):
def __init__(self, dim, eps=1e-6):
super().__init__()
self.eps = eps
self.weight = nn.Parameter(torch.ones(dim))
def forward(self, x):
xf = x.float()
inv_rms = torch.rsqrt(xf.pow(2).mean(-1, keepdim=True) + self.eps)
return (xf * inv_rms).to(x.dtype) * self.weight
Cost: O(d) per token for both, but RMSNorm does one reduction and no subtraction.
Common mistakes
- Saying RMSNorm "removes the variance" or "does not normalize". It normalizes the scale, just not the location.
- Claiming it is strictly better or that the speedup is large end to end.
- Forgetting that the two coincide when the mean is zero.
- Confusing the normalized axis with BatchNorm's batch axis.
- Computing the sum of squares in half precision.
Summary answer
RMSNorm = LayerNorm minus re-centering and bias. It keeps scale invariance, gives up shift invariance, costs less memory traffic and parameters, and in practice performs about as well in transformers. The benefit is a modest efficiency and simplicity win, not a guaranteed quality gain.