RMSNorm › Mathematical Connection Between RMS and Standard Deviation

Michael BrenndoerferUpdated June 7, 20259 min read
Language AI HandbookAll chapters
Part 15 of 67Transformer Blocks
Chapter 105RMSNorm › Mathematical Connection Between RMS and Standard Deviation
Reading section 3 of 15 · Mathematical Connection Between RMS and Standard Deviation · 9 minSection 3 of 15 · 9 min

Book contents

Section 3 of 15 · 9 min read

RMSNorm › Mathematical Connection Between RMS and Standard DeviationOne section · Sections in this chapter3 / 15 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

We've claimed that RMSNorm works because activations in neural networks tend to be near-centered. But how near is near enough? To answer this precisely, we need to understand the mathematical relationship between what RMSNorm computes (the RMS) and what LayerNorm computes (the standard deviation). If the two quantities are nearly identical under typical transformer conditions, then the two normalizations are nearly identical in their effect.

This section derives the exact connection between these two quantities. The derivation is worth following carefully because it reveals a beautiful geometric relationship and tells us exactly when the two normalizations diverge. Understanding this relationship gives you the theoretical confidence to choose RMSNorm over LayerNorm: you will know precisely the conditions under which the choice matters and when it does not.

The key insight is that RMS and standard deviation are related through a Pythagorean identity involving the mean. This identity makes the trade-off perfectly precise: the larger the mean of the activations, the more the two normalizations differ. Conversely, when the mean is small relative to the standard deviation, the two normalizations are nearly interchangeable.

The Goal: Relating RMS to Standard Deviation

We want to express RMS(x)\text{RMS}(\mathbf{x}) in terms of σ\sigma (the standard deviation) and μ\mu (the mean). If we can do this, we'll know how much the two normalizations differ based on the mean alone.

Start with the definition of variance, which measures how spread out values are around their mean. The variance is defined as the average squared deviation from the mean:

σ2=1d∑i=1d(xi−μ)2\sigma^2 = \frac{1}{d} \sum_{i=1}^{d} (x_i - \mu)^2

where:

  • σ2\sigma^2: the variance of the input vector, measuring spread around the mean
  • dd: the number of elements in the vector
  • xix_i: the ii-th element of the input vector
  • μ\mu: the mean of the input elements, equal to 1d∑i=1dxi\frac{1}{d}\sum_{i=1}^{d} x_i

This formula takes each element, measures its distance from the mean, squares that distance, and averages all the squared distances. The square root of variance gives us the standard deviation σ\sigma, which has the same units as the original data.

Step-by-Step Derivation

Our strategy is to expand the variance formula and recognize familiar terms. We'll use the algebraic identity (a−b)2=a2−2ab+b2(a - b)^2 = a^2 - 2ab + b^2 to expand the squared term:

σ2=1d∑i=1d(xi2−2xiμ+μ2)\sigma^2 = \frac{1}{d} \sum_{i=1}^{d} (x_i^2 - 2x_i\mu + \mu^2)

Now we can distribute the sum across the three terms. Each term gets its own summation:

σ2=1d∑i=1dxi2−2μd∑i=1dxi+1d∑i=1dμ2\sigma^2 = \frac{1}{d} \sum_{i=1}^{d} x_i^2 - \frac{2\mu}{d} \sum_{i=1}^{d} x_i + \frac{1}{d} \sum_{i=1}^{d} \mu^2

Let's simplify each term:

  • First term: 1d∑i=1dxi2\frac{1}{d} \sum_{i=1}^{d} x_i^2 is the mean of squared values. This is exactly RMS(x)2\text{RMS}(\mathbf{x})^2.
  • Second term: The sum ∑i=1dxi\sum_{i=1}^{d} x_i equals d⋅μd \cdot \mu by the definition of mean. So this term becomes 2μd⋅(dμ)=2μ2\frac{2\mu}{d} \cdot (d\mu) = 2\mu^2.
  • Third term: We're summing the constant μ2\mu^2 exactly dd times, so this equals d⋅μ2d=μ2\frac{d \cdot \mu^2}{d} = \mu^2.

Substituting these simplifications:

σ2=RMS(x)2−2μ2+μ2=RMS(x)2−μ2\sigma^2 = \text{RMS}(\mathbf{x})^2 - 2\mu^2 + \mu^2 = \text{RMS}(\mathbf{x})^2 - \mu^2

Rearranging to isolate the RMS:

RMS(x)2=σ2+μ2\text{RMS}(\mathbf{x})^2 = \sigma^2 + \mu^2

Taking square roots of both sides:

RMS(x)=σ2+μ2\text{RMS}(\mathbf{x}) = \sqrt{\sigma^2 + \mu^2}

where:

  • RMS(x)\text{RMS}(\mathbf{x}): the root mean square of the input vector
  • σ\sigma: the standard deviation of the input vector (spread around the mean)
  • μ\mu: the mean of the input vector (location of the center)

Why does this formula make sense? Notice that the RMS combines information about both the mean and the spread of the distribution. The standard deviation σ\sigma only captures spread: it measures how far values deviate from their center. The mean μ\mu captures location: it measures where the center is. The RMS captures both because it measures distance from zero, not from the mean. A vector with large mean but small variance still has large RMS.

The Geometric Interpretation

This formula has a beautiful geometric meaning. Think of σ\sigma and μ\mu as the two legs of a right triangle. The RMS is the hypotenuse. The Pythagorean theorem tells us that hypotenuse2=leg12+leg22\text{hypotenuse}^2 = \text{leg}_1^2 + \text{leg}_2^2, which is exactly what we derived.

Think of it this way: if you draw a 2D coordinate system where the horizontal axis represents "standard deviation" and the vertical axis represents "mean," then any distribution maps to a point at coordinates (σ,μ)(\sigma, \mu). The RMS is the distance from the origin to that point, the length of the line segment from (0,0)(0,0) to (σ,μ)(\sigma, \mu). LayerNorm normalizes by the horizontal coordinate (divides by σ\sigma). RMSNorm normalizes by the diagonal distance from the origin (divides by σ2+μ2\sqrt{\sigma^2 + \mu^2}). When the vertical coordinate (the mean) is small, these two quantities are nearly identical.

Out[4]:
Visualization
Geometric diagram showing right triangles where RMS is the hypotenuse and standard deviation and mean are the legs. This shows the Pythagorean relationship.
The Pythagorean relationship between RMS, standard deviation, and mean. RMS forms the hypotenuse of a right triangle with σ and μ as legs. When μ approaches zero (dashed triangle), RMS approaches σ, explaining why the two normalizations converge for centered data.

This geometric picture immediately reveals when RMSNorm and LayerNorm behave similarly:

  • When μ=0\mu = 0: The triangle collapses to a line. The hypotenuse equals the remaining leg: RMS=σ\text{RMS} = \sigma. The two normalizations are identical.
  • When μ\mu is small relative to σ\sigma: The triangle is nearly flat. The hypotenuse is only slightly longer than σ\sigma. The normalizations are nearly equivalent.
  • When μ\mu is comparable to or larger than σ\sigma: The triangle is more equilateral or tall. The hypotenuse differs significantly from σ\sigma. The normalizations diverge.

The practical question becomes: in real neural networks, how large is μ\mu compared to σ\sigma? The simulation we ran earlier gave us the empirical answer: means are very small relative to standard deviations in well-initialized transformers, placing us firmly in the "nearly flat triangle" regime.

In[5]:
Code
import numpy as np


def layer_norm(x, gamma, eps=1e-6):
    """Standard layer normalization with centering."""
    mu = np.mean(x, axis=-1, keepdims=True)
    var = np.var(x, axis=-1, keepdims=True)
    x_norm = (x - mu) / np.sqrt(var + eps)
    return gamma * x_norm


def rms_norm(x, gamma, eps=1e-6):
    """RMSNorm without centering."""
    rms = np.sqrt(np.mean(x**2, axis=-1, keepdims=True))
    x_norm = x / (rms + eps)
    return gamma * x_norm


# Generate random activations (typical neural network values)
d = 256  # Hidden dimension
batch_size = 32

# Simulate activations after a linear layer + activation
activations = np.random.randn(batch_size, d) * 0.5 + 0.1  # Small positive bias

gamma = np.ones(d)  # Identity scaling for comparison
Out[6]:
Console
Input statistics (per sample):
  Mean magnitude: 0.1053
  Std deviation:  0.4904
  RMS value:      0.5022

Comparison of LayerNorm vs RMSNorm outputs:
  Mean absolute difference: 0.209557
  Max absolute difference:  0.444943
  Correlation:              0.998802

The outputs are highly correlated but not identical. The differences arise from the mean subtraction in LayerNorm. Let's visualize how the two normalizations compare across different input distributions.

In[7]:
Code
# Test with different mean magnitudes
mean_offsets = [0.0, 0.5, 1.0, 2.0, 5.0]
results = []

for offset in mean_offsets:
    x = np.random.randn(1000, d) * 0.5 + offset

    ln_out = layer_norm(x, gamma)
    rms_out = rms_norm(x, gamma)

    # Compute RMS difference
    rms_diff = np.sqrt(np.mean((ln_out - rms_out) ** 2))

    # Compute statistics
    input_mean = np.mean(np.abs(np.mean(x, axis=-1)))
    input_std = np.mean(np.std(x, axis=-1))

    results.append(
        {
            "offset": offset,
            "input_mean": input_mean,
            "input_std": input_std,
            "rms_difference": rms_diff,
        }
    )
Out[8]:
Visualization
Line plot showing RMS difference between LayerNorm and RMSNorm increasing as input mean offset grows from 0 to 5.
Difference between LayerNorm and RMSNorm outputs as input mean increases. When inputs are centered near zero, both normalizations produce nearly identical results. As the mean shifts away from zero, the difference grows because RMSNorm does not subtract the mean.

The key insight emerges: when inputs are approximately centered (mean near zero), the two normalizations are nearly equivalent. In deep neural networks with proper initialization and residual connections, activations tend to stay roughly centered. This explains why RMSNorm works as well as LayerNorm in practice.