Rotary Position Embedding (RoPE) › The Complete RoPE Formula

Michael BrenndoerferUpdated June 5, 20252 min read
Language AI HandbookAll chapters
Part 14 of 67Positional Encoding
Chapter 100Rotary Position Embedding (RoPE) › The Complete RoPE Formula
Reading section 6 of 19 · The Complete RoPE Formula · 2 minSection 6 of 19 · 2 min

Book contents

Section 6 of 19 · 2 min read

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:

  1. Rotation encodes position: Each position mm corresponds to a rotation angle mθm\theta
  2. Dot products extract relative position: When query and key are rotated by different amounts, their dot product depends only on the angle difference
  3. 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 x∈Rd\mathbf{x} \in \mathbb{R}^d at position mm, we apply RoPE as follows:

RoPE(x,m)=(R(θ0⋅m)R(θ1⋅m)⋱R(θd/2−1⋅m))(x1x2x3x4⋮xd−1xd)\text{RoPE}(\mathbf{x}, m) = \begin{pmatrix} R(\theta_0 \cdot m) & & & \\ & R(\theta_1 \cdot m) & & \\ & & \ddots & \\ & & & R(\theta_{d/2-1} \cdot m) \end{pmatrix} \begin{pmatrix} x_1 \\ x_2 \\ x_3 \\ x_4 \\ \vdots \\ x_{d-1} \\ x_d \end{pmatrix}

where:

  • RoPE(x,m)\text{RoPE}(\mathbf{x}, m): the rotated vector, a function of both the input vector and position
  • x∈Rd\mathbf{x} \in \mathbb{R}^d: the input query or key vector with dd dimensions
  • mm: the sequence position (integer index)
  • R(θi⋅m)R(\theta_i \cdot m): the 2×\times2 rotation matrix for angle θi⋅m\theta_i \cdot m, applied to dimension pair ii
  • θi=1/100002i/d\theta_i = 1 / 10000^{2i/d}: the base frequency for dimension pair ii (decreases exponentially with ii)
  • The block-diagonal structure means each 2×\times2 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 (x2i+1,x2i+2)(x_{2i+1}, x_{2i+2}):

(x2i+1′x2i+2′)=(cos⁡(mθi)−sin⁡(mθi)sin⁡(mθi)cos⁡(mθi))(x2i+1x2i+2)\begin{pmatrix} x'_{2i+1} \\ x'_{2i+2} \end{pmatrix} = \begin{pmatrix} \cos(m\theta_i) & -\sin(m\theta_i) \\ \sin(m\theta_i) & \cos(m\theta_i) \end{pmatrix} \begin{pmatrix} x_{2i+1} \\ x_{2i+2} \end{pmatrix}

where:

  • x2i+1x_{2i+1}, x2i+2x_{2i+2}: the (2i+1)(2i+1)-th and (2i+2)(2i+2)-th components of the input vector (using 1-based indexing)
  • x2i+1′x'_{2i+1}, x2i+2′x'_{2i+2}: the corresponding components after rotation
  • mθim\theta_i: the rotation angle, which increases linearly with position mm at a rate determined by frequency θi\theta_i

Written out element-wise, the transformation is:

  • x2i+1′=x2i+1cos⁡(mθi)−x2i+2sin⁡(mθi)x'_{2i+1} = x_{2i+1} \cos(m\theta_i) - x_{2i+2} \sin(m\theta_i)
  • x2i+2′=x2i+1sin⁡(mθi)+x2i+2cos⁡(mθi)x'_{2i+2} = x_{2i+1} \sin(m\theta_i) + x_{2i+2} \cos(m\theta_i)