A visual, step-by-step deep dive into how modern frontier models — DeepSeek V4 Pro, GLM 5.2, and MiniMax M3 — store, compress, and retrieve context across million-token windows without melting your GPUs.
In a standard transformer, every token attends to every previous token. Without caching, generating token N requires recomputing attention over all N-1 prior tokens — that's O(n²) work per step. For a 1M-token conversation, the compute becomes astronomical.
Watch how the KV cache grows as each token is processed — enabling fast, O(n) inference
The single most important number in LLM inference economics — and why providers desperately want you to structure your prompts for cacheability.
Prefill is compute-bound (GPU math). Decode is memory-bandwidth-bound (GPU HBM reads). Cache hits eliminate the expensive one.
Adjust the prompt composition below. The real-time chart shows cache hit vs cache miss token costs.
Every cache hit skips the quadratic prefill attention. Same GPU serves more requests per second.
Prefill is the slowest part. Skipping it makes responses feel instant — better user experience.
More requests per GPU = better unit economics. The 10× discount is a nudge, not charity.
The input text "The cat sat on the" is tokenized into individual tokens. Each token is converted into a dense vector (embedding) of dimension d_model (e.g., 4096 for DeepSeek V4, 7168 for GLM 5.2). Positional encodings (RoPE) are added so the model knows token order.
Each token embedding is projected through three learned weight matrices to produce Query (Q), Key (K), and Value (V) vectors. Q asks "what am I looking for?", K encodes "what information do I contain?", and V holds the actual content to be retrieved.
Here's the key insight: K and V for past tokens never change. Instead of recomputing them, we store them in the KV cache — a large tensor in GPU memory (HBM). Each new token only computes its own K and V, then appends to the cache.
The new token's Q attends to ALL cached K entries. Attention scores are computed: softmax(Q·K^T / √d). These scores weight the cached V entries to produce the attention output. Only O(n) work instead of O(n²).
The attention output passes through a feed-forward network (MLP) to produce the next token prediction. The new token's K and V are appended to the cache. The process repeats — each new token costs O(n) instead of O(n²), making autoregressive generation practical.
Three radically different approaches to the same problem: fitting a million tokens of KV cache into GPU memory without losing retrieval quality.
Key Innovation: Compression along the sequence-length dimension, not just the head dimension
Key Innovation: Share sparse position indices across head groups + Multi-Token Prediction
Key Innovation: Linear-scaling sparse attention with "KV outer gather Q" operator optimization
| Dimension | DeepSeek V4 Pro | GLM 5.2 | MiniMax M3 |
|---|---|---|---|
| Core Mechanism | Hybrid CSA + HCA + Sliding Window | Index Share Sparse + GQA + MTP | MiniMax Sparse Attention (MSA) |
| Compression Axis | Sequence-length dimension (token aggregation) | Head dimension (GQA) + sequence sparsity | Sequence sparsity (block partitioning) |
| Max Compression | 128× (HCA) | ~20× (combined) | N/A (sparse, not compressed) |
| KV Cache @ 1M (bf16) | 9.62 GiB | ~33 GiB (est.) | ~42 GiB (est.) |
| vs Traditional GQA | ~2% (50× smaller) | ~5% (20× smaller) | ~6% (17× smaller) |
| Quantization | FP8 cache + FP4 indexer + BF16 RoPE dims | INT8 with per-head scale factors | FP8 (standard) |
| Sparsity Pattern | Top-k sparse (CSA) + Dense (HCA) | Triple index: local + global + landmark | Block-wise top-k with contiguous gather |
| Short Context Fallback | Sliding window always active | Full attention < 32K tokens | MSA applied uniformly |
| Speculative Decoding | MTP (Multi-Token Prediction) | MTP (k=4) with 1.5–2.5× throughput | None |
| Multimodal KV Cache | Text-only | Text-only | Native image + video from Step 0 |
| Memory Management | vLLM unified page pools (3 bucket sizes) | PagedAttention + DP-aware routing | PagedAttention + contiguous gather |
All three frontier models rely on vLLM's PagedAttention for efficient GPU memory management. Here's how it works.
Traditional allocation reserves contiguous memory for the maximum possible sequence length — wasting 60–80%. PagedAttention breaks the KV cache into fixed-size blocks (like OS pages), allocating them on demand. A block table maps logical positions to physical blocks — enabling non-contiguous storage while presenting a continuous view to attention kernels.
Configure model parameters and see real-time KV cache memory estimates.