How does Rotary Position Embedding (RoPE) encode relative position information?

Last updated 22 November 2026

  • RoPE splits each query and key into 2D pairs and rotates pair i by angle m·θ_i at position m, with θ_i = 10000^(−2i/d) giving different frequencies.

  • Rotations compose, so (R_m q)ᵀ(R_n k) = qᵀ R_{n−m} k. The attention score depends only on the offset, turning absolute rotations into relative position.

  • It applies to Q and K only, adds no learned parameters, and works with KV caching. Longer contexts need rescaling (position interpolation, NTK scaling, YaRN).

  • https://emergent.sh/glossary/rotary-position-embedding
  • https://blog.eleuther.ai/rotary-embeddings/
  • https://dev.to/umeshtharukaofficial/rotary-position-embeddings-how-rotating-queries-and-keys-encodes-relative-position-26d9
All Transformers interview questions