← Back to LLMs Mind Map
⚡ LLMsID: attention-variants

Attention Variants MHA/MQA/GQA

注意力变体 MHA/MQA/GQA
🎯Core Definition
MHA (Multi-Head Attention) gives every head its own K/V projections; MQA (Multi-Query Attention) shares a single K/V pair across all heads; GQA (Grouped-Query Attention) partitions the HH query heads into GG groups that share K/V — a middle ground: LLaMA-2/3 70B uses 8 KV heads, LLaMA-2 7B uses MQA, and Mistral/Gemma/Qwen all adopt GQA.
💡Use Cases
any autoregressive serving scenario where KV-cache memory is the constraint; since 2023 GQA is the de-facto standard in open models (LLaMA-2/3, Mistral, Gemma, Qwen).
⚡Key Problems Solved
at decode time every token caches all history K/V, and memory grows linearly with sequence length: cache size =2⋅2⋅L⋅H⋅d⋅s⋅b= 2 \cdot 2 \cdot L \cdot H \cdot d \cdot s \cdot b (2 bytes per fp16 element × K and V × layers × heads × head dim × seq len × batch); LLaMA-3 70B at s=8192s = 8192, b=32b = 32 needs about 687GB with MHA — dwarfing its 140GB of weights — and about 86GB with GQA (8 groups), saving ~87%; MQA saves the most but degrades quality slightly, so it is mostly used in small models.
🎯Core Interview Exam Points
Fundamentals · Production Scenarios · Deep Dive
Core Def
Explain the core definition, underlying mechanism/math, and key principles of Attention Variants MHA/MQA/GQA.
Use Cases
In production engineering, what are the primary use cases for Attention Variants MHA/MQA/GQA and what core pain points does it solve?
🔥 High-Frequency Deep-Dive Questions
1
What does GQA save vs MHA? Meaning of each term in the KV-cache formula 2⋅2⋅L⋅H⋅d⋅s⋅b2 \cdot 2 \cdot L \cdot H \cdot d \cdot s \cdot b?
2
How to choose the number of groups G in GQA? The quality vs memory trade-off?
3
Difference between MQA and GQA? Pros and cons of each?
4
Why does sharing K/V barely affect training but matter a lot for inference (KV cache exists only at inference)?
5
Configs of mainstream models: KV-head counts for LLaMA-2/3, Mistral, Qwen?
Updated 2026-08-12
🎯
Verify Mastery: Practice Exam Questions for "Attention Variants MHA/MQA/GQA"
Take pitfall multiple-choice questions with derivation options; mistakes are automatically logged.
🚀 Start Card Practice ➔
← Previous CardScaled Dot-Product AttentionNext Card →Multi-head Latent Attention

🔗 More LLMs Knowledge Cards

引用出处: TalentMe 450+ 工业级 AI 架构考点知识库 (Tech Vault) · 遵循 CC-BY-4.0 知识共享协议
URL: https://talentme.airsota.com/resources/tech/roadmap/mindmap/LLM/card/attention-variants
中文速览与面试要点

注意力变体 MHA/MQA/GQA (Attention Variants MHA/MQA/GQA)

定义: MHA (Multi-Head Attention) 每个头独立投影 K/V;MQA (Multi-Query Attention) 所有头共享同一组 K/V(即只有 1 个 KV 头);GQA (Grouped-Query Attention) 把 H 个查询头分成 G 组、组内共享 K/V,是两者的折中:LLaMA-2/3 70B 用 8 个 KV 头,LLaMA-2 7B 用 MQA,Mistral/Gemma/Qwen 普遍用 GQA。 使用场景: 推理时 KV cache 显存受限的所有自回归服务场景;2023 年后 GQA 成为大模型事实标准(LLaMA-2/3、Mistral、Gemma、Qwen 全系)。 解决的问题: 解码时每个 token 都要缓存全部历史的 K/V,显存随序列线性增长:缓存大小 = 2 2 L H d s b(fp16 每元素 2 字节 × K/V 两份 × 层数 × 头数 × 每头维度 × 序列长 × batch);LLaMA-3 70B 在 s = 8192、b = 32 时 MHA 约需 687GB,远超 140GB 权重,换 GQA(8 组)后约 86GB,省约 87%;MQA 省得最多但质量损失略大,通常只用于小模型。

核心要点

  • GQA 相比 MHA 省了什么?KV cache 显存公式 2 2 L H d s b 每项含义?
  • GQA 组数 G 如何选择?质量与显存的权衡?
  • MQA 与 GQA 的区别?各自优缺点?
  • 为什么共享 KV 对训练影响小、对推理影响大?(KV cache 只在推理出现)
  • 主流模型分别用什么配置?LLaMA-2/3、Mistral、Qwen 的 KV 头数?