Back to LLMs Mind Map
中文·English
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 =22LHdsb= 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.
🎯5 High-Frequency Exam Points
1
What does GQA save vs MHA? Meaning of each term in the KV-cache formula 22LHdsb2 \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
🎯
Test Your Knowledge: Practice Questions for "Attention Variants MHA/MQA/GQA"
Single choice pitfall questions with instant feedback and mistake tracking.
🚀 Start Card Practice
Previous CardScaled Dot-Product AttentionNext CardMulti-head Latent Attention

🔗 More LLMs Knowledge Cards

Agent & Tool CallingAlignment Tax & Preference DataBenchmarks MMLU/GSM8KTokenization: BPE / WordPiece