Rotary Position Embedding (RoPE) › From Rotation to Relative Position
Language AI HandbookAll chapters Rotary Position Embedding (RoPE) › From Rotation to Relative PositionOne section · Sections in this chapter4 / 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
Now let's see how rotation makes dot products depend on relative position. Take two 2D vectors (query) and (key). Rotate by angle (for position ) and by angle (for position ).
The dot product of the rotated vectors is:
where:
- : the query vector at position
- : the key vector at position
- : rotation matrix that rotates by angle (position times base angle)
- : rotation matrix that rotates by angle
Using properties of rotation matrices, we can simplify this expression step by step:
The derivation proceeds as follows:
- Rewrite the dot product as matrix multiplication:
- Apply the transpose-inverse property: The transpose of a rotation matrix equals its inverse, so . This gives us .
- Apply the composition property: Multiplying two rotation matrices adds their angles, so . Therefore .
The final result depends only on the difference , not on the absolute values of and separately.
When we rotate query by and key by , their dot product depends only on , the relative position. The absolute positions and vanish, replaced by their difference.
This is remarkable. We encode absolute position through rotation angle, but the attention mechanism, which uses dot products, automatically extracts relative position. No architectural changes needed. No explicit bias terms. Just geometry.
The derivation also reveals something that's easy to overlook: the original vectors and are still present in the final expression . Their content still matters, it's just that the rotation wraps that content in a position-dependent frame. The model can still learn that "verb tokens should attend to their subject nouns"; it just does so in a coordinate system where position is already factored out. This is a cleaner separation of concerns than additive encodings, where position and content signals are entangled from the start.
Let's verify this numerically:
def verify_relative_position():
"""Verify that rotated dot products depend only on relative position."""
# Random query and key vectors
q = np.random.randn(2)
k = np.random.randn(2)
theta = 0.5 # Base rotation angle
# Different absolute positions, same relative distance
results = []
for m, n in [(1, 3), (5, 7), (10, 12), (100, 102)]:
q_rotated = rotate_2d(q, m * theta)
k_rotated = rotate_2d(k, n * theta)
dot_product = np.dot(q_rotated, k_rotated)
results.append((m, n, n - m, dot_product))
return results
position_results = verify_relative_position()Verifying relative position property: Position m Position n n - m Dot Product -------------------------------------------------- 1 3 2 -0.651890 5 7 2 -0.651890 10 12 2 -0.651890 100 102 2 -0.651890
All four pairs have the same relative distance (2 positions apart), and their dot products are identical despite wildly different absolute positions. This confirms the relative position property holds numerically. Notice that positions 1 and 3, 5 and 7, 10 and 12, and 100 and 102 all produce the same score, exactly as the geometric derivation predicted.