The positional encoding that won the war
RoPE (Rotary Position Embedding) is used by LLaMA, Mistral, Qwen, DeepSeek, Gemma, Phi... in fact, virtually all modern LLMs. Why this absolute dominance? Because it's both mathematically elegant and devastatingly effective in practice.
RoFormer: Enhanced Transformer with Rotary Position EmbeddingThe fundamental idea: encoding position through rotation
Classical positional encodings (sinusoidal from the original Transformer, or learned like in GPT-2) add a position vector to the token embedding. It's like sticking a label "position 42" on the token.
RoPE does things differently: instead of adding information, it rotates the query/key vector in space. The token's position is encoded in the geometry of the vector itself, not in an external addition.
Concretely, for two consecutive dimensions of the vector, we apply a rotation of angle , where controls the rotation speed, the first dimension pairs rotate fast (capturing local relationships), the last rotate slowly (capturing long-range relationships):
Where are the pair's two components, is the token's position in the sequence, and is a frequency specific to this pair of dimensions. The matrix in the middle is simply the standard recipe for turning a point of the plane by an angle : the vector is , pivoted, its length unchanged.

Imagine that each pair of dimensions of the vector is a clock hand. The token's position in the sequence determines how much each hand has turned. The hands turn at different speeds: some complete a full rotation in a few tokens (high frequency, fine local position), others take thousands of tokens for one rotation (low frequency, global position). Together, they encode the position uniquely.

Extension to all dimensions
For a vector of dimension , we treat dimensions in pairs. Each pair has its own rotation frequency:
where by default. Reading the formula: is the rotation frequency of the -th pair of dimensions, and the negative exponent makes it shrink as grows. The first pairs ( small) have close to 1: fast rotation, a clean notch at every token, they finely distinguish neighboring positions. The last pairs ( large) have a tiny : slow rotation, taking thousands of tokens to complete one turn, they place the token at document scale. The exponent uniformly distributes frequencies on a logarithmic scale, like octaves in music: each "octave" covers a doubling of frequency, and dimensions are evenly spread between these octaves. This creates a frequency spectrum covering all distance scales:
| Dimension pair | Frequency | Behavior |
|---|---|---|
| D1-D2 | Fast rotation, encodes fine local position | |
| D3-D4 | Medium rotation | |
| D5-D6 | Slow rotation | |
| ... | ... | ... |
| D127-D128 | Very small | Ultra-slow rotation, encodes global position |
It's like a multi-scale coordinate system: the first dimension pairs distinguish neighboring tokens, the last distinguish tokens separated by thousands of positions.

The true genius of RoPE: relative positions emerge naturally
Here's the mathematical property that makes all the magic. When you compute the dot product (the sum of term-by-term products, see dot product and projections) between a query at position and a key at position , after the rotations:
Not on alone, not on alone. Only the difference . This is a pure mathematical property of rotations: when you rotate two vectors by two different angles, the dot product depends only on the difference of the angles.
In practice, this means attention automatically encodes relative positions: "this token is 3 positions before that one" rather than "this token is at position 2012." That's much more useful for understanding text structure.
Attention with relative positions generalizes much better to context lengths different from those seen during training. "Am I 3 tokens before this one?" is stable, reusable information. "Am I at position 2012?" is an artifact of the specific sequence length.
Efficient implementation with complex numbers
In practice, rotations are implemented with complex numbers (two-component numbers that are the natural mathematical tool for describing rotations of the plane) which is both elegant and fast.
Each pair is interpreted as a complex number:
In these lines, the standalone is the imaginary unit of complex numbers (nothing to do with the dimension index), and multiplying by = rotating by angle . A complex multiplication translates to 4 real multiplications + 2 additions. This is very fast on GPUs, and most libraries have optimized implementations.
RoPE is applied to queries and keys before the keys are stored in the KV cache. The keys in the cache are therefore already "rotated." Result: negligible inference overhead. The rotation is computed once per token, at the time of processing.
The concrete advantages of RoPE
- Works for any length: rotations are defined for any integer position, no theoretical limit
- Compatible with KV cache: the rotation is applied once, then the cache is reused without recomputation
- Negligible cost: a few multiplications per dimension, lost in the noise of the attention computation
- Natural continuity: neighboring positions have neighboring angles, therefore close representations. No artificial discontinuity.
The limits (and the solutions)
Extrapolation doesn't work directly
A model trained with RoPE on 4K token contexts doesn't generalize well to 16K tokens. Why? High-frequency dimension pairs produce rotation angles never seen during training. The model encounters geometric configurations it has never experienced.
The solutions: YaRN and NTK-aware scaling modify rotation frequencies to "stretch" positions in a coherent way. This is covered in detail in the context extension lesson.
The choice of base
By default, . But recent work shows that massively increasing the base (500K for LLaMA 3, for example) allows better handling of long sequences. A larger base = lower frequencies = slower rotations = the model "distinguishes" more distant positions.
RoPE imposes a fixed structure: a logarithmic frequency spectrum dictated by the base. You can't arbitrarily redefine how positions work. It's a deliberate trade-off: you exchange flexibility for elegance and efficiency.
Who uses RoPE?
- LLaMA (1, 2, 3)
- Mistral / Mixtral
- Qwen
- DeepSeek
- Gemma
- Phi
Virtually 100% of modern open-source LLMs. It's the undisputed de facto standard.
RoPE has replaced the absolute sinusoidal encodings from the original Transformer (2017) and the learned relative positions from T5. It's the standard that will likely remain for a good while. Simple to implement, mathematically elegant, effective in practice, compatible with the KV cache. Hard to do better.
1. How does RoPE encode a token's position, in contrast with classic encodings?
2. After rotation, the dot product between a query at position m and a key at position n depends only on m − n. Why is that so valuable?
3. Why do the dimension pairs rotate at different speeds, like the hands of a clock?
4. A model trained with RoPE on 4K-token contexts degrades sharply at 16K. What is going on?