Extending a context window from 4K to 128K takes three fixes: RoPE extensions (PI, NTK-aware scaling, YaRN) for unseen positions, staged continued training on long text, and a budget for attention compute and KV cache. This page covers all three with formulas, code and two interactive tools.
Self-attention cannot see token order, so positional encodings tell the model where a token is or how far apart two tokens are: sinusoidal and learned encodings are added to the input, relative biases and ALiBi act on attention scores, and RoPE rotates queries and keys.
Linear attention, state space models (Mamba) and the gated delta rule compress history into a fixed-size state: memory stays flat as context grows, but capacity is limited by the state size. Hybrids keep a few attention layers for exact recall; Jamba's KV cache is 1/8 of Mixtral's.
DeepSeek-V4.1-Flash cuts the global KV cache to 890 bytes per token, about 1/4 of V4-Flash and 1/437 of DeepSeek-V1, with a causal encoder-decoder, cross-layer sharing in CSA2 and an FP4 main KV. This post explains the paper and rebuilds its numbers from the public configs.
MSA (MiniMax, 2026) adds an index branch to GQA that picks top-k KV blocks per group and attends exactly over only those. On a 109B MoE it matches full attention while cutting per-token attention compute 28.4× at 1M context. We verify the FLOPs figure and the coverage limits of block selection.
DeepSeek-V3.2 adds DeepSeek Sparse Attention to V3.1-Terminus by continued training: a lightning indexer picks 2048 tokens per query, so core attention drops from O(L²) to O(Lk). Then scaled-up GRPO and 1,800+ synthetic environments. I recompute the numbers and rebuild DSA in miniature.
NSA combines compressed global tokens, selected important blocks and local sliding windows. Its trainable, hardware-aligned design targets long-context speed without losing global information.
MiniMax-01 combines seven Lightning Attention layers with one softmax layer per eight, using a 456B-parameter MoE with 45.9B active parameters and reporting inference contexts up to four million tokens.
Gao et al.'s survey sorts retrieval-augmented generation into Naive, Advanced and Modular RAG. This page recounts its Tables I and II, flags two entries that disagree with the original papers, and reruns RGB's four abilities on 50 fictional companies.
Mamba makes a state space model's Δ, B and C depend on the current token (selection) and runs it with a hardware-aware scan: linear cost in length, fixed-size state at inference. We recompute its parameters and Table 3, then compare LTI and selective SSMs on selective copying.
StreamingLLM (ICLR 2024) finds that language models dump much of their attention on the first few tokens, the attention sinks; evict them from a sliding-window KV cache and perplexity explodes. Keeping 4 initial tokens fixes it with no fine-tuning, but it does not extend the context.
YaRN extends a RoPE model's context window by interpolating each rotary dimension according to how many turns it makes within the training length and adding a softmax temperature tied to ln s. 400 to 600 fine-tuning steps take Llama 2 to 128K. This post covers PI, NTK-aware and the evidence.
Liu et al. (2023) moved the answer around inside long inputs and found a U-shaped curve: accuracy is highest at the start or end and lowest in the middle. With 20 documents GPT-3.5-Turbo loses 22.0 points between its best and worst position. This post recomputes the results and discusses RAG.