RMSNorm › Worked Example: Step-by-Step Numerical Trace

Michael BrenndoerferUpdated June 7, 20253 min read
Language AI HandbookAll chapters
Part 15 of 67Transformer Blocks
Chapter 105RMSNorm › Worked Example: Step-by-Step Numerical Trace
Reading section 4 of 15 · Worked Example: Step-by-Step Numerical Trace · 3 minSection 4 of 15 · 3 min

Book contents

Section 4 of 15 · 3 min read

RMSNorm › Worked Example: Step-by-Step Numerical TraceOne section · Sections in this chapter4 / 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

Before moving to code, let's trace through a complete numerical example of both LayerNorm and RMSNorm on a small vector. This gives you a concrete feel for the computations and reinforces the mathematical relationship we derived.

Suppose we have a small input vector with d=4d = 4 elements:

x=[2.0,−1.0,0.5,1.5]\mathbf{x} = [2.0, -1.0, 0.5, 1.5]

We'll normalize this vector with both methods and compare the results step by step.

Step 1: Compute the Statistics

First we compute the mean μ\mu and the root mean square RMS(x)\text{RMS}(\mathbf{x}):

μ=14(2.0+(−1.0)+0.5+1.5)=3.04=0.75\mu = \frac{1}{4}(2.0 + (-1.0) + 0.5 + 1.5) = \frac{3.0}{4} = 0.75

For the RMS, we first square each element: [4.0,1.0,0.25,2.25][4.0, 1.0, 0.25, 2.25]. The mean of these squares is:

14(4.0+1.0+0.25+2.25)=7.54=1.875\frac{1}{4}(4.0 + 1.0 + 0.25 + 2.25) = \frac{7.5}{4} = 1.875

So RMS(x)=1.875≈1.369\text{RMS}(\mathbf{x}) = \sqrt{1.875} \approx 1.369.

Let's verify the Pythagorean relationship. The variance is:

σ2=14[(2.0−0.75)2+(−1.0−0.75)2+(0.5−0.75)2+(1.5−0.75)2]\sigma^2 = \frac{1}{4}\left[(2.0 - 0.75)^2 + (-1.0 - 0.75)^2 + (0.5 - 0.75)^2 + (1.5 - 0.75)^2\right] =14[1.5625+3.0625+0.0625+0.5625]=5.254=1.3125= \frac{1}{4}\left[1.5625 + 3.0625 + 0.0625 + 0.5625\right] = \frac{5.25}{4} = 1.3125

So σ=1.3125≈1.146\sigma = \sqrt{1.3125} \approx 1.146. Now let's check: σ2+μ2=1.3125+0.5625=1.875=RMS2\sigma^2 + \mu^2 = 1.3125 + 0.5625 = 1.875 = \text{RMS}^2. The identity holds exactly.

Step 2: Apply LayerNorm

LayerNorm subtracts the mean, then divides by the standard deviation. Using ϵ=10−6\epsilon = 10^{-6} (negligible here):

x−μ=[2.0−0.75,−1.0−0.75,0.5−0.75,1.5−0.75]=[1.25,−1.75,−0.25,0.75]\mathbf{x} - \mu = [2.0 - 0.75, -1.0 - 0.75, 0.5 - 0.75, 1.5 - 0.75] = [1.25, -1.75, -0.25, 0.75]

Dividing by σ≈1.146\sigma \approx 1.146:

x−μσ≈[1.090,−1.527,−0.218,0.654]\frac{\mathbf{x} - \mu}{\sigma} \approx [1.090, -1.527, -0.218, 0.654]

With γ=1\gamma = \mathbf{1} and β=0\beta = \mathbf{0}, the LayerNorm output is [1.090,−1.527,−0.218,0.654][1.090, -1.527, -0.218, 0.654]. The output has mean exactly 0 and standard deviation exactly 1 (up to floating point precision).

Step 3: Apply RMSNorm

RMSNorm simply divides by the RMS, with no mean subtraction. Using RMS≈1.369\text{RMS} \approx 1.369:

xRMS≈[2.01.369,−1.01.369,0.51.369,1.51.369]≈[1.461,−0.730,0.365,1.096]\frac{\mathbf{x}}{\text{RMS}} \approx \left[\frac{2.0}{1.369}, \frac{-1.0}{1.369}, \frac{0.5}{1.369}, \frac{1.5}{1.369}\right] \approx [1.461, -0.730, 0.365, 1.096]

With γ=1\gamma = \mathbf{1}, the RMSNorm output is [1.461,−0.730,0.365,1.096][1.461, -0.730, 0.365, 1.096].

Step 4: Compare the Results

The two outputs differ because the mean μ=0.75\mu = 0.75 is non-trivial relative to the standard deviation σ≈1.146\sigma \approx 1.146. Their ratio is μ/σ≈0.65\mu / \sigma \approx 0.65, which is substantial. Using the geometric picture, this corresponds to a triangle with a large vertical leg relative to the horizontal leg, so the hypotenuse (RMS) differs noticeably from the horizontal leg (σ\sigma).

In a real transformer, the mean of activations is typically much smaller relative to the standard deviation. If we had μ=0.05\mu = 0.05 and σ=1.0\sigma = 1.0, then RMS=1.0+0.0025≈1.001\text{RMS} = \sqrt{1.0 + 0.0025} \approx 1.001, which is indistinguishable from σ=1.0\sigma = 1.0 for practical purposes. This is the regime that justifies RMSNorm's approximation in practice.

Step 5: Verify the RMS Property

We can verify that the RMSNorm output has RMS equal to 1 (when γ=1\gamma = \mathbf{1}):

RMS([1.461,−0.730,0.365,1.096])=1.4612+0.7302+0.3652+1.09624=2.134+0.533+0.133+1.2014=4.0014≈1.0\text{RMS}([1.461, -0.730, 0.365, 1.096]) = \sqrt{\frac{1.461^2 + 0.730^2 + 0.365^2 + 1.096^2}{4}} = \sqrt{\frac{2.134 + 0.533 + 0.133 + 1.201}{4}} = \sqrt{\frac{4.001}{4}} \approx 1.0

The output RMS equals 1.0 exactly (up to rounding), confirming that RMSNorm correctly normalizes the magnitude of the input vector to 1.