Normalization  · July 2016

Layer Normalization

intro

training-stability

Normalize the activations of a layer across the feature dimension rather than across the batch — letting recurrent and attention-based networks train stably at any batch size, including batch-of-one inference.

§ 1 · Premise

What Batch Normalization could not do

By 2016 the standard normalizer for feed-forward and convolutional networks was Batch Normalization (Ioffe & Szegedy, arXiv 1502.03167). BatchNorm normalizes a single feature channel across the minibatch: for a channel jj at position tt in a batch of size BB, it estimates the mean and variance μj=1Bbxj,t(b)\mu_j = \tfrac{1}{B}\sum_{b} x^{(b)}_{j,t} and σj2=1Bb(xj,t(b)μj)2\sigma^2_j = \tfrac{1}{B}\sum_{b}(x^{(b)}_{j,t} - \mu_j)^2 and rescales each activation by these per-channel statistics. On ImageNet-scale ConvNets with batches of 256+ this works extremely well — the empirical statistics are stable, and BatchNorm reduces both training time and final error.

Three structural problems surfaced when the same idea was carried to sequence models, all documented in Ba, Kiros, and Hinton (arXiv 1607.06450, §2):

  1. Recurrence breaks the moving-average assumption. An RNN unrolled across TT time steps sees a different activation distribution at each tt. BatchNorm either has to maintain TT separate sets of statistics — wasteful and brittle for sequences of variable length — or share statistics across time and pay an accuracy cost from the resulting mismatch.
  2. Variable batch size at inference. Autoregressive decoding produces one token at a time, often with batch size 1. BatchNorm’s training behavior (batch statistics) and inference behavior (cached moving averages) diverge sharply at B=1B=1; small estimation error in the running averages compounds across many decode steps.
  3. Padding pollution. When sentences of different length are batched together, padded positions either contaminate the per-batch statistics or have to be masked out with per-channel reductions that complicate the kernel.

Layer Normalization fixes all three by moving the reduction off the batch axis and onto the feature axis. Each token computes its own statistics from its own dd-dimensional activation vector, independently of every other token in the batch. The contribution sentence: one normalizer that needs neither a batch nor a sequence-position context to be well-defined.

§ 2 · Derivation

From batch statistics to per-token statistics

Let xRd\mathbf{x} \in \mathbb{R}^d be the pre-normalization activation vector at a single token position, where dd is the hidden width (often called HH in the paper; we use dd to avoid collision with the head dimension in attention contexts). The components of x\mathbf{x} index the feature axis. LayerNorm reduces along that axis to produce two scalar summaries:

μ(x)=1di=1dxi,σ2(x)=1di=1d(xiμ(x))2.\mu(\mathbf{x}) = \frac{1}{d}\sum_{i=1}^{d} x_i, \qquad \sigma^2(\mathbf{x}) = \frac{1}{d}\sum_{i=1}^{d}\bigl(x_i - \mu(\mathbf{x})\bigr)^2.

The mean μR\mu \in \mathbb{R} measures the dc offset of the activation across features; the variance σ2R\sigma^2 \in \mathbb{R} measures the average squared deviation around that offset. Both are functions of x\mathbf{x} alone — no other tokens, no other batch elements, no moving average over training history. The normalized activation is

x^=xμ(x)1σ2(x)+ε,\hat{\mathbf{x}} = \frac{\mathbf{x} - \mu(\mathbf{x})\,\mathbf{1}}{\sqrt{\sigma^2(\mathbf{x}) + \varepsilon}},

where 1Rd\mathbf{1} \in \mathbb{R}^d is the all-ones vector and ε>0\varepsilon > 0 is a small constant (Ba et al. use ε=105\varepsilon = 10^{-5} in the recurrent experiments of §6) added to the variance to keep the square root differentiable when σ20\sigma^2 \to 0. By construction, x^\hat{\mathbf{x}} has empirical mean 00 and empirical variance 11 over its dd components.

A pure normalization to zero mean and unit variance would destroy the network’s ability to represent feature scales that the upstream weights have learned. The fix, identical to BatchNorm’s, is a learnable affine reparameterization with gain γRd\boldsymbol{\gamma} \in \mathbb{R}^d and bias βRd\boldsymbol{\beta} \in \mathbb{R}^d:

LNγ,β(x)=γx^+β,\mathrm{LN}_{\boldsymbol{\gamma},\boldsymbol{\beta}}(\mathbf{x}) = \boldsymbol{\gamma} \odot \hat{\mathbf{x}} + \boldsymbol{\beta},

where \odot is the elementwise (Hadamard) product. The gain vector restores per-feature scale and the bias vector restores per-feature offset; together they make LayerNorm a strict generalization of the identity (set γ=1,β=0\boldsymbol{\gamma} = \mathbf{1}, \boldsymbol{\beta} = \mathbf{0} and apply only the normalization). Parameter count is 2d2d per LayerNorm instance, which is negligible compared to the O(d2)O(d^2) projection matrices it sits between.

Why this and not channel-only rescaling? A naive alternative would be to skip mean subtraction and only rescale by σ\sigma. Ba et al. (§5) argue from the invariance properties laid out in their Table 1: BatchNorm is invariant to per-channel shifts of the weights but not to per-token shifts of the inputs; LayerNorm flips that picture. Mean subtraction is what gives LayerNorm its input-shift invariance property — adding the same constant to every feature of x\mathbf{x} leaves LN(x)\mathrm{LN}(\mathbf{x}) unchanged. This invariance is the reason LayerNorm tolerates input distributions whose dc level drifts between examples (as they do across RNN time steps and across batched-padded sequences). RMSNorm (Zhang & Sennrich, arXiv 1910.07467) would later show empirically that re-scaling alone is sufficient for transformer training, but the 2016 analysis correctly identifies why the centering step might matter and includes it.

Gradient picture. Differentiating the LayerNorm output with respect to the input yields, after a standard quotient-rule expansion (Ba et al. §3, also derived in Xu et al. arXiv 1911.07013 §3):

x^ixj=1σ2+ε(δij1dx^ix^jd),\frac{\partial \hat{x}_i}{\partial x_j} = \frac{1}{\sqrt{\sigma^2 + \varepsilon}}\left(\delta_{ij} - \frac{1}{d} - \frac{\hat{x}_i\,\hat{x}_j}{d}\right),

where δij\delta_{ij} is the Kronecker delta. The first term is the dominant identity-like contribution; the 1/d-1/d term subtracts the gradient leaking through μ\mu; the x^ix^j/d-\hat{x}_i\hat{x}_j/d term subtracts the gradient leaking through σ\sigma. Two observations follow from this expression. First, all three terms are O(1/σ)O(1/\sigma), so very small variance inputs make gradients explode — hence the ε\varepsilon floor. Second, the rank-11 correction terms enforce that the upstream gradient is projected onto the subspace orthogonal to 1\mathbf{1} and x^\hat{\mathbf{x}}, which is the geometric statement of “gradients flow only through the directions the normalization did not already fix.”

Computational cost. A single LayerNorm call requires two reductions across the feature axis (one for μ\mu, one for σ2\sigma^2), a subtraction, a square-root and reciprocal, an elementwise multiplication by γ\boldsymbol{\gamma}, and an elementwise addition of β\boldsymbol{\beta}. Total operation count is O(d)O(d) per token, dominated by the two reductions. On GPUs the cost is bounded by memory traffic — reading x\mathbf{x} once and writing the output once — rather than by arithmetic. For a transformer with LL layers, sequence length TT, batch size BB, and two LayerNorms per block, the LayerNorm budget over a forward pass is 4BTLd4 B T L d floating-point reads and writes (gain and bias account for the constant 4).

§ 3 · Reference implementation

Reference implementation

def layer_norm(x, gamma, beta, eps=1e-5):
    # x:     [..., d]
    # gamma: [d]   per-feature learnable gain, init to 1
    # beta:  [d]   per-feature learnable bias, init to 0
    mu = x.mean(-1, keepdim=True)                  # [..., 1]
    var = x.var(-1, keepdim=True, unbiased=False)  # [..., 1]
    x_hat = (x - mu) * (var + eps).rsqrt()         # zero mean, unit var
    return gamma * x_hat + beta                    # per-feature affine

def transformer_block_pre_ln(x, attn, ffn, ln1_g, ln1_b, ln2_g, ln2_b):
    # Pre-Norm placement (see norm-placement entry for Post-Norm).
    x = x + attn(layer_norm(x, ln1_g, ln1_b))      # [B, T, d]
    x = x + ffn(layer_norm(x, ln2_g, ln2_b))
    return x

The contract on the input: any tensor whose last axis is the feature axis. The output has the same shape. Production kernels (Apex FusedLayerNorm, PyTorch’s built-in nn.LayerNorm, and the fused implementations in cuDNN / Triton) collapse the two reductions and the affine into a single pass over memory, but the math is what the four lines above describe.

§ 4 · Empirical evidence

Empirical evidence

The original paper (Ba et al. 2016, §6) evaluates LayerNorm primarily on RNN-based tasks where BatchNorm was known to struggle. Headline numbers:

The decisive cross-task pattern is convergence speedup, not final-accuracy headroom — the unnormalized baselines often catch up if trained long enough, but the LayerNorm runs get there faster and with less hyperparameter sensitivity.

The transformer adoption. Vaswani et al. (arXiv 1706.03762, §3.1) chose LayerNorm for the original transformer “Add & Norm” block without an ablation, citing the recurrent-network results above as sufficient evidence that batch-axis normalization was the wrong primitive for sequence models. Every subsequent transformer paper inherited that choice by default through roughly 2019. The placement they used (Post-Norm — normalize after the residual addition) turned out to be the load-bearing weakness; see the Pre/Post/Sandwich placement entry for the analysis from Xiong et al. (arXiv 2002.04745).

The 2019 simplification. Zhang and Sennrich (arXiv 1910.07467, Tables 1–5) ran a direct ablation of LayerNorm’s mean-subtraction step across six tasks (machine translation, reading comprehension, image-sentence ranking, two LM benchmarks, and a CIFAR classifier). They report no statistically significant quality difference between LayerNorm and RMSNorm — which drops μ\mu and β\boldsymbol{\beta} entirely — across any of the six. The wall-clock saving attributed to removing one reduction and one elementwise operation ranged from 7% to 64% depending on the model and framework. This ablation is the strongest single piece of evidence that LayerNorm’s centering step was carrying very little weight for transformers, which in turn explains why every open frontier dense LLM from Llama 1 (arXiv 2302.13971) onward has shipped RMSNorm instead.

Where LayerNorm with bias still survives. The legacy decoder-only family — GPT-2 (Radford et al. 2019), GPT-NeoX-20B (arXiv 2204.06745), the BLOOM-176B release (arXiv 2211.05100) — uses plain LayerNorm. The encoder-decoder line (T5, mT5) uses a modified LayerNorm variant without bias, sitting between the two. Outside of these legacy lines, no recent open dense or MoE LLM in this knowledge base’s adoption tables retains the full LayerNorm formulation; the per-block normalizer is either RMSNorm, a sandwich variant of RMSNorm, or QK-Norm-augmented RMSNorm. No public study has, to this author’s knowledge, isolated whether the bias term in LayerNorm hurts modern transformer quality at all — the empirical case for dropping it rests on parameter count and wall-clock alone.

Subsequent theoretical work. Xu et al. (arXiv 1911.07013, “Understanding and Improving Layer Normalization”) dissect the gradient expression above and report that the derivatives of γ\boldsymbol{\gamma} and β\boldsymbol{\beta} contribute non-trivially to optimization dynamics (their Table 3) — removing the affine entirely hurts convergence on machine-translation benchmarks even when the normalization itself is kept. Their AdaNorm variant rescales the affine adaptively and reports small but consistent BLEU gains on WMT En-De and En-Fr. Neither AdaNorm nor any other LayerNorm-with-modified-affine appears in the production open-weight stack tracked here; the field consolidated on the simpler RMSNorm rather than chasing affine improvements on top of LayerNorm.

Lineage

Cite

BibTeX entry for the original paper
@article{arxiv1607_06450,
  title  = {Layer Normalization},
  author = {Jimmy Lei Ba, Jamie Ryan Kiros, Geoffrey E. Hinton},
  year   = {2016},
  eprint = {1607.06450},
  archivePrefix = {arXiv},
  url    = {https://arxiv.org/abs/1607.06450}
}

Or cite the paper directly: arXiv:1607.06450.

Export

BibTeX
@article{arxiv_1607_06450,
  title         = {Layer Normalization},
  author        = {Jimmy Lei Ba and Jamie Ryan Kiros and Geoffrey E. Hinton},
  year          = {2016},
  eprint        = {1607.06450},
  archivePrefix = {arXiv},
  url           = {https://arxiv.org/abs/1607.06450}
}
CSL JSON
{
  "id": "arxiv_1607_06450",
  "type": "article-journal",
  "title": "Layer Normalization",
  "author": [
    {
      "literal": "Jimmy Lei Ba"
    },
    {
      "literal": "Jamie Ryan Kiros"
    },
    {
      "literal": "Geoffrey E. Hinton"
    }
  ],
  "issued": {
    "date-parts": [
      [
        2016
      ]
    ]
  },
  "URL": "https://arxiv.org/abs/1607.06450",
  "number": "1607.06450",
  "source": "arXiv"
}
RIS
TY  - JOUR
TI  - Layer Normalization
AU  - Jimmy Lei Ba
AU  - Jamie Ryan Kiros
AU  - Geoffrey E. Hinton
PY  - 2016
JO  - arXiv
AN  - arXiv:1607.06450
UR  - https://arxiv.org/abs/1607.06450
ER  -