Batch, layer, group and RMS normalization: choose the reduction axes first
Normalization controls the scale of intermediate neural-network activations. The important questions are which values share statistics, whether the mean is removed, and which statistics are used at inference. BN, LN and GN center and rescale; RMSNorm rescales without centering. None promises that the final learned output has zero mean and unit variance.
See which values share statistics
svg
Each panel shows the same small tensor: two samples, four channels and two spatial positions. Colored cells form one reduction set. BN shares a channel's statistics across samples and positions. Full-sample LN uses every channel and position within one sample. GN uses a subset of channels and every spatial position within one sample. RMSNorm can use the same chosen axes as LN but computes a second moment instead of a centered variance. Other valid LayerNorm shapes are explained below.
The centered normalization equation
For a reduction set $S$ containing $m$ values, define:
The positive stabilizer $\epsilon$ prevents division by zero. Its placement inside the square root matters. Before the affine transformation, the mean over this set is zero, but its population variance is:
This is below one when epsilon is positive and the variance is finite; a constant set becomes all zeros. Learned scale and bias can change the output statistics further. Use the population denominator $m$ for these normalization statistics; do not silently substitute a sample-variance correction.
Batch normalization: one reduction set per channel
For an image tensor with shape $(N,C,H,W)$, training BN reduces over batch and spatial axes, independently for each channel:
Scale and bias are shared per channel. For a dense $(N,C)$ input the reduction count is only $N$. This distinction explains why there is no universal requirement such as batch size 32: spatial positions also contribute, and correlations affect how representative those statistics are. A single value per channel is a degenerate training case; supported frameworks may reject it.
With running statistics enabled, training uses current batch statistics while evaluation uses stored estimates. For PyTorch's momentum convention, a fixed update weight $a$ gives:
PyTorch normalizes a training batch with population variance, but updates its running variance using the corrected batch estimate:
This convention is implementation-specific; inspect the framework when porting weights. If running-statistic tracking is disabled, PyTorch BN uses batch statistics in evaluation too. Ordinary distributed BN can use local-device statistics; SyncBatchNorm explicitly aggregates across a process group and introduces communication. Gradient accumulation does not itself combine BN statistics across microbatches.
Distribution shift or poorly estimated running statistics can create a train/evaluation mismatch. Check both modes with representative data. The original BN paper motivated the method through internal covariate shift; later work questions that explanation and investigates optimization smoothness. Treat improved optimization as an empirical outcome, not proof of one universal mechanism.
Layer normalization: specify the normalized shape
LN computes statistics within each example over selected trailing dimensions. In a token tensor shaped $(N,T,D)$, normalizing the final dimension uses $D$ features independently for every token. Other tokens and other samples do not enter that token's statistics.
For the image tensor above, LayerNorm over $(C,H,W)$ instead uses an entire sample. This is the full-sample LN shown in the illustration. LayerNorm over channels at each spatial location is a different operation and requires the appropriate layout/axes. Saying only “normalize a layer” is not enough to reproduce a model.
LN uses current-input statistics in both training and evaluation. Its normalization operation has no BN-style running-statistics mismatch, although dropout and other model components can still behave differently between modes. Its affine parameters are commonly per element of the normalized shape. Pre-norm and post-norm refer to placement around a residual sublayer; neither placement alone guarantees stable training at arbitrary depth.
Group normalization: split channels, never samples
GN partitions $C$ channels into $G$ equal groups, requiring divisibility. Each set contains all spatial positions in one channel group of one sample:
Increasing the number of groups makes each group smaller. GN uses input statistics in both modes and commonly has per-channel affine parameters. For example, 512 channels and 32 groups give 16 channels per group.
At $G=1$, GN's reduction axes match full-sample LN over $(C,H,W)$, but their affine parameterization may differ. At $G=C$, GN's reduction axes match per-channel, per-sample instance normalization; running-statistic options and affine settings must still be matched for equivalence. No group count turns GN into BN, because GN never pools statistics across samples.
GN is useful when batch-dependent statistics are undesirable, including memory-constrained vision workloads. It does not guarantee a fixed memory saving or a particular regularization strength. A group with only one value is degenerate for centered normalization.
RMSNorm: second moment, not variance
For the chosen set of features:
This is the common scale-only form; implementations can differ in parameterization and epsilon defaults. The second moment satisfies:
Consequently RMSNorm does not subtract the mean or normalize the centered variance to one. Before learned scaling, the mean square becomes $q_S/(q_S+\epsilon)$. RMSNorm is not invariant to adding a constant offset. Centered normalization is invariant to a shared additive offset within a reduction set. Exact positive rescaling invariance for either form holds in the nondegenerate epsilon-zero limit; finite epsilon breaks exact scale invariance.
Worked example: the same inputs, different geometry
Take $(1,3)$ and temporarily set epsilon to zero for transparent arithmetic. The mean is 2 and population variance is 1, so centered normalization returns $(-1,1)$. The second moment is 5, so RMS normalization returns approximately $(0.447214,1.341641)$. Its mean is approximately $0.894427$ and its variance is $0.2$, even though its mean square is 1. The lab checks all of these values and shows that adding 10 preserves centered outputs but changes RMS outputs.
A practical selection and verification flow
Preserve the normalization expected by a pretrained checkpoint. For a new design, compare alternatives under the same training and tuning budget. Log the reduction axes, epsilon, affine shape, BN running-statistic convention, train/evaluation mode, and any synchronization across devices. Check constant inputs, batch composition changes and realistic mixed-precision ranges.
BN may suit vision workloads with representative reduction sets; GN avoids cross-sample dependence; token-wise LN and RMSNorm suit independent per-token feature normalization. These are design considerations, not universal accuracy rankings. Sequence length variability requires attention to axes and padding; it does not make every form of BN mathematically impossible.
RMSNorm removes the centered mean calculation and subtraction, but elapsed time depends on fusion, precision, feature width, memory traffic and device kernels. Benchmark the deployed model rather than promising a fixed percentage speedup. Likewise, normalization is not the only way to train deep networks, and it cannot by itself prevent all exploding or vanishing gradients.
Numerical lab — Python standard library
import math
def mean(x):
return sum(x)/len(x)
def variance(x):
u = mean(x)
return mean([(v-u)**2 for v in x])
def centered(x, eps=1e-5):
u, v = mean(x), variance(x)
return [(a-u)/math.sqrt(v+eps) for a in x]
def rms(x, eps=1e-5):
q = mean([a*a for a in x])
return [a/math.sqrt(q+eps) for a in x]
def close(a, b):
assert len(a) == len(b)
assert all(abs(x-y) < 1e-12 for x, y in zip(a, b))
x = [1.0, 3.0]
close(centered(x, 0), [-1, 1])
r = rms(x, 0)
close(r, [1/math.sqrt(5), 3/math.sqrt(5)])
assert abs(mean(r)-2/math.sqrt(5)) < 1e-12
assert abs(variance(r)-0.2) < 1e-12
assert abs(mean([a*a for a in r])-1) < 1e-12
close(centered(x), centered([11, 13]))
assert abs(rms(x)[0]-rms([11, 13])[0]) > 0.1
assert abs(variance(centered(x, 0.1))-1/1.1) < 1e-12
close(centered([7, 7]), [0, 0])
close(rms([0, 0]), [0, 0])
# Shape N=2, C=4, P=2; P combines spatial positions.
data = [[[1+10*n+2*c+p for p in range(2)] for c in range(4)] for n in range(2)]
def group_sample(sample, groups):
channels = len(sample)
assert channels % groups == 0
width = channels//groups
out = []
for start in range(0, channels, width):
values = [a for channel in sample[start:start+width] for a in channel]
out.extend(centered(values))
return out
flat = [a for channel in data[0] for a in channel]
close(group_sample(data[0], 1), centered(flat))
close(group_sample(data[0], 4), [a for channel in data[0] for a in centered(channel)])
assert len(group_sample(data[0], 2)) == 8
assert 512//32 == 16
def bn_channel(batch, channel):
return centered([a for sample in batch for a in sample[channel]])
changed = [data[0], [[v+100 for v in channel] for channel in data[1]]]
assert abs(bn_channel(data, 0)[0]-bn_channel(changed, 0)[0]) > 0.01
close(group_sample(data[0], 2), group_sample(changed[0], 2))
# Population vs sample variance and running update.
pop = variance([1, 3])
assert pop == 1 and pop*2/(2-1) == 2
running = (1-0.1)*1 + 0.1*2
assert abs(running-1.1) < 1e-12
print('Centered:', centered(x, 0))
print('RMS:', [round(a, 6) for a in r])
print('RMS mean / variance:', round(mean(r), 6), round(variance(r), 6))
print('All normalization and reduction-set checks passed.')The lab independently checks the worked numbers, epsilon's variance effect, constant inputs, group-size examples, and the fact that changing another sample changes training BN but leaves sample-local normalization unchanged. It is a mathematical reference, not a GPU performance benchmark.
Sources and implementation references
Ioffe and Szegedy, Batch Normalization (2015): https://proceedings.mlr.press/v37/ioffe15.html
Ba et al., Layer Normalization (2016): https://arxiv.org/abs/1607.06450
Wu and He, Group Normalization (2018): https://arxiv.org/abs/1803.08494
Zhang and Sennrich, Root Mean Square Layer Normalization (2019): https://arxiv.org/abs/1910.07467
Santurkar et al., How Does Batch Normalization Help Optimization? (2018): https://arxiv.org/abs/1805.11604
PyTorch BatchNorm2d, GroupNorm and LayerNorm reference: https://docs.pytorch.org/docs/stable/nn.html