Before you move a control in the demo, say what you expect to change.
Foundation reference
Rotary Position Embeddings (RoPE)
Rotary position embeddings (RoPE) encode token position by rotating query and key vectors. RoPE is the position encoding of many large language models, including GPT-NeoX-20B (Black et al., 2022), PaLM (Chowdhery et al., 2023) and the Llama family (Touvron et al., 2023).
Study guide
Where to start
Why use rotations: why does the identity R(mω)ᵀR(nω) = R((n − m)ω) make the attention score depend only on the offset between query and key?
How to study this pagePredict · Check · Generalize · Continue
Compare what the demo shows with the key equation and with the papers cited below.
Identify the part of the idea that survives a change of notation or model, so you can recognize it in another paper or system.
The newer notebook covers the same concept and has an interactive demo.
Why It Matters for Modern Models
- Rotary position embeddings (RoPE) encode token position by rotating query and key vectors. RoPE is the position encoding of many large language models, including GPT-NeoX-20B (Black et al., 2022), PaLM (Chowdhery et al., 2023) and the Llama family (Touvron et al., 2023).
- RoPE models do not extrapolate well past their training length; perplexity rises sharply beyond it (Press et al., 2022; Chen et al., 2023). Position Interpolation and YaRN extend the context window by rescaling positions or frequencies, followed by a short fine-tuning run.
- Fast-rotating pairs distinguish nearby offsets, while slow-rotating pairs change little over short distances and vary only across long ones.
Questions to Think About
- How do the different frequencies act like the hands of a clock, with fast pairs resolving nearby tokens and slow pairs distinguishing distant ones?
- Why do Position Interpolation and YaRN rescale positions or frequencies, and what goes wrong when a model meets rotation angles it never saw during training?
- Shifting the query and key positions by the same amount leaves the RoPE score unchanged; how does this compare with the translation equivariance of convolutions?
Interactive Demo
Core Math
If you prefer intuition first, try the demo above, then come back for the math.
Self-attention without position information is permutation-equivariant: reorder the input tokens and the outputs are reordered the same way, so the layer itself cannot tell token order. (A causal mask gives decoder-only models some implicit position signal; Haviv et al., 2022.)
RoPE encodes position as a rotation applied to queries and keys. In one 2D subspace, a query at position and a key at position become
where and is the pair's frequency.
Because and rotations compose by adding angles, the score depends only on the offset :
Full RoPE splits each head into pairs with frequencies (; base 10,000 in the original paper), so different pairs measure offsets on different scales.
In complex notation, each pair is a complex number and , so relative position becomes a phase difference.