Encode distance by rotating meaning through phase.

RoPE rotates query and key vector pairs according to token position. Explore frequency bands, token distance, and context scaling, then watch relative phase change attention compatibility along a 3D helix.

Open the phase lab

Order lives in the angle between tokens.

Instead of adding a position vector, rotary embeddings multiply pairs of features by a position-dependent rotation. When a rotated query meets a rotated key, their dot product depends on relative displacement, giving attention direct access to how far apart two tokens are.

Token 4 → token 20 · relative distance 16Drag to orbit · wheel to zoom
query phasekey phaserelative arc
Relative angle0.00°

Δm multiplied by the selected angular frequency.

Cosine compatibility1.000

Contribution of this rotated feature pair to q·k.

Wavelength0

Tokens required for one full rotation in this band.

Effective distance16

Distance after optional context interpolation.

Pair dimensions into a plane

Two scalar features become a 2D vector. Position m rotates that vector by m times the band's angular frequency.

R(mω) [x₂ᵢ, x₂ᵢ₊₁]ᵀ

Subtract positions inside the dot product

Rotation matrices are orthogonal. The query rotation transposes against the key rotation, leaving a single rotation by their relative position.

(R(mω)q)ᵀ(R(nω)k) = qᵀR((n−m)ω)k

Stretch carefully for longer context

Linear interpolation divides positions before rotation, preserving phase longer but compressing distinctions. Other methods alter frequencies nonuniformly or fine-tune on longer sequences.

m′ = m / scale

High frequency

Rapid rotation distinguishes nearby positions but aliases after relatively short wavelengths.

Useful for local token order.

Low frequency

Slow rotation changes gently across long spans, retaining coarse position relationships.

Useful for long-range structure.

Extrapolation

Positions far beyond training can enter unfamiliar phase combinations even though the formula still computes.

Scaling trades resolution for range.

Primary reading

RoFormer: Enhanced Transformer with Rotary Position Embedding Su et al.Extending Context Window with Position Interpolation Chen et al.YaRN: Efficient Context Window Extension Peng et al.LongLoRA: Efficient Long Context Fine-tuning Chen et al.