PythiloLesson 6 of 9 · progress kept in this browser

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()))
This example uses torch. Run it in the Pythilo app, where these features are available.
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.

This lesson uses torch, which Python in this browser does not include. Read it here, and run and check it in the Pythilo app on iPhone or iPad.