ConceptHow an LLM Works
RoPE & Positional Encoding
At a glance
How transformers know token order, and the rotary trick behind every modern long-context model.
- Who this is for
- Engineers and technical readers learning the terms used in AI systems.
- Topics
- How an LLM Works
- Concept
In short
RoPE, rotary position embedding, is a positional encoding that rotates pairs of numbers in each query and key by an angle tied to the token's position, so attention scores depend on how far apart two tokens are. Bigcircle recommends reading a model's scaling rule and testing at the lengths the product will send before trusting a long context window, because every token past the original training length depends on how the rotations were stretched.
Key facts
Definition
Rotary embeddings turn each pair of dimensions at its own speed as position grows, and when a query meets a key the rotations cancel except for the offset between them.
How Bigcircle does it
- Bigcircle recommends reading the config for the scaling rule, the factor and the original length, because a factor of 8 over 8,192 positions means everything past 8,192 depends on the stretch.
- Test with tasks that connect facts far apart in the document, since finding one planted sentence is the easy case and comparing two distant clauses is where a stretched context degrades.
- Bigcircle recommends identical scaling settings in fine-tuning and serving, because an engine that ignores the rule serves the unstretched model past its original length and only long inputs show it.
Comparison
Absolute encodings give each slot its own vector, RoPE makes the score depend on the angle difference between positions, and ALiBi subtracts a penalty from attention scores that grows with distance.
Key number
Meta's Llama 3.1 config sets a RoPE base of 500,000 and a scaling factor of 8 over an original 8,192 positions, reaching 131,072.
RoPE, rotary position embedding, is a positional encoding: a way of telling a transformer where each token sits in a sequence. It rotates pairs of numbers inside each query and key vector by an angle that grows with the token's position, so when attention compares a query with a key, the result depends on how far apart the two tokens are. Jianlin Su and colleagues introduced it in 2021, and the Llama, Qwen, Mistral and DeepSeek families all use it.
For someone choosing a model rather than training one, RoPE matters for one reason. Every long-context claim on a RoPE model is a claim about how its rotations were stretched past the length it was first trained on, and that stretch can work well, partly, or badly at the lengths you need.
Why position has to be added#
Attention on its own has no sense of order. Shuffle the tokens, keep the same vectors, and plain self-attention sees the same set. "Dog bites man" and "man bites dog" contain the same words, so the position has to be put into the numbers somehow. The original 2017 transformer added fixed sine and cosine patterns to each token's embedding, and later models such as BERT and GPT-2 learned a vector for each position. Both say "this token is at position 184", and both cope poorly with positions beyond those seen in training.
The rotary trick#
RoPE rotates each pair of dimensions in the query and key by an angle proportional to position, with each pair turning at a different speed, from fast-turning pairs that track nearby order to slow ones that track long distances. When a query at position i meets a key at position j, the rotations cancel except for the difference between them, so the score depends on the offset: "this key is 12 tokens back", not only "this key is at slot 412". The position goes into the comparison itself rather than being added to the token's meaning.
How context gets stretched#
A model trained on 4,096 positions has only ever seen the angles those positions produce. Ask it for 128,000 and the slow-turning pairs reach angles it never learned. The fixes all change the angles so long inputs look more like what the model knows:
- Position interpolation squeezes long positions back into the trained range. Chen and colleagues extended the original LLaMA models up to 32,768 tokens with about 1,000 steps of fine-tuning. The cost is that nearby tokens end up closer together in angle than the model is used to, which can blur local order.
- NTK-aware scaling changes the rotation speeds instead, stretching the slow pairs more than the fast ones, so local order is better preserved.
- YaRN combines the two ideas per frequency and adjusts attention scaling. Its authors report reaching their results with 10 times fewer tokens and 2.5 times fewer training steps than earlier methods, and extended Llama 2 to 128k.
Meta's Llama 3.1 is a concrete case. Its published config sets a RoPE base of 500,000 and a "llama3" scaling rule with a factor of 8 over an original 8,192 positions, which is how it reaches 131,072. Expect each of these to trade a little short-range precision for range, and the trade differs by method, which is why the method matters when you choose a model. ALiBi is the main alternative to all of this: instead of rotating vectors, it subtracts a penalty from attention scores that grows with distance. Press and colleagues trained a model on 1,024 tokens that matched, at 2,048, the perplexity of a sine-pattern model trained on 2,048, while training 11% faster with 11% less memory.
Checking a long-context claim#
An advertised context window tells you the longest input the model accepts, not how well it reads at that length. Bigcircle recommends three checks before choosing a RoPE model for long-document work, because each catches a different way the stretch fails.
Read the model's config for its scaling rule, its factor and its original length. A factor of 8 over 8,192 means everything past 8,192 tokens depends on the stretch, so that is where to look for degradation.
Test at the lengths your product will actually send, with tasks that need the model to connect facts far apart, not only to find one planted sentence. Finding a single needle is the easy case. Comparing a clause on page 3 with one on page 90 is where the stretch shows.
Keep the scaling settings identical across training and serving. If you fine-tune at a longer length, train with the same scaling rule the base model used, and confirm your serving engine reads the rule from the config. A model served without its scaling rule behaves like the unstretched model past its original length, and the failure shows up only on long inputs, which short smoke tests never send.