GO TO A SECTION

↑ ↓ to navigate · Enter to openExplore all sections →
FDEInterviews logo
★ Read firstNeurocomputing

RoFormer: Enhanced Transformer with Rotary Position Embedding

Instead of adding a position vector, rotate each query and key by an angle set by its position. The score then depends only on distance. LLaMA and most open models since use this scheme.

Breakdown by FDE Interviews · 11 min read · Updated

About the original · 2021
AUTHORS
Su, Lu, Pan, Murtadha, Wen, Liu
FROM
Zhuiyi Technology
PUBLISHED
Apr 2021 · Neurocomputing
CITED BY
7,216Semantic Scholar · Nov 2026
IN THIS BREAKDOWN · 10 SECTIONS

You can read 26% of this breakdown.

01

The paper in one minute

Attention compares every token with every other, with no built-in sense of order. The original Transformer fixed this by adding a position vector, a set of sines and cosines, to each token's input. That works, but position and content get mixed in the same numbers, and the model has to learn how to extract relative distance. Relative-position methods added terms to the attention score instead, at the cost of extra parameters or incompatibility with faster linear attention.

RoPE, rotary position embedding, encodes position by rotation. Take the query and key vectors after their usual projections and split each into pairs of dimensions. Treat each pair as a point in a plane and rotate it by an angle equal to the token's position times a frequency; each pair has its own frequency, from fast to slow, set as 10,000 to the power of minus 2i/d, the same schedule as the original sinusoids. When a rotated query at position m meets a rotated key at position n, the angles subtract, so their dot product depends on the vectors and on n minus m, never on absolute position. Rotation also leaves vector lengths unchanged.

The authors derive this from the requirement that the score depend only on content and relative position, show an efficient elementwise way to compute it, prove a long-term decay property (an upper bound on the score shrinks as distance grows), and show it can be combined with linear attention. They test it as RoFormer: a small BLEU gain over Transformer-base in translation, faster-falling pre-training loss than BERT, mixed GLUE results, faster loss with Performer, and better accuracy on a Chinese legal document matching task when the model reads 1,024 rather than 512 characters.

The experiments are modest and the authors say they cannot fully explain why RoPE converges faster or does better on long text. RoPE spread because it is simple, has no learned parameters, and plays well with attention kernels and caches. LLaMA uses it, and context-extension methods for open models are built on adjusting its frequencies.

THE IDEA, VISUALLY
ROTATE, DON'T ADD (SECTION 3.2) q at m k at n angle (n - m) θ Each 2-d pair of q and k turns by position x frequency θi, with θi = 10000^(-2i/d). q·k depends on n - m only; lengths never change. GLUE, ROFORMER MINUS BERT (TABLE 2) MRPC +0.6 SST-2 -2.8 QNLI -2.5 STS-B +1.2 QQP +15.2 MNLI-m -4.4 MNLI-mm -3.6 BERT row = BERT-Base GLUE test scores; QQP there is F1. arXiv 2104.09864 v5, Zhuiyi Technology LONG-TERM DECAY, d = 128 (OUR COMPUTATION) 0% 25% 50% 75% 100% 0 4 16 64 256 1024 distance between tokens (log scale) paper's bound, share of distance 0 a vector scored against itself A bound and an average, not a guarantee for any one pair.
Left: RoPE rotates each two-dimensional pair of a query and key by position times a frequency, so their score depends on the angle between them, set by the distance; below, Table 2's GLUE differences between RoFormer and BERT. Right: our computation of the paper's long-term decay bound and of a vector's score against itself as distance grows.
Research notes: the problem, approach and results
0

change in the query-key score when both positions shift by the same amount, in our implementation (Section 3.4.1)

27.5 vs 27.3

BLEU for RoFormer vs Transformer-base on WMT 2014 English to German, one run each (Table 1)

3 of 6

GLUE tasks where RoFormer beat BERT; the BERT numbers are BERT-Base's published test-server scores (Table 2)

THE PROBLEM

Transformers need to know word order. The original added a position vector to each input, which mixes position into the content and does not directly encode how far apart two tokens are; relative schemes added extra terms that do not fit linear attention.

THE IDEA

Rotate the query and key vectors: split them into pairs of dimensions and turn each pair by the token's position times a fixed frequency. The dot product of a rotated query and key then depends only on their contents and the difference in positions.

THE RESULT

Small gains: 27.5 vs 27.3 BLEU on WMT14 English-German, faster pre-training loss than BERT and Performer, and 69.79% vs 68.10% on a Chinese long-document matching task when allowed 1,024 tokens. The method's simplicity, not these numbers, made it the default.

02

Why it matters to an FDE

It is inside most open models you will deploy. LLaMA-family and many other open-weight models use RoPE. When you read a model card that mentions a "RoPE base" or "rope scaling", this is the mechanism being adjusted.

Context extension is a RoPE story. Models trained at one length are stretched to longer contexts by changing the rotation frequencies. When a customer asks whether a model really handles 128,000 tokens, the honest test is retrieval and reasoning quality at that length on their documents, because the frequencies were adjusted after training and quality can fall off.

Relative position is why long prompts behave as they do. Scores depend on distance, and the paper's bound shrinks with distance. Our computation shows a token's score against an identical token at distance 1,024 at about 21% of its value at distance 0. Put the instructions and key facts where the model is most likely to use them, and test, rather than assuming position does not matter.

Small published gains, big practical adoption. The paper's numbers are within noise in places and mix evaluation setups. Adoption came from simplicity and compatibility. When evaluating newer position methods for a customer's long-document task, measure on their data.

Fine-tuning keeps the same scheme. If you fine-tune a RoPE model on longer documents than it was trained on, the frequency settings and the training length together decide how well it generalizes; follow the model provider's guidance for scaling.

THE FULL BREAKDOWN

Understand the mechanism. Try it on a real problem.

Unlock this breakdown and 290 more papers.

Premium opens every paper in full, plus every answer, concept, course and guide on the site. One payment for six months of access.

6 months · One payment · No auto-renewal

Get Premium · ₹2,000 / $25
  • ◇1 architecture diagram
  • </>1 runnable code experiment
  • ▦2 results tables
  • ?5 interview questions with answers
  1. 03Key takeaways

    The derivation, the rotation and its fast form, decay, linear attention, experiments and limits

    Free account
  2. 04How it works

    The derivation, block-diagonal rotation, fast form, decay bound and linear attention

    Premium
  3. 05The numbers that matter

    Translation, GLUE, Performer, the Chinese stages and CAIL, with what each comparison controls

    Premium
  4. 06Try it yourself

    Re-read the tables, implement RoPE two ways, verify the relative property, and compute the decay

    Premium
  5. 07Where it breaks

    Small and confounded experiments, unexplained gains, and lengths beyond training

    Premium
  6. 08Using it on a deployment

    Check context claims on real documents, place key content deliberately, and follow scaling guidance

    Premium
  7. 09Interview angles

    Five questions this paper prepares you for, with answer hints

    Premium
Back to the paper library →