VISUAL LESSON · 1 PICTURES

Rotary Position Embedding (RoPE) Architecture

How do 2D clock-face rotations encode relative token positions directly into complex attention inner products?

Progress stays on this device · no login
BEFORE THE FIRST PICTURE

How this lesson unfolds.

Standard transformers lack an inherent sense of sequence order. Rather than adding fixed absolute positional vectors, Rotary Position Embeddings (RoPE) rotate query and key vectors by angles proportional to token position, guaranteeing that pairwise dot products depend strictly on relative displacement.

Picture 1 of 1 · tap to inspect in full resolution
01

How do 2D clock-face rotations encode relative token distance directly into attention inner products?

INTUITION

Imagine every token holds a tiny 2D analog clock. Token at position 0 sets its clock hand to 12:00. Token at position 1 rotates its hand by 30^∘ (1:00). Token at position 2 rotates by 60^∘ (2:00), and so on. When token 5 talks to token 2, their mutual attention depends only on the difference between their clock hands (5 - 2 = 3 hours = 90^∘). Even if you slide both tokens 1,000 steps further down the paragraph, their relative angle difference remains exactly 90^∘. Position becomes pure rotation.

  • Learning goalDerive the orthogonality property of RoPE rotation matrices and demonstrate that pairwise dot products depend strictly on relative token displacement.
  • Helpful foundations2D rotation matrices · RoFormer: Enhanced Transformer with Rotary Position Embedding (Su et al., 2021)
  • Common trapAssuming RoPE requires full matrix multiplications; in practice, it is computed in linear time via element-wise multiplication: x cos(m theta) + rotate_half(x) sin(m theta).
  • Next connectionWhy should a token only look in one single direction? What if a word has multiple semantic roles simultaneously? We explore multi-lane highways.
✦ More context & mechanics
Conceptual Mechanics & Logic

RoPE pairs vector coordinates into 2D orthogonal slices [x_(2i), x_(2i+1)] and multiplies them by a 2D rotation matrix:

R_(\Theta, m)^((i)) = cos(mθᵢ) & -sin(mθᵢ) \ sin(mθᵢ) & cos(mθᵢ)

Where θᵢ = 10000^(-2(i-1)/d).

When taking the inner product between query q at position m and key k at position n:

⟨ Rₘ q, Rₙ k ⟩ = q^T Rₘ^T Rₙ k = q^T R_(n-m) k

Because orthogonal rotation matrices satisfy Rₘ^T Rₙ = R_(n-m), the absolute indices m and n vanish, leaving only the relative distance n - m.

Real-World Usage

RoPE is the foundational positional mechanism in modern frontier open-weights models, including LLaMA-3, Mistral, Gemma, and DeepSeek, replacing older absolute learned positional embeddings.

History & Origin Story

Jianlin Su introduced Rotary Position Embeddings in 2021 (RoFormer), solving the critical extrapolation weakness of sinusoidal embeddings by framing relative position through complex number multiplication.

How to Memorize & Golden Words
  • Angle equals position times frequency.
  • Dot product of rotated vectors measures relative distance, not absolute position.