Skip to content
class

RMSNorm

extends_AffineParameters
RMSNorm(normalized_shape: int | list[int] | tuple[int, ...], eps: float | None = None, elementwise_affine: bool = True, device: DeviceLike = None, dtype: DTypeLike = None)

Implementing kernel

C++ engine symbols that back this Python API.

Root Mean Square Layer Normalization.

Normalises the input by its root-mean-square value and applies a learnable per-element scale, but intentionally omits the mean subtraction step of standard layer norm:

y=xRMS(x)⋅γ,RMS(x)=1d∑i=1dxi2+εy = \frac{x}{\mathrm{RMS}(x)} \cdot \gamma, \qquad \mathrm{RMS}(x) = \sqrt{\frac{1}{d}\sum_{i=1}^{d} x_i^2 + \varepsilon}

where dd is the number of elements in normalized_shape.

Skipping mean subtraction reduces computation while retaining the re-scaling invariance property that makes normalization useful. This formulation is widely used in large language model architectures such as LLaMA.

Parameters

normalized_shapeint or list[int] or tuple[int, ...]
Shape of the trailing dimensions to normalize over. An integer d is equivalent to the single-element tuple (d,).
epsfloat= None
Small constant added inside the square root for numerical stability. None (the default) uses the machine epsilon of the input's dtype at each call.
deviceDeviceLike= None
Device on which to allocate weight. Default: None.
dtypeDTypeLike= None
Data type of weight. Default: None.

Attributes

weightParameter
Learnable per-element scale γ\gamma of shape normalized_shape, initialised to ones. Unlike LayerNorm, RMSNorm has no bias parameter by design.

Notes

  • Input: (∗,normalized_shape)(*, \text{normalized\_shape}).
  • Output: same shape as the input.
  • RMSNorm has no bias term; if a shift is needed, add a separate bias or use LayerNorm.
  • With eps=None the epsilon follows the input's dtype, as the reference framework's does. A fixed 1e-8 — the old default — rounds to zero in float16, so a half-precision Core ML export of an all-zero row divided by an exact zero.
  • The weight is initialised to all ones so the transformation starts as a pure normalisation.

Examples

Typical use in a transformer feed-forward block:
>>> import lucid
>>> import lucid.nn as nn
>>> norm = nn.RMSNorm(256)
>>> x = lucid.randn(4, 32, 256)   # (batch, seq_len, dim)
>>> out = norm(x)
>>> out.shape
(4, 32, 256)
Normalize over two trailing dimensions:
>>> norm2d = nn.RMSNorm((16, 16))
>>> x2d = lucid.randn(2, 8, 16, 16)
>>> out2d = norm2d(x2d)
>>> out2d.shape
(2, 8, 16, 16)

Used by 1

Constructors

1
dunder

__init__

→None
__init__(normalized_shape: int | list[int] | tuple[int, ...], eps: float | None = None, elementwise_affine: bool = True, device: DeviceLike = None, dtype: DTypeLike = None)

Initialise the RMSNorm module. See the class docstring for parameter semantics.

Instance methods

3
fn

extra_repr

→str
extra_repr()

Return a string representation of the layer's configuration.

fn

forward

→Tensor
forward(x: Tensor)

Apply normalisation to the input tensor.

Parameters

Input tensor whose shape is documented in the class docstring.

Returns

Tensor

Normalised tensor of the same shape as x.

fn

reset_parameters

→Nonefrom _AffineParameters
reset_parameters()

Put weight to ones and bias to zeros, where the layer has them.