JA EN
LearnHow Transformers Work
·★ MEMBER·PAPER·11 min read

How Long-Context LLMs Work — From RoPE Interpolation to Ring Attention

A "128K context window" is two different walls, knocked down by two unrelated families of tricks. This walks through positional interpolation, NTK-aware scaling and YaRN for the position wall; sliding windows and ring attention for the compute wall; and how to read a needle-in-a-haystack chart without being fooled by it.

ModalitytextTaskattention

Extending Context Window of Large Language Models via Positional Interpolation


What exactly does "128K context" fix?

The model card says "context length: 128K." So what actually happens when you push 128,000 tokens through that model? The answer is that two walls with completely different personalities are being knocked down by two unrelated sets of tricks. Conflate them and every long-context discussion turns to mush.

The first is the position wall. The model was trained at 4K or 8K. Feed it "position 90,000" — a position it has literally never seen — and the scheme for representing position wanders into unseen territory, and the output falls apart. This has nothing to do with compute; it would happen on a machine with infinite memory.

The second is the compute wall. Attention has every token look at every token, so both arithmetic and memory grow with the square of the length. Even if you solved the position wall perfectly, the matrix does not fit on one GPU.

Positional interpolation and YaRN attack the first. Windowing and ring attention attack the second. And needle tests are how you check whether either actually worked. We'll take them in that order.

The metaphor: a 12-inch ruler and a crowded desk

The position wall is a ruler problem. You are a measurer trained only on a 12-inch ruler; reading its marks is muscle memory. One day someone hands you a 36-inch plank. You have two options.

Extrapolate: trust that the marks keep going past 12 at the same spacing, and imagine them onto the plank. It's territory you've never seen, so you'll usually be wrong.

Interpolate: shrink the plank to a third of its size and measure the copy with the ruler you know. Every mark now falls in familiar range — but fine distinctions get crushed. What was a 1mm gap becomes a 0.33mm gap, and two adjacent points start looking like one.

Essentially every long-context positional method is an argument about extrapolating versus interpolating, and about which parts get which treatment. The compute wall, by contrast, is a desk-space problem: you cannot spread everyone's exam papers on a single desk. The two families look nothing alike because the problems don't either.

Recap: RoPE rotates in proportion to position

Nearly every modern open LLM uses RoPE (rotary position embedding). Positional Encoding from Scratch covers it properly; here we need exactly one fact.

RoPE splits the Query and Key vectors into two-dimensional pairs and rotates each pair by an angle proportional to the position mm. How fast the ii-th pair turns is set by:

θi=b2i/d,λi=2πθi\theta_i = b^{-2i/d}, \qquad \lambda_i = \frac{2\pi}{\theta_i}
(1)

Here bb is a constant called the base (10000 in most models), dd is the head dimension, θi\theta_i is the angle added per token, and λi\lambda_i is how many tokens it takes that pair to complete one full turn — its wavelength. In words, equation (1) says only this: each pair spins at its own rate, and the larger ii is, the slower it spins.

The payoff is that when you take the dot product of a Query at position mm with a Key at position nn, mm and nn individually cancel and only a function of the difference mnm-n survives. That is why an operation as crude as "renumber the positions" is even meaningful.

FIG 1Rotate two vectors and watch the dot product. RoPE turns Q and K each in proportion to their position, so the dot product depends only on the difference between the two rotations — and interpolation is just shrinking the step size of that rotation

Positional interpolation — squeeze the marks inward

The simplest fix is Position Interpolation (PI). Let LL be the trained length and LL' the target, pick a scale s=L/Ls = L'/L, and shrink the position numbers themselves:

m=msm' = \frac{m}{s}
(2)

In words, equation (2) says: treat position 90,000 as position 2812.5 when s=32s=32. The fact that it needn't be an integer is the whole point — RoPE's rotation angle is continuous, so fractional positions work fine. Now every rotation angle lands inside the trained range, and no unseen angle ever appears.

This is the shrink-the-plank move from the metaphor. You have enough marks now, but they mean less. In practice a model right after PI performs worse than before, which is why the original paper proposes it together with a short fine-tuning run.

Read it in wavelengths and the damage becomes obvious

What PI breaks becomes clear the moment you lay out the wavelengths λi\lambda_i from equation (1). Pairs with small ii have wavelengths just a few tokens long, and they are what distinguishes one word from its neighbor. Pairs with large ii have wavelengths of tens of thousands of tokens and never complete a single turn within the trained length LL.

PI shrinks all of them by the same 1/s1/s. But only the second group was in trouble. Long-wavelength pairs saw only a slice of the angle range during training, so extrapolating pushes them somewhere unseen. Short-wavelength pairs, meanwhile, spun tens of thousands of times during training and have covered the entire angle range — for them there is no unseen territory to extrapolate into. Shrinking them anyway costs you local positional resolution for nothing.

Which hands you the fix. Leave the fast pairs alone; only shrink the slow ones.

The first implementation of that idea was community-invented NTK-aware scaling, and it is one line. Don't touch the positions; multiply the base by . Raising the base slows every pair down, but unevenly across : the fast pairs barely change while the slow pairs stretch a lot. You get roughly the distribution you wanted

What's behind this

§

Members-only from here

371 walkthroughs, 26 textbook chapters, 48 student units and 6 close readings — all included for $4.99/mo, with three new explainers every day. Cancel any time; access runs to the end of the period.

Already a member? Sign in to keep reading

References

  1. Extending Context Window of Large Language Models via Positional Interpolation. arXiv:2306.15595Paper page·PDF
  2. YaRN: Efficient Context Window Extension of Large Language Models. arXiv:2309.00071Paper page·PDF
  3. Efficient Streaming Language Models with Attention Sinks. arXiv:2309.17453Paper page·PDF
  4. Ring Attention with Blockwise Transformers for Near-Infinite Context. arXiv:2310.01889Paper page·PDF
  5. Lost in the Middle: How Language Models Use Long Contexts. arXiv:2307.03172Paper page·PDF
  6. RULER: What's the Real Context Size of Your Long-Context Language Models?. arXiv:2404.06654Paper page·PDF

This article is written from the source paper above. Where they differ, the original is authoritative.

Comments

Sign in to comment