Rotary Position Embedding (RoPE) › Worked Example: Step-by-Step RoPE for a 4-Dimensional Vector
Language AI HandbookAll chapters Rotary Position Embedding (RoPE) › Worked Example: Step-by-Step RoPE for a 4-Dimensional VectorOne section · Sections in this chapter8 / 19 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Before we look at batched implementations, it helps to trace through a complete example by hand. This makes the abstract rotation formula concrete and shows exactly what changes when you apply RoPE to a real vector.
Suppose we have a 4-dimensional query vector at position :
With , we have two dimension pairs and two frequencies:
At position , the rotation angles for each pair are:
- Pair 0: radians
- Pair 1: radians
Now apply the 2D rotation formula to each pair separately. For pair 0, with and :
For pair 1, with and (small angle, so and ):
The rotated query is . Notice that the first pair changed dramatically (2 radians is a large rotation), while the second pair barely moved (0.02 radians is about 1 degree). This illustrates the multi-scale nature of RoPE: high-frequency pairs carry a strong position signal that varies quickly from token to token, while low-frequency pairs change slowly and provide a coarser sense of position.
To confirm the key property, you would perform the same calculation for a key vector at position , compute the dot product , and verify that it equals the dot product you would get at any other pair of positions with relative distance . The numbers work out exactly because the composition of rotations always produces the relative angle regardless of where in the sequence you started.