RMSNorm vs LayerNorm — When and Why
import torch
def layer_norm(x, gamma, beta, eps=1e-5):
mu = x.mean(-1, keepdim=True)
var = x.var(-1, keepdim=True, unbiased=False)
return (x - mu) / torch.sqrt(var + eps) * gamma + beta
def rms_norm(x, gamma, eps=1e-5):
rms = x.pow(2).mean(-1, keepdim=True)
return x * torch.rsqrt(rms + eps) * gamma
Why it matters: RMSNorm drops the mean-centering step and the bias, normalizing by the root-mean-square instead of the standard deviation — same gain term, fewer ops, ~7–15% lower latency (Zhang & Sennrich 2019). The empirical finding that justified the simplification: re-centering isn’t actually needed for transformer training to converge, which is why Llama, Qwen, Mistral and Gemma all use RMSNorm by default.