Ask what should change when the equation is manipulated, then let the visualization test that expectation.
Foundation Lab
Long Context Engineering: RoPE Scaling, KV Compression & Memory Optimization
RoPE scaling methods (YaRN, LongRoPE) are dominant route to extending context windows without architecture change—how 32k→128k→256k→1M+ happens
\text{Memory}_{KV} = 2 \times L \times H \times T \times d_h \times \text{bytes}Selected Foundation Object
Keep the equation fixed; move through the evidence.
Failure mode isn't "model forgets"—it's phase/angle OOD: RoPE uses wavelengths, beyond training range you hit unseen rotations
\text{Memory}_{KV} = 2 \times L \times H \times T \times d_h \times \text{bytes}Use the runnable panel, the key equation, and canonical papers as separate forms of evidence for the same object.
The useful learning product is the reusable mechanism you can carry into another model, paper, or engineering tradeoff.
This atlas page keeps the working demo; the domain notebook carries the fuller Intuition -> Math -> Code -> Demo sequence.
Why It Matters for Modern Models
- RoPE scaling methods (YaRN, LongRoPE) are dominant route to extending context windows without architecture change—how 32k→128k→256k→1M+ happens
- KV cache becomes bottleneck at very long contexts—KVQuant enables ~1M context on single A100-80GB via quantization to 3-4 bits
- Position encoding extrapolation is "angle OOD"—RoPE dimensions have wavelengths, beyond training you hit unseen rotations ("critical dimensions")
- After RoPE basics (#18) and KV cache/FlashAttention (#19), this explains how frontier models actually break the pretraining ceiling
- Memory math becomes destiny—at long contexts, KV storage and bandwidth dominate compute; quantization/compression is not optional
What Tutorials Skip
What is still poorly explained in textbooks and papers:
- Failure mode isn't "model forgets"—it's phase/angle OOD: RoPE uses wavelengths, beyond training range you hit unseen rotations
- Long context breaks attention in two ways: (a) position encoding extrapolation, (b) attention entropy/softmax temperature drift as T grows
- Non-uniform scaling is key insight—high-frequency RoPE dims (fine detail) need less rescaling than low-frequency (global position)
- KV compression tradeoff: quantization/pruning saves memory but degrades long-range retrieval—you're trading capacity for length
- Serving implications: KV cache memory >> compute at long contexts, changes deployment economics (memory-bound not compute-bound)
Interactive Visualization
Core Math (Optional Deep Dive)
If you want intuition first, start with the key equation and the visualization. Come back here for the full walkthrough.
Long context is a two-front war: (1) position extrapolation beyond pretraining, and (2) KV cache memory explosion. Frontier models use RoPE scaling + aggressive KV compression.
RoPE injection (complex form, key idea: relative phase):
Rotation encodes position; attention sees (relative).
YaRN "NTK-by-parts" wavelength scaling:
Non-uniform scaling: keep high-frequency dims, interpolate low-frequency → better extrapolation.
KV cache memory vs sequence length:
where layers, heads, tokens, head dimension—grows linearly in , dominates at long contexts.