Rotary Position Embedding (RoPE) › The Complete RoPE Formula
Language AI HandbookAll chapters Rotary Position Embedding (RoPE) › The Complete RoPE FormulaOne section · Sections in this chapter6 / 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 that we understand the individual components, let's bring everything together into the complete RoPE transformation. We've established three key ideas:
- Rotation encodes position: Each position corresponds to a rotation angle
- Dot products extract relative position: When query and key are rotated by different amounts, their dot product depends only on the angle difference
- Multiple frequencies create richness: Different dimension pairs rotate at different rates, capturing both local and global position information
The complete RoPE formula combines these insights into a single elegant operation. Given a query or key vector at position , we apply RoPE as follows:
where:
- : the rotated vector, a function of both the input vector and position
- : the input query or key vector with dimensions
- : the sequence position (integer index)
- : the 22 rotation matrix for angle , applied to dimension pair
- : the base frequency for dimension pair (decreases exponentially with )
- The block-diagonal structure means each 22 rotation block operates independently on its corresponding dimension pair
- Empty off-diagonal blocks are zeros, so dimensions in different pairs don't interact during rotation
The large block-diagonal matrix applies different rotations to each dimension pair simultaneously. This is efficient because each 2D rotation is independent of the others, allowing for parallel computation.
Expanding the rotation for a single dimension pair :
where:
- , : the -th and -th components of the input vector (using 1-based indexing)
- , : the corresponding components after rotation
- : the rotation angle, which increases linearly with position at a rate determined by frequency
Written out element-wise, the transformation is: