Long context

Wiki chapters and blog posts about Long context.

Wiki

Long Context: Position Extrapolation, Training Recipes and Cost

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.

Advanced 40 min read

Positional Encoding Explained: Absolute, Relative and RoPE

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.

Intermediate 26 min read

State Space Models and Hybrids: Trading the KV Cache for a Fixed-Size State

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.

Advanced 47 min read

Blog posts

DeepSeek-V4.1-Flash: Getting the Global KV Cache Down to 890 Bytes per Token

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.

Paper notes arXiv 2026

MiniMax-01: Lightning Attention and million-token context

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.

Paper notes arXiv 2025

Mamba: linear-time sequence modeling with selective state spaces

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.

Paper notes arXiv 2023

YaRN: Extending RoPE Models to a 128K Context Window with Light Fine-Tuning

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.

Paper notes arXiv 2023

Lost in the Middle: Models Use the Start and End of a Long Context Best

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.

Paper notes TACL 2023

Topics