Worked examples — Rotary positional embeddings (RoPE)
Everything here uses only the two objects from the parent note:
- a rotation matrix — "spin a 2D vector counter-clockwise by angle ";
- the key identity — "undo one spin, then do the other, and only the gap survives".
Two more symbols show up below, so let us pin them down now in plain words:
- = the head dimension — how many numbers are in one query or key vector for a single attention head (e.g. ). RoPE always chops this into independent pairs of coordinates.
- = the pair index, running . Pair is the -th such 2-number block, and it gets its own spin-rate .
If the rotation matrix or the key identity feels shaky, re-read Rotary positional embeddings (RoPE) first, then come back.
The scenario matrix
Every RoPE calculation is one cell of this table. The examples below are labelled by cell. (Recall = head dimension, defined just above.)
| Cell | Case class | What is unusual about it | Example |
|---|---|---|---|
| A | Same position, | relative distance → no rotation gap | Ex 1 |
| B | Positive gap, | ordinary "key is after query" | Ex 2 |
| C | Negative gap, | angle gap flips sign → cosine unchanged, geometry mirrored | Ex 3 |
| D | Degenerate vector, or | rotation of the zero vector is still zero → score | Ex 4 |
| E | Wrap-around / limiting, huge | angle — length extrapolation | Ex 5 |
| F | Multi-frequency, | two dimension-pairs spinning at different rates | Ex 6 |
| G | Real-world word problem | translate a sentence into positions | Ex 7 |
| H | Exam twist | "does rotating both by the SAME extra angle change the score?" | Ex 8 |
We check the score-only-depends-on-gap property everywhere, and along the way we cover both signs of the gap, zero, wrap-around, degeneracy, and multiple frequencies.
Recall What does
collapse to, and why do we care? It collapses to ::: because is the inverse rotation, so the two spins partly cancel and only the difference remains — that difference is the relative position.
Example 1 — Cell A: same position ()
Forecast: rotating both vectors by the same amount — does the dot product change at all compared to not rotating? Guess before reading.
-
Rotation angle for each. Both sit at position , so each is spun by . Why this step? Position base angle is exactly how RoPE turns a position number into a rotation.
-
Use the key identity instead of rotating by hand. The score is Why this step? (spin by nothing = do nothing), so we never touch the numbers.
-
Compute the plain dot product.
Verify: Rotation is rigid — it preserves lengths and angles between vectors, so a shared rotation cannot change a dot product. Score , the same as the un-rotated dot product. When two tokens share a position, RoPE is invisible. ✅
Example 2 — Cell B: positive gap ()
The geometry of this example is drawn in Figure s01: the cyan arrow is the rotated query at position 1, the amber arrow is the rotated key at position 4, and the white arc between them is the effective gap angle . Use the picture to see why the score comes out negative before you crunch the numbers.

Forecast: the gap is , so the effective rotation is . Will the score be positive or negative? Look at Figure s01 and guess from the angle between the cyan and amber arrows.
-
Effective gap angle. . Why this step? Only the difference matters, so we skip the two separate spins.
-
Collapse to one rotation. Why this step? Key identity again — .
-
Multiply out. With , ,
-
Dot with .
Verify: In Figure s01 the two arrows point more than apart, so their dot product must be negative — matches . ✅ (This mirrors Example 1 in the parent note, which used positions and ; same gap of , same answer.)
Example 3 — Cell C: negative gap ()
Forecast: the gap flipped sign. Will the score match Example 2, or flip sign too?
-
Gap angle. . Why this step? The formula is — negative gap → negative angle.
-
Apply . Here (even) but (odd): Why this step? We must track the sign flip in carefully — this is exactly where careless students drop a minus.
-
Dot with .
Verify: The score is , the negative of Example 2's . That is not a coincidence: swapping and here swaps which vector was vs , and for these particular perpendicular vectors the score is antisymmetric. The magnitude is identical because of and agree — the geometry is a mirror image. ✅
Example 4 — Cell D: degenerate vector ()
Forecast: does the position machinery still do anything?
-
Rotate the zero vector. for any . Why this step? A rotation is a linear map; it sends the origin to the origin no matter the angle.
-
Dot product. .
Verify: No rotation, no frequency, no position could ever make this non-zero — a token with a zero key contributes zero to the pre-softmax score. Units check: a dot product of vectors is a pure number, and is dimensionless. ✅ This is the degenerate boundary of the whole method.
Example 5 — Cell E: wrap-around & length extrapolation
Forecast: position 2000 was never seen in training (max length 512). Guess whether RoPE breaks.
-
Compute the base frequency. Why this step? This is the actual spin-rate of the first (fastest) dimension pair.
-
Accumulated raw angle. radians. Why this step? Position frequency, exactly as in the parent note.
-
Wrap into . Divide by : full turns. Subtract the whole turns (): radians. Why this step? Rotation is -periodic: spinning rad lands in the same place as spinning rad. The absolute number is huge but the geometry is ordinary.
-
The gap that actually matters. For positions and , the gap angle is Why this step? Attention only uses the gap. A relative distance of was seen thousands of times in training (any pair 5 apart), so this angle is completely familiar.
Verify: ✅ and the gap radians is independent of the absolute positions — the model reuses knowledge it already has. This is precisely why RoPE extrapolates to unseen lengths. (Matches Example 2 in the parent note.) ✅
Example 6 — Cell F: multiple frequencies ()
Figure s02 shows the pay-off of splitting a vector into pairs: for the same relative distance, the cyan "fast" pair (spin-rate ) sweeps a wide -radian arc, while the amber "slow" pair (spin-rate ) barely turns. That is why one head can be sensitive to short and long ranges at once.

Forecast: two pairs spin at different speeds. Does the total score just add the two pair-scores?
-
Split into pairs. Pair 1 = , pair 2 = . Each pair is rotated independently — that is exactly the block-diagonal structure defined just above (see Figure s02). Why this step? RoPE never mixes dimension-pairs; each is its own little 2D rotation.
-
Gap angle per pair. Gap , so
- pair 1: rad
- pair 2: rad.
-
Score of each pair . For : Why this step? , and dotting with picks out .
- pair 1 score
- pair 2 score .
-
Total score = sum of pair scores (a dot product over is the sum of the two 2D dot products):
Verify: Figure s02 shows the fast pair (cyan) swept rad and the slow pair (amber) swept only rad for the same relative distance — the slow pair stays closer to its start, so its cosine () is larger than the fast pair's (). Sum . Different frequencies = different sensitivity to distance. ✅
Example 7 — Cell G: word problem
Forecast: guess the angle before computing, and whether absolute positions matter.
-
Identify query & key positions. Query "ran" ; key "cat" . Why this step? By convention the token doing the looking is the query.
-
Relative position. (the key is 6 tokens before the query).
-
Gap angle. rad. Why this step? Attention score uses ; sign tells us direction (key before query → negative).
Verify: Any subject–verb pair exactly 6 apart — say positions — gives rad, identical. RoPE lets "ran" recognise its subject by the pattern of the gap, not by where the clause happens to sit in the sentence. Units: radians, an angle, as required. ✅
Example 8 — Cell H: exam twist (global shift invariance)
Forecast: does adding 100 to both positions change anything?
-
Original gap. . Score .
-
Shifted gap. New positions and : gap . Unchanged. Why this step? The constant appears in both, so it cancels in the difference — this is the heart of "relative, not absolute".
-
Numerical score. , so . Then
Verify: Both the original and shifted setups give score , because is regardless of . Global position shifts are invisible to RoPE — exactly the property that distinguishes it from Sinusoidal Positional Encoding additions and lets it match ideas like Relative Position Bias (T5) and ALiBi (Attention with Linear Biases). ✅
Wrap-up
Recall Which two vectors get rotated, and which does not?
Queries and keys are rotated ::: values are left untouched, because position must shape which tokens to attend to, not what content to aggregate.
Recall Why does the score depend only on
? Because ::: the two absolute spins partially cancel and only their difference — the relative position — survives.
See also Multi-Head Attention (each head can rotate independently), Attention Mechanism (where the rotated lives), and Length Extrapolation in LLMs (Example 5's real payoff). The Hinglish walkthrough is at 4.1.07 Rotary positional embeddings (RoPE) (Hinglish).