arXiv:2410.10819cs.CL2024-10ICLR被引 308

通过分离关键头与普通头,显著降低长文本推理的内存与延迟。

DuoAttention: Efficient Long-Context LLM Inference with Retrieval and Streaming Heads

  • 区分检索头与流式头,仅对前者缓存完整键值,后者用固定长度轻量缓存。
  • 在保持长文本能力下,内存减少2.55倍,解码速度提升2.18倍。
  • 适合部署超长上下文大模型,尤其支持单卡运行330万词长的Llama-3-8B。

长上下文大语言模型(LLM)的部署面临显著的计算与内存挑战。缓存所有注意力头的键值(KV)状态会消耗大量内存。现有KV缓存压缩方法或损害长文本能力,或效率提升有限。本文发现,仅少数注意力头(称作检索头)对处理长上下文至关重要,需对所有词元保持完整注意力;而其余头主要关注近期词元及注意力汇聚点(称作流式头),无需完整注意力。基于此,我们提出DuoAttention框架:仅对检索头使用完整KV缓存,对流式头采用轻量、固定长度缓存,从而在不损失长上下文能力的前提下,同时降低解码与预填充的内存占用和延迟。DuoAttention采用基于合成数据的轻量优化算法准确识别检索头。实验显示,该方法在MHA与GQA模型上分别实现最高2.55倍与1.67倍的长上下文推理内存压缩,解码速度提升2.18倍与1.50倍,预填充加速1.73倍与1.63倍,且精度损失极小。结合量化技术,可实现单个A100 GPU上对Llama-3-8B进行330万词长的解码。代码已开源。

原文摘要 · Abstract (English)

Deploying long-context large language models (LLMs) is essential but poses significant computational and memory challenges. Caching all Key and Value (KV) states across all attention heads consumes substantial memory. Existing KV cache pruning methods either damage the long-context capabilities of LLMs or offer only limited efficiency improvements. In this paper, we identify that only a fraction of attention heads, a.k.a, Retrieval Heads, are critical for processing long contexts and require full attention across all tokens. In contrast, all other heads, which primarily focus on recent tokens and attention sinks--referred to as Streaming Heads--do not require full attention. Based on this insight, we introduce DuoAttention, a framework that only applies a full KV cache to retrieval heads while using a light-weight, constant-length KV cache for streaming heads, which reduces both LLM's decoding and pre-filling memory and latency without compromising its long-context abilities. DuoAttention uses a lightweight, optimization-based algorithm with synthetic data to identify retrieval heads accurately. Our method significantly reduces long-context inference memory by up to 2.55x for MHA and 1.67x for GQA models while speeding up decoding by up to 2.18x and 1.50x and accelerating pre-filling by up to 1.73x and 1.63x for MHA and GQA models, respectively, with minimal accuracy loss compared to full attention. Notably, combined with quantization, DuoAttention enables Llama-3-8B decoding with 3.3 million context length on a single A100 GPU. Code is provided in https://github.com/mit-han-lab/duo-attention.

长上下文推理优化注意力机制模型部署

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。