Grimoire
Sheet
patharchitectures/position
difficultyintermediate
timeO(n·d)
memoryO(n·d)
described2021
revised2w ago

Rotary Position Embedding

Position enters through a rotation applied to queries and keys, chosen so that the dot product between two positions depends only on their offset.

Theory

Split each dd-dimensional query into d/2d/2 planes. RoPE rotates plane ii at position mm by angle mθim\theta_i, with θi=b2i/d\theta_i = b^{-2i/d} and base bb typically 10410^4. Because rotations compose, the inner product between a rotated query at mm and a rotated key at nn collapses to a function of mnm - n.

Rmq,  Rnk=q,  Rnmk\langle R_m q,\; R_n k \rangle = \langle q,\; R_{n-m} k \rangle
eq. 1 — relativity from orthogonality of R

The low-frequency planes rotate slowly and carry long-range order; the high-frequency ones alias almost immediately. Context extension methods exploit exactly this split — position interpolation divides every angle by the extension factor, while NTK-aware scaling raises the base instead so that high frequencies are left alone.

θi=b2i/d,b=bsd/(d2)\theta_i = b^{-2i/d}, \qquad b' = b \cdot s^{\,d/(d-2)}
eq. 2 — NTK-aware base rescale for extension factor s
Implementation
python · torch ≥ 2.1
import torch
from torch import Tensor


def rope_cache(seq_len: int, dim: int, base: float = 10_000.0):
    """Precompute cos/sin tables of shape [seq_len, dim // 2]."""
    inv_freq = base ** -(torch.arange(0, dim, 2).float() / dim)
    t = torch.arange(seq_len).float()
    angles = torch.outer(t, inv_freq)
    return angles.cos(), angles.sin()


def apply_rope(x: Tensor, cos: Tensor, sin: Tensor) -> Tensor:
    """x: [B, H, N, D] with D even. Rotates adjacent pairs."""
    x1, x2 = x[..., 0::2], x[..., 1::2]
    cos, sin = cos[None, None], sin[None, None]
    out = torch.stack(
        (x1 * cos - x2 * sin, x1 * sin + x2 * cos), dim=-1
    )
    return out.flatten(-2)

Build the tables in fp32 and cast at the point of use — computing the angles in fp16 loses enough precision at long context to visibly degrade retrieval.

Related
References
[1]Su et al. — RoFormer: Enhanced Transformer with Rotary Position Embedding (2021)arXiv:2104.09864
[2]Chen et al. — Extending Context Window via Position Interpolation (2023)arXiv:2306.15595
[3]Press et al. — Train Short, Test Long: ALiBi (2021)arXiv:2108.12409