Mini WorldLaplace AI / Plate F73 — AI

Rotary Position Embedding

Attention can’t tell word order. Rotary position embedding turns queries and keys by angles set by position, some pairs fast, some slow, so a score sees positions only as a distance.

Scroll

01

Attention Can’t See Order

Attention scores each pair of words, a dot product of one word’s query and another word’s key; a higher score means that word is looked at more (see the plate “Attention in Transformers”). The score depends only on the two words’ own numbers, not on where they stand.

So without positions, “dog bites man” and “man bites dog” get the same nine scores, only in different places. The 2017 paper says it plainly: with no recurrence and no convolution, the model can only use word order if information about position is put in.

Dog bites man · man bites dog · no positions: the same nine scores, moved around

02

2017: Add Sines and Cosines

The original Transformer gives each position a fixed list of sines and cosines and adds it to the word’s numbers; their wavelengths run from 2π to 10,000 × 2π positions. BERT and GPT-⁠2 learn their position vectors instead, and also add them.

Added, the position and the word’s meaning share the same list of numbers. Rotary position embedding does something else: it turns instead of adding.

A list of sines and cosines per position · added to the word · wavelengths 2π to 10,000 × 2π

03

Turn, Don’t Add

Rotary position embedding touches only queries and keys, never values. A query’s numbers are taken two at a time, each pair an arrow in a plane; for a word at position m that arrow is turned by m·θ. Each dial on the bank is one pair, and its pointer shows the angle the pair has been turned, m·θ, not the arrow’s own direction, which depends on the word.

Turning changes direction, never length. Position 0 is not turned, position 1 by θ, position 2 by 2θ. The front dial’s θ is 1 radian: one slot further along, its pointer turns 57.3°. Values are not turned, so position changes which words are looked at, not what is brought back.

Position m: turned by m·θ · queries and keys only · length unchanged

04

Fast Dials and Slow Dials

Each pair turns at its own speed: pair i by θᵢ = 10000^(−2i/d) per position. A LLaMA attention head has 128 numbers, so 64 pairs. Pair 0 turns 1 radian per position, each pair after it more slowly, and pair 63 only about 0.0066° per position.

The eight dials on each carriage are pairs 0, 8, 16 … 56, each √10 times slower than the one in front. The positions it takes to turn once round is a dial’s wavelength: about 6.3 for the front dial, about 19,870 for the back one, about 54,400 for pair 63. A fast dial differs a lot between neighbouring words; a slow one needs hundreds or thousands of positions to show a difference.

θᵢ = 10000^(−2i/d) · d = 128 · the eight dials: wavelengths about 6.3 to 19,870 positions

05

Only the Distance Is Left

A score is the dot product of the query at position m and the key at position n. In each pair one arrow has been turned by m·θᵢ and the other by n·θᵢ; the dot product depends only on the angle between them, and their turns differ by (m − n)·θᵢ. Rotations add up, so m and n themselves do not matter, only m − n.

On the bank, “bit” stands at position 5 and gives the query; “dog” stands at 1 and gives the key. Move the whole sentence three slots along: every dial turns, but in every pair the angle between the two pointers stays, and so does the score. This page checked it with 128 numbers: positions 5 and 1, 8 and 4, 1,005 and 1,001 give the same score, to within 10⁻¹², the computer’s rounding.

qₘ · kₙ depends only on m − n · 5 and 1, 8 and 4: one score

06

Deck Lifted: The Gear Train

The last carriage’s deck is lifted with its eight dials. Below it is a gear train: a wheel on each dial’s arbor and a pinion between each two, every stage slower than the one before. At the front a small pinion meshes with the rack, so each slot along turns it by a fixed angle.

This is the page’s metaphor, not a real machine. A real model has no gears: it computes cos(m·θᵢ) and sin(m·θᵢ) and replaces each pair of numbers with the turned pair, two multiplications and one addition a number. The θᵢ are fixed in advance; there is nothing to learn.

The rack: one slot a position · the train: each stage slower · a real model computes cos and sin

07

Further Apart, a Lower Bound

The paper also derives this: the further apart two words are, the lower, on the whole and with wiggles, the most their score can be. The chart is that bound recomputed on this page for d = 128, and it matches the paper’s Figure 2.

It is a bound, not the score: the score of two particular words can rise and fall with distance, and a distant word can still score high. A later paper on longer contexts pointed out that the bound may be too loose to say much.

The bound: about 19.8 at distance 8 · 12.6 at 50 · 6.5 at 256 · a bound, not the score

08

Why Models Took It Up

LLaMA (2023) dropped the added position vectors and applies rotary embeddings at every layer, with 128 numbers a head and at most 2,048 tokens. PaLM uses it too, for better performance on long sequences. In the original paper it scored 27.5 BLEU on English–German translation against the Transformer’s 27.3, and a little better on matching long legal cases.

It costs almost nothing: no parameters to learn, and turning a query or key is a multiply and an add per number. When text is generated, each token’s key is turned before it goes into the KV cache (see the plate “The KV Cache and PagedAttention”). The base can change: Llama 3 replaced 10,000 with 500,000, which takes the slowest pair’s wavelength from about 54,400 positions to about 2.56 million; it trained at 8K and was then extended to 128K.

LLaMA: every layer, d = 128 · Llama 3: base 500,000 · no parameters to learn

09

Stretching the Context

Past the positions seen in training, the slow dials stop at angles the model has never seen: read a longer text directly and perplexity can go above 1,000. Position interpolation (PI, 2023) divides every position by the same factor, so the dials turn more slowly and the angles stay inside the trained range; it took LLaMA from 2,048 to 32,768 tokens with under 1,000 steps of fine-⁠tuning, and does much worse without them.

“NTK-⁠aware” scaling changes the base instead: the fastest dial keeps its speed, and the slower a dial, the more it is slowed. YaRN works by groups: fast dials that turn many times within the trained length are left alone, slow ones that never finish a turn are slowed by the full factor, those between are blended, and a temperature is added; with under about 0.1 % of the pre-⁠training data it took Llama 2 from 4K to 128K. Each of these needs fine-⁠tuning or stretches only so far.

PI: every position ÷ s · NTK: a new base · YaRN: fast dials kept, slow ones slowed · all fine-⁠tuned

10

Taken Apart

The desk, the rail, the carriages, the dials, the gear trains, the word tiles and the counter, one by one.

Rest the pointer on one (tap it on a phone) and it lifts alone and is named; the rest fade.

11

The Bench

Pick two words, one for the query and one for the key. The curve is this pair’s score against the distance m − n; the open dot is their distance now. Move the start into the thousands and the whole sentence moves along: every dial turns, and the open dot stays where it is.

Switch on “stretch ×2” and every position is divided by 2 (position interpolation): the curve widens twice over. This is the page’s model: the query and key are 128 numbers drawn from a fixed seed, not numbers from a trained model, and in a real model the scores then go through a softmax.

Try thisIn the lab, let “bit” give the query and “dog” the key, and note the score. Drag the start to 3,000: does the score change? Then switch on “stretch ×2”: what is the score for the same two words now?

What the model leaves out

The bank is this page’s metaphor, not a real machine. The dial angles follow LLaMA’s settings (128 numbers a head, base 10,000), and only 8 of the 64 pairs are drawn; the gears are not counted to the √10 ratio, and a real model has no gears, it computes cos and sin. Each word is drawn as one carriage; in a real model every token has its own query and key in every layer and every head, and all of them are turned. Some code pairs neighbouring numbers, some pairs numbers d/2 apart; the turning is the same. The laboratory’s query and key are a random draw, and its scores skip the softmax.

Common misreadings

Rotary position embedding adds no position vector to the word; it turns queries and keys, and values are not turned. The score depends on positions only through m − n, exactly, for the dot product, because rotations add up; it still depends on what the two words are. “Weaker with distance” is a bound, not a promise that every pair’s score falls with distance. Nor does it let a model read texts much longer than in training on its own: extrapolating directly fails, and position interpolation, NTK and YaRN need fine-⁠tuning or stretch only so far.

Common questions · 5
Why can’t attention tell word order on its own?

Attention scores each pair of words, a dot product of one word’s query and another word’s key; a higher score means that word is looked at more (see the plate “Attention in Transformers”). The score depends only on the two words’ own numbers, not on where they stand.

01Attention Can’t See Order
What does rotary position embedding turn?

Rotary position embedding touches only queries and keys, never values. A query’s numbers are taken two at a time, each pair an arrow in a plane; for a word at position m that arrow is turned by m·θ.

03Turn, Don’t Add
How fast does each pair turn in rotary position embedding?

Each pair turns at its own speed: pair i by θᵢ = 10000^(−2i/d) per position. A LLaMA attention head has 128 numbers, so 64 pairs.

04Fast Dials and Slow Dials
Why does the score depend only on the distance between two words?

In each pair one arrow has been turned by m·θᵢ and the other by n·θᵢ; the dot product depends only on the angle between them, and their turns differ by (m − n)·θᵢ. Rotations add up, so m and n themselves do not matter, only m − n.

05Only the Distance Is Left
How does position interpolation stretch the context?

Position interpolation (PI, 2023) divides every position by the same factor, so the dials turn more slowly and the angles stay inside the trained range; it took LLaMA from 2,048 to 32,768 tokens with under 1,000 steps of fine-⁠tuning, and does much worse without them.

09Stretching the Context

Sources

  1. Su, Lu, Pan, Murtadha, Wen and Liu (2021): RoFormer: Enhanced Transformer with Rotary Position Embedding, arXiv:2104.09864 (θᵢ = 10000^(−2i/d); qₘ · kₙ depends only on n − m; only queries and keys turned; multiplicative, not additive; long-⁠term decay as an upper bound; WMT 2014 English–German BLEU 27.5 against 27.3; CAIL2019-⁠SCM)
  2. Vaswani et al. (2017): Attention Is All You Need, arXiv:1706.03762 (no recurrence and no convolution, so position must be injected; added sines and cosines; wavelengths from 2π to 10,000 × 2π)
  3. Touvron et al. (2023): LLaMA: Open and Efficient Foundation Language Models, arXiv:2302.13971 (rotary embeddings at every layer; 4,096 dimensions, 32 heads)
  4. Meta: llama/model.py (base 10,000; keys turned before they go into the KV cache; at most 2,048)
  5. Chowdhery et al. (2022): PaLM: Scaling Language Modeling with Pathways, arXiv:2204.02311 (RoPE, for better performance on long sequences)
  6. Llama Team (2024): The Llama 3 Herd of Models, arXiv:2407.21783 (base 500,000; 8K, then extended to 128K)
  7. Chen, Wong, Chen and Tian (2023): Extending Context Window of Large Language Models via Position Interpolation, arXiv:2306.15595 (extrapolating can push perplexity above 10³; positions scaled down; 2,048 → 32,768 within 1,000 fine-⁠tuning steps; plain fine-⁠tuning reached only 2,560; the decay bound may be too loose)
  8. Peng, Quesnelle, Fan and Shippole (2023): YaRN: Efficient Context Window Extension of Large Language Models, arXiv:2309.00071 (“NTK-⁠aware” changes the base; interpolation by parts, α = 1, β = 32; temperature 0.1 ln s + 1; Llama 2 extended to 128K with under about 0.1 % of the pre-⁠training data)
  9. Biderman et al. (2021): Rotary Embeddings: A Relative Revolution, EleutherAI blog

Data

The brass dial bank is this page’s metaphor, not a machine anyone built. Its pointer angles are computed from the formula of Su et al. (RoFormer, arXiv 2104.09864) with LLaMA’s head size (128) and base (10,000); the bound, the context-stretching rules and the laboratory are computed by this page (src/topics/rope/science.js, figure.js, tests/rope.test.js) from the formulas quoted in its sources. The laboratory’s query and key are a fixed random draw, not numbers from a trained model. Material presets, parts and the softbox studio of the site’s model kit are ported from AirsupHQ/machine-skill (MIT, © 2026 Air Supply Technologies Inc.).

The text, pictures and models on this page are licensed CC BY-NC 4.0: free to use and adapt for teaching and other non-commercial purposes, with credit to world.lapla.si. Third-party material listed above keeps its own licence.

Cite this plate

Laplace’s Mini World, Plate F73, “Rotary Position Embedding”, https://world.lapla.si/en/rope/, CC BY-NC 4.0, updated 2026-11-06.

Embed & API

AI · all plates