Rotary Position Embeddings (RoPE)

How do query and key rotations make an attention score depend on relative position?

The idea

RoPE rotates pairs of query and key coordinates by angles proportional to position. Shifting both positions by the same amount turns the query and key by the same extra angle in each pair, which leaves their dot product unchanged, so the score depends on position only through the offset between them.

Builds on Scaled Dot-Product Attention & Transformer Layers.

The picture

Attention compares words by dot products, and a dot product does not know where a word sits. Swap two words and every pair of words still gets the same score as before.

RoPE writes position into the vectors themselves. Before a query and a key meet, each is turned: the word at position mm by mm times a fixed angle. The figure uses 30° per position, one query [2,1][2, 1] and one key [1,1][1, 1].

Turning keeps every length, so the only thing position can change is the angle between the two arrows. Move both words later by the same amount and both arrows turn by the same extra angle: the gap between them, and so the score, stays put. What reaches the score is the relative offset, how far back the key sits.

One speed of turning repeats: at 30° per place, 12 places back looks the same as 0. So a real head turns many pairs of coordinates at many speeds, like clock hands:

  • fast pairs tell neighbouring words apart,
  • slow pairs tell distant words apart.

Predict first

A query q=[2,1]q = [2, 1] at position 5 looks back at a key k=[1,1]k = [1, 1] at position 2. Each is turned 30° per position, and their score is 1. Now both words move four places later, to positions 9 and 6. Before you touch the figure: what is the score now?

The mathematics

In a 2D subspace, define a rotation matrix:

R(θ)=(cos⁡θ−sin⁡θsin⁡θcos⁡θ).R(\theta) = \begin{pmatrix} \cos\theta & -\sin\theta \\\\ \sin\theta & \cos\theta \end{pmatrix}.

Write mm for the position of the query and nn for the position of the key. RoPE turns the query by the angle θm\theta_m and the key by θn\theta_n, and their dot product depends only on the difference of the angles:

q~m⊤k~n=q⊤R(θn−θm) k,q~m=R(θm) q,k~n=R(θn) k,R(θm)⊤R(θn)=R(θn−θm).\tilde q_m^\top \tilde k_n = q^\top R(\theta_n - \theta_m)\, k, \qquad \tilde q_m = R(\theta_m)\, q, \qquad \tilde k_n = R(\theta_n)\, k, \qquad R(\theta_m)^\top R(\theta_n)=R(\theta_n-\theta_m).

So attention can depend on relative position through θn−θm\theta_n-\theta_m.

In practice, RoPE applies this to many 2D pairs with different frequencies. The usual choice (Su et al., 2024, §3.2.2) is:

θm,i=m ωi,ωi=base−2i/d,\theta_{m,i} = m\,\omega_i, \qquad \omega_i = \mathrm{base}^{-2i/d},

where dd is the head dimension, ii indexes the 2D pairs and the base is usually 10,000. With θm,i=m ωi\theta_{m,i}=m\,\omega_i, the angle between a query at mm and a key at nn in pair ii is (n−m) ωi(n-m)\,\omega_i: only the offset n−mn-m enters.

RoPE applies this rotation independently to each 2D coordinate pair (2i,2i+1)(2i,2i+1) of a head, so dd is typically even and i∈{0,…,d2−1}i \in \{0,\dots,\frac d2-1\}.

Try it

First a worked example, then one to finish, then problems on your own. Check your answer before you look at the solution. Answers typed here are not kept when you leave.

  1. 1 · Worked example

    The figure's numbers, by hand

    Query q=[2,1]q = [2, 1] at position 5; key k=[1,1]k = [1, 1] at position 2; 30° per position. Find the score of the turned vectors.

    1. Turn the query by 5×30∘=150∘5 \times 30^{\circ} = 150^{\circ}: R(150∘) q=[−2.232, 0.134]R(150^{\circ})\,q = [-2.232,\ 0.134]. Its length is still 2.2362.236.

    Say the next step to yourself first.
  2. 2 · Finish it

    Two places back

    Same qq and kk, but now the query is at position 6 and the key at position 4. The first steps are done. What is the score? Give three decimal places.

    1. Gap. The key is 2 places back, so it trails by 2×30∘=60∘2 \times 30^{\circ} = 60^{\circ}.

    2. Turn the key back by the gap. R(−60∘) k=[cos⁡60∘+sin⁡60∘, cos⁡60∘−sin⁡60∘]=[1.366, −0.366]R(-60^{\circ})\,k = [\cos 60^{\circ} + \sin 60^{\circ},\ \cos 60^{\circ} - \sin 60^{\circ}] = [1.366,\ -0.366].

    3. Your turn.

  3. 3 · On your own

    The score comes back

    With the key 0 places back, the score is q⋅k=3q \cdot k = 3. How many places back must the key sit, at the fewest (more than 0), for the score to be exactly 3 again?

  4. 4 · On your own

    Nearest is not always best

    The same key sits 0, 1, 2 or 3 places before the query. Which placement gives the highest score?

    Choose one

  5. 5 · On your own

    A key that comes later

    In a model that lets a word attend to words after it, as an encoder does, the same key can sit 3 places after the query: query at position 2, key at position 5. Same q=[2,1]q = [2, 1], k=[1,1]k = [1, 1] and 30° per position. What is the score?

The code

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(q, k, m, n, w):
    # Query at position m, key at position n, one rotation speed w.
    return float((R(m * w) @ q) @ (R(n * 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]:
    a = rope_dot(q, k, m=0, n=delta, w=w)
    b = rope_dot(q, k, m=5, n=5 + delta, w=w)  # same offset, both moved 5 places
    print("delta =", delta, "dot =", round(a, 3), "dot (shifted) =", round(b, 3))

Keep it

  1. What does RoPE do to a query or key at position mm?

    Answer from memory, then check

    It turns each pair of coordinates by the angle mθm\theta, where θ\theta is that pair's speed. The value vectors are not turned.

  2. Why does the score of a turned query and key depend only on how far apart they are?

    Answer from memory, then check

    (R(mθ)q)⊤R(nθ)k=q⊤R((n−m)θ) k(R(m\theta)q)^\top R(n\theta)k = q^\top R((n-m)\theta)\,k: turning back by one angle and forward by another is a single turn by the difference.

  3. True or false: RoPE makes vectors at later positions longer.

    Answer from memory, then check

    False. A rotation keeps every length. Position enters the score only through the angle between the query and the key.

  4. Why does RoPE turn different pairs of coordinates at different speeds?

    Answer from memory, then check

    One speed repeats: at 30° per place, 12 places back looks the same as 0. Fast pairs separate neighbours; slow pairs separate distant words. In a 64-dimensional head with base 10000, the fastest pair repeats every 6.3 places and the slowest about every 47,000.

  5. Does RoPE make every score fall as two words move apart?

    Answer from memory, then check

    No. RoFormer shows that a bound on the size of a score, built from all its pairs of coordinates together, tends to fall with distance (Su et al., 2024, §3.4.3). A single score can rise and fall: in this lesson one pair gives 3, 3.098, 2.366 and 1 at gaps 0 to 3, and 3 again at 12.

  6. Every word in a sentence moves 100 places later. What happens to the attention scores between them?

    Answer from memory, then check

    Nothing, as far as RoPE is concerned: each query and key turns by the same extra angle, so every pair keeps its gap.

Recalling an answer from memory makes it last longer than rereading it (Roediger and Karpicke, 2006). Add these 6 cards and they come back tomorrow, then after gaps of 3, 7, 16, 35 and 75 days while you still remember them. They stay in this browser only.

The edge

The algebra is exact; how far it stretches is not. A model only ever sees the angles its training contexts reach, and past that length its slow pairs turn to angles it has never seen. Models trained with RoPE degrade sharply there (Press et al., 2022; Chen et al., 2023). Position interpolation (Chen et al., 2023) squeezes a longer input's positions back into the trained range, so every angle is a familiar one, then fine-tunes briefly. YaRN (Peng et al., 2024) squeezes unevenly, the slow pairs more than the fast ones that carry local order. Both work up to some multiple of the training length. Whether any fixed rule for position lets a model use contexts far longer than it trained on, or whether something else is needed, is still an open question.

Next thoughtPast its training length, a model meets angles it has never seen, and every earlier word's key must still be stored. What does it take to make attention work at a hundred thousand words?Long Context Engineering: RoPE Scaling, KV Compression & Memory Optimization →
The older RoPE lab: two tokens, four rotation speeds, a global shift and a test where you guess the dot product