Skip to content

Rotary embeddings put position in the query-key angle

A position vector added to the input tells each token where it is. Attention, though, needs to know how far apart two tokens are. Rotary embeddings give attention that directly.

Take a query or key vector and group its coordinates into pairs. Rotate pair i by the angle m·θᵢ, where m is the token's position and θᵢ = 10000^(−2(i−1)/d) for a head of width d. The angles are fixed constants, "pre-defined parameters" in the paper's words, so rotating adds nothing to learn.

Why does this give relative position? A rotation by a followed by the inverse of a rotation by b is a rotation by a − b. So for a query at position m and a key at position n, the dot product of the rotated vectors equals the dot product of the unrotated ones with a single rotation by n − m between them. Shift both tokens by the same amount and the score does not change.

What does it do to long distances? The paper argues that with these angles the score tends to decay as the offset grows, a property it calls long-term decay. It also says it lacks "a faithful explanation" for why the model does better on long texts, so treat the decay as an argument about the design, not a measured cause.

Su et al., RoFormer, §3.2 (Eqs. 14 to 16) and §3.3