[论文解读] MInference 1.0: Accelerating Pre-filling for Long-Context LLMs via Dynamic Sparse Attention
MInference 1.0 通过利用通过注意力矩阵分析识别出的动态稀疏注意力模式(A型、反斜杠型和分块稀疏型),加速了长上下文大语言模型的预填充推理。该方法在单张 A100 GPU 上对 100 万 token 的提示实现高达 10 倍的加速,且无需模型微调,同时在 Needle In A Haystack 和 InfiniteBench 等基准测试中保持了高精度。
The computational challenges of Large Language Model (LLM) inference remain a significant barrier to their widespread deployment, especially as prompt lengths continue to increase. Due to the quadratic complexity of the attention computation, it takes 30 minutes for an 8B LLM to process a prompt of 1M tokens (i.e., the pre-filling stage) on a single A100 GPU. Existing methods for speeding up prefilling often fail to maintain acceptable accuracy or efficiency when applied to long-context LLMs. To address this gap, we introduce MInference (Milliontokens Inference), a sparse calculation method designed to accelerate pre-filling of long-sequence processing. Specifically, we identify three unique patterns in long-context attention matrices-the A-shape, Vertical-Slash, and Block-Sparsethat can be leveraged for efficient sparse computation on GPUs. We determine the optimal pattern for each attention head offline and dynamically build sparse indices based on the assigned pattern during inference. With the pattern and sparse indices, we perform efficient sparse attention calculations via our optimized GPU kernels to significantly reduce the latency in the pre-filling stage of long-context LLMs. Our proposed technique can be directly applied to existing LLMs without any modifications to the pre-training setup or additional fine-tuning. By evaluating on a wide range of downstream tasks, including InfiniteBench, RULER, PG-19, and Needle In A Haystack, and models including LLaMA-3-1M, GLM4-1M, Yi-200K, Phi-3-128K, and Qwen2-128K, we demonstrate that MInference effectively reduces inference latency by up to 10x for pre-filling on an A100, while maintaining accuracy. Our code is available at https://aka.ms/MInference.
研究动机与目标
- 解决长上下文大语言模型预填充阶段计算成本过高的问题,该阶段在单张 A100 上处理 100 万 token 提示时可能耗时长达 30 分钟。
- 克服现有稀疏注意力方法的局限性,这些方法需要微调,或无法适应长上下文设置中的动态注意力模式。
- 通过识别并利用长上下文大语言模型中稳定且数据驱动的稀疏注意力模式,实现高效、低开销的推理加速。
- 设计一种系统,在推理时动态为每个注意力头分配最优稀疏模式,同时最小化估计开销。
- 在保持模型精度的同时,显著降低延迟,且适用于多样化的长上下文基准和模型架构。
提出的方法
- 通过长上下文大语言模型中注意力矩阵的经验分析,识别出三种重复出现的稀疏注意力模式——A型、反斜杠型和分块稀疏型。
- 通过内核感知搜索,基于对注意力动态的离线分析,为每个注意力头分配最优稀疏模式。
- 在推理时通过轻量级、输入相关的近似方法动态构建稀疏索引:对于反斜杠型,使用最后一个查询和键向量;对于分块稀疏型,对 64 个 token 的块应用平均池化。
- 实现三种针对三种稀疏模式优化的 GPU 内核——基于 PIT、Triton 和 FlashAttention——以实现高效的稀疏矩阵乘法。
- 无需修改预训练过程或要求微调,即可将该方法集成到现有大语言模型中,实现即插即用的部署。
- 使用可随输入动态调整的稀疏掩码,同时保持模式估计的计算开销极低。
实验结果
研究问题
- RQ1是否可以在长上下文大语言模型中识别出稳定且可泛化的稀疏注意力模式,从而实现在无需微调的情况下高效预填充?
- RQ2如何以极低的计算开销在线估计动态稀疏注意力,以维持低延迟?
- RQ3针对特定模式的稀疏计算在多大程度上能减少 FLOPs 和长上下文大语言模型的推理延迟,同时保持精度?
- RQ4动态稀疏注意力的性能与固定稀疏或密集注意力相比,在多样化长上下文基准上表现如何?
- RQ5该方法是否可普遍应用于不同大语言模型架构和上下文长度,而无需修改模型架构?
主要发现
- MInference 将单张 A100 GPU 上 100 万 token 提示的预填充延迟最高减少 10 倍,将原本 30 分钟的处理时间缩短至 3 分钟以内。
- 该方法在多个基准测试中保持了高精度,在 Needle In A Haystack、InfiniteBench、RULER 和 PG-19 上与基线模型相当或更优。
- 长上下文大语言模型中的注意力矩阵在 128K 上下文长度下表现出高达 96.8% 的稀疏度,验证了稀疏计算的可行性。
- A型、反斜杠型和分块稀疏型模式在多个模型中(包括 LLaMA-3-1M、GLM-4-1M 和 Qwen2-128K)均被一致观察到。
- 动态稀疏掩码构建引入的开销可忽略不计,实现了对延迟影响极小的实时推理。
- 该技术与模型无关,可无需微调或架构修改直接应用于现有大语言模型,已在 LLaMA-3-8B-1M、Phi-3-Mini-128K 和 Qwen-2-7B-128K 上得到验证。
阅读本论文
在 Nubint 阅读器中打开原文,边读边就任意段落提问。
在 Nubint 中阅读本论文本解读由 AI 根据论文原文生成,引用前请核对原文。