🧠 Rotary Position Embedding (RoPE) — AI / ML Interview Guide

LLM Internals · interactive visualization + interview prep

Open the interactive Rotary Position Embedding (RoPE) visualization on PrepGrind → Step through a live animation, tune the parameters, and read the full theory, math, reference code, and interview Q&A below — free, in your browser.

What it is

RoPE injects position by ROTATING each pair of dimensions in the Query and Key vectors by an angle proportional to the token's position. Because rotations compose, the attention score between two tokens ends up depending only on their RELATIVE distance — giving relative-position awareness for free, with great length generalization.

Mental model

Stamp each token's Q/K with a clock reading set by its position. Two tokens compare their hands, and what matters is the ANGLE BETWEEN the clocks — i.e. how far apart they are — not the absolute time. So the model reads relative position directly from the dot product, and a clock works the same whether it's position 50 or 50,000.

Theory

Absolute positional encodings (sinusoidal or learned) ADD a position vector to the embedding. RoPE instead applies a position-dependent ROTATION to the Q and K vectors just before attention. The d-dimensional vector is split into d/2 pairs, and pair i is rotated by angle position × θᵢ, where θᵢ = 1/10000^(2i/d) — low pairs rotate fast, high pairs slowly (the same frequency ladder as sinusoidal PE).

The crucial property is relative-position awareness. Rotating qₘ by angle mθ and kₙ by nθ makes their dot product depend on (m−n)θ — the OFFSET between the tokens — not on m and n separately. So attention naturally scores "how far apart" two tokens are, which is what language structure actually needs.

Because the rotation is defined for any position by the same formula, RoPE EXTRAPOLATES to sequence lengths unseen in training far better than learned absolute embeddings (which simply have no vector for position 5000 if trained to 2048). This is a big reason modern long-context models adopt it.

RoPE is applied to Q and K only (not V), inside every attention layer, and adds essentially no parameters and negligible compute. It composes cleanly with the KV cache: cached keys are already rotated, so nothing special is needed at decode time.

Long-context tricks build directly on RoPE: scaling or interpolating the rotation frequencies (position interpolation, NTK-aware scaling, YaRN) lets a model trained at 4K stretch to 32K+ by slowing the rotation so unseen positions stay in a familiar angular range.

Concrete example

Llama, Mistral, Qwen, and Gemma all use RoPE. When you see a model "extended" from 4K to 32K context via position interpolation or YaRN, that is literally rescaling RoPE's rotation frequencies so the angles for far-out positions stay in range.

Key equations

Step by step

  1. Take a position p and the Q (or K) vector for that token.
  2. Pair up its dimensions; each pair is a 2D point.
  3. Rotate pair i by angle p·θᵢ — low pairs spin fast, high pairs slowly.
  4. Do the same for every token at its own position.
  5. In attention, q′·k′ now encodes the relative offset between the two tokens.

Interview questions & answers

How is RoPE different from sinusoidal positional encoding?

Sinusoidal PE is ADDED to the embedding (absolute position). RoPE ROTATES Q and K by a position-dependent angle, so the attention dot product depends on RELATIVE position. RoPE also extrapolates to longer sequences much better.

Why does RoPE give relative-position awareness?

Rotations compose: rotating q by mθ and k by nθ makes ⟨q,k⟩ a function of (m−n)θ. The absolute positions cancel, leaving only the offset — exactly the relative distance the model wants.

Is RoPE applied to the value vectors too?

No — only to Q and K, because position should affect WHERE attention looks (the scores), not the content being aggregated (V).

How do you extend a RoPE model to longer context?

Rescale the rotation frequencies: position interpolation, NTK-aware scaling, or YaRN slow the rotation so positions beyond the training length map into the angular range the model already learned — often with little or no fine-tuning.

Common pitfalls

Where it shows up

More AI / ML interview concepts

PrepGrind runs entirely in your browser, free, no installation required. Loading the interactive playground…