The central problem: RoPE doesn't scale
You train your model on 4K token sequences. Everything goes well. Then you give it a 16K token sequence at inference. Catastrophe: quality collapses completely.
Why? RoPE (previous lesson) encodes positions via rotations at specific frequencies. During training, the model learned to interpret these rotations for positions 0 to 4095. At position 16000, the rotations take values the model has never seen. It's like asking someone to read a clock that suddenly makes 40 revolutions per hour instead of 1: the hands are moving, but it no longer means anything.
Imagine a radio trained to pick up frequencies 100-200 MHz. At 500 MHz, it receives nothing but noise. That's exactly what happens to RoPE beyond its training window: the positional frequencies become noise for the model.
Solution 1: Position Interpolation (PI)
The most direct approach: compress positions to fit them within the known range.
In plain terms: every position is multiplied by the ratio between the length seen in training () and the targeted length (). Trained on 4K, you test on 16K? The ratio is 1/4: position 16000 becomes 4000. All rotations stay within the range the model knows.
Advantage: it works right away, it's just a multiplicative factor on positions.
Problem: you crush the resolution. Imagine taking a ruler graduated from 0 to 100 cm and compressing it to measure from 0 to 400 cm. The markings are now 4 times closer together: you lose precision for distinguishing small distances. Concretely, neighboring tokens become artificially closer, and the model loses its ability to finely distinguish local positions.

With a 4x factor, two adjacent tokens go from "distance 1" to "distance 0.25." The high frequencies of RoPE (those encoding local relationships between neighboring tokens) suffer enormously. The model confuses consecutive tokens.
Solution 2: NTK-aware (smarter)
A word about the name first: NTK stands for Neural Tangent Kernel, the theoretical framework this trick's authors drew it from, just remember "the NTK method," the reasoning below needs none of the theory.
The key observation: in RoPE, low frequencies encode global positions (where am I in the document?) while high frequencies encode fine locality (which token is just before or after me?).
The NTK-aware idea: instead of uniformly crushing all positions (like PI), modify RoPE's base to affect frequencies differently:
where (alpha) is the extension factor (for example 4 to go from 4K to 16K), is the dimension of one attention head, and "base" is the number (10,000 by default) that sets the spacing of RoPE's frequencies. The new base is simply the old one, inflated by a factor slightly above .
Increasing the base reduces all frequencies, but low frequencies (already small) are proportionally more stretched than high ones. Result: high frequencies maintain good local resolution, while low frequencies stretch to cover a longer context.
High frequencies preserve their ability to distinguish nearby tokens. Low frequencies adapt to cover more ground. You keep local precision while gaining global range. It's a much better trade-off than uniform interpolation.
Dynamic NTK: automatic adjustment
Instead of fixing the base once and for all, it's computed dynamically based on the actual length of the current context:
Same formula as above, but the fixed extension factor is replaced by the ratio between the actual context length and the training length. For a 5K token sequence, the base adjusts slightly. For 20K, it adjusts much more. The advantage: instead of collapsing abruptly beyond the training length, the model degrades progressively. That's much more graceful.

Solution 3: YaRN (the modern approach)
YaRN (Yet another RoPE extensioN, the authors own the self-deprecation) intelligently combines several techniques by treating each frequency according to its needs:
YaRN: Efficient Context Window Extension of Large Language Models- High frequencies (short wavelength): left untouched, they already work well
- Mid frequencies: progressive linear interpolation
- Low frequencies (long wavelength): full NTK scaling
- Temperature correction: without correction, attention becomes too "sharp" (concentrated on too few tokens) as the sequence gets longer. YaRN adjusts the attention scale to compensate.
It's like an audio equalizer: instead of applying the same treatment to all frequencies, you adjust each band independently based on its needs.

In the paper, LLaMA 2 goes from 4K to 64K, even 128K tokens, with only ~400 fine-tuning steps. Perplexity stays under control up to 32x the original length (4K × 32 = 128K). That's a remarkable effort/result ratio.
Other approaches
LongRoPE
Uses an automatic search to find the best interpolation factors per dimension. The paper reaches a 2-million-token context (a factor of ~500 from 4K) but requires more fine-tuning and compute for the search phase.
Self-Extend
No fine-tuning needed! Combines local attention (normal positions) with "grouped" attention (positions divided by a factor). Relies on the fact that the model already handles local positions well and only needs an approximation for distant positions.
Long Training (the brute force method)
Retrain directly on long sequences with a RoPE base of 500K. It's slow and expensive, but gives the best results because the model truly learns to handle long distances.
Comparison table
The orders of magnitude below are indicative, each paper measures on its own models and settings:
| Method | Fine-tune? | Typical extension | Quality | Cost |
|---|---|---|---|---|
| Position Interp | Yes, short | up to ~16x | Good | Low |
| NTK-aware | No/Yes | ~4-16x | Good | Low-Medium |
| Dynamic NTK | No | ~4-8x | Decent | Very low |
| YaRN | Yes, minimal | up to ~32x | Excellent | Medium |
| LongRoPE | Yes, short | ~500x (2M tokens) | Very good | Medium-High |
| Long Training | Yes, massive | No fixed limit | Optimal | Very high |
In the vast majority of cases: YaRN. The quality/effort ratio is unbeatable. For extreme extensions (millions of tokens): LongRoPE. For immediate use without any fine-tuning: Dynamic NTK (acceptable for 2-4x extension).
Evaluating a context extension
Needle in a Haystack
The classic test: insert specific information at a random position in a very long text, then ask the model to retrieve it. If the model finds it regardless of position, the extension works. If not, it's a bluff: the model "accepts" the long context but doesn't really exploit it.
RULER and advanced benchmarks
More demanding tests that don't just check whether the model retrieves information, but whether it can reason about information scattered throughout a long context. Extensions through simple interpolation often fail at these more realistic tests.
Context extension is both a science and an art. The techniques are based on relatively simple modifications to RoPE parameters, but hyperparameter tuning is delicate. YaRN is the current sweet spot: minimal fine-tuning, excellent results.
1. Position Interpolation squashes all positions by a factor of 4 to fit 16K tokens into a 4K range. What is the price?
2. Rather than squashing all positions uniformly, the NTK method inflates RoPE's base. What does that achieve?
3. In what way does YaRN behave like an audio equalizer?
4. What is Dynamic NTK's distinctive advantage?
5. What does the « needle in a haystack » test verify?