Equations · Su et al., “RoFormer: Enhanced Transformer with Rotary Position Embedding” (2021); used in LLaMA and most recent LLMs

Rotary position embedding (RoPE), explained term by term

RoPE encodes position by rotating each query and key vector, in pairs of coordinates, by an angle proportional to the token's position. Because rotations compose, the attention score between positions mm and nn depends only on their offset n−mn - m, not on where they sit.

qm⊤kn=(RΘ,mdWqxm)⊤(RΘ,ndWkxn)=xm⊤Wq⊤RΘ,n−mdWkxn,θi=10000−2(i−1)/d\mathbf{q}_m^\top \mathbf{k}_n = \left(\mathbf{R}^d_{\Theta,m} W_q \mathbf{x}_m\right)^\top \left(\mathbf{R}^d_{\Theta,n} W_k \mathbf{x}_n\right) = \mathbf{x}_m^\top W_q^\top \mathbf{R}^d_{\Theta,n-m} W_k \mathbf{x}_n, \quad \theta_i = 10000^{-2(i-1)/d}

Open in the decoder →

Term by term

  1. xm\mathbf{x}_m

    The embedding of the token at position mm.

  2. Wqxm, WkxnW_q \mathbf{x}_m, \ W_k \mathbf{x}_n

    The ordinary query and key projections, before any position information is added.

  3. RΘ,md\mathbf{R}^d_{\Theta,m}

    A d×dd \times d rotation matrix for position mm. It is block-diagonal: d/2d/2 separate 2×22 \times 2 rotations, the ii-th turning coordinates (2i−1,2i)(2i-1, 2i) by the angle mθim\theta_i, using cos⁡mθi\cos m\theta_i and sin⁡mθi\sin m\theta_i. The superscript dd is its size, a label, and Θ\Theta is the set of all θi\theta_i.

  4. θi=10000−2(i−1)/d\theta_i = 10000^{-2(i-1)/d}

    The rotation speed of pair ii, for i=1,…,d/2i = 1, \ldots, d/2. The first pair turns by 1 radian per position; later pairs turn ever more slowly, down to about 1/100001/10000. Here the superscript really is a power.

  5. RΘ,n−md\mathbf{R}^d_{\Theta,n-m}

    Rotating one vector by mθm\theta and the other by nθn\theta, then taking their dot product, is the same as rotating by the difference: Rm⊤Rn=Rn−m\mathbf{R}_m^\top \mathbf{R}_n = \mathbf{R}_{n-m}. That is where relative position comes from. (The paper writes WqW_q without the transpose.)

Shapes

A small worked example

Take d=2d = 2, so θ1=1\theta_1 = 1, and Wq=Wk=IW_q = W_k = I. A query (1,0)(1, 0) at position m=1m = 1 rotates to (cos⁡1,sin⁡1)≈(0.540,0.841)(\cos 1, \sin 1) \approx (0.540, 0.841); a key (1,0)(1, 0) at n=3n = 3 rotates to (cos⁡3,sin⁡3)≈(−0.990,0.141)(\cos 3, \sin 3) \approx (-0.990, 0.141). Their dot product is ≈−0.416=cos⁡2\approx -0.416 = \cos 2. Move both tokens 10 places along and the score is still cos⁡2\cos 2.

In code

import numpy as np

def rope(x, pos, base=10000.0):
    # x: (n, d) queries or keys, pos: (n, 1) positions; pairs are (0,1), (2,3), ...
    d = x.shape[-1]
    theta = base ** (-np.arange(0, d, 2) / d)    # (d/2,)
    ang = pos * theta                            # (n, d/2)
    x1, x2 = x[..., 0::2], x[..., 1::2]
    out = np.empty_like(x)
    out[..., 0::2] = x1 * np.cos(ang) - x2 * np.sin(ang)
    out[..., 1::2] = x1 * np.sin(ang) + x2 * np.cos(ang)
    return out

Every symbol in it

x∈Rd\mathbf{x} \in \mathbb{R}^{d}Bold lowercase (vector)xi,  Wq,  pθ,  dmodelx_i,\; W_q,\; p_\theta,\; d_{\text{model}}Subscript (index or label)W∈Rm×n\mathbf{W} \in \mathbb{R}^{m \times n}Bold uppercase (matrix)Q,K,VQ, K, VQ, K, V (query, key, value)x2,  βt,  WQx^2,\; \beta^t,\; W^QSuperscript (power or label)pθ(x)p_\theta(x)Thetacos⁡(u,v)=u⊤v∥u∥ ∥v∥\cos(\mathbf{u}, \mathbf{v}) = \frac{\mathbf{u}^\top \mathbf{v}}{\|\mathbf{u}\| \, \|\mathbf{v}\|}CosinePE(pos,2i)=sin⁡(pos100002i/d)PE_{(pos, 2i)} = \sin\left(\frac{pos}{10000^{2i/d}}\right)Sinex⊤y\mathbf{x}^\top \mathbf{y}Transpose

Other equations