Practice in Lab: Normalize RMS with stable accumulation and autograd →
Normalize RMS with stable accumulation and autograd
RMS normalization rescales by the root mean square and differs from centering-based layer normalization. The distinction is visible on a constant nonzero row, where RMS normalization retains a signal rather than removing it. Squaring low-precision values before promoting them can overflow even when the final normalized output is ordinary. This Lab promotes the accumulation first and checks both input and learned-weight gradients. It is an inspectable CPU formula, not a fused normalization kernel or a benchmark of accelerator throughput.
import torch
x = torch.tensor([3., 4.])
print(torch.sqrt(x.square().mean()))Review the design
Specify accumulation dtype, epsilon and learned-weight dtype when matching another implementation. Epsilon makes exact scale invariance inappropriate near zero.
Your task
Compute x / sqrt(mean(x**2,last_axis,keepdim=True)+eps) times a learned last-axis weight. Do not subtract the mean. Accumulate in float32 for float16/bfloat16/float32 input and float64 for float64 input, cast the normalized values back to x.dtype, then multiply weight. Require a nonempty last axis, weight shape [D] and finite eps>0. Return a new differentiable tensor without changing inputs.
The explanation opens here once every check passes.