Long context: extension and its limits
Explain how RoPE scaling or interpolation extends the context window. Explain why models still use information unevenly across long contexts (for example 'lost in the middle').
Builds on: Rotary position embeddings (RoPE), Quadratic cost of attention in context length
What the animation says
Every pair is a dial
This model was trained on prompts of up to four thousand and ninety-six tokens. Rotary position encoding has no table of positions, so you can feed it thirty-two thousand tokens. It runs, but quality collapses past the training length. To see why, picture each rotation pair as a dial. With every token, it turns by its own fixed angle. The fastest pair turns one radian per token. The slowest turns about a ten-thousandth of a radian. Past four thousand and ninety-six tokens, which dials point to angles that training never showed?
Unseen angles, then squeeze
The slow dials. The fast dial spun hundreds of times in training and saw every angle. The slowest dial only reached about zero point four seven radians by the last trained token. Double the window to eight thousand one hundred ninety-two tokens, and it reaches about zero point nine five. Training never covered that arc. Position interpolation fixes this: divide every position by the stretch factor, here two. Now the last token lands back at zero point four seven. Familiar. But that division hits every dial. In the fastest pair, how far apart are two neighbours now?
Squeeze only the slow pairs
Half a radian. Neighbours that sat one radian apart now crowd together, so fine word order gets blurrier. Base scaling raises the rotation base instead. The slowest pair is squeezed by the full factor, and the fastest is untouched. So the slow dial still returns to zero point four seven, while fast neighbours stay one radian apart. YaRN goes further. It keeps fast pairs, interpolates slow ones, blends in between, and sharpens attention scores slightly. Whichever you pick, a short run of training on long text finishes the job.
Legal isn't well used
Rescaling makes long inputs legal. It doesn't make them well used. Softmax splits attention among every key a query can see. Give one relevant key a score lead of five over equal distractors. At a thousand tokens, that key gets about thirteen percent of the weight. At ten thousand, about one and a half percent. At a hundred thousand tokens, what share does it get?
Lost in the middle
About fifteen hundredths of a percent. Every fact gets harder to find as the context grows. Position makes it uneven. Training text mostly rewards attending to recent tokens, which favours the end. And every later query sees the start, so models learn to attend there heavily. Now plot how well a buried fact gets used against where it sits. Tap the spot where a fact is used worst.
Unseen angles, and squeezing them back
Three ways to rescale RoPE
Stretching 4,096 to 16,384 tokens
Why long inputs are still used unevenly
import numpy as np
delta = 5.0 # score lead of the one relevant key over equal distractors
for T in [1_000, 10_000, 100_000]:
w = np.exp(delta) / (np.exp(delta) + (T - 1))
print(T, round(w, 4)) # 0.1293, 0.0146, 0.0015
Remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses