Math Catch-Up · Same Length, Different Dot Product: Rotation and RoPE
RoPE makes position affect the matching relationship between Q and K. Start with an arrow in a plane: rotating it preserves length but changes direction and coordinates. Rotating two arrows by different angles can change their dot product.
The rotation derivation uses column vectors; network projections can still use row vectors, with the appropriate transpose. All numerical examples use teaching values.
Two numbers form an arrow, not two Tokens
The vector [3,4] points from the origin to (3,4) and has length √(3²+4²)=5. In a network, these can be two features within one attention head. They need not represent physical coordinates.
Keep the axes fixed, with x pointing right, y upward, and positive angles counterclockwise:
| Angle | Input | Output | Length |
|---|---|---|---|
| 0° | [3,4] | [3,4] | 5 |
| 90° | [3,4] | [−4,3] | 5 |
| 180° | [3,4] | [−3,−4] | 5 |
| 360° | [3,4] | [3,4] | 5 |
A ninety-degree rotation is not merely swapping two numbers; their signs matter.
Sine and cosine: components of a direction
On the unit circle, rotating from the positive x-axis by φ reaches (cosφ,sinφ). Thus (1,0) rotated by ninety degrees becomes (0,1).
Programming functions normally use radians: 180°=π, 90°=π/2. An angle of 1 means one radian, about 57.3°, not one degree.
Derive the matrix from two basis vectors
Write an arbitrary vector as a combination of unit arrows:
[a,b]ᵀ = a[1,0]ᵀ + b[0,1]ᵀAfter rotation by φ, the first basis vector is [cosφ,sinφ]ᵀ, and the second is [−sinφ,cosφ]ᵀ. The second remains ninety degrees ahead. Combine them using the original coefficients a and b:
a′ = a cosφ − b sinφb′ = a sinφ + b cosφ
[a′] [cosφ −sinφ] [a][b′] = [sinφ cosφ] [b] = R(φ)vInputs are a, b, and φ; the output still contains two numbers. R’s entries are computed from φ, not learned projection weights. With row vectors the equivalent formula is v′=vR(φ)ᵀ.
Calculate every term
For [a,b]=[1,2] and φ=1:
cos1 ≈ 0.540302306sin1 ≈ 0.841470985
a′ = 1×0.540302306 − 2×0.841470985 ≈ −1.142640b′ = 1×0.841470985 + 2×0.540302306 ≈ 1.922076Length remains √5. Displayed decimals are rounded; calculations should retain intermediate precision.
★ Why length stays fixed
(a′)²+(b′)²= (a cosφ−b sinφ)² + (a sinφ+b cosφ)²= a²cos²φ − 2ab cosφ sinφ + b²sin²φ + a²sin²φ + 2ab sinφ cosφ + b²cos²φ= a²(cos²φ+sin²φ) + b²(sin²φ+cos²φ)= a²+b²Cross terms cancel. Preserving the sum of squares does not preserve each coordinate or every dot product with another vector.
Insert RoPE: project, split heads, pair features, rotate
The route is normalized input → Q/K projection → head splitting → feature pairing inside each head → position-dependent rotation → attention dot product. W_Q/W_K are parameters, q/k are projection results, and the rotation is constructed from position and frequency.
For a four-dimensional head q=[1,2,3,4], use adjacent pairs (1,2) and (3,4). Rotate each with its own angle and put the results back in order. The total dimension is unchanged. Implementations can use other pairing layouts; their conventions must be considered together with weights. Changing pairing alone is not an equivalent replacement.
K rotates according to its own position, not the query’s position. The RoPE discussed here rotates Q/K, not V. GPT-2 instead adds a learned position vector at the input; RoPE changes Q/K geometry in every layer.
Position and pair index determine the angle
ω_r = θ^(−2r/d_h)φ(t,r) = t ω_r| Symbol | Meaning |
|---|---|
| t | Token position, starting at zero |
| r | Feature-pair index inside a head, not a head index |
| d_h | Head dimension; there are d_h/2 pairs |
| θ | Fixed frequency base, not the current rotation angle |
| ω_r | Angle added per position for this pair |
| φ(t,r) | Actual angle in radians |
Choose a rotational speed for each pair, then multiply by position. For θ greater than one, larger r means slower rotation. Adjacent frequencies have ratio θ^(−2/d_h), a geometric progression. The 2 reflects two dimensions per pair, not two rotations.
Original RoPE uses base 10000; the original Llama 3 configuration uses 500000. Use the latter with a teaching head dimension of four:
ω₀ = 1ω₁ = 1/√500000 ≈ 0.001414214| Position | Pair 0 angle | Pair 1 angle |
|---|---|---|
| 0 | 0 | 0 |
| 1 | 1 | 0.001414214 |
| 2 | 2 | 0.002828427 |
| 3 | 3 | 0.004242641 |
At position 1, [1,2,3,4] becomes approximately [-1.142640,1.922076,2.994340,4.004239]. This four-dimensional example does not represent all frequencies in a real model. See Meta’s original Llama 3 implementation for the configuration and computation.
★ How two absolute positions yield relative position
Within one pair, Q at m rotates by mω and K at n rotates by nω:
q_rotᵀ k_rot= (R(mω)q)ᵀ (R(nω)k)= qᵀ R(mω)ᵀ R(nω) k= qᵀ R((n−m)ω) kThe transpose reverses a rotation, and successive rotation angles add. Only the angle difference remains. Positions (2,5) and (7,10) both differ by three; holding unrotated q/k fixed gives the same dot product.
This identity concerns the positional rotation. Real q/k also depend on content and earlier layers. Equal distance does not guarantee equal attention scores. The causal mask separately determines whether a position may be read.
Section B fixes q=[1,2,3,4] at position 1. Section C uses the same unrotated k=[2,1,0,1] at positions 0 and 1 to isolate position. In section D, reading position 0 gives a dot product of about 3.641035 and a scaled score of 1.820517. Reading position 1 rotates Q/K together, preserving the dot product 8 and scaled score 4. These are not probabilities; masking and softmax follow.
What multiple frequencies provide—and their limits
Using the same speed for every pair would repeat the same positional variation. Different speeds supply multiple scales: fast pairs change substantially over small displacements, while slow pairs vary gradually over larger ones.
Position multiplied by frequency gives the relative-position property. Geometrically spaced frequencies are a design choice for arranging scales, not a unique consequence of planar rotation. See the construction in RoFormer §3.
It does not follow that every score falls as distance grows, or that RoPE allows reliable unlimited context. Computing a positional formula beyond previous positions and reliably using that context are different capabilities.
Return to the full network
RoPE does not replace normalization, QKV projections, masking, or softmax. It adds position to the matching relationship after Q/K have been produced. The route continues through QKᵀ/√d_h → mask → softmax → AV, head concatenation, and the residual path.
When revisiting the computation, identify the Token, head, and feature pair; determine its angle; then explain why the rotated Q/K dot product depends on their angle difference. These questions place the formula back on the complete forward map.