RoPE: Rotary Position Embedding


Paper: Su, J., et al., RoFormer: Enhanced Transformer with Rotary Position Embedding, 2021. arXiv:2104.09864

Why this paper matters

Position encoding is the subtle mechanism that lets a Transformer know order. Absolute positional embeddings (the original sine/cosine and learned vectors) work, but they don’t expose relative position cleanly — and relative position is what really matters for language. RoPE solves this elegantly: it rotates query and key vectors by an angle that depends on their position, so that the attention dot-product naturally depends only on the distance between positions. It is now the default position encoding in LLaMA, Mistral, Qwen, DeepSeek, and most open-weight models. If you read a modern model card, you are looking at RoPE.

The core idea: rotate, don’t add

For a pair of dimensions $(2i, 2i+1)$ of a vector at position $m$, RoPE applies a rotation by angle $m\theta_i$ with $\theta_i = 10000^{-2i/d}$:

\[R_m = \begin{bmatrix} \cos m\theta_i & -\sin m\theta_i \\ \sin m\theta_i & \cos m\theta_i \end{bmatrix}, \qquad f(q, m) = R_m\, q\]

Apply the same to the key: $\tilde{k}_n = R_n\, k$. The attention score between positions $m$ and $n$ becomes:

\[\tilde{q}_m^\top \tilde{k}_n = q^\top R_{m-n}\, k\]

The crucial property: the score depends on $m$ and $n$ only through their difference $m-n$ — relative position falls out of the math for free, while the absolute position is still encoded in each rotated vector.

Length extrapolation

Because positions enter as rotations, you can extend to longer contexts than seen in training by adjusting the base (NTK-aware scaling, used by LLaMA 2/3 and others) instead of re-training. This is why “extend context to 32k/128k” is often a RoPE-base tweak rather than a full retrain.

Key results

Why it matters today

Every time you see “context length 128k” on an open model, RoPE (and its NTK scaling variants) is the reason it works without retraining from scratch. Read this alongside the Transformer post: RoPE is the position-encoding answer to “how does attention know word order?”

References

© 2026 Jiawei    粤ICP备18035774号