RoPE (Rotary Position Embedding)
Definition
Rotary Position Embedding (Su et al., 2021; arXiv:2104.09864) injects position by rotating each query and key by an angle proportional to its absolute position, so that the attention logit depends only on the relative offset . It is the de-facto positional scheme in modern LLMs (LLaMA, Qwen, Mistral, DeepSeek) because it is parameter-free, norm-preserving, drops into any FlashAttention kernel, and extends to long context by retuning frequencies.
Key math
Split the head dim into coordinate pairs, each with frequency (base default). Let be the block-diagonal rotation spinning pair by . RoPE sets , . The logit collapses to a relative form:
using and additive angle composition . The absolute positions cancel exactly the way does — absolute stamping, relative interaction. Fast pairs (, wavelength ) resolve local offsets; slow pairs () carry global position.
Context extension. Position is a continuous angle, so unseen distances are reached by manipulating : linear position interpolation rescales ; NTK-aware / YaRN raise the base so the fastest pairs don’t alias. The slowest pair (wavelength tokens at ) is the one at risk when serving beyond training length. Cost is elementwise on only — negligible vs. the matmuls.
Why it matters
RoPE turned the sinusoidal “relative offset is learnable” property into a structural guarantee, and its continuous-angle form is the lever behind the entire long-context race (PI, NTK, YaRN). It is also the reason MLA needs decoupled RoPE: a position rotation sitting between the up-projection matrices breaks weight absorption.
Taught in
- 03-positional-information — §3.3 derivation, §5 the base-scaling / extrapolation semantics.
See also
- positional-encoding · alibi — sibling schemes
- mla — decoupled RoPE and the absorption clash
- flash-attention — RoPE fused on-the-fly into
- attention-glossary · attention-mechanisms