KV cache

Wiki chapters and blog posts about KV cache.

Wiki

Efficient Attention: MQA, GQA, MLA and FlashAttention

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.

Intermediate 27 min read

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

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

KV Cache and Inference Performance

Understand how the KV cache removes repeated computation from autoregressive decoding, and master the trade-offs between latency, throughput, memory and request scheduling.

Intermediate 18 min read

Prefix Caching and PD Disaggregation: Pay for Repeated Compute Once

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.

Intermediate 12 min read

LLM Cheat Sheet: Estimating Parameters, Compute, Memory and Decoding Speed

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.

Intermediate 24 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

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

vLLM and PagedAttention: Managing the KV Cache Like Paged Memory

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.

Paper notes SOSP 2023

Topics