Before you move a control in the demo, say what you expect to change.
Foundation reference
Long Context Engineering: RoPE Scaling, KV Compression & Memory Optimization
Rescaling RoPE frequencies lets a pretrained model use a longer context after a short fine-tune instead of full retraining; position interpolation, YaRN and LongRoPE are published methods of this kind (Chen et al., 2023; Peng et al., 2024; Ding et al., 2024).
Study guide
Where to start
Why does a model fail beyond its training length because of unseen rotation angles rather than because it "forgets"?
How to study this pagePredict · Check · Generalize · Continue
Compare what the demo shows with the key equation and with the papers cited below.
Identify the part of the idea that survives a change of notation or model, so you can recognize it in another paper or system.
The newer notebook covers the same concept and has an interactive demo.
Why It Matters for Modern Models
- Rescaling RoPE frequencies lets a pretrained model use a longer context after a short fine-tune instead of full retraining; position interpolation, YaRN and LongRoPE are published methods of this kind (Chen et al., 2023; Peng et al., 2024; Ding et al., 2024).
- At long contexts the KV cache becomes the memory bottleneck: KVQuant quantizes it to about 3 bits and reports serving LLaMA-7B with a context of up to 1 million tokens on a single A100-80GB GPU (Hooper et al., 2024).
- Failures beyond the training length are largely a matter of unseen rotation angles: low-frequency RoPE dimensions reach angles they never reached during training.
- Long context builds directly on RoPE and on KV caching: the first decides whether distant positions can be told apart, the second whether their keys and values fit in memory.
- When generating at long contexts, reading the KV cache from memory, rather than arithmetic, often sets cost and throughput, which is why cache quantization and compression matter in practice.
Questions to Think About
- Apart from position extrapolation, how does a growing number of keys change the softmax, spreading attention more thinly as the context grows?
- Why should high-frequency RoPE dimensions, which encode fine local order, be rescaled less than low-frequency ones, which encode coarse position?
- What does quantizing or evicting KV-cache entries cost in long-range retrieval accuracy, and when is that trade worth making?
- Why is processing a long prompt (prefill) mostly limited by computation while generating each new token is mostly limited by memory bandwidth, and how does a long context change the second?
Interactive Demos
Core Math
If you prefer intuition first, try the demos above, then come back for the math.
Extending a model's context window raises two separate problems: positions beyond those seen in pretraining, and a key–value (KV) cache whose memory grows with every token.
RoPE as a rotation per frequency:
Pair the coordinates of each query and key vector into two-dimensional blocks and treat block as a complex number. RoPE (rotary position embedding) rotates block at position by the angle :
The attention score depends on the positions only through , the relative offset. Block has wavelength . Low-frequency blocks have wavelengths longer than the training context, so beyond it they produce angles the model never saw.
Position interpolation and YaRN:
Position interpolation divides every position by the extension factor , which keeps all angles inside the trained range but squeezes nearby positions together (Chen et al., 2023). YaRN's "NTK-by-parts" rule interpolates only the low-frequency blocks:
Here is the original context length and set the ends of the ramp. High-frequency blocks () keep their wavelengths and so their fine local resolution; low-frequency blocks () are stretched by . YaRN also rescales the attention logits by a temperature that depends on (Peng et al., 2024).
KV cache memory for one sequence:
with layers, key–value heads, tokens and head dimension ; the factor 2 counts keys and values. Memory grows linearly in , so at long contexts the cache, not the weights, limits how many sequences fit on a device.