LayerNorm #
A quick chapter that extends the normalization family to what ViT needs: a
structural footnote to BatchNorm, not new territory — which is itself the point.
The smooth activations ViT and EfficientNet use (GELU, Swish, sigmoid) are in
Architectures.Activations, which this file re-exports.
LayerNorm: BatchNorm on a different axis #
BatchNorm reduces over (batch, H, W) for each channel. LayerNorm
reduces over the feature dimension for each (batch, token).
The 1D normalization primitive is literally the same function. What
differs is the axis you slice along before applying it.
Concretely, for a 4D activation x : Tensor4 B C H W:
- BN computes
Cmeans/variances, each overB · H · Welements. - LN computes
B · H · Wmeans/variances, each overCelements.
The mean/var/istd/xhat/affine math is identical. The consolidated
three-term backward is identical. Only the index being summed over
changes. In our Vec n formalism, BN and LN collapse to the same
function. This file just renames it to tell the reader "yes, really,
it's the same thing."
Also in this file #
- Layer scale:
layerScale,layerScaleHasVJP. - Vector-
[D]LayerNorm (per-feature affine):layerNormVec,layerNormVecHasVJP, and its per-token liftlayerNormVecPerTokenHasVJPMat. - Its γ/β parameter gradients:
vecLNGradGamma,vecLNGradBeta, the bridgeslayerNormVec_gamma_grad_bridge/layerNormVec_beta_grad_bridge, and their SGD formslayerNormVec_gamma_sgd_certified/layerNormVec_beta_sgd_certified.
Imported directly by SE, Attention, DropPath and Nets/ConvNeXt/ConvNeXt; the rest of the corpus reaches it through them.
References #
- Ba, Kiros, Hinton 2016, Layer Normalization. https://arxiv.org/abs/1607.06450
- Touvron et al. 2021, Going deeper with Image Transformers (CaiT, layer-scale). https://arxiv.org/abs/2103.17239
LayerNorm forward — renamed bnForward to make the book's
claim unambiguous: this is the same function, operating on a
different slice of the tensor.
For a single "token's feature vector" x : Vec n:
mu = (1/n) sum_i x_i— mean across featuressigma^2 = (1/n) sum_i (x_i - mu)^2— variance across featuresistd = 1/sqrt(sigma^2 + eps)xhat_i = (x_i - mu) * istd— normalizedy_i = gamma * xhat_i + beta— affine
Here γ and β are scalars, exactly as in bnForward. The
per-feature [D] affine that LN uses in practice is layerNormVec
below, built as γ ⊙ layerNormForward D ε 1 0 x + β.
MLIR (MlirCodegen.emitLayerNormForward):
identical reduction structure to BN, just across a different axis.
Equations
- Proofs.layerNormForward n ε γ β x = Proofs.bnForward n ε γ β x
Instances For
LayerNorm input gradient — identical closed form to BN.
dx_i = (1/n) * istd * (n * dxhat_i - sum_j dxhat_j - xhat_i * sum_j xhat_j * dxhat_j)
where dxhat_i = gamma * dy_i.
layerNormForward is bnForward by definition, so layerNormHasVJP
is bnHasVJP.
Equations
- Proofs.layerNormHasVJP n ε γ β hε = id (Proofs.bnHasVJP n ε γ β hε)
Instances For
Why this isn't a new chapter #
The practical differences between BN and LN (batch dependence, inference vs training, running statistics) are engineering concerns, not VJP concerns. The backward pass is the same three-term formula either way. This is a general lesson about formal work: engineering distinctions often dissolve at the math level, and that's worth making explicit. A reader who assumed BN and LN needed separate proofs learns that the separation was an implementation artifact.
BN and LN share one HasVJP (layerNormForward is bnForward).
GroupNorm and InstanceNorm apply the same 1D primitive to other slices
of the tensor; RMSNorm drops the mean centering and is a different
function. None of those three is formalized here.
Public correctness theorem for layerNormHasVJP: LayerNorm
reuses the BN proof template (LayerNorm is BN on a different axis), so
the contract is identical — backward equals the pdiv-contracted
Jacobian of layerNormForward.
Layer scale — per-channel learnable elementwise multiply by γ.
layerScale γ x i = γ i * x i. A diagonal linear map.
Equations
- Proofs.layerScale γ x i = γ i * x i
Instances For
layerScale γ is differentiable everywhere (diagonal linear).
Jacobian of layerScale — ∂(γ_j x_j)/∂x_i = γ_i δ_{ij}.
Layer scale VJP: back(x, dy)_i = γ i * dy i.
Equations
- Proofs.layerScaleHasVJP γ = { backward := fun (_x dy : Proofs.Vec n) (i : Fin n) => γ i * dy i, correct := ⋯ }
Instances For
Vector-[D] LayerNorm: per-token normalize (the scalar LN at γ=1, β=0 — pure
x̂), then the per-channel affine γ ⊙ x̂ + β. The committed ViTRender LN form.
Equations
- Proofs.layerNormVec D ε γv βv x k = γv k * Proofs.layerNormForward D ε 1 0 x k + βv k
Instances For
The bias translation's VJP — backward is the identity (dx = dy).
Equations
- Proofs.biasAddHasVJP βv = { backward := fun (_z dy : Proofs.Vec n) => dy, correct := ⋯ }
Instances For
Vector-LN VJP — (+β) ∘ layerScale γ ∘ LN(1,0), three proven pieces glued
by vjpComp. Only 0 < ε.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Per-token vector-LN across a sequence — the rowwise lift.
Equations
- Proofs.layerNormVecPerTokenHasVJPMat N D ε γv βv hε = Proofs.rowwiseHasVJPMat (Proofs.layerNormVecHasVJP D ε γv βv hε) ⋯
Instances For
Generic flat differentiability of a rowwise lift — each output coordinate is a coordinate of the per-row map applied to one row of the input.
Jacobian of the rowwise vector-LN site w.r.t. γv —
∂y_(r,k)/∂γv_i = δ_(i,k)·x̂_r(k).
Jacobian of the rowwise vector-LN site w.r.t. βv — ∂y_(r,k)/∂βv_i = δ_(i,k).
The rendered vector-LN γ gradient: per-channel, the batch+token reduce
dγ_k = Σ_r dY_(r,k)·x̂_r(k) (KEEPS the channel axis — ViTRender's form).
Equations
- Proofs.vecLNGradGamma N D ε X dY i = ∑ r : Fin N, dY r i * Proofs.layerNormForward D ε 1 0 (X r) i
Instances For
The rendered vector-LN β gradient: dβ_k = Σ_r dY_(r,k).
Equations
- Proofs.vecLNGradBeta N D dY i = ∑ r : Fin N, dY r i
Instances For
Vector-LN γ-gradient bridge.
Vector-LN β-gradient bridge.
Vector-LN γ output, certified. γvⁿ_k = γv_k − lr·(Σ_tokens dy·x̂)_k denotes
the certified rowwise vector-LN ∂/∂γv contraction: a rewrite by
layerNormVec_gamma_grad_bridge, stated at one LN site with generic N, D.
Vector-LN β output, certified.