All courses

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

▶ Watch this lesson Start the whole course

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.

Can a 4k-trained model read 100k tokens?

A model is trained with context length $T_{train}$, say 4,096 tokens. RoPE has no table of positions, so you can feed it 32,768 tokens, but quality collapses past $T_{train}$. How do labs stretch the window cheaply? And why does a model that accepts long prompts still use a fact buried in the middle worse than one at the start or end?

Unseen angles, and squeezing them back

Each RoPE pair rotates at its own frequency. Fast pairs cycle many times within $T_{train}$, so training covered all their angles. Slow pairs never finish a turn. Offsets beyond $T_{train}$ give them angles the model never saw, and attention scores become unreliable. The fix is to rescale positions so long offsets land on familiar angles. Squeeze all pairs (interpolation) or mainly the slow ones (base scaling). Then train briefly on long text.

Three ways to rescale RoPE

Pair $i$ ($i=0,\dots,d_{head}/2-1$) rotates by $t\,\theta_i$, with $\theta_i=b^{-2i/d_{head}}$ and base $b=10000$. Let $s=T_{new}/T_{train}$. - **Position interpolation (PI):** use $t/s$, so the angle is $t\theta_i/s$. Every offset fits the trained range, but neighbours get $s\times$ closer in every pair. - **Base scaling (NTK-aware):** $b'=b\,s^{d_{head}/(d_{head}-2)}$. The slowest pair's $\theta$ is divided by $s$; the fastest is unchanged. - **YaRN:** keep fast pairs, interpolate slow pairs by $s$, blend smoothly in between, and multiply attention scores by a factor slightly above 1 so attention stays as sharp as in training.

Stretching 4,096 to 16,384 tokens

Take $d_{head}=128$, $T_{train}=4096$, $T_{new}=16384$, so $s=4$. Slowest pair: $\theta_{63}=10000^{-126/128}\approx1.15\times10^{-4}$ rad/token. Largest trained angle: $4096\times1.15\times10^{-4}\approx0.47$ rad. At position 16,384 it would reach $1.89$ rad, which training never covered. With PI: $16384\times1.15\times10^{-4}/4\approx0.47$ rad, which is familiar. The cost: in the fastest pair ($\theta_0=1$), neighbours are now 0.25 rad apart, not 1. Base scaling also brings the slowest pair back to about 0.47 rad, but keeps fast neighbours 1 rad apart.

Why long inputs are still used unevenly

Rescaling makes long inputs legal, not well used. Softmax splits weight among all keys a query can see, so a fixed score lead buys less as $T$ grows (run it). That makes every fact harder to find. Position adds more. Training text mostly rewards attending to recent tokens, which favours the end. Every later query sees the start, and models learn to attend there heavily. The middle gets neither boost. The observed result is a U-shape called **lost in the middle**: facts at the start or end are used better than facts in the middle.
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

Context extension rescales RoPE positions so long offsets map onto angles seen in training. PI rescales all pairs; base scaling and YaRN rescale mostly the slow pairs. A short run of training on long text follows. This makes long inputs usable but not evenly used. Models show a U-shaped 'lost in the middle' curve. The leading explanations are softmax dilution, recency-heavy training data and heavy attention to the start.

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.