Exercises — Rotary positional embeddings (RoPE)
The tools we will use over and over
Before any exercise, let us earn every symbol that does the work. A smart 12-year-old should be able to read from here.
Figure 1 (below) shows this: the magenta arrow is the original , the violet arrow is after turning by the orange angle , and both arrows have the same length — look at how only the direction changes, never the size.

Throughout, is a query vector, a key vector, and are integer positions (which word slot in the sentence), and is a fixed base angle for a given pair. We write for "the query at position after RoPE".
Level 1 — Recognition
Exercise 1.1 (L1)
Which of the following does RoPE rotate: (a) the value vectors , (b) the query and key vectors, (c) the raw token embeddings before the attention layer, (d) all of them?
Recall Solution
(b) — only queries and keys. The attention score is where position needs to live, because it decides which tokens attend to which. Values carry content to be summed and are left untouched. Answer: (b).
Exercise 1.2 (L1)
State the rotation matrix from memory, then evaluate .
Recall Solution
A zero-angle turn does nothing: , . So a token at position 0 keeps its query unchanged. This is the natural "anchor" of the sequence.
Exercise 1.3 (L1)
Using the pairing from the preliminaries, in the frequency schedule , does the lowest dimension pair () rotate fast or slow?
Recall Solution
Fast. With small, the exponent is close to , so is close to — a large angle per step. Low dimension pairs spin quickly (fine, local detail); high dimension pairs spin slowly (coarse, long-range detail). See the parent note's Mistake 3.
Level 2 — Application
Exercise 2.1 (L2)
Take at position and at position , with base angle . Compute the RoPE'd key and then the score .
Recall Solution
Since , . The key rotates by . Then , : Wait — check the sign. Column 2 of is . Multiplying by the in the second slot gives . Score . Answer: .
(The parent note wrote for the same setup — matches, once the second column's signs are read correctly.)
Exercise 2.2 (L2)
Same as 2.1 but now at and at . Show the score is identical to 2.1 and say why.
Recall Solution
The score is . Relative distance is , exactly as in 2.1 where . So the angle inside is both times. Concretely: , and of that is . Answer: , unchanged — this is the whole point of RoPE: only the gap matters, not the absolute slots.
Exercise 2.3 (L2)
Compute for a head of dimension (first pair, ). Then compute the accumulated angle at position .
Recall Solution
Take logs: , so . Accumulated angle at : radians. Since , that is just past one full turn. Answer: , angle at position 10 rad ( rad mod ).
Level 3 — Analysis
Exercise 3.1 (L3)
Prove that preserves length: for any vector , . Why does this matter for attention?
Recall Solution
Let and . Expand: the cross terms and cancel. What remains is using . Why it matters: RoPE only turns the query/key arrows, never stretches them. So it injects position without secretly boosting or shrinking the content magnitude — the attention score changes only through the angle, exactly as intended.
Exercise 3.2 (L3)
For pair of a head, the frequency is . Find the period in positions (how many positions make one full rotation) for and for (the last pair). Interpret.
Recall Solution
One full turn happens when , so .
- : , so positions. This pair completes a loop roughly every 8 tokens — it tracks local structure.
- : , so positions. This pair barely turns across an entire long document — it tracks global structure. Interpretation: the head simultaneously holds a fast "second hand" (pair 1) and a slow "hour hand" (pair 32), covering all scales at once.
Figure 2 (below) plots one coordinate, , against position for a fast pair (magenta) and a slow pair (violet). Notice the magenta curve completes many wiggles while the violet one barely bends — that visual gap is the local-vs-long-range split.

Exercise 3.3 (L3)
The score between two positions equals , where is the gap. Treat this first as a continuous function of a real-valued gap , with , , rad. Find where the continuous curve is maximum, zero, and minimum for . Then read off what the integer gaps actually give.
Recall Solution
, and of that picks the first component: score — a smooth curve defined for every real . Continuous landmarks (imagine sliding continuously):
- Maximum at (a token attending to itself, arrows aligned).
- First zero at (arrows perpendicular).
- Minimum at (arrows opposed), then it climbs back — the score oscillates and wraps.
Integer gaps (what a real sentence can actually have, since positions are whole numbers): , , . So real gaps just sample points off the smooth cosine curve; the continuous zero at falls between integer gaps 1 and 2, which is why no integer gap gives exactly zero here. Answer: score ; continuous max at , zero at , min at ; the reachable integer gaps sample that curve.
Level 4 — Synthesis
Exercise 4.1 (L4)
Derive the master identity from scratch, then use it to show the RoPE attention score depends only on . Cover the case too.
Recall Solution
Step A — transpose is reverse rotation. From the preliminaries, . What we just did: recognised that un-doing a rotation is rotating backwards. Step B — multiply out one entry to see the pattern. Write and compute, say, the top-left entry. Row 1 of is (since , ); column 1 of is . Their product is which is precisely the angle-difference identity — and precisely the top-left entry of . Do the same for the other three entries (top-right gives , etc.) and every slot lines up, so Step C — apply to attention. With , : The case: if the key sits before the query, then is negative. Nothing breaks — the identity holds for any real angles, so we simply rotate by a negative angle, and . Geometrically the relative arrow just turns the other way. The score becomes , giving for the case (because is even: ) — so the raw score is symmetric in the two directions, while the sign of the rotation still distinguishes "before" from "after" in the off-diagonal terms.
Exercise 4.2 (L4)
Represent the 2D vector as the complex number . Show that rotating by is the single multiplication , and confirm it reproduces the RoPE formula.
Recall Solution
Euler's formula: . Multiply: Expand, remembering : The real part and imaginary part are exactly the two rows of from the definition box. So one complex multiply = one 2D rotation. Why libraries do this: it is one multiply instead of a matrix product, and it vectorises cleanly across all pairs. This is the "complex number trick" in the parent note.
Exercise 4.3 (L4)
Position 2000, dimension , first pair . The raw accumulated angle is radians. Reduce it modulo and explain why length extrapolation survives.
Recall Solution
: divide , so full turns use up radians. Remainder radians. (The parent note's used a heavily rounded ; with exactly the remainder is — same idea either way: it lands somewhere on the circle we have already visited.) Why extrapolation survives: the score depends on the relative angle . A token pair at positions has gap , giving rad — a relative angle the model met countless times while training on short sequences. Absolute position 2000 is brand new, but its gap-angles are old friends, because rotations wrap around every . So the rotation matrices generalise to unseen absolute positions as long as the relative distances were seen. See Length Extrapolation in LLMs. Answer: rad.
Level 5 — Mastery
Exercise 5.1 (L5)
A student proposes: "Skip RoPE — just rotate values too so position flows everywhere." Show the flaw by tracking what happens to the aggregated output.
Recall Solution
Attention output for query is . If we also rotate values to , then the summed output carries a jumble of absolute rotations that does not reduce to a clean relative form — different 's get different absolute turns and never cancel, because there is no second transpose to meet them. The content vector delivered to the next layer would now spin with absolute position, re-introducing exactly the absolute-position bias RoPE set out to kill. Conclusion: rotating breaks relative invariance. Keep untouched. Contrast with Relative Position Bias (T5), which also keeps values clean and only edits the score.
Exercise 5.2 (L5)
Design a sanity check: for a head (two pairs) with frequencies , , query , key , verify by direct computation that the full 4D RoPE score at gap equals , and evaluate at .
Recall Solution
RoPE is block-diagonal: pair 1 (dims 1–2) rotates by , pair 2 (dims 3–4) by , and the 4D dot product is the sum of the two 2D dot products.
- Pair 1: , . Score (from Exercise 3.3).
- Pair 2: , . Score . Total . At : . Answer: total score at is . This confirms the multi-frequency, block-diagonal picture: each pair contributes an independent cosine, summed. Different heads in Multi-Head Attention can lean on different pairs.
Exercise 5.3 (L5)
Explain, using the oscillation-plus-summation idea from Exercise 3.3, why RoPE tends to give an approximate long-range attention decay even though no single pair decays — and connect to why NTK/interpolation scaling is needed for extreme extrapolation.
Recall Solution
Each pair contributes . At a large gap , the fast pairs ( near 1) have argument sweeping through many periods; across the pairs these cosines land at effectively random phases and partly cancel when summed, so the aggregate score shrinks toward its average — an emergent decay, not a designed one (this is the correct reading of the L3 trap). But the slow pairs () barely move even at in the thousands, so they cannot represent a new large relative distance distinctly — the model has no trained signal for "gap = 3000" if it only ever saw gaps up to 512. Fix in practice: position-interpolation / NTK scaling stretches or re-bases the frequencies so that unseen large gaps map back into the trained angular range, restoring usable relative signal. See Length Extrapolation in LLMs. Answer (conceptual): decay is an emergent phase-cancellation effect; extreme extrapolation still needs frequency rescaling because slow pairs run out of unseen angular resolution.
Recall Quick self-test (cloze)
RoPE rotates only the queries and keys, never the values ::: because the score is where position must live, while values carry content. The identity that collapses two positions into their gap is ::: . Low dimension pairs use high frequencies for ::: local/fine structure; high pairs use low frequencies for long-range structure. A single RoPE pair's score as a function of gap is ::: — it oscillates, it does not decay.