What RoPE is
A query and a key are just two vectors, and their dot product does not change if the two tokens trade places: attention by itself has no idea where anything is. GPT-2 adds a learned vector per position to its input, BLOOM subtracts a distance penalty from every score (the ALiBi tab). Rotary position embedding (RoPE) does neither. It turns each query and each key by an angle that grows with the token's position, after the projections and just before the dot product. Because turning both vectors by the same extra angle leaves their dot product alone, the score ends up depending on how far apart the two tokens are and not on where they stand.
This page records Qwen2.5-0.5B, which uses RoPE in all 24 layers; 7 are recorded (1, 5, 9, 13, 17, 21 and 24). Each layer has 14 query heads that share 2 key heads (the GQA tab), every head 64-dimensional, on three prompts: Repeated sentence, Repeated phrase and Two names.
Pairs and frequencies
RoPE splits a 64-dimensional head into 32 pairs of coordinates. Qwen2's code pairs coordinate with (the original RoFormer paired neighbours; the two are the same up to a fixed permutation). At position , pair is turned by the angle :
With Qwen's base , pair 0 turns one radian per token, a full turn every tokens, and each later pair turns more slowly, down to pair 31, which needs about 4 million tokens for one turn. The Frequencies panel draws each pair's wavelength on a log axis, with a dial above it showing the angle the pair has reached at the selected token. The dashed line is 32,768, the context Qwen2.5-0.5B was trained on: every pair whose bar rises above it never completes a full turn inside that context. RoFormer used ; raising the base slows every pair down, which Xiong et al. found helps when a model is trained on longer sequences, and Qwen2.5 uses .
Rotating real vectors
The two circles in the Rotate column show one pair of the selected query and of a key (click a key on the head card to choose it), as q_proj and k_proj produce them (dashed, before) and after the turn (solid, after). A rotation never changes a pair's length, only its direction, and the values are never rotated at all. The dial strip makes the frequencies concrete: fast pairs have wrapped around many times by the end of a short prompt, slow ones have barely moved.
Block 1 shows that the rotation is the only place position enters. Its input is a token's embedding after RMSNorm, so a token that occurs twice, like “ cat” or “ mat” in the Repeated sentence, has exactly the same q_proj and k_proj output at both places. After RoPE the two copies differ, because they stand at different positions.
Only the distance counts
Rotations compose: turning by and then undoing is one turn by . So for a query at position and a key at position ,
where and are pair of the unrotated vectors. The Score at distance d panel evaluates this for the selected query and key at every distance from 0 to 40; the dot is the score the model actually computed, which sits on the curve at . Two experiments on the recording confirm it. In block 1, the same two tokens at the same distance get the same score: the three prompts have 26 such pairs, and their scores agree to within of their size (the caption under the head card names one). And the whole prompt was run again with every position moved by 10,000 (the positions + 10,000 switch): each query and key turns much further and the circles change, but every score in every recorded layer stays within 0.14 % of its value. Moved by 30,000, the scores stay within 0.52 %. The small differences come from float32: an angle like can only be stored to about radians.
Long-range decay
Su et al. argued that RoPE makes attention fade with distance. Write pair 's contribution as a complex number , where combines pair of the query and the key. Summing by parts bounds the score by with , and that sum shrinks as the distance grows because the fast pairs' terms point in scattered directions. The upper chart of the Long-range decay panel draws for Qwen's 32 frequencies, divided by its value at distance 0. It is a statement about how large a score can be, not about any one score.
The lower chart measures what the trained model does, on the first 4,096 tokens of a Sherlock Holmes story: the mean score over every pair of tokens a given distance apart (leaving out token 0, where Qwen parks attention it does not need). In every recorded layer, all 14 heads score a key 1,024–1,447 tokens back lower on average than the token just before, with median drops between 2.7 and 16.3. The dashed line keeps the same q_proj and k_proj vectors but skips the rotation, which the model never does; it shows how much of the fall is RoPE's. Its median drop is between 0.03 and 1.7: without the rotation the scores barely depend on distance, most clearly in block 1, whose inputs carry no position at all.
Where it came from
The Transformer added fixed sinusoids to its inputs; BERT and GPT-2 learned one vector per position; Shaw et al. made attention depend on relative position with learned vectors per distance, and T5 reduced that to one learned bias per distance bucket. RoPE (Su et al., 2021) got relative positions from the dot product itself, with no extra parameters, and was taken up by GPT-J, GPT-NeoX, PaLM and LLaMA, then by most open decoder models, Qwen2.5 among them. Its wavelengths also set how far a model can read: past the trained context, slow pairs reach angles the model never saw. Position Interpolation, NTK-aware scaling and YaRN rescale the frequencies to stretch it; the YaRN tab records that on this same model.
Papers
- RoFormer: Enhanced Transformer with Rotary Position Embedding Introduces rotary position embeddings, the relative-position property and the long-term decay bound drawn on this page.
- Qwen2.5 Technical Report The model family this page records, with RoPE at base 10⁶ on 64-dimensional heads.
- Attention Is All You Need Sinusoidal position encodings, whose geometric ladder of frequencies RoPE reuses.
- Self-Attention with Relative Position Representations First made attention depend on the distance between tokens, with learned vectors per distance.
- Effective Long-Context Scaling of Foundation Models Raises RoPE's base for long-context training, the change behind bases like Qwen's 10⁶.
- Round and Round We Go! What makes Rotary Positional Encodings useful? Studies how trained models use RoPE's fast and slow pairs, and questions the decay argument.