RoFormer: Enhanced Transformer with Rotary Position Embedding
This research paper explains how encoding absolute position with a rotation matrix gives self-attention an explicit relative-position dependency, length flexibility, and a natural long-range decay.
Transformers need to know where a token sits. Absolute sinusoidal embeddings add a position vector to each token; relative schemes try to make attention depend on the offset between tokens, but many of those tricks add position into the content stream in ways that break linear / kernelized attention.
RoPE’s punchline is geometric. Treat each even/odd pair of dimensions as a 2-D plane. At position , rotate the query (and key) by angle . Then the attention score between positions and depends on the relative angle — absolute indices on each side, relative structure in the product.
What this paper explains
Position information in PLMs is usually injected in one of three ways:
- Absolute embeddings (sinusoidal or learned) added to token embeddings.
- Relative biases or embeddings that modify attention logits using .
- Hybrids that try to keep both.
Su et al. formulate a functional constraint: after position-aware transforms , the inner product should be a function of the content and the relative offset . They then exhibit a solution: multiply and by rotation matrices indexed by and .
The paper asks:
- Can relative position enter self-attention through a multiplicative (rotation) path instead of an additive one?
- Does that form keep sequence-length flexibility and a long-term decay intuition?
- Can the same idea attach to linear attention, where additive relative methods struggle?
Prior limits
- Additive absolute PE (Vaswani et al.) works, but position and content are mixed in the same vector before . Relative structure is only implicit.
- Relative position methods (Shaw et al., Transformer-XL, DeBERTa-style biases, etc.) often add position-specific terms into attention. The paper notes that adding position into the context representation can make those ideas awkward for linear self-attention.
- Complex-space or other PE variants help empirically but still commonly follow the “add position to content” pattern the authors want to avoid.
- Practitioners want length extrapolation and decaying influence with distance — properties that need to fall out of the formulation, not only from training data.
The mechanism
Constraint: relative in the product
Write queries and keys as position-aware maps of content . The authors require that the attention affinity depend on , , and . In the 2-D case they solve this with complex multiplication (rotation):
The product then involves — relative phase — while each side only needed its own absolute index.
Matrix view (same geometry)
Equivalently, with real 2-D vectors,
Rotating by and by leaves the inner product equal to rotating one vector by against the other.
General even dimension
Split into planes and apply a block-diagonal rotation:
with pairwise angles (same base as the Transformer sinusoids). Implementation never materializes the huge matrix: rotate each pair in place.
Properties the paper highlights
- Relative by construction — absolute rotations compose into an dependency in attention.
- Length flexibility — positions are angles; you can evaluate at indices not seen in training (with the usual caveats).
- Long-term decay — with the schedule, the expected contribution of distant pairs tends to fade as grows (their §3.3 / §3.4.3 intuition).
- Linear attention compatible — because position is a rotation of and , not an additive term glued onto the value pathway the way some relative methods need.
Algorithm walkthrough
- Project tokens to , as usual.
- Choose even head dimension ; set for .
- For each position index and each pair , rotate by (same for keys with ).
- Compute attention with the rotated — e.g. (values are typically not rotated in the standard RoPE recipe).
- Stack as in any Transformer; the paper’s RoFormer is “Transformer + RoPE” evaluated on MT, MLM-style pretraining, GLUE, and a Performer-style linear-attention check.
The opening visual shows additive PE as a vertical stack of content+position versus RoPE as a turn of the query/key arrows. The slider above lets you change and watch a 2-D pair rotate while the relative angle to a fixed key updates.
What to notice when reading
- The derivation order: constraint on first, rotation solution second — not “we tried a rotation and it worked.”
- Values are left unrotated in the usual formulation; position enters the routing (scores), not the value vectors themselves.
- The schedule is doing real work for the decay story; it is not an arbitrary constant.
- Their linear-attention experiment is about compatibility of the PE form, not a claim that Performer+RoPE is SOTA everywhere.
Results and evidence
Numbers below are from the paper’s reported tables (arXiv:2104.09864), not independent re-runs.
Machine translation (Table 1). On WMT 2014 English→German, under their fairseq setup (joint 37k BPE, Adam , beam 4, length penalty 0.6):
| Model | BLEU |
|---|---|
| Transformer-base (Vaswani et al., 2017) | 27.3 |
| RoFormer | 27.5 |
Language modeling / downstream. They replace BERT’s sinusoidal PE with RoPE during MLM pretraining (BookCorpus + Wikipedia split) and fine-tune on GLUE; the narrative claim is that RoFormer consistently overcomes the sinusoidal baseline under their settings (see paper §4.2–4.3 for per-task scores — report from the PDF rather than inventing cells here).
Linear attention. §4.4 pairs RoPE with Performer-style linear attention to argue the multiplicative PE form still applies when softmax attention is replaced.
Chinese data. §4.5 adds further classification-style checks beyond the English GLUE suite.
Limitations
Compressed from the paper’s setting and from what it does not claim:
- Empirical lifts vs strong absolute PE can be modest on short-sequence tasks (Table 1’s +0.2 BLEU).
- Long-context extrapolation later became a whole research area (NTK-aware scales, YaRN, ALiBi comparisons, etc.); this paper supplies the mechanism and early evidence, not the final 2024–2026 length-extrapolation cookbook.
- Most experiments are encoder or encoder–decoder style PLMs of their period; decoder-only LLM training recipes post-date the work.
- Theoretical decay arguments are about the PE form’s inductive bias; they are not a guarantee for every downstream metric.
How to read the paper
- Abstract + §1 — relative-in-the-product goal vs additive PE.
- §3.1–3.2 — 2-D complex/rotation derivation, then block-diagonal .
- §3.3 properties — length flexibility, decay, linear-attention hook.
- §4.1 Table 1 — WMT En→De sanity check.
- §4.2–4.4 — pretraining/GLUE + Performer experiment.
- §5 — claims to keep separate from later industrial RoPE folklore.
Knowledge check
What does RoPE rotate to inject position?
Why does rotating q at m and k at n make attention depend on relative position?
Which property do the authors emphasize for the θ_i = 10000^(−2i/d) schedule?
Keep reading
- Original paper (arXiv:2104.09864) — §3 derivation, Figure 1, Table 1.
- Vaswani et al., Attention Is All You Need — sinusoidal absolute PE — arXiv:1706.03762.
- Press et al., ALiBi — alternative relative bias for length extrapolation — arXiv:2108.12409.
- Related Fanout Daily: Attention Is All You Need, GQA, FlashAttention.
Sources
- Su, Lu, Pan, Murtadha, Wen, Liu, RoFormer: Enhanced Transformer with Rotary Position Embedding, 2021 — arXiv:2104.09864
- Vaswani et al., Attention Is All You Need, NeurIPS 2017 — arXiv:1706.03762
- Press, Smith, Lewis, Train Short, Test Long: Attention with Linear Biases… (ALiBi), 2021 — arXiv:2108.12409