M4-044M4: Sequences & TransformersAttention Variants (MHA / MQA / GQA)Hard
⚡Flashcard Drill➔Mastery:

Attention Entropy Collapse and How QK-Norm Resolves It

📐 Mathematical Definition
H(αi)=−∑jαijlog⁡αij→0;QK-Norm: Q,K←RMSNorm(⋅)H(\alpha_i)=-\sum_j\alpha_{ij}\log\alpha_{ij}\to0;\qquad \text{QK-Norm}:\ Q,K\leftarrow\mathrm{RMSNorm}(\cdot)
⚡ One-Sentence Core Answer
Entropy collapse occurs when unconstrained query-key magnitudes cause attention logits to explode, collapsing softmax into rigid one-hot distributions; QK-Norm bounds logits to preserve entropy.

📌 Key Takeaways

  • •
    Entropy collapse: Shannon entropy H(Ai)=−∑pjlog⁡pj→0H(A_i) = -\sum p_j \log p_j \to 0; attention focuses exclusively on 1 single token across all heads
  • •
    Gradient vanishing: one-hot softmax derivative p(1−p)→0p(1 - p) \to 0, freezing backpropagation through attention pathways
  • •
    QK-Norm remedy: applies RMSNorm to Queries and Keys, bounding logit magnitudes to ≤dk\le \sqrt{d_k} and sustaining healthy entropy

📐 Mathematical Derivations

Mathematical Mechanics (Dehghani et al., ViT-22B 2023; Henry et al., 2020):
In deep models, weight norms ∥WQ∥,∥WK∥\|W_Q\|, \|W_K\| grow monotonically during training. The attention logit is zij=qiTkjdkz_{ij} = \frac{q_i^T k_j}{\sqrt{d_k}}.
- If ∥qi∥2\|q_i\|_2 and ∥kj∥2\|k_j\|_2 grow from dk\sqrt{d_k} to 10dk10\sqrt{d_k}, logit magnitudes expand to ±100\pm 100.
- In softmax: pi=ezi∑ezjp_i = \frac{e^{z_i}}{\sum e^{z_j}}. When zmax⁡−zsecond>15z_{\max} - z_{\text{second}} > 15, pmax⁡≈1.0p_{\max} \approx 1.0 and all other pj≈0p_j \approx 0.
- Attention Entropy Collapse:
The attention entropy H(p)=−∑j=1Npjlog⁡pjH(p) = - \sum_{j=1}^N p_j \log p_j collapses toward 0.
- Gradient Vanishing:
The Jacobian of softmax is ∂pj∂zk=pj(δjk−pk)\frac{\partial p_j}{\partial z_k} = p_j (\delta_{jk} - p_k). When pp is a one-hot vector, the diagonal is 1(1−1)=01(1 - 1) = 0 and off-diagonals are 0(0−0)=00(0 - 0) = 0. Gradients cannot flow through the attention matrix, freezing model learning.
- The QK-Norm Fix:
Normalize: q~=RMSNorm(q),  k~=RMSNorm(k)\tilde{q} = \text{RMSNorm}(q), \; \tilde{k} = \text{RMSNorm}(k).
Then ∥ildeqi∥2=dk\| ilde{q}_i\|_2 = \sqrt{d_k} and ∥ildekj∥2=dk\| ilde{k}_j\|_2 = \sqrt{d_k}.
The logit satisfies: ∣q~iTk~jdk∣=∣dkdkcos⁡(θ)dk∣=dk∣cos⁡(θ)∣≤dk\left| \frac{\tilde{q}_i^T \tilde{k}_j}{\sqrt{d_k}} \right| = \left| \frac{\sqrt{d_k} \sqrt{d_k} \cos(\theta)}{\sqrt{d_k}} \right| = \sqrt{d_k} |\cos(\theta)| \le \sqrt{d_k}.
When dk=64d_k = 64, logits are strictly bounded within [−8,8][-8, 8], guaranteeing healthy, non-vanishing attention entropy.

🏭 Production Trade-offs

Zero Overhead: QK-Norm adds simple element-wise root-mean-square normalization in registers, adding <0.5%<0.5\% compute overhead while eliminating one of the primary failure modes in scaling foundation models.
⚠️ Common Interview Pitfalls
  • ✕
    Attempting to fix attention entropy collapse by lowering learning rate, which slows training without solving unconstrained logit growth
  • ✕
    Applying QK-Norm without the 1/dk1/\sqrt{d_k} factor, causing under-confident attention distributions
🎯 Interviewer Follow-up Inquiries
  • ?
    How does tracking attention entropy (H(A)H(A)) during training serve as an early warning for loss spikes?
  • ?
    Why does the one-hot collapse of softmax attention destroy backpropagated gradients?
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔
📚

Associated Knowledge Base Guides & Mindmaps

Explore the comprehensive technical article, exam cards, and global architecture tree.

← PreviousM4-043: Cross-Attention vs Self-Attention: Structural Differences and Multimodal Roles📋Back to BankNext →M4-045: Alternatives to Softmax Attention: Sigmoid Attention and Normalization Variants
中文速览与面试要点

解释注意力熵崩塌(entropy collapse)与 QK-Norm 的作用

训练中注意力分布趋于尖锐(熵趋 0)、注意力集中在少数位置,损害长上下文与训练稳定;QK-Norm 归一化 Q/K 抑制它。

核心要点

  • 数学机理: 现象 ——训练过程中,注意力分布的 熵 (H(α_i)=−Σ_j α_ij log α_ij)会持续下降,最终趋于接近 0(即注意力趋近 one-hot、集中在极少数位置)。 后果 ——(a) 丢失多位置信息 ——注意力只能选一个位置,无法同时聚合多个相关信息,损害需要'多源整合'的任务(多跳推理、聚合统计);(b) 长上下文退化 ——长序列中注意力集中在局部或 sink,远处的相关信息被忽略;(c) 梯度消失 ——softmax 在饱和区导数极小,注意力层难以继续学习。 成因 ——关键是 Q/K 的范数增长 :随着训练,Q、K 投影的权重可能增长,使 logits=q·k/√d 的幅度变大;softmax 在大 logits 下趋于 one-hot(熵趋 0)。这与'√d_k 缩放'假设'方差为 1'的前提相冲突——训练中方差不再为 1,缩放失效。 QK-Norm 的解法 ——在计算 QKᵀ 之前对 Q、K 施加 RMSNorm(或 LayerNorm), 强制 它们的尺度稳定(范数归一到固定值),从而使 logits 幅度稳定、注意力熵保持在合理水平。这是'用显式归一化替代'假设方差为 1''的更鲁棒方案,被 ViT-22B、Gemma-2、以及多个长上下文模型采用。 与'注意力汇聚'的区别 ——汇聚(sink)指注意力集中到 特定位置 (序列开头);熵崩塌指注意力变得 尖锐 (无论集中到哪里)。两者常同时出现(sink 是熵崩塌的一种表现),但概念不同:前者描述'集中在哪里',后者描述'有多集中'。
  • 深度剖析与工程权衡:① 监控方法 ——记录每层每头的注意力熵(或其均值/最小值);健康训练中熵应保持在某个区间(不过高=注意力太分散、不过低=崩塌);也可记录'最大注意力权重'的均值(趋 1 说明崩塌)。② 与其他稳定手段的关系 ——(a) z-loss 稳定 log-sum-exp(间接影响熵);(b) 注意力温度 (显式调节 softmax 锐度);(c) 熵正则 (直接惩罚低熵);(d) QK-Norm (归一化 Q/K);(e) attention sink logit (提供'不关注'的出口)。它们作用点不同,可组合。③ 与长上下文的关系 ——熵崩塌是长上下文能力退化的重要机制;若模型在长序列上把注意力集中在少数位置,则无法利用远距离信息。故长上下文训练需特别关注熵。④ 与量化的关系 ——熵崩塌常伴随激活离群值(少数极大值),对量化不利;QK-Norm 可缓解离群值,故对量化推理也有益。⑤ 实现代价 ——QK-Norm 增加对 Q/K 的归约(O(L·d)),开销很小;是'低成本高收益'的稳定性手段。⑥ 面试要点 ——被问'大模型注意力有什么病态',应给出' 熵崩塌(趋 one-hot)+ 汇聚(集中到 sink) '两个现象与' Q/K 范数增长使 logits 变大 '这一成因,并列出 QK-Norm/z-loss/温度/熵正则等对策;这是'训练稳定性'方向的高分回答。
  • 熵崩塌 = 注意力趋近 one-hot,丢失多位置信息
  • 成因:Q/K 范数增长使 logits 变大 → softmax 饱和
  • QK-Norm 使 logits 尺度稳定,保持注意力熵

常见陷阱

  • 把熵崩塌与注意力汇聚混为一谈
  • 忽略 Q/K 范数增长使 √d_k 缩放失效