Position and rotary embeddings
Rotating vectors so attention knows who is near whom
Chapter 8 of 21 · II. The transformer · about 40 minutes. Builds on: 6. Attention.
Attention cannot see order
Section titled “Attention cannot see order”Look back at the formula . If you shuffle the rows of , the rows of , , shuffle the same way, and the output rows shuffle identically. Attention is permutation-equivariant: “dog bites man” and “man bites dog” would produce the same bag of vectors. Position must be injected deliberately.
Approaches, in historical order:
- Absolute encodings added to the embedding: fixed sinusoids at many frequencies , or a learned vector per position. Simple, but the model must learn relations between positions indirectly and does not extend to longer sequences than it saw.
- Attention biases: ALiBi subtracts a penalty proportional to the distance || from each score, with a different slope per head. No extra parameters, and extrapolates better than absolute encodings.
- Rotary embeddings (RoPE) : rotate queries and keys by a position-dependent angle. This is used by Llama, DeepSeek, gpt-oss and most current open models.
Prerequisite: rotations in the plane
Section titled “Prerequisite: rotations in the plane”A rotation of the plane by angle is the matrix
Facts used below: ; (the transpose is the inverse); and rotations preserve lengths and dot products, so .
The RoPE idea
Section titled “The RoPE idea”We want a score between a query at position and a key at position that depends on the two vectors and on the offset , and on nothing else. Split the components of and into pairs, and treat each pair as a point in the plane. Rotate the pair at position by angle :
Then the score for that pair is
It depends only on . Nothing about absolute position survives; the model sees relative offset for free. The full score is the sum over all pairs, each pair using its own rotation speed:
The original paper uses base = 10,000.
Think of clocks. Pair 0 spins fastest ( radian per token) and can tell neighbors apart; the last pair spins very slowly and distinguishes tokens only thousands of positions apart. The wavelength of pair is π/ tokens per full turn. Position is applied to queries and keys only, never to values, and it is applied afresh in every layer. Check the identity numerically:
Implementation is cheap: rotating each pair is two multiply-adds with precomputed cosines and sines, so no matrices are materialized.
Extending the context window
Section titled “Extending the context window”A model trained on sequences up to length has only ever seen rotation angles up to . At position beyond the slow clocks show angles never seen in training, and quality collapses. Several methods adapt a trained RoPE model to a longer window ( = the extension factor).
Position interpolation
Section titled “Position interpolation”squeeze the new positions into the old range by using in place of . Every angle stays within the trained range, but all clocks are slowed by the same factor, so the fast clocks lose their ability to tell neighbors apart, and a short fine-tune is needed to recover.
NTK-aware and YaRN
Section titled “NTK-aware and YaRN”YaRN treats each frequency according to how many full turns it makes inside the original window, :
- Slow clocks (, they never completed one turn in training): interpolate fully, divide the frequency by .
- Fast clocks (, they turned many times): leave unchanged; they encode local order and were seen at all phases.
- Between: blend linearly with a ramp ; the new frequency is . The paper finds and work well for the Llama family.
YaRN also multiplies attention logits by a temperature correction: both and are scaled by ·ln() + 1, so logits scale by . It is applied inside the rotary tables, so it adds no inference cost. Explore the frequency treatment:
Real examples from published configurations: gpt-oss-120b was extended from a 4,096 window by YaRN with factor 32 to 131,072 tokens , and DeepSeek-V3 used YaRN with factor 40 across two 1,000-step fine-tuning phases to reach 128K . Both multiply to exactly the window the config reports (; ).
Long context is not uniformly used
Section titled “Long context is not uniformly used”Having a long window is not the same as using it well. found that models retrieve information best from the start and end of a long prompt and worse from the middle.
Check yourself
Section titled “Check yourself”Prove the identity ⟨R(mθ)q, R(nθ)k⟩ = ⟨q, R((n−m)θ)k⟩.
.
With base 10,000 and d = 64, what is the longest wavelength?
for the last pair () is , so tokens.
Why does YaRN leave the fastest frequencies unchanged?
They complete many turns in the original window, so the model has seen every phase of them; compressing them would destroy the fine resolution needed to tell adjacent tokens apart.
Papers
Section titled “Papers”- RoPE.
- ALiBi, a different route to length extrapolation.
- Position interpolation.
- YaRN: sections 3.1–3.4 contain the formulas.
- How models actually use long contexts.