Attention has two bottlenecks: the KV cache that decoding rereads at every step, and the N×N score matrix of training and prefill. MQA, GQA and MLA shrink the KV cache; FlashAttention cuts memory traffic with tiling and online softmax, and its output is identical to standard attention.
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.
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.
Every decode step reads all the weights, so putting several requests in one batch raises throughput almost for free. This page measures concurrency, continuous batching, paged KV caches and prefix caching with llama.cpp to show how one GPU serves many requests.
Understand how the KV cache removes repeated computation from autoregressive decoding, and master the trade-offs between latency, throughput, memory and request scheduling.
Prefix caching reuses the KV cache of system prompts, shared documents, and chat history across requests (RadixAttention, CacheBlend, LMCache). PD disaggregation places compute-bound prefill and bandwidth-bound decoding on different hardware; DistServe, Splitwise, and Mooncake show when it pays off.
A serving system has to balance time to first token, time per output token and total throughput. This page measures where one request's time goes with llama.cpp, shows why laptop benchmarks need a warm-up, and compares llama.cpp, Ollama, vLLM and SGLang.
One page for the estimates you reuse most: counting parameters, training compute (about 6ND), memory for weights, training state and the KV cache, the bandwidth bound on decoding speed, and the LoRA and pass@k formulas, each with a standard-library script checked against published numbers.
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.
DeepSeek-V2 (DeepSeek-AI, 2024) is a 236B-parameter MoE model that activates 21B per token: Multi-head Latent Attention (MLA) shrinks the inference cache and DeepSeekMoE lowers training cost. This post rechecks 93.3%, 236B and 21B, and verifies MLA's absorption and decoupled RoPE numerically.
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.
The vLLM paper (SOSP 2023) brings operating-system paging to the KV cache: fixed-size blocks, on-demand allocation, a block table, copy-on-write sharing. This post checks the paper's numbers and shows the real range behind the headline 2-4x, what it was compared with, and where it stops applying.
GQA lets groups of query heads share one key/value head, between multi-head (MHA) and multi-query attention (MQA); MHA checkpoints convert by mean-pooling heads plus about 5% extra pretraining. On T5-XXL it nears MHA quality at close to MQA speed. We check Table 1 and compute Llama 3's KV cache.