Rotary Position Embedding (RoPE) › Rotation Matrices in 2D
Language AI HandbookAll chapters Rotary Position Embedding (RoPE) › Rotation Matrices in 2DOne section · Sections in this chapter3 / 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
To understand why RoPE works, you first need a firm grip on how rotation changes a vector and why the dot product between rotated vectors has a simple form. Let's build up the mechanics from scratch.
In two dimensions, rotating a vector by angle counterclockwise uses the rotation matrix:
where:
- : the 2D rotation matrix that rotates vectors by angle
- : the rotation angle in radians (counterclockwise is positive)
- , : trigonometric functions evaluated at angle
Applying this rotation matrix to a 2D vector transforms its coordinates:
where:
- : the original vector coordinates
- : the rotated vector coordinates
- The first row computes the new -coordinate by combining the original coordinates with and
- The second row computes the new -coordinate using and
The rotated vector has the same length as but points in a direction shifted by . This is because rotation matrices are orthogonal, meaning they preserve vector lengths (norms) and angles between vectors. Orthogonality is the key property that makes everything else work: when you rotate both a query and a key, their magnitudes stay the same, and the only thing that changes in their dot product is the angular relationship between them.
In practice, two properties of orthogonal matrices matter most for RoPE. First, the transpose of a rotation matrix equals its inverse: . This tells you that "undoing" a rotation by angle is the same as transposing the matrix, which will become important when we simplify the dot product expression. Second, multiplying two rotation matrices combines their angles: . This composition rule is what allows absolute angles to cancel and leave only their difference.
def rotate_2d(vector, theta):
"""Rotate a 2D vector by angle theta (in radians)."""
cos_t, sin_t = np.cos(theta), np.sin(theta)
rotation_matrix = np.array([[cos_t, -sin_t], [sin_t, cos_t]])
return rotation_matrix @ vectorLet's visualize how rotation transforms a vector:
# Original vector
original = np.array([1.0, 0.5])
# Rotate by several angles
angles = [0, np.pi / 6, np.pi / 3, np.pi / 2]
rotated_vectors = [rotate_2d(original, theta) for theta in angles]
The dashed circle shows the path traced by the vector tip as it rotates. The length (magnitude) never changes. Rotation is an isometry, a transformation that preserves distances. This matters for attention: the magnitude of a query or key vector carries information about how strongly that token "wants" to attend or "wants" to be attended to. Rotation leaves that strength unchanged while shifting the directional relationship between the two vectors.