Attention compares content through:
but pure content attention has no inherent token-order signal.
RoPE — Rotary Position Embeddings — injects position by rotating pairs of Q and K dimensions according to token position.
Start in 2D¶
Let:
A 2D rotation is:
At position \(m\):
If \(\theta=30^\circ\): - position 0 → [1,0] - position 1 → [0.866,0.5] - position 2 → [0.5,0.866]
Relative position emerges naturally¶
Do the same to key \(k\) at position \(n\).
Then:
Using rotation properties:
we get dependence on:
rather than absolute positions separately.
That is the elegant core of RoPE.
Multiple frequencies¶
A real head might have dimension 128.
RoPE groups dimensions into 64 pairs, each rotating at a different frequency.
Fast rotations help distinguish nearby positions; slow rotations carry information over longer scales.
Where it happens¶
X
├─ WQ → Q → RoPE ─┐
└─ WK → K → RoPE ─┤→ QKᵀ
V is generally not rotated.
KV-cache implication¶
Cached K vectors already contain their positional rotation.
When token 10001 generates a query, its RoPE-rotated Q compares naturally against cached keys from positions 1…10000.
Long-context caveat¶
Mathematical position values can extend indefinitely, but a model trained mainly on shorter contexts may not interpret unseen rotation regimes well.
That is why long-context extension often involves RoPE rescaling and additional training.
Why relative position is useful for language¶
Many language relationships depend more on distance than absolute index.
A closing quote often relates to a recent opening quote whether the pair occurs at token 50 or token 50,000.
RoPE makes relative offsets naturally visible in the query-key dot product.
Different frequency bands¶
For a head with many dimensions, some rotated pairs change rapidly with position and some slowly.
You can think of this as a bank of positional frequencies.
Nearby distinctions can be represented by high-frequency rotations; long-range structure can use slower rotations.
Long-context extrapolation¶
If training mostly occurred up to, say, 8K positions, the network learned to interpret the rotation patterns occurring in that range.
Simply evaluating at 128K introduces combinations it may rarely have seen.
RoPE scaling methods alter the position-to-angle mapping so long contexts fall into a more usable regime, often combined with long-context fine-tuning.
Cache consequence¶
Because positional information is baked into cached keys, you cannot always take a KV block computed at one position and arbitrarily move it somewhere else.
Position is part of the cached representation.
This becomes relevant for prefix caching, context editing, and advanced cache-reuse techniques.
RoPE is applied per head¶
After projection and reshape, each head has Q/K vectors such as:
RoPE pairs dimensions:
(q0,q1)
(q2,q3)
...
(q126,q127)
and rotates each pair with its own frequency.
This operation is inexpensive relative to the large linear projections and attention products, which is one reason RoPE is attractive.
Relative does not mean purely relative¶
The dot-product identity gives a clean relative-position property, but the full model can still infer absolute-position-like information from patterns of rotations, boundaries, causal structure, and learned behavior.
So it is better to say RoPE encodes position in a way that makes relative offsets natural than to say the model knows only relative position.
The exact way models use these signals is learned rather than manually specified.
Why moving cached tokens is tricky¶
Suppose a prefix cache contains K vectors computed for positions 1–1000. Reusing those vectors at positions 5001–6000 is not generally valid, because RoPE has already rotated them according to their original positions.
Advanced systems can sometimes transform or recompute positional components, but naïvely copying the cache changes the attention geometry.
This is another example of model math constraining systems optimizations: position encoding is not merely metadata attached to tokens; it changes the vectors stored in the cache.