# 残差流

残差流是 Transformer 各组件借以通信的共享、累积状态向量，每个词元位置各有一个：每个注意力头和 MLP 块都从流中读取输入，再通过加法把输出写回流中。<sup>[1](https://transformer-circuits.pub/2021/framework/index.html)</sup><sup> • </sup><sup>[2](https://inferensys.com/glossary/algorithmic-explainability-and-interpretability/mechanistic-interpretability/residual-stream)</sup> 它并不是架构代码中可见的某个模块，而是对贯穿整个 Transformer 块堆栈的残差连接所做的一种概念性重新表述。<sup>[3](https://mnzes.com/en/guide/inside-the-model/residual-stream)</sup> 这一表述由 Nelson Elhage 及其同事在 [Anthropic](https://www.edgepedia.org/zh/anthropic) 2021 年的论文《Transformer 电路的数学框架》中正式提出，如今已成为机制可解释性研究的标准视角——这一研究领域致力于逆向解析训练好的 Transformer 内部电路究竟在计算什么。<sup>[1](https://transformer-circuits.pub/2021/framework/index.html)</sup><sup> • </sup><sup>[4](https://quantmemo.com/concepts/the-residual-stream-view-of-transformers)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义 | 每个词元位置一个固定维度 \( d_{\text{model}} \) 的向量，以词元嵌入和位置编码初始化，每个子层都从中读取、并向其写回<sup>[5](https://inferensys.com/glossary/algorithmic-explainability-and-interpretability/transformer-mechanistic-analysis/residual-stream)</sup> |
| 更新规则 | 每个注意力和 MLP 子层通过线性投影读取，并通过将其结果的线性投影加回流中来完成写入<sup>[1](https://transformer-circuits.pub/2021/framework/index.html)</sup> |
| 最终状态 | 忽略归一化，最终的残差等于嵌入加上所有层中全部注意力头与 MLP 输出之和<sup>[6](https://github.com/tegridydev/Mechanistic-Interpretability/blob/main/01-foundations/residual-stream.md)</sup> |
| 带宽压力 | 单个 MLP 层的神经元数量通常是流维度的四倍；在一个 50 层 Transformer 的第 25 层，流两侧需要经由它通信的神经元数量各约为其维度的 100 倍<sup>[1](https://transformer-circuits.pub/2021/framework/index.html)</sup> |
| 训练作用 | 由于各贡献是相加而非替换，梯度可直接流向浅层，模型也能学会绕过整个层<sup>[5](https://inferensys.com/glossary/algorithmic-explainability-and-interpretability/transformer-mechanistic-analysis/residual-stream)</sup> |
| 可解释性用途 | logit 透镜与调谐透镜通过反嵌入读取中间状态；激活修补在两次运行之间交换流状态；向流中加入控制向量可引导模型行为<sup>[2](https://inferensys.com/glossary/algorithmic-explainability-and-interpretability/mechanistic-interpretability/residual-stream)</sup><sup> • </sup><sup>[7](https://proceedings.iclr.cc/paper_files/paper/2025/file/6e73c39cc428c7d264d9820319f31e79-Paper-Conference.pdf)</sup> |
| 架构替代方案 | 注意力残差以逐深度注意力取代固定累积；超连接将流拓宽为并行通道；ResiDual 使用双重残差连接<sup>[8](https://huggingface.co/papers/2603.15031)</sup><sup> • </sup><sup>[9](https://www.sciencedirect.com/science/article/pii/S0004370226001281)</sup> |

## 流的工作方式

Transformer 前向传播的第一步，是把每个词元嵌入为一个向量。该向量加上位置信息，就是每个位置上流的初始状态。随后每个 Transformer 块依次执行两轮“读取—修改—写回”循环：注意力子层在位置之间搬运信息，MLP 子层则在单个位置内部变换信息。两种情况下，子层都读取当前的流状态，计算出一个更新量，再把更新量加回去，而不是替换原状态。<sup>[5](https://inferensys.com/glossary/algorithmic-explainability-and-interpretability/transformer-mechanistic-analysis/residual-stream)</sup><sup> • </sup><sup>[10](https://www.kiru.solutions/lab/lessons/residual-stream-and-block/)</sup>

由于每次写入都是加法，任意层的状态都是此前所有组件输出的累加和。忽略归一化的细节，最终残差可示意性地写为

\[ r_{\text{final}} = r_{\text{embed}} + \sum_{l,h} a_{l,h} + \sum_{l} m_{l}, \]

其中 \( a_{l,h} \) 是第 \( l \) 层第 \( h \) 个注意力头的输出，\( m_l \) 是第 \( l \) 层 MLP 的输出。<sup>[6](https://github.com/tegridydev/Mechanistic-Interpretability/blob/main/01-foundations/residual-stream.md)</sup> 正是这种加性分解使直接 logit 归因成为可能：最终 logit 可以分解为若干独立的、逐层的贡献之和，而每一项贡献都可以投影到一个反嵌入方向上。<sup>[2](https://inferensys.com/glossary/algorithmic-explainability-and-interpretability/mechanistic-interpretability/residual-stream)</sup><sup> • </sup><sup>[6](https://github.com/tegridydev/Mechanistic-Interpretability/blob/main/01-foundations/residual-stream.md)</sup>

加性结构也带来了训练上的一个后果。由于每层的贡献是相加而非替换，反向传播时梯度可直接流向浅层，模型若不需要某些层，也能学会绕过整个层。<sup>[5](https://inferensys.com/glossary/algorithmic-explainability-and-interpretability/transformer-mechanistic-analysis/residual-stream)</sup> 因此，流扮演着一个累积记忆的角色，有助于避免深层网络中的梯度消失问题。<sup>[2](https://inferensys.com/glossary/algorithmic-explainability-and-interpretability/mechanistic-interpretability/residual-stream)</sup>

## 一条承压的通信通道

残差流最好被理解成一条带宽有限的通信通道。用框架论文的术语来说，它是一个瓶颈激活：介于高维激活之间的低维中间量，也是早期 MLP 层与任意晚期 MLP 层通信的唯一通路。<sup>[1](https://transformer-circuits.pub/2021/framework/index.html)</sup> 单个 MLP 层的神经元数量通常是流维度的四倍，而压力随深度累积：在一个 50 层 Transformer 的第 25 层，流之前的神经元数量是流维度的 100 倍，之后的神经元数量也是 100 倍，因此各组件必须以叠加方式通信，把许多特征打包进相同的坐标。<sup>[1](https://transformer-circuits.pub/2021/framework/index.html)</sup>

一个后果是，各层可以把信息存放在流的不同子空间中，从而向不同的读取者发送不同的信息，同时把大部分维度留给其他组件。<sup>[1](https://transformer-circuits.pub/2021/framework/index.html)</sup> 由于流（在加性意义上）是线性的，人们还可以定义虚拟权重：将一层的输出权重与另一层的输入权重相乘，就得到任意两层之间（无论相隔多远）的隐式直接连接。这些乘积描述了后一层在多大程度上读取了前一层写下的信息。<sup>[1](https://transformer-circuits.pub/2021/framework/index.html)</sup> 一些 MLP 神经元似乎承担着记忆管理的角色，写入其他层放入流中信息的负值，这暗示着对流维度的有意清空。<sup>[1](https://transformer-circuits.pub/2021/framework/index.html)</sup>

## 应当假定多少线性？

加性分解是精确的，但把流称为“线性”需要谨慎。哥伦比亚大学计算机科学助理教授 John Hewitt 认为，流仅在给定索引处对子层贡献而言是线性的，而这提供不了多少便利：在第 \( q \) 层做一次加性干预后，由于 MLP 的非线性、层归一化或 RMS 归一化，以及注意力从其他位置读取，所有后续子层的贡献都会以非线性方式改变。<sup>[11](https://www.cs.columbia.edu/~johnhew/residual-stream-not-linear.html)</sup> 特别是，流对前一层并不线性，而一个没有残差连接的网络，在完全相同的平凡意义上，对其子层贡献也同样是线性的。<sup>[11](https://www.cs.columbia.edu/~johnhew/residual-stream-not-linear.html)</sup> 他的建议是：那些隐含假定线性的可解释性方法，例如引导向量和 logit 透镜，应当靠其经验结果来证明合理性，而不是靠架构来证明。<sup>[11](https://www.cs.columbia.edu/~johnhew/residual-stream-not-linear.html)</sup>

一个相关的意外发现涉及流的坐标。数学框架预测，单个坐标不应具有特殊意义，因为对流的任意旋转，只要用相应的旋转补偿读取和写入矩阵，网络的函数就保持不变。<sup>[12](https://travisml.ai/courses/mechanistic-interpretability/textbook/)</sup> Anthropic 后来关于特权基的工作发现这一预测在实践中不成立：在大型 Transformer 中，某些坐标具有超乎寻常的重要性，作者暂时将这一效应归因于 Adam 优化器中逐维的归一化器。<sup>[13](https://www.anthropic.com/news/privileged-bases-in-the-transformer-residual-stream)</sup>

## 读取与干预流

由于流是被迭代精炼的，中间状态可以被读作部分预测。logit 透镜把反嵌入矩阵直接作用于中间残差状态，揭示模型的下一词元预测如何逐层收敛；调谐透镜是对这一思路的改进。<sup>[2](https://inferensys.com/glossary/algorithmic-explainability-and-interpretability/mechanistic-interpretability/residual-stream)</sup> 稀疏自编码器从残差流激活中提取特征，这一方法的动机在于：由于[多义性](https://www.edgepedia.org/zh/polysemanticity)——即多个概念被打包进同一个神经元——流是比单个神经元更好的分析层次。<sup>[7](https://proceedings.iclr.cc/paper_files/paper/2025/file/6e73c39cc428c7d264d9820319f31e79-Paper-Conference.pdf)</sup>

干预则把流当作计算草稿纸。把一个由对比激活对导出的控制向量加到选定层的流中，可以引导诸如情绪效价、诚实性等行为，在一项 ICLR 2025 研究中还可引导任务套件上的推理表现。<sup>[7](https://proceedings.iclr.cc/paper_files/paper/2025/file/6e73c39cc428c7d264d9820319f31e79-Paper-Conference.pdf)</sup> [激活修补](https://www.edgepedia.org/zh/activation-patching)把受污染运行中的流状态替换为干净运行中的对应状态，并测量正确输出被恢复的程度；[TransformerLens](https://www.edgepedia.org/zh/transformerlens) 正是为此暴露了 `blocks.{i}.hook_resid_pre` 和 `blocks.{i}.hook_resid_post` 等具名钩子点，其修补工具会返回每个块起始处流在各层和各位置上的修补指标网格。<sup>[14](https://github.com/actusintelligence/transformerlens/blob/main/docs/source/content/hook_system.md)</sup><sup> • </sup><sup>[15](https://transformerlensorg.github.io/TransformerLens/generated/code/transformer_lens.patching.html)</sup>

一个实用警告：原始激活的幅度在不同层之间不可比较，因为流的范数随深度增长，这也是为什么每个子层读取流之前都要先做层归一化。<sup>[10](https://www.kiru.solutions/lab/lessons/residual-stream-and-block/)</sup>

## 单一加性流的替代方案

标准流以固定的单位权重累积所有层的输出，而近期一系列架构重新审视了这一设计。[Moonshot AI](https://www.edgepedia.org/zh/moonshot-ai) 的注意力残差（AttnRes）以对更早层输出的逐深度 softmax 注意力取代固定的残差累积，让每一层能够选择性地复用先前的表示；它被集成进 Kimi Linear 架构（总参数 48B，激活参数 3B，在 1.4T 词元上预训练），在那里缓解了 PreNorm 稀释，使输出幅度和梯度分布沿深度更加均匀，并提升了所评估任务上的下游表现。<sup>[8](https://huggingface.co/papers/2603.15031)</sup> 后续的多头注意力残差（MHAR）把共享的路由查询重塑为按子空间划分的多个头，每个头在深度历史上各自做 softmax，在 100M、350M 和 1B 规模上较标准 Transformer 改善了验证损失，验证损失随头数呈 U 形，在 4 或 8 个头处达到平坦的最优点。<sup>[16](https://arxiv.org/abs/2607.27230)</sup> ResiDual 保留单一流，但使用双重残差连接，意在结合 Post-LN 与 Pre-LN 设计的优点。<sup>[9](https://www.sciencedirect.com/science/article/pii/S0004370226001281)</sup>

这些变体改变了逐深度信息流动的机制，但它们都是相对于同一个基线对象来定义的：那个自诞生以来一直支撑 Transformer 架构的、简单的“恒等加更新”流。<sup>[17](https://silkeplessers.github.io/deep-learning/transformers/2026/03/20/Attention_Residuals.html)</sup>

## References

1. [Elhage, N. et al. (2021). A Mathematical Framework for Transformer Circuits. Transformer Circuits Thread.](https://transformer-circuits.pub/2021/framework/index.html)
2. [Residual Stream: Transformer Architecture Definition, Inference Systems glossary](https://inferensys.com/glossary/algorithmic-explainability-and-interpretability/mechanistic-interpretability/residual-stream)
3. [What is the residual stream, mnzes guide](https://mnzes.com/en/guide/inside-the-model/residual-stream)
4. [The Residual Stream View of Transformers, Explained, Quant Memo](https://quantmemo.com/concepts/the-residual-stream-view-of-transformers)
5. [Residual Stream: The Transformer's Information Highway, Inference Systems glossary](https://inferensys.com/glossary/algorithmic-explainability-and-interpretability/transformer-mechanistic-analysis/residual-stream)
6. [Residual Stream and Virtual Weights, Mechanistic Interpretability foundations notes](https://github.com/tegridydev/Mechanistic-Interpretability/blob/main/01-foundations/residual-stream.md)
7. [Steering LLM reasoning via residual-stream control vectors, ICLR 2025](https://proceedings.iclr.cc/paper_files/paper/2025/file/6e73c39cc428c7d264d9820319f31e79-Paper-Conference.pdf)
8. [Attention Residuals (AttnRes), Hugging Face paper page](https://huggingface.co/papers/2603.15031)
9. [ResiDual: Transformer with dual residual connections, Artificial Intelligence (ScienceDirect)](https://www.sciencedirect.com/science/article/pii/S0004370226001281)
10. [The Residual Stream and the Transformer Block, Kiru Lab lessons](https://www.kiru.solutions/lab/lessons/residual-stream-and-block/)
11. [John Hewitt (2026). How the Residual Stream is (Not) Linear, Columbia University](https://www.cs.columbia.edu/~johnhew/residual-stream-not-linear.html)
12. [Mechanistic Interpretability of Large Language Models, Travis Lelle textbook](https://travisml.ai/courses/mechanistic-interpretability/textbook/)
13. [Privileged Bases in the Transformer Residual Stream, Anthropic](https://www.anthropic.com/news/privileged-bases-in-the-transformer-residual-stream)
14. [The Hook System, TransformerLens documentation](https://github.com/actusintelligence/transformerlens/blob/main/docs/source/content/hook_system.md)
15. [transformer_lens.patching module, TransformerLens documentation](https://transformerlensorg.github.io/TransformerLens/generated/code/transformer_lens.patching.html)
16. [Multi-Head Attention Residuals, arXiv:2607.27230](https://arxiv.org/abs/2607.27230)
17. [Attention Residuals: When Residuals Start Attending To Themselves, S. Plessers blog](https://silkeplessers.github.io/deep-learning/transformers/2026/03/20/Attention_Residuals.html)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 现代人工智能：基础模型、生成式人工智能与人工智能产业 › Foundation-model methods and training › Large language model architecture and scaling

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/residual-stream

English original: https://www.edgepedia.org/residual-stream
