Transformer Systems Lab

RoPE: what position does to an attention score

Rotary position embedding (RoPE) rotates each query and key by an angle proportional to its position. Predict whether shifting both positions by the same amount changes their score, then predict what happens when only the key moves.

Step 2 of 8Predict, then checkAnswers saved in this browser

What this step asks

The same query–key score, now with positions.

Step 1 scored a query against keys with a plain dot product. RoPE rotates the query and the key before that dot product, by angles set by their positions (Su et al., 2024). This step asks which part of the score still depends on position after the rotation.

PredictionWill an equal shift change the dot-product score?
ExampleOne query and one key, like those scored in step 1, in two dimensions.
What you checkPhase gap and score in the baseline, the equal shift and the key-only move.
NextWhich keys and values have to be cached during decoding?

Step 2 of 8

RoPE: rotate the query and key by position

The same kind of query–key score as step 1, now with positions. Which part of the score survives when both positions change?

Your answerPrediction neededYour first answer is kept even if you change it later.

Example

One query and one key in two dimensions, at one frequency

(R_m q)^T (R_n k)

R_m rotates the query q by m·ω, its position m times the frequency ω; R_n rotates the key k by n·ω. The vectors q and k stay fixed; only the positions and the frequency change, and each rotation is small enough to check by hand.

q[0.72, 0.32]k[0.58, 0.45]ω0.42
Open the RoPE notebook

Your prediction

Shift both positions by +3: query 2 → 5, key 0 → 3. What happens to the query–key score?

First answerNo prediction yetChanging your answer later does not replace this one.

Controls

Change a setting, then predict again.

query position m
key position n
equal shift
frequency ω

Medium (default): 0.42 rad per position, enough movement to see each change clearly.

Outline of this lesson
Exampleone query–key score, now with position(R_m q)^T (R_n k)
Baselinem=2, n=0the starting phase gap
Equal shiftm+3, n+3does the score move?
Key onlyn+1the new case
01Example

one rotated query/key pair

02Question

absolute position or the gap between positions?

03Prediction

what an equal shift does to the score

04Controls

move m, n, the shift and the frequency

05Result

phase table and dot-product score

06What stays true

shown after you answer

07New case

move only the key

08Next

KV memory

Where this step sits

From the score to the cache.

Finish the equal-shift and key-only cases, then save what you found. The next step uses it: the keys stored in a KV cache are the rotated keys from this step.

  1. 01Text to one update

    Toy text, causal attention, the p − y loss gradient

    previous
  2. 02RoPE phase

    Rotated query/key pair and relative phase

    current
  3. 04Long-context use

    Position range, retrieval quality, and memory pressure

    later