Back to LLMs Mind Map
中文·English
LLMsID: kv-cache-management

KV Cache Management

KV Cache 管理
🎯Core Definition
at autoregressive decode time every new token must attend to all history, so past K/V are cached to avoid recomputation: cache size =22LHdsb= 2 \cdot 2 \cdot L \cdot H \cdot d \cdot s \cdot b (2 bytes per fp16 element × K and V × layers × heads × head dim × seq len × batch); LLaMA-3 70B at s=8192s = 8192, b=32b = 32 needs about 687GB — far exceeding its 140GB of weights — and about 86GB with GQA (8 groups). Management techniques: PagedAttention (vLLM) manages the cache via page tables in 16-token blocks to remove fragmentation; StreamingLLM uses attention sinks plus a rolling window; PD separation runs prefill and decode on separate instances.
💡Use Cases
every autoregressive inference and long-context service; a favorite interview exercise is estimating KV memory from layers/heads/dim/seq and comparing the impact of GQA/MLA/quantization (cross-module — see the AI_Infra KV Cache & PagedAttention guide).
Key Problems Solved
the cache grows linearly with sequence length and is the #1 serving memory cost, while fixed pre-allocation wastes 50-60%+ to fragmentation; PagedAttention decouples occupancy from position, cutting fragmentation to near zero and boosting throughput 2-4x (up to 24x vs FasterTransformer in the vLLM paper); StreamingLLM bounds per-layer cache to a fixed window for very long streaming dialogues; PD separation keeps compute-heavy prefill from stalling decode.
🎯5 High-Frequency Exam Points
1
Meaning of each term in 22LHdsb2 \cdot 2 \cdot L \cdot H \cdot d \cdot s \cdot b? A concrete worked example?
2
Why does the KV cache exist only at inference, not training (parallel full-sequence training vs sequential generation)?
3
How PagedAttention works? How do block size and page tables reduce fragmentation?
4
What are StreamingLLM's attention sinks, and why are the first tokens special?
5
How much do GQA, MLA and INT4 quantization each shrink the KV cache (687 → 86GB etc.)?
Updated 2026-08-12
🎯
Test Your Knowledge: Practice Questions for "KV Cache Management"
Single choice pitfall questions with instant feedback and mistake tracking.
🚀 Start Card Practice
Previous CardSpeculative Decoding (cross-module)Next CardScaled Dot-Product Attention

🔗 More LLMs Knowledge Cards

Agent & Tool CallingAlignment Tax & Preference DataAttention Variants MHA/MQA/GQABenchmarks MMLU/GSM8K