---
title: "【AI 核心深度 M4-040】解释注意力汇聚（attention sink）现象（The Attention Sink Phenomenon: Origins, Mechanics, and StreamingLLM）深度数理推导与工程落地解析"
canonical_url: "https://airsota.com/2026/10/01/science-depth-m4-040-attention-variants-mha-mqa-gqa/"
date_published: "2026-10-01T12:24:12-07:00"
date_modified: "2026-10-01T12:24:12-07:00"
language: "zh-CN"
categories: ["AI 核心深度 856 题 (Science Depth)", "TalentMe (AI 学习与算法架构)", "大厂算法与面试备考 (Interview Guides &amp; Math)"]
tags: ["applied-scientist", "attention-variants-mha-mqa-gqa", "machine-learning", "module-m4", "science-depth"]
summary: "大量注意力集中到序列开头的少数 token（尤其首 token），即使它们语义无关；因 softmax 需'归一化出口'。"
publisher: "AirSOTA (https://airsota.com)"
---

# 【AI 核心深度 M4-040】解释注意力汇聚（attention sink）现象（The Attention Sink Phenomenon: Origins, Mechanics, and StreamingLLM）深度数理推导与工程落地解析

> **Executive Summary**: 大量注意力集中到序列开头的少数 token（尤其首 token），即使它们语义无关；因 softmax 需'归一化出口'。

> 
**所属模块**：`M4 · 序列与 Transformer (Sequences & Transformers)` | **专题分类**：`注意力变体 (Attention Variants (MHA / MQA / GQA))` | **难度等级**：`Hard`

### 一、核心一句话结论 (One-Sentence Summary)

**大量注意力集中到序列开头的少数 token（尤其首 token），即使它们语义无关；因 softmax 需'归一化出口'。**

*Softmax requires attention weights to sum to 1.0, forcing models to dump excess probability mass onto initial tokens (BOS) as an information sink; preserving initial tokens enables infinite-length streaming.*

### 二、核心考点要义 (Key Insights)

- 📌 首 token 常获得极高注意力（即使无语义相关性）

- 📌 原因是 softmax 强制权重和为 1，需要'垃圾桶'

- 📌 去掉 sink token 会导致性能崩溃（StreamingLLM 的关键发现）

**English Insights:**

- *Mathematical root: softmax constraint $sum_{j=1}^t A_{ij} = 1$; when no relevant keys exist, the model requires an attention dumping ground*

- *Initial token bias: the very first token (BOS) is visible to all subsequent causal tokens, naturally evolving into the universal attention sink*

- *StreamingLLM (Xiao et al., 2023): keeping just 4 initial sink tokens alongside a sliding local window prevents perplexity collapse over millions of tokens*

### 三、核心数学原理与机理推导 (Mathematical Principles & Derivation)

$$sum_jalpha_{ij}=1 Rightarrow text{softmax must place mass somewhere};qquad text{sink}=text{mass dump}$$

数学机理：**现象**——在预训练好的 LLM 中，大量注意力（有时超过 50%）集中在**序列开头的少数 token**（尤其第一个 token），即使这些 token 语义上无关（如 BOS、句号）。**原因**——关键在于 **softmax 的归一化约束**：Σ*j α*{ij}=1，即每个 query 必须把'注意力质量'全部分配出去。对许多 query 而言，**没有特别相关的 key**（例如处理一个常见的功能词时），但 softmax 不允许'不关注任何位置'（权重不能全为 0，除非加 mask）。此时模型学到的解法是：把多余的质量**倾倒**到一个固定的'垃圾桶'位置（通常是序列开头，因为它在 causal mask 下对所有位置都可见）——这就是 **attention sink**。它还可解释为'模型需要一个 no-op 的默认行为'：当无需关注具体内容时，关注 sink 相当于'什么都不做'。**StreamingLLM 的关键发现**——如果从 KV cache 中**丢弃** sink token，模型性能**崩溃**（困惑度爆炸）；而**保留少量初始 token（作为 sink）+ 最近的滑动窗口**，就能让模型稳定处理**无限长**的流式输入，且无需微调。这是'利用 sink 做高效长上下文推理'的经典工作。

  📖 查看英文严格数学推导 (English Mathematical Derivation)

Mathematical Mechanics (Xiao et al., ICLR 2024; StreamingLLM):
In autoregressive causal attention: $o_i = sum_{j=1}^i alpha_{ij} v_j$.
- The Softmax Normalization Dilemma: At step $i$, even if query $q_i$ requires zero contextual information from predecessors, the mathematical constraint $sum_{j=1}^i alpha_{ij} = 1$ forces the sum of attention weights to equal 1.0.
- Why the First Token Becomes the Sink:
Because of causal masking, token 1 is present in the receptive field of every single future token. The network learns to use token 1 ($x_1$) as an 'idle trash bin'. Even when token 1 is a semantically meaningless formatting token (`` or `
`), it receives $30%-70%$ of all attention weights in deep layers.
- **The Rolling Window Collapse Paradox**:
When implementing sliding window inference for long text, engineers intuitively discard the oldest tokens to save memory. As soon as token 1 is evicted from the KV cache, the attention normalizer collapses, and perplexity immediately explodes to $>10^3$.
- **StreamingLLM Solution**:
Preserve: (1) **Attention Sinks** (the first 4 tokens: $0, 1, 2, 3$) + (2) **Local Sliding Window** (the most recent 4,096 tokens). Memory stays fixed at $O(W)$, while language generation streams smoothly across millions of tokens without retraining.

  

### 四、工业级落地权衡与工程考量 (Industrial Trade-offs)

深度剖析与工程权衡：① **sink 与'softmax 的必要性'**——有研究（如 gpt-oss）在注意力 logits 中**显式加一个可学习的 sink logit**，使'不关注任何位置'成为合法选项，从而让模型不再需要借用真实 token 当垃圾桶；这是'从架构上解决 sink'的思路。② **与位置编码的交互**——sink 通常在序列开头，与 RoPE 的'远距离衰减'共同塑造了注意力模式（'近处局部 + 远处 sink'）；这被称为注意力分布的**双峰结构**。③ **与 KV cache 压缩的关系**——因为大部分注意力在 sink 与局部窗口，**中间位置的 KV 贡献很小**，故可激进压缩（如 H2O 只保留'重击 token'、StreamingLLM 只留 sink + 窗口）；这是长上下文 KV 压缩的理论依据。④ **与'lost in the middle'的联系**——中间位置既不在局部窗口、也不是 sink，故最易被忽略；这解释了'长文档中间信息难被检索'的现象。⑤ **差分注意力的对照**——差分注意力用相减抵消 sink；gpt-oss 用 sink logit；StreamingLLM 保留 sink——三种思路说明 sink 是'必须处理的结构性现象'。⑥ **面试要点**——被问'attention sink 是什么'，应给出'**softmax 必须分配权重 → 需要一个垃圾桶位置 → 序列开头成为默认 sink**'的机制解释，并举出 StreamingLLM 的'保留 sink + 滑窗'应用；能联系到'KV 压缩'与'lost in the middle'是深度理解的标志。

  ⚙️ 查看英文落地权衡分析 (English Systems & Trade-offs)

Training mitigation: Models trained with explicit zero-attention capabilities (e.g., adding a learnable constant key/value bias, or using Softmax1: $frac{e^{z_i}}{1 + sum e^{z_j}}$) prevent attention sinks from forming on text tokens.

  

### 五、常见面试避坑陷阱 (Common Pitfalls & Traps)

- ⚠️ 把 sink 当作'模型学会了关注 BOS'（本质是 softmax 归一化约束）

- ⚠️ 丢弃 sink token 后仍期望性能不变（会崩溃）

**English Pitfalls:**

- *Evicting initial tokens in sliding-window KV caches, triggering catastrophic perplexity explosion within 10 tokens*

- *Interpreting massive attention weights on BOS as semantic importance; attention sinks are purely numerical artifacts of softmax normalization*

### 六、高频深度面试追问与预测 (Follow-Up Questions)

- **为什么 softmax 需要'垃圾桶'？**

- *Why does evicting the first token from the KV cache cause perplexity explosion in sliding-window inference?*

- **如何利用 sink 做长上下文推理（StreamingLLM）？**

- *How does Softmax1 (adding $+1$ to the softmax denominator) eliminate attention sinks during pretraining?*

### 七、知识图谱对齐 (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 本地记忆。

  [👉 前往 TalentMe 交互式研读本题 (M4-040) →](https://talentme.airsota.com/tools/science-depth/M4-040)

---
*Source: [AirSOTA Knowledge Matrix](https://airsota.com/2026/10/01/science-depth-m4-040-attention-variants-mha-mqa-gqa/) · Complete Index: [llms.txt](https://airsota.com/llms.txt)*
