参考了 (Purshow, 2026; Weng, 2023; 苏剑林, 2024)。
Multi-Head Attention
Multi-Head Attention (MHA) (Vaswani et al., 2023) 大概就是把 QKV 分成多组:
为了压缩 KV cache,Multi-Query Attention (MQA) (Shazeer, 2019) 让
相同的 8 个 Query heads自回归推理时缓存的 K/V
MHAMulti-Head Attention
每个 Query head 独享一组 K/V
8 组 K/V100%
GQAGrouped-Query Attention
每 4 个 Query heads 共享一组 K/V
2 组 K/V25%
MQAMulti-Query Attention
每 8 个 Query heads 共享一组 K/V
1 组 K/V12.5%
Multi-Head Latent Attention
Multi-Head Latent Attention (MLA) (DeepSeek-AI et al., 2024) 尝试将 QKV 低秩压缩。
首先将
然后考虑
因为
但问题是,我们之前因为需要得到
下面沿用上文的矩阵记号展开 MLA 的结构:
Query 分支K/V 内容分支RoPE 分支推理时缓存
从下往上读 · 横向滑动查看完整结构 →
每层的 KV cache 和
References
Ainslie, J., Lee-Thorp, J., de Jong, M., Zemlyanskiy, Y., Lebrón, F., & Sanghai, S. (2023). GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints. arxiv.org
DeepSeek-AI, Liu, A., Feng, B., Wang, B., Wang, B., Liu, B., Zhao, C., Dengr, C., Ruan, C., Dai, D., Guo, D., Yang, D., Chen, D., Ji, D., Li, E., Lin, F., Luo, F., Hao, G., Chen, G., … Xie, Z. (2024). DeepSeek-V2: A Strong, Economical, and Efficient Mixture-of-Experts Language Model. arxiv.org
Purshow. (2026). Attention Architecture Notes. github.com
Shazeer, N. (2019). Fast Transformer Decoding: One Write-Head is All You Need. arxiv.org
Su, J., Lu, Y., Pan, S., Murtadha, A., Wen, B., & Liu, Y. (2023). RoFormer: Enhanced Transformer with Rotary Position Embedding. arxiv.org
Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., Kaiser, L., & Polosukhin, I. (2023). Attention Is All You Need. arxiv.org
Weng, L. (2023). The Transformer Family Version 2.0. Lilianweng.Github.Io. lilianweng.github.io
苏剑林. (2024, May). 缓存与效果的极限拉扯:从MHA、MQA、GQA到MLA. spaces.ac.cn