RoPE: Rotate, Don’t Add
Position can change a comparison instead of an embedding. RoPE rotates queries and keys. Their relative rotation depends on the gap, while longer contexts still require adaptation and testing.
#Section 27Rotate, Don’t Add
Rotary position embedding, or RoPE, was introduced by Jianlin Su and colleagues in RoFormer in 2021. A query describes what a token seeks; a key describes how another token can be found. Their dot product determines a matching score.
Treat each pair of query or key features as a point on a plane. Rotate it by an angle determined by its position, rather than adding a position vector to the input.
The formula is the school formula for rotating a point by an angle :
Read it out loud: the new first number is the old first number times minus the old second number times , and the new second number is the old first times plus the old second times . The angle is the token's position times a fixed rotation speed (the Greek letter theta) for this pair.
The figure rotates an illustrative query pair. Its direction changes while its length remains fixed.
Rotation preserves length, so the position operation changes orientation without changing the vector's size.
#Section 28Every Pair Has Its Own Speed
A real query has many pairs, not one. Finch's attention heads are 64 numbers wide, so each query has 32 pairs. RoPE gives each pair its own dial and its own speed, exactly like the clock hands of Part IV:
With base 10,000 and a 64-wide head, pair 0 turns at radian per position, pair 8 at 0.1, pair 16 at 0.01, pair 24 at 0.001 and the last pair, 31, at 0.000133. Notice that here is the head size (64), not the model width (512), because RoPE acts inside each head separately.
Fast pairs distinguish nearby positions but wrap around often. Slow pairs vary over longer distances. Their combined angles describe gaps at different scales; the base controls the frequency range.
#Section 29The Trick: Only the Gap Survives
Rotate the query by its position and the key by its position , then take their dot product. Using for "rotate by angle ", the result can be rewritten as:
Rotating both vectors by the same extra angle preserves their relative angle. The positional part of their dot product therefore depends on the gap, while the content still affects the score.
Shift both positions together in the figure: the score stays unchanged. Change their gap and the score changes. This isolates relative-position behavior.
This is the relative position property, delivered exactly and for free. "Two words apart" scores the same on page 1 and on page 300. The model learns "a pronoun often looks a few tokens back" once, and it applies everywhere.
#Section 30Where RoPE Sits in Attention
Where exactly does the rotation happen? Part VI builds attention properly, but the outline is short. Each token's vector is multiplied by three learned matrices to produce its query , its key and its value , the content it will hand over if another token attends to it. RoPE rotates by the query token's position and by the key token's position . Then the rotated queries and keys are compared, the scores pass through softmax (Part VII) to become weights, and the weights blend the values.
Values are not rotated. RoPE shapes the matching scores, while the values supply the content to blend. Applying it inside every attention layer makes position available at each comparison.
The rotation as one matrix
If you want to write the whole rotation as one matrix for a 64-wide head, it is a grid that is almost entirely zeros, with small rotation blocks down the diagonal. Each block rotates one pair and leaves every other pair alone. For a 64-wide head that is 32 blocks, and 3,968 of the 4,096 entries are zero.
Real code never builds that matrix, because multiplying by thousands of zeros is wasted work. It multiplies by precomputed tables of and directly, using a little helper called rotate_half, which Unit II walks through line by line. The answer is identical.
#Section 31Far-Apart Tokens Get a Weaker Pull
RoPE has a pleasant side effect. Imagine a query and a key with exactly the same content, so that without rotation their score would be as high as possible. As the distance between them grows, the fast pairs end up pointing in all sorts of directions relative to each other, and their contributions to the dot product partly cancel out. The slow pairs still agree, but there are fewer of them.
The illustrated average over equally weighted pairs tends to decrease with distance, with oscillations. Learned queries and keys need not follow a monotonic decay.
That is a sensible default for language, where nearby words usually matter more, and the model can still overcome it: a query and key with strongly matching content in the slow pairs can attend across thousands of tokens. The RoFormer paper called this long-term decay.
#Section 32Beyond the Training Length
Beyond training length, slow pairs can enter angles the model has never learned to interpret. Being able to compute those angles does not establish reliable long-context behavior.
Position interpolation compresses the position range before rotation. Chen and colleagues proposed it in 2023. It keeps long-context angles closer to familiar ones but changes nearby gaps too, so the model needs adaptation and evaluation.
Interpolation slows every pair down equally, including the fast ones that did not need it, which blurs their fine view of nearby words. Better methods, NTK-aware scaling and YaRN, slow the pairs unevenly, leaving fast pairs nearly alone and stretching the slow ones the most. Unit II compares them in detail.
| Learned (Part III) | Sinusoidal (Part IV) | RoPE (Part V) | |
|---|---|---|---|
| Acts on | the input, once | the input, once | and , in every layer |
| Parameters | none | none | |
| Relative position | learned indirectly | implicit, blurred by adding | exact, by construction |
| Mixes into content? | yes | yes | no ( and untouched) |
| Longer than training | impossible without new rows | computable, untrained | stretch with interpolation plus short training |
That last row, plus the exact relative behaviour and zero parameters, is why RoPE won.
Sharp edges of RoPE
Never rotate v. Only queries and keys get rotated. If the values are rotated too, the content passed between tokens depends on absolute position, and output quality drops in confusing ways. If outputs drift with absolute position, check this first.
Which numbers form a pair? Some code pairs neighbours (entry 0 with entry 1, 2 with 3). Other code pairs the first half of the head with the second half (entry 0 with entry 32 in a 64-wide head). Both train equally well, but weights trained one way give garbage in code written the other way. Unit II shows the conversion.
Rotated keys in the cache. During generation (Part XI), old keys are stored already rotated. The new token must be rotated by its true position, which is the number of tokens already stored, not 0.
Partial rotary. Some models, such as GPT-NeoX and Phi, rotate only part of each head and leave the rest alone. Rotating the whole head breaks them. Read the config's rotary fraction before porting.
Precision of the angles. Angles get large at long positions. Computing and in 16-bit precision loses accuracy exactly where you need it. Precompute the tables in 32-bit.