所属模块:
M4 · 序列与 Transformer (Sequences & Transformers)| 专题分类:注意力变体 (Attention Variants (MHA / MQA / GQA))| 难度等级:Easy
一、核心一句话结论 (One-Sentence Summary)
MQA 让所有头共享一组 K/V;GQA 把头分组、组内共享。KV cache 显存与带宽降为 1/h 或 1/g。
MQA shares a single Key/Value head across all Query heads; GQA groups Query heads to share Key/Value heads, slashing inference KV-cache by $4-8times$ with zero quality degradation.
二、核心考点要义 (Key Insights)
- 📌 KV cache 显存 ∝ K/V 头数(MHA 为 h,MQA 为 1,GQA 为 g)
- 📌 Q 头数不变(保持表达力),只减 K/V 头数
- 📌 decode 阶段是 memory-bound,故减 KV 直接提速
English Insights:
– Multi-Head Attention (MHA): $H$ query heads, $H$ key heads, $H$ value heads; full KV cache redundancy
– Multi-Query Attention (MQA / Shazeer 2019): $H$ query heads, $1$ key head, $1$ value head; cuts KV cache by factor $H$ ($97%$ reduction)
– Grouped-Query Attention (GQA / Ainslie et al. 2023): $H$ query heads grouped into $G$ groups, each group sharing $1$ key and $1$ value head ($G=8$ standard in LLaMA-3)
三、核心数学原理与机理推导 (Mathematical Principles & Derivation)
$$text{MHA}: h text{KV heads};quad text{GQA}: g text{KV heads};quad text{MQA}: 1 text{KV head};qquad text{KV}propto n_{text{kv}}$$
数学机理:KV cache 的显存 = 2(K 与 V)× 层数 L × KV 头数 n_kv × head_dim d_h × 序列长度 S × 精度字节。MHA 下 n_kv=h(如 32),故 KV cache ∝ h。MQA(Multi-Query Attention,Shazeer 2019) 让所有 Q 头共享同一组 K/V(n_kv=1),KV cache 显存与读取带宽降为 1/h;GQA(Grouped-Query Attention,Ainslie 等 2023) 折中:把 h 个 Q 头分成 g 组,每组共享一组 K/V(n_kv=g,通常 g=h/8 或 h/4)。为什么可行——注意力的表达力主要来自 Q 的多样性(不同的 query 投影产生不同的注意力模式);K/V 的作用是’被查询的内容’,其冗余度较高。故减少 K/V 头数(而非 Q 头数)能在大幅节省显存/带宽的同时保留大部分表达力。为什么在推理中收益巨大——decode 阶段每步只生成 1 个 token,但需读取整个 KV cache(∝S×n_kv);故 decode 是memory-bound(算术强度低、受显存带宽限制)。降低 n_kv 直接减少每步的数据搬运量,从而提升吞吐。实证:MQA 可带来 2~5 倍的 decode 吞吐提升;GQA 在质量上接近 MHA、速度接近 MQA,是当前主流(LLaMA-2/3、Mistral 等均用 GQA)。
📖 查看英文严格数学推导 (English Mathematical Derivation)
Mathematical Formulations and KV Cache Reductions:
Let model have hidden dimension $d$, query head count $H$, head dimension $d_k = d/H$, and sequence length $S$.
① KV Cache Memory Formula per Token:
$text{Memory}_{text{token}} = 2 times [2 cdot L cdot n_{text{kv}} cdot d_k cdot B] text{ bytes}$ (in FP16).
– MHA: $n_{text{kv}} = H$. Total KV memory $propto H cdot d_k = d$.
– MQA: $n_{text{kv}} = 1$. Total KV memory $propto 1 cdot d_k = d / H$. Cuts KV memory and memory bandwidth by factor $H$ (e.g., $32times$ reduction!).
– GQA: Partition $H$ query heads into $G$ groups (each group has $H/G$ query heads sharing 1 KV head). $n_{text{kv}} = G$. Cuts KV memory by factor $H/G$ (e.g., $8times$ reduction when $H=64, G=8$).
② Why GQA is the Frontier Standard (LLaMA-3, Mistral):
While MQA slashes memory drastically, it suffers from minor capacity drops in large models ($>30text{B}$) and training instabilities. GQA ($G=8$) achieves virtually identical speedups to MQA while recovering $100%$ of MHA’s modeling perplexity and task reasoning accuracy.
四、工业级落地权衡与工程考量 (Industrial Trade-offs)
深度剖析与工程权衡:① MQA 的质量损失与恢复——MQA 因 K/V 表达力受限,训练不稳、质量略降;uptraining(从 MHA 检查点出发,把 K/V 投影平均后继续少量训练)可快速恢复质量,成本远低于从头训练。这是’改架构 + 少量训练’的经典工程手法。② GQA 的分组数选择——g 越大质量越好但显存/带宽越高;实践中 g 常取 8(h=32 时每 4 个 Q 头共享一组 KV),在质量与效率间取得良好平衡。③ 与 MLA 的对比——MQA/GQA 通过’减少头数’省 KV;MLA(多头潜在注意力) 通过’低秩压缩 KV 的维度’省 KV(见下一题);两者可组合(DeepSeek-V2 用 MLA)。④ 训练侧的影响——减少 K/V 头数也减少了 K/V 投影的参数与计算,但训练阶段是 compute-bound,故收益不如推理明显;主要动机是’让训练出的模型推理友好’。⑤ 与 KV cache 量化的关系——MQA/GQA 减少 KV 的’元素数’,量化减少 KV 的’每元素位数’;两者正交、可叠加。⑥ 面试要点——被问’MQA/GQA’,应给出’KV cache ∝ n_kv → 减少 K/V 头数 → decode 是 memory-bound 故直接提速‘的因果链,并说明’保留 Q 头数、只减 K/V 头数’是关键;能提到 uptraining 与 GQA 的主流地位是加分。
⚙️ 查看英文落地权衡分析 (English Systems & Trade-offs)
Serving throughput impact: During autoregressive LLM decoding, inference is strictly memory-bandwidth bound. Slashing KV cache size by $8times$ with GQA directly enables $8times$ larger serving batch sizes and up to $4times$ higher generation throughput on GPUs.
五、常见面试避坑陷阱 (Common Pitfalls & Traps)
- ⚠️ 以为 MQA 同时减少了 Q 头数(Q 头数不变)
- ⚠️ 忽略 decode 阶段的 memory-bound 特性
English Pitfalls:
– Confusing Query heads with Key/Value heads; GQA leaves the number of Query heads ($H$) completely untouched
– Assuming GQA reduces computational FLOPs; FLOP reduction is minor ($sim 5%$), the massive gain is in memory bandwidth
六、高频深度面试追问与预测 (Follow-Up Questions)
- 为什么减 K/V 头数不显著损害质量?
- Why is autoregressive LLM inference memory-bandwidth bound rather than compute bound?
- GQA 的分组数 g 如何选?
- How does uptraining allow an existing MHA pretrained model to convert into a GQA model with minimal compute?
七、知识图谱对齐 (Knowledge Graph Anchor)
- 🔗 关联底层卡片:
注意力变体:Multi-Head (MHA)、Multi-Query (MQA) 与 Grouped-Query (GQA)(Attention Variants: MHA, MQA & Grouped-Query Attention (GQA)) - 🗺️ 知识图谱模块:
AI 基础设施工程导图
🔬 算法科学家与机器学习深度考察全量题库 (Science Depth)
本题收录于 TalentMe 算法科学家深度考察真题库 (Science Depth)。全库共 856 道硬核考点,深度覆盖数学统计、经典ML、深度学习、Transformer、大语言模型、多模态、推荐系统与 MLOps。支持 Jev 面经智能匹配、一键离线单文件 HTML 手册导出并直连 Obsidian 本地记忆。