Bring the mental model from Scaled Dot-Product Attention & Transformer Layers; this page will reuse it instead of restarting from zero.
Attention & Transformers
Rotary Position Embeddings (RoPE)
A positional encoding that rotates queries and keys so attention depends on relative position via phase differences.

Concept Structure
Rotary Position Embeddings (RoPE)
Start with the picture, metaphor, or geometric mechanism.
Make the objects explicit and connect them with notation.
Mirror the equations with runnable implementation details.
Manipulate the mechanism and watch the idea respond.
Learning map
Rotary Position Embeddings (RoPE)Conceptual Bridge
What should feel connected as you move through this page.
A positional encoding that rotates queries and keys so attention depends on relative position via phase differences.
The next edge should feel earned: use the demo prediction here before following Efficient Attention at Scale: KV Cache, GQA & FlashAttention.
01
Intuition
Build the mental picture first so the rest of the page has something to attach to.
Self-attention by itself does not know token order: it only sees a set of vectors and compares them.
RoPE injects position by rotating each token's query and key vectors by an angle that depends on its absolute position. That sounds absolute at first: token gets one rotation, token gets another.
The key step is what happens when attention compares them. A dot product between two rotated vectors does not keep both rotations separately. The shared frame cancels, leaving the gap between their angles:
- rotate the query by position ,
- rotate the key by position ,
- compare them, and the score can see the phase difference .
So RoPE uses absolute rotations as the coordinate system, but the query-key score receives a relative-position signal.
A good mental model is "clock hands at multiple speeds":
- high-frequency rotations capture local order (nearby tokens),
- low-frequency rotations capture long-range order (far-apart tokens).
The careful caveat is that real attention scores still depend on the token content inside and . RoPE does not make every equal-distance token pair have the same attention score; it makes the positional part of the comparison available as a relative phase.
02
Math
Translate the story into symbols, assumptions, and a derivation you can inspect.
In a 2D subspace, define a rotation matrix:
RoPE rotates queries and keys by position-dependent angles:
Now expand the dot product one step at a time:
That last equality is the bridge: . The score can therefore depend on relative position through the phase gap.
In practice, RoPE applies this to many 2D pairs with different frequencies. A common choice is:
where is head dimension and indexes the 2D pairs.
For one frequency, the phase gap is:
RoPE applies this rotation independently to each 2D coordinate pair of a head, so is typically even and . Multiple frequencies let different coordinate pairs act like clocks with different tick rates.
03
Code
Keep the implementation aligned with the notation so the algorithm is legible.
import numpy as np
def R(theta):
c, s = np.cos(theta), np.sin(theta)
return np.array([[c, -s], [s, c]])
def rope_dot_direct(q, k, p, qpos, w):
return float((R(p * w) @ q) @ (R(qpos * w) @ k))
def rope_dot_relative(q, k, p, qpos, w):
return float(q @ (R((qpos - p) * w) @ k))
q = np.array([1.0, 0.2])
k = np.array([0.3, 1.0])
w = 0.7 # one frequency, for illustration
for delta in [0, 1, 2, 4, 8]:
direct = rope_dot_direct(q, k, p=5, qpos=5 + delta, w=w)
relative = rope_dot_relative(q, k, p=5, qpos=5 + delta, w=w)
shifted = rope_dot_direct(q, k, p=0, qpos=delta, w=w)
assert np.allclose(direct, relative)
print("delta =", delta, "dot =", round(direct, 3), "shifted =", round(shifted, 3))
04
Interactive Demo
Use direct manipulation to connect the explanation to a moving system.
Use the demo to predict the invariant before the rotating-vector lab appears. In the toy setting, the same base query/key vectors are compared at shifted positions, so preserving the relative phase preserves the positional part of the score.
Keep the scope narrow: RoPE is the rotary query-key phase mechanism. Long-context scaling methods such as position interpolation, YaRN, or LongRoPE change how positions map to phases; KV-cache methods change serving memory. Those are follow-on repairs, not the basic RoPE claim.
Live Concept Demo
Explore Rotary Position Embeddings (RoPE)
The stage is code-native and interactive. Use it to test the explanation against the mechanism.
Manipulate one control and predict the visible change.
Commit to what Rotary Position Embeddings (RoPE) should make visible before reading the result.
After The First Pass
Turn the concept into an inspected object.
Once the invariant is visible in the intuition, math, code, and demo, use these panels to inspect the mechanism visually, check source support, practice the idea, and attach a grounded research question.
Mechanism Storyboard
See the idea move before the page explains it
A positional encoding that rotates queries and keys so attention depends on relative position via phase differences.

Start with the picture, metaphor, or geometric mechanism.
Before reading further, choose the kind of change Rotary Position Embeddings (RoPE) should make visible.
Visual Inquiry
Make the image answer a mathematical question
A positional encoding that rotates queries and keys so attention depends on relative position via phase differences.
Which visible object should carry the first intuition?
Pick the cue that should make Rotary Position Embeddings (RoPE) easier to reason about before the page gives the answer.
Source Grounding
Canonical references for the mechanism on this page.
Primary RoPE source. Sections 3.1-3.2 derive position-dependent rotations for q/k and show the query-key inner product uses the relative rotary product R_{Theta,n-m}.
Open sourceClaim Review
A positional encoding that rotates queries and keys so attention depends on relative position via phase differences.
Claims without a substantive review badge still need exact source-support review.
su-2021-roformer
Use equation, code, and demo objects to check whether the source support is operational.
Su et al. state that RoPE encodes absolute position with a rotation matrix and incorporates explicit relative-position dependency in self-attention; Sec. 3.1 frames the q-k inner product as a function of embeddings plus m-n, and Sec. 3.2 derives q/k rotations whose dot product contains R_{Theta,n-m}.
Sources: RoFormer: Enhanced Transformer with Rotary Position EmbeddingChecks only RoPE's rotary q/k attention-score mechanism; not RoPE scaling, arbitrary long-context extrapolation, YaRN/LongRoPE, KV-cache behavior, or production model performance.A bounded review summary is present; still check caveats and exact source scope.Checked RoFormer abstract/introduction and Sec. 3.1-3.2: RoPE uses position-dependent rotations for q/k, the 2D complex form has phase gap m-n, and the general self-attention score contains R_{Theta,n-m}. Local math/code/demo witness the toy relative-angle mechanism.
Reviewer: codex+oracle; reviewed 2026-05-07Practice Loop
Try the idea before it explains itself
A positional encoding that rotates queries and keys so attention depends on relative position via phase differences.
Before touching the demo, predict one visible change that should happen in Rotary Position Embeddings (RoPE).
Reveal when your model needs a nudge.
Reveal when your model needs a nudge.
Reveal when your model needs a nudge.
A concrete answer is on the canvas.
The answer names why the claim should hold.
It touches the page context or a neighboring idea.
Research Room
Attach the question to an exact object
Pick the concept, equation, source, code witness, claim, misconception, or demo state before asking for help. The handoff stays grounded to that object.Open the draft below to save one note and next action in this browser.
Rotary Position Embeddings (RoPE)
What is the smallest example that makes Rotary Position Embeddings (RoPE) click without losing the math?
Local action draftNo local draft saved yetExpand only when ready to capture one local next action
This draft stays locally in this browser for concept:attention-transformers/rope.
- Source ids to inspect: su-2021-roformer
- Definition, prerequisite, and contrast concept links
- The equation or code witness that makes the concept operational
- One demo state that shows the invariant instead of a slogan
- The learner can state the mechanism in their own words
- The learner can name the prerequisite that would repair confusion
- The learner can predict how the mechanism changes under one perturbation
I am working in Continuous Function's research reading room. Object: concept - Rotary Position Embeddings (RoPE) Object key: concept:attention-transformers/rope Context: Attention & Transformers Anchor id: concept/concept-notebook/attention-transformers/rope Open question: What is the smallest example that makes Rotary Position Embeddings (RoPE) click without losing the math? Evidence to inspect: - Source ids to inspect: su-2021-roformer - Definition, prerequisite, and contrast concept links - The equation or code witness that makes the concept operational - One demo state that shows the invariant instead of a slogan What would resolve this: - The learner can state the mechanism in their own words - The learner can name the prerequisite that would repair confusion - The learner can predict how the mechanism changes under one perturbation Answer as a careful research tutor: stay source-grounded, separate verified evidence from assumptions, name the relevant math objects, and end with one next action.
concept/concept-notebook/attention-transformers/rope
concept:attention-transformers/rope