Rotary Position Embedding (RoPE) › Worked Example: Step-by-Step RoPE for a 4-Dimensional Vector

Michael BrenndoerferUpdated June 5, 20253 min read
Language AI HandbookAll chapters
Part 14 of 67Positional Encoding
Chapter 100Rotary Position Embedding (RoPE) › Worked Example: Step-by-Step RoPE for a 4-Dimensional Vector
Reading section 8 of 19 · Worked Example: Step-by-Step RoPE for a 4-Dimensional Vector · 3 minSection 8 of 19 · 3 min

Book contents

Section 8 of 19 · 3 min read

Rotary Position Embedding (RoPE) › Worked Example: Step-by-Step RoPE for a 4-Dimensional VectorOne section · Sections in this chapter8 / 19 ↓
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 we look at batched implementations, it helps to trace through a complete example by hand. This makes the abstract rotation formula concrete and shows exactly what changes when you apply RoPE to a real vector.

Suppose we have a 4-dimensional query vector at position m=2m = 2:

q=(1.0, 0.5, −0.8, 0.3)\mathbf{q} = (1.0,\ 0.5,\ -0.8,\ 0.3)

With d=4d = 4, we have two dimension pairs and two frequencies:

θ0=1100000/4=1.0,θ1=1100002/4=1100=0.01\theta_0 = \frac{1}{10000^{0/4}} = 1.0, \qquad \theta_1 = \frac{1}{10000^{2/4}} = \frac{1}{100} = 0.01

At position m=2m = 2, the rotation angles for each pair are:

  • Pair 0: ϕ0=2×θ0=2×1.0=2.0\phi_0 = 2 \times \theta_0 = 2 \times 1.0 = 2.0 radians
  • Pair 1: ϕ1=2×θ1=2×0.01=0.02\phi_1 = 2 \times \theta_1 = 2 \times 0.01 = 0.02 radians

Now apply the 2D rotation formula to each pair separately. For pair 0, with (x1,x2)=(1.0,0.5)(x_1, x_2) = (1.0, 0.5) and ϕ0=2.0\phi_0 = 2.0:

x1′=x1cos⁡(ϕ0)−x2sin⁡(ϕ0)=1.0×cos⁡(2.0)−0.5×sin⁡(2.0)=1.0×(−0.416)−0.5×(0.909)=−0.416−0.455=−0.871x2′=x1sin⁡(ϕ0)+x2cos⁡(ϕ0)=1.0×0.909+0.5×(−0.416)=0.909−0.208=0.701\begin{aligned} x'_1 &= x_1 \cos(\phi_0) - x_2 \sin(\phi_0) \\ &= 1.0 \times \cos(2.0) - 0.5 \times \sin(2.0) \\ &= 1.0 \times (-0.416) - 0.5 \times (0.909) \\ &= -0.416 - 0.455 = -0.871 \\[6pt] x'_2 &= x_1 \sin(\phi_0) + x_2 \cos(\phi_0) \\ &= 1.0 \times 0.909 + 0.5 \times (-0.416) \\ &= 0.909 - 0.208 = 0.701 \end{aligned}

For pair 1, with (x3,x4)=(−0.8,0.3)(x_3, x_4) = (-0.8, 0.3) and ϕ1=0.02\phi_1 = 0.02 (small angle, so cos⁡(0.02)≈1.0\cos(0.02) \approx 1.0 and sin⁡(0.02)≈0.02\sin(0.02) \approx 0.02):

x3′=−0.8×1.0−0.3×0.02=−0.8−0.006=−0.806x4′=−0.8×0.02+0.3×1.0=−0.016+0.3=0.284\begin{aligned} x'_3 &= -0.8 \times 1.0 - 0.3 \times 0.02 = -0.8 - 0.006 = -0.806 \\ x'_4 &= -0.8 \times 0.02 + 0.3 \times 1.0 = -0.016 + 0.3 = 0.284 \end{aligned}

The rotated query is q′=(−0.871, 0.701, −0.806, 0.284)\mathbf{q}' = (-0.871,\ 0.701,\ -0.806,\ 0.284). Notice that the first pair changed dramatically (2 radians is a large rotation), while the second pair barely moved (0.02 radians is about 1 degree). This illustrates the multi-scale nature of RoPE: high-frequency pairs carry a strong position signal that varies quickly from token to token, while low-frequency pairs change slowly and provide a coarser sense of position.

To confirm the key property, you would perform the same calculation for a key vector at position n=5n = 5, compute the dot product q′Tk′\mathbf{q}'^T \mathbf{k}', and verify that it equals the dot product you would get at any other pair of positions with relative distance 5−2=35 - 2 = 3. The numbers work out exactly because the composition of rotations always produces the relative angle regardless of where in the sequence you started.