← Back to AI Infrastructure Mind Map
中文·English
🖥️ AI InfrastructureID: kv-cache-optimization

KV Cache Optimization

KV 缓存优化
🎯Core Definition
KV Cache optimization compresses the K/V tensors cached per layer during autoregressive inference to break the memory wall of long contexts and large batches. Memory formula: 2×2×L×H×d×s×b2 \times 2 \times L \times H \times d \times s \times b bytes (K and V per token × 2 bytes for FP16 × layers × heads × head dim × sequence length × batch size). Four main lines: ① structural compression — GQA shares one KV head group across multiple Q heads (e.g. 64 → 8, memory /8); MLA (DeepSeek-V2) compresses each layer's K/V into a low-rank latent vector ctKV=WDKVhtc_t^{KV} = W^{DKV} h_t, and at inference the WUKW^{UK} projection is absorbed into WQW^Q and the output projection, caching only the latent — vs MHA's 86GB, KV memory drops to 5.1GB (−93.3%); ② KV quantization — storing K/V in INT8/FP8 halves memory with little accuracy loss; ③ StreamingLLM — keeping only the "attention sink" (first 4 tokens) plus a sliding window, so long-session memory drops from O(s)O(s) to O(window)O(\text{window}); ④ H2O sparsity — retaining only heavy-hitter tokens by cumulative attention score, with dynamic replacement.
💡Use Cases
long-context (32K/128K) online inference, multi-turn chatbots, and the decode side of PD separation; interview favorites: the KV cache memory formula, GQA vs MHA, how MLA saves memory, and how to survive memory blowup on long conversations.
⚡Key Problems Solved
with large batches and long sequences, KV cache grows far faster than weights (each new token adds 2×2×L×H×d2 \times 2 \times L \times H \times d bytes), directly capping concurrency and context length. GQA/MLA cut an order of magnitude structurally (MLA: 86GB → 5.1GB), KV quantization halves it again, and StreamingLLM/H2O make memory depend on the window instead of full length — combined, these let the same GPU serve several times more concurrent connections and longer contexts.
🎯Core Interview Exam Points
Fundamentals · Production Scenarios · Deep Dive
Core Def
Explain the core definition, underlying mechanism/math, and key principles of KV Cache Optimization.
Use Cases
In production engineering, what are the primary use cases for KV Cache Optimization and what core pain points does it solve?
🔥 High-Frequency Deep-Dive Questions
1
Write the KV cache memory formula 2×2×L×H×d×s×b2 \times 2 \times L \times H \times d \times s \times b and explain each factor; roughly how many GB does a 70B (e.g. 80 layers, 64 heads, head dim 128) use at 8K context with batch 1?
2
GQA vs MHA/MQA: why sharing KV heads (e.g. 64 → 8) barely hurts accuracy (attention head redundancy) and how much memory it saves?
3
MLA's low-rank compression: how does ctKV=WDKVhtc_t^{KV} = W^{DKV} h_t compress per-layer KV into a latent; why can the WQW^Q/WUW^U projections be fused at inference; the 86GB → 5.1GB numbers?
4
KV quantization (INT8/FP8): why are K/V easier to quantize than activations (stable value ranges); the cost of halving memory and how it composes with GQA/MLA?
5
Why must StreamingLLM keep the attention sink (softmax normalization stability, first 4 tokens); how does H2O keep heavy-hitter tokens by cumulative attention score, and how does it differ from sliding windows?
Updated 2026-08-12
🎯
Test Your Knowledge: Practice Questions for "KV Cache Optimization"
Single choice pitfall questions with instant feedback and mistake tracking.
🚀 Start Card Practice ➔
← Previous CardFlashAttention Kernel & Online SoftmaxNext Card →Prefill/Decode Disaggregation

🔗 More AI Infrastructure Knowledge Cards

Activation Memory EstimationAgent Runtime (cross-module)Autoscaling & CostCheckpointing & Recovery