Files
myWiki/concepts/kv-cache-bottleneck.md

36 lines
1.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# KV 缓存内存瓶颈
**自回归推理中的核心内存瓶颈**KV 缓存的线性增长严重限制 LLM 推理效率。
## 问题定义
自回归推理中,每个新生成的 Token 需要与所有历史 Token 的 KV 进行注意力计算。KV 缓存的大小为:
$$\text{KV Cache Size} = 2 \times n \times d \times \text{layers} \times \text{precision}$$
其中 n 为已生成的 Token 数d 为模型维度。当上下文长度达到 1M 时KV 缓存可能达到数百 GB。
## 瓶颈表现
- **批处理受限**: KV 缓存占用大量 GPU 内存,限制并行推理的 batch size
- **长上下文成本**: 上下文每翻倍KV 缓存也翻倍
- **吞吐量下降**: 内存压力导致推理吞吐量大幅下降
## 解决方案矩阵
| 策略 | 代表方法 | 缓存缩减 | 质量影响 |
|------|---------|---------|---------|
| 结构压缩 | MLA ([[multi-head-latent-attention]]) | 10-20x | 极小 |
| 头共享 | GQA ([[grouped-query-attention]]) | ~8x | 轻微 |
| 量化压缩 | KVQuant | 4-8x | 可控 |
| 选择性淘汰 | H2O/SnapKV ([[attention-sinks]]) | 2-5x | 中等 |
| 低秩投影 | Palu, ReCalKV | 3-5x | 轻微 |
## 相关概念
- [[multi-head-latent-attention]] — MLA: 结构压缩(最大杠杆)
- [[grouped-query-attention]] — GQA: 头共享方案
- [[kvcache-transfer]] — KVCache 跨节点传输
- [[attention-sinks]] — 缓存淘汰策略
- [[llm-attention-survey-2026]] — 综述参考