arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2412.20677cs.CL
收藏

在合并注意力头之前对齐它们:一种将 MHA 转换为 GQA 的有效方法

Align Attention Heads Before Merging Them: An Effective Way for Converting MHA to GQA

Beihang University(北航大学);OPPO AI Center(OPPO人工智能中心)

Qingyun Jin, Xiaohui Song, Feng Zhou, Zengchang Qin

AI总结:

本文提出先用 Procrustes 分析对齐注意力头、再以 $L_0$ 正则化剪枝的 MHA 转 GQA 方法,可在任意 KV 头压缩比下显著减少缓存并保持可接受性能。

摘要:

大语言模型(LLM)已在多种自然语言处理任务上展现出卓越性能。然而,随着模型规模和输入序列长度的增加,线性增长的键值(KV)缓存会显著降低推理吞吐量。因此,分组查询注意力(GQA)作为多头注意力(MHA)的替代方案,已被广泛引入 LLM。在本工作中,我们提出了一种高性价比的方法,可将 MHA 转换为具有任意 KV 头压缩比的 GQA。该方法的关键在于对注意力头应用 Procrustes 分析,在保持计算不变性的同时增强注意力头之间的相似性,从而提升模型训练后的性能。随后,我们采用 $\mathit{L_0}$ 正则化剪枝冗余参数。剪枝后的模型可适配标准 GQA 框架。实验结果表明,我们的策略能够将 LLaMA2-7B 模型的 KV 头压缩至多 87.5%,并将 Sheared-LLaMA-1.3B 的 KV 头压缩 75%,且性能下降在可接受范围内。代码已发布于 https://github.com/fpcsong/mha2gqa。

英文摘要:

Large language models (LLMs) have demonstrated exceptional performance across diverse natural language processing tasks. However, as the model size and the input sequence's length increase, the linearly increasing key-value (KV) cache significantly degrades inference throughput. Therefore, grouped-query attention (GQA), as an alternative to multi-head attention (MHA), has been widely introduced into LLMs. In this work, we propose a cost-effective method for converting MHA into GQA with any compression ratio of KV heads. The key point of our method lies in the application of Procrustes analysis to the attention heads, which enhances the similarity among attention heads while preserving computational invariance, thereby improving the model's post-training performance. Subsequently, we employ $\mathit{L_0}$ regularization to prune redundant parameters. The model after pruning can be adapted to the standard GQA framework. Experimental results show that our strategy can compress up to 87.5\% KV heads of LLaMA2-7B model and 75\% KV heads of Sheared-LLaMA-1.3B with acceptable performance degradation. Our code is released at https://github.com/fpcsong/mha2gqa.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑