The attention dot product $\langle q, k \rangle$ is permutation-invariant: shuffling the input tokens does not change which keys match which queries. Transformers need some signal of position. Three approaches have been tried:
This chapter develops RoPE.
Let $q, k \in \mathbb{R}^{d}$ with $d$ even. Partition each into $d/2$ adjacent pairs:
\[q = (q_0, q_1, q_2, q_3, \dots), \quad k = (k_0, k_1, k_2, k_3, \dots)\]Treat each pair $(q_{2m}, q_{2m+1})$ as a 2D vector. For position $i$, choose a frequency $\theta_m = 10000^{-2m/d}$, and rotate the $m$-th pair by angle $i \theta_m$:
\[R_{i,m} = \begin{pmatrix} \cos(i\theta_m) & -\sin(i\theta_m) \\ \sin(i\theta_m) & \cos(i\theta_m) \end{pmatrix}\]Apply this rotation to every pair to get the position-encoded query $q^{(i)}$. Do the same to $k$ at position $j$ to get $k^{(j)}$.
Compute the dot product between $q^{(i)}$ and $k^{(j)}$:
\[\langle q^{(i)}, k^{(j)} \rangle = \sum_m (R_{i,m} q_m)^T (R_{j,m} k_m) = \sum_m q_m^T R_{i,m}^T R_{j,m} k_m\]Because rotations satisfy $R_a^T R_b = R_{b-a}$:
\[\langle q^{(i)}, k^{(j)} \rangle = \sum_m q_m^T R_{j-i,m} k_m\]The result depends only on the relative offset $j - i$, not on the absolute positions $i$ or $j$. RoPE delivers relative positional information through a transformation that is applied independently at each position — no quadratic-size bias matrix needed.
Let $d = 2$, so there is one pair. Set $\theta_0 = 1$. Take $q = (1, 0)$, $k = (1, 0)$. At positions $i = 0$ and $j = 2$:
At positions $i = 5$ and $j = 7$ (same offset of 2):
Identical, as the theorem promised.
The choice $\theta_m = 10000^{-2m/d}$ mirrors the sinusoidal scheme of Vaswani et al. Each pair rotates at a different rate: low-index pairs spin fast (encoding fine positional differences), high-index pairs spin slowly (encoding long-range structure). One model, multiple positional scales, no extra parameters.
The natural extension is RoPE scaling — methods like NTK-aware interpolation, YaRN, and LongRoPE that let a model trained at, say, 4K context generalize to 32K or 128K by remapping the frequencies $\theta_m$ at inference time. Read Peng et al.’s YaRN paper alongside the Su et al. original (2021).