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

Qingyun Jin, Xiaohui Song, Feng Zhou, Zengchang Qin
12/30/2024
cs.CL

Official code: fpcsong/mha2gqa (3 stars, last commit 2025-10-29).

Abstract

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.

View on arXivView PDF

Code Implementations(1)

fpcsong/mha2gqaOfficial100%

Code for EMNLP 2025 Findings: Align Attention Heads Before Merging Them: An Effective Way for Converting MHA to GQA

31Shell, PythonApr 25, 202511 months ago

Similar Papers

Cite this paper

@article{jin2024align,
  title  = {Align Attention Heads Before Merging Them: An Effective Way for Converting MHA to GQA},
  author = {Qingyun Jin and Xiaohui Song and Feng Zhou and Zengchang Qin},
  year   = {2024},
  eprint = {2412.20677},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/abs/2412.20677v2}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2412.20677.svg)](https://codeofpaper.com/paper/2412.20677)

Discussion