Positional Encoding  · April 2021

Rotary Position Embedding

intermediate

Encode position by rotating Q and K vectors — so that attention's inner product depends only on the relative offset between tokens, with no separate position-embedding lookup.

§ 1 · Premise

Position information is not in the input

Self-attention as defined by Vaswani et al. (2017, §3.2) is permutation-equivariant: permuting the input tokens permutes the output tokens identically. The attention score between query at position mm and key at position nn,

amn=(WQxm)(WKxn)/dh,a_{mn} = (W_Q \mathbf{x}_m)^\top (W_K \mathbf{x}_n) / \sqrt{d_h},

is a symmetric bilinear function of the two embeddings. It contains no information about mm, nn, or nmn - m. For language — where “the dog bit the man” and “the man bit the dog” share a multiset of tokens but mean different things — a positional signal must be injected somewhere.

The original Transformer added a fixed sinusoidal vector pt\mathbf{p}_t to the token embedding etok(t)\mathbf{e}_{\text{tok}(t)} before the first attention layer (Vaswani et al. 2017, §3.5). BERT, GPT-2, and GPT-3 replaced the sinusoidal table with a learned matrix PRTmax×dmodelP \in \mathbb{R}^{T_{\max} \times d_{\text{model}}} of the same shape (Devlin et al. 2018, §3.2; Radford et al. 2019). Both share the additive form

xtetok(t)+pt.\mathbf{x}_t \leftarrow \mathbf{e}_{\text{tok}(t)} + \mathbf{p}_t.

Three failures recur. First, the learned table is a hard cap. GPT-3’s PP matrix has shape 2048×122882048 \times 12288, 100\sim 100 MB at fp32, and is exactly undefined at position 20492049 (Brown et al. 2020, §2.1). Sinusoidal embeddings, although mathematically defined for any tt, in practice degrade sharply past the trained length: ALiBi Fig. 3 shows sinusoidal-trained models diverging in perplexity by 1.5×1.5\times training length. Second, the additive form leaks position into the entire residual stream: every token embedding carries an absolute-position fingerprint even through layers where position is irrelevant. Third, the absolute-position signal must be re-derived as a relative offset by every attention head in every layer; the network has to learn, from data, the trigonometric identity sin(ab)=sinacosbcosasinb\sin(a-b) = \sin a \cos b - \cos a \sin b in order to compare two positions. Shaw et al. (2018, §3.2), T5 (Raffel et al. 2019, §2.1), and ALiBi (Press et al. 2021) all sidestepped this by editing the attention score itself with a relative-position term — but at the cost of breaking the clean inner-product formulation that Flash Attention–style fused kernels rely on.

RoPE’s claim is that the relative-only attention score is a property of geometry, not something the network has to learn: rotate each query and key by an angle that scales with its absolute position, and the inner product collapses to a function of the offset alone.

§ 2 · Derivation

From the dot product to rotation

Let qm,knRdh\mathbf{q}_m, \mathbf{k}_n \in \mathbb{R}^{d_h} denote a query at position mm and a key at position nn, where dhd_h is the per-head dimension (even). The pre-softmax attention logit is qm,kn\langle \mathbf{q}_m, \mathbf{k}_n \rangle. We seek functions fq,fkf_q, f_k such that the position-aware vectors fq(q,m)f_q(\mathbf{q}, m) and fk(k,n)f_k(\mathbf{k}, n) satisfy

fq(q,m),fk(k,n)  =  g(q,k,nm)(2.1)\langle f_q(\mathbf{q}, m), f_k(\mathbf{k}, n) \rangle \;=\; g(\mathbf{q}, \mathbf{k}, n - m) \tag{2.1}

for some function gg that depends on the two raw vectors and the relative offset alone. Su et al. 2021, §3.2 frame this as the defining property they want. The substitution fq(q,m)=qf_q(\mathbf{q}, m) = \mathbf{q}, fk(k,n)=kf_k(\mathbf{k}, n) = \mathbf{k} — no positional information — trivially satisfies (2.1) with gg independent of nmn - m, which is the starting failure mode. The substitution fq(q,m)=q+pmf_q(\mathbf{q}, m) = \mathbf{q} + \mathbf{p}_m — additive embedding — does not satisfy (2.1) in general, because cross terms qpn\mathbf{q}^\top \mathbf{p}_n and pmk\mathbf{p}_m^\top \mathbf{k} depend on absolute positions.

The 2-D case. Specialize to dh=2d_h = 2. Identify R2\mathbb{R}^2 with C\mathbb{C} by q=q0+iq1\mathbf{q} = q_0 + i q_1. Define

fq(q,m)=qeimθ,fk(k,n)=keinθ,(2.2)f_q(\mathbf{q}, m) = \mathbf{q}\, e^{i m \theta}, \qquad f_k(\mathbf{k}, n) = \mathbf{k}\, e^{i n \theta}, \tag{2.2}

i.e. multiplication by a unit-modulus complex number that rotates the vector by angle mθm\theta. The Hermitian inner product is

fq(q,m),fk(k,n)C=(qeimθ)(keinθ)=qkei(nm)θ,\langle f_q(\mathbf{q}, m), f_k(\mathbf{k}, n) \rangle_{\mathbb{C}} = (\mathbf{q}\, e^{i m \theta})^* (\mathbf{k}\, e^{i n \theta}) = \overline{\mathbf{q}}\, \mathbf{k}\, e^{i(n - m)\theta},

and the real part — which is what the dot product over R2\mathbb{R}^2 recovers — is Re(qkei(nm)θ)\operatorname{Re}(\overline{\mathbf{q}} \mathbf{k}\, e^{i(n-m)\theta}), a function of q,k,nm\mathbf{q}, \mathbf{k}, n - m only. Property (2.1) is satisfied. Equivalently, in matrix form, the same map is the planar rotation

Rt=(cos(tθ)sin(tθ)sin(tθ)cos(tθ)),RmRn=Rnm,(2.3)R_t = \begin{pmatrix} \cos(t\theta) & -\sin(t\theta) \\ \sin(t\theta) & \cos(t\theta) \end{pmatrix}, \qquad R_m^\top R_n = R_{n - m}, \tag{2.3}

so (Rmq)(Rnk)=qRmRnk=qRnmk(R_m \mathbf{q})^\top (R_n \mathbf{k}) = \mathbf{q}^\top R_m^\top R_n \mathbf{k} = \mathbf{q}^\top R_{n - m} \mathbf{k}. The dependence on mm and nn separately has cancelled.

Lifting to dhd_h dimensions. A single 2-D rotation cannot encode more than one frequency. Su et al. (2021, §3.2.2) lift the construction by partitioning the dhd_h coordinates of q,k\mathbf{q}, \mathbf{k} into dh/2d_h/2 disjoint pairs (q2i,q2i+1)(q_{2i}, q_{2i+1}) for i=0,1,,dh/21i = 0, 1, \dots, d_h/2 - 1, and rotating each pair by its own frequency θi\theta_i. The position-tt rotation is the block-diagonal matrix

Rt=diag ⁣(Rt(0),Rt(1),,Rt(dh/21)),Rt(i)=(cos(tθi)sin(tθi)sin(tθi)cos(tθi)).(2.4)R_t = \operatorname{diag}\!\bigl(R_t^{(0)}, R_t^{(1)}, \dots, R_t^{(d_h/2 - 1)}\bigr), \qquad R_t^{(i)} = \begin{pmatrix} \cos(t\theta_i) & -\sin(t\theta_i) \\ \sin(t\theta_i) & \cos(t\theta_i) \end{pmatrix}. \tag{2.4}

Each Rt(i)R_t^{(i)} is orthogonal, so RtR_t is orthogonal; the product RmRnR_m^\top R_n is block-diagonal with blocks Rnm(i)R_{n-m}^{(i)}; the global identity RmRn=RnmR_m^\top R_n = R_{n - m} holds dimension-pair by dimension-pair, and (2.1) holds in Rdh\mathbb{R}^{d_h}.

Frequency choice. The frequencies are

θi=b2i/dh,b=10000,i=0,,dh/21,(2.5)\theta_i = b^{-2i / d_h}, \qquad b = 10000, \quad i = 0, \dots, d_h/2 - 1, \tag{2.5}

mirroring the wavelength geometric series of the sinusoidal embedding of Vaswani et al. 2017, §3.5. The pair i=0i = 0 rotates at 11 radian per step (wavelength 2π6.32\pi \approx 6.3 tokens); the pair i=dh/21i = d_h/2 - 1 rotates at b1=104\sim b^{-1} = 10^{-4} radians per step (wavelength 2πb6.28×104\sim 2\pi b \approx 6.28 \times 10^4 tokens). Low-index pairs resolve short-range offsets; high-index pairs carry long-range phase. The choice of base bb is the single hyperparameter; long-context recipes (Position Interpolation Chen et al. 2023, NTK-aware scaling Peng & Quesnelle 2023, and YaRN) act entirely by reweighting these θi\theta_i.

Three structural properties follow directly from (2.4):

  1. Linearity in the rotated vector. RtR_t is a linear map. Scaling q\mathbf{q} by cc scales RtqR_t \mathbf{q} by cc. This is what lets RoPE be applied as a post-projection mutation, independent of WQ,WKW_Q, W_K.
  2. Norm preservation. RtR_t is orthogonal, so Rtq2=q2\|R_t \mathbf{q}\|_2 = \|\mathbf{q}\|_2. RoPE does not change the magnitude of any pre-RoPE Q or K vector — only its phase. This matters for stability: norms set by WQ,WKW_Q, W_K initialization survive the rotation unchanged.
  3. Distance decay. Su et al. (2021, §3.4.3, Fig. 2) compute the upper bound on attention logits for two random vectors as a function of offset and show it decays roughly like 1/nmα1/|n - m|^{\alpha} for moderate offsets — an inductive bias toward local attention, without an ALiBi-style hand-coded slope.

Crucially, RoPE does not rotate the value matrix WVW_V: only Q and K participate in (2.1). The value path remains a pure linear function of the token content.

Parameter and compute cost. RoPE introduces zero learnable parameters: θi\theta_i is fixed by (2.5); cos(tθi)\cos(t\theta_i) and sin(tθi)\sin(t\theta_i) are deterministic. Per query or key vector the rotation is dh/2d_h/2 planar rotations — 2dh2 d_h multiply-adds total — so the per-token cost is O(dh)O(d_h) FLOPs per head, O(Hdh)=O(dmodel)O(H d_h) = O(d_{\text{model}}) per layer. For a Llama-2-70B attention head (dh=128d_h = 128) this is 256\sim 256 FMAs per token per head, negligible against the O(dhT)O(d_h T) cost of the attention dot products themselves.

Rotation of a Q vector by RoPE. Left: unit circle showing the rotated vector. Right: rotation angle as a function of position for the selected dimension pair.Q vector at position t = 64t=0angle = 66.9° (+10×360°)Angle (wrapped) over positions 0..8,192008,192θ_i = 1.00e+0 (10 full rotations across the sweep)
i = 0 rotates fast — the vector spins through the unit circle in just a few positions. i = 63 (the slowest pair at d_h = 128, base 10000) barely moves across the full 8K context. The relative phase between any two positions is what attention reads off.

§ 3 · Reference implementation

Paired-coordinate rotation

def precompute_freqs(d_h, max_pos, base=10000.0):
    # theta_i = base^{-2i / d_h}, i = 0 ... d_h/2 - 1
    i = torch.arange(0, d_h, 2).float()                 # [d_h/2]
    theta = base ** (-i / d_h)                          # [d_h/2]
    t = torch.arange(max_pos).float()                   # [max_pos]
    freqs = torch.outer(t, theta)                       # [max_pos, d_h/2]
    return freqs.cos(), freqs.sin()                     # each [max_pos, d_h/2]

def apply_rope(x, cos, sin):
    # x:   [B, T, H, d_h]   one of Q or K, post-projection
    # cos: [T, d_h/2]       sin: [T, d_h/2]
    x_even = x[..., 0::2]                               # [B, T, H, d_h/2]
    x_odd  = x[..., 1::2]                               # [B, T, H, d_h/2]
    cos = cos[None, :, None, :]                         # broadcast over B, H
    sin = sin[None, :, None, :]
    # planar rotation per pair: (x_even, x_odd) -> (x_even*cos - x_odd*sin,
    #                                                x_even*sin + x_odd*cos)
    out_even = x_even * cos - x_odd * sin
    out_odd  = x_even * sin + x_odd * cos
    return torch.stack([out_even, out_odd], dim=-1).flatten(-2)   # [B, T, H, d_h]

# usage inside attention forward:
#   q = apply_rope(W_Q(x), cos, sin)
#   k = apply_rope(W_K(x), cos, sin)
#   v = W_V(x)                            # V is NOT rotated
#   logits = einsum("bthd,bshd->bths", q, k) / sqrt(d_h)

§ 4 · Empirical evidence

What RoPE actually buys you

RoFormer paper. Su et al. 2021 report three primary result sets. Table 1 compares RoPE to sinusoidal absolute embeddings on WMT 2014 En→De machine translation with a Transformer-Base: RoPE reaches 27.5 BLEU vs sinusoidal 27.3 BLEU (§4.1). Table 2 swaps in RoPE on a BERT-base architecture pretrained on the BookCorpus + Wikipedia mix and reports a \sim1-point MLM accuracy improvement over the absolute baseline at matched compute (§4.2). Table 3 fine-tunes the resulting RoFormer on GLUE; RoPE matches or narrowly beats BERT on 5 of 6 tasks. The gains are real but small; the paper’s contribution is the form of the encoding, not large absolute numbers.

Llama 1 / 2 / 3 adoption rationale. Llama 1 (Touvron et al. 2023a, §2) adopted RoPE without ablation, citing GPT-NeoX-20B (Black et al. 2022, §3.4) as the reference data point — GPT-NeoX had reported faster convergence and slightly lower validation loss vs learned absolute embeddings on the Pile. Llama 2 (Touvron et al. 2023b) retained RoPE unchanged. Llama 3 (Grattafiori et al. 2024, §3.1) kept RoPE and pushed the base bb to 5×1055 \times 10^5 to support an 128K-token context window — see also the partial-rotary and base-scaling configurations in the adoption list above.

Independent reproductions and follow-ups. Press et al. 2021, Fig. 4 (the ALiBi paper) include RoPE in their length-extrapolation benchmark: on WikiText-103 with training length 512 and evaluation lengths up to 3072, RoPE perplexity grows from 19.3 at training length to 28.1 at 6×6\times — better than learned absolute embeddings (which diverge above 36 perplexity) but worse than ALiBi (which holds near 19.7). This is the canonical “RoPE doesn’t extrapolate cleanly” data point that motivated the subsequent extension work. Chen et al. 2023, §3 (Position Interpolation) reproduce the RoPE perplexity blowup at 4×4\times training length on Llama-7B and recover it by rescaling positions ttLtrain/Levalt \to t \cdot L_{\text{train}} / L_{\text{eval}} before applying (2.5). YaRN (Peng et al. 2023, Table 2) improves on this by reweighting θi\theta_i frequency-by-frequency, taking Llama-2-7B from 5.4 perplexity at 8K context to 3.8 at 64K — a public, reproduced result that defines the practical ceiling of RoPE-based length extension as of 2024.

Length-extrapolation cliff. Across all three independent studies, the qualitative finding is consistent: without intervention, a RoPE model trained on context length LL loses calibration on context length >1.5L> 1.5 L to 2L2 L, with perplexity climbing monotonically. The fast-rotating dimensions i0i \approx 0 wrap around the unit circle many times within the training window and generalize; the slow-rotating dimensions idh/2i \approx d_h/2 never complete a full rotation within the training window and have no signal beyond it. This frequency-ladder asymmetry is the lever that Position Interpolation, NTK-aware scaling, and YaRN all pull on.

Adopted by

  • Llama 1 65B · Meta — RoPE base 10000; 2K base context.  [source]
  • Llama 2 70B · Meta — RoPE base 10000, no scaling (4K context).  [source]
  • Llama 3.1 70B · Meta — RoPE base scaled for 128K context.  [source]
  • DeepSeek LLM 67B · DeepSeek-AI — RoPE base 10000; 4K base context. Standard pre-MLA configuration.  [source]
  • DeepSeek V3 · DeepSeek-AI — RoPE on the decoupled head of MLA; YaRN scaling for 128K.  [source]
  • Gemma 3 27B · Google DeepMind — Two RoPE bases: 10K for local SWA layers, 1M for global layers.  [source]
  • OLMo 2 13B · Allen Institute for AI (AI2) — RoPE with base 500K.  [source]
  • OLMo 3 32B · Allen Institute for AI (AI2) — RoPE base 500K; pretrained at 8K context and extended to 64K via YaRN.  [source]
  • OLMoE 1B/7B · Allen Institute for AI (AI2) — RoPE (4K base context).  [source]
  • MiniMax-Text-01 · MiniMax — RoPE; 4M context via Lightning Attention's linear cost.  [source]
  • Kimi Linear 48B-A3B · Moonshot AI — RoPE base 10K applied only to a 64-dim decoupled head in MLA layers; MLA body uses NoPE.  [source]
  • Qwen3 235B-A22B · Alibaba (Qwen Team) — RoPE base scaled from 10K to 1M via ABF; YaRN + DCA for long-context extension.  [source]
  • Qwen3 32B · Alibaba (Qwen Team) — Same ABF + YaRN + DCA recipe as the Qwen 3 MoE flagship.  [source]
  • Qwen3 30B-A3B · Alibaba (Qwen Team) — RoPE with ABF-scaled base, shared across the Qwen 3 family.  [source]
  • Hunyuan-Large 389B · Tencent — RoPE base pushed to 10⁹ to support the 256K context.  [source]
  • GLM-4.5 · Zhipu AI — RoPE with theta = 10⁶ and a partial rotary factor of 0.5 — half the head dim carries rotation.  [source]
  • MiniMax-M1 · MiniMax — RoPE carried over from MiniMax-Text-01; 1M context with the 7:1 Lightning + softmax hybrid.  [source]
  • Qwen3-Next 80B-A3B · Alibaba (Qwen Team) — RoPE applied only to the Gated Attention layers (rotary dim 64); Gated DeltaNet layers carry no positional encoding.  [source]

Lineage

Cite

BibTeX entry for the original paper
@article{arxiv2104_09864,
  title  = {RoFormer: Enhanced Transformer with Rotary Position Embedding},
  author = {Jianlin Su, Yu Lu, Shengfeng Pan, Ahmed Murtadha, Bo Wen, Yunfeng Liu},
  year   = {2021},
  eprint = {2104.09864},
  archivePrefix = {arXiv},
  url    = {https://arxiv.org/abs/2104.09864}
}

Or cite the paper directly: arXiv:2104.09864.

Export

BibTeX
@article{arxiv_2104_09864,
  title         = {RoFormer: Enhanced Transformer with Rotary Position Embedding},
  author        = {Jianlin Su and Yu Lu and Shengfeng Pan and Ahmed Murtadha and Bo Wen and Yunfeng Liu},
  year          = {2021},
  eprint        = {2104.09864},
  archivePrefix = {arXiv},
  url           = {https://arxiv.org/abs/2104.09864}
}
CSL JSON
{
  "id": "arxiv_2104_09864",
  "type": "article-journal",
  "title": "RoFormer: Enhanced Transformer with Rotary Position Embedding",
  "author": [
    {
      "literal": "Jianlin Su"
    },
    {
      "literal": "Yu Lu"
    },
    {
      "literal": "Shengfeng Pan"
    },
    {
      "literal": "Ahmed Murtadha"
    },
    {
      "literal": "Bo Wen"
    },
    {
      "literal": "Yunfeng Liu"
    }
  ],
  "issued": {
    "date-parts": [
      [
        2021
      ]
    ]
  },
  "URL": "https://arxiv.org/abs/2104.09864",
  "number": "2104.09864",
  "source": "arXiv"
}
RIS
TY  - JOUR
TI  - RoFormer: Enhanced Transformer with Rotary Position Embedding
AU  - Jianlin Su
AU  - Yu Lu
AU  - Shengfeng Pan
AU  - Ahmed Murtadha
AU  - Bo Wen
AU  - Yunfeng Liu
PY  - 2021
JO  - arXiv
AN  - arXiv:2104.09864
UR  - https://arxiv.org/abs/2104.09864
ER  -