# Logit归因

**Logit归因**，最常被称为**直接Logit归因**（direct logit attribution，DLA），是一种面向Transformer语言模型的可解释性方法。它通过将每个内部组件（如注意力头和MLP层）的残差流写入投影到反嵌入方向上，把模型的输出logit分解为各组件贡献之和。它回答的问题是：在一次前向传播中，哪些内部组件直接促进或抑制了某个特定的下一词元预测。<sup>[1](https://loganthomson.com/Direct-Logit-Attribution/)</sup><sup> • </sup><sup>[2](https://nnsight.net/vllm/attribution/)</sup>

| 关键事实 | 详情 |
|---|---|
| 计算内容 | 将每个组件的输出投影到目标词元的反嵌入行上，或投影到两个词元行之差上以得到logit差值<sup>[2](https://nnsight.net/vllm/attribution/)</sup> |
| 为何有效 | 残差流是可加的，因此它的任何线性函数都能精确分解为逐组件的贡献<sup>[1](https://loganthomson.com/Direct-Logit-Attribution/)</sup> |
| 归一化 | 将LayerNorm或RMSNorm线性化——把范数统计量固定为前向传播中的实际值，使该次传播的分解精确成立<sup>[1](https://loganthomson.com/Direct-Logit-Attribution/)</sup> |
| 起源 | 由《Transformer电路的数学框架》（Elhage等，2021）提出；CoNLL 2025教程将其归功于Nostalgebraist（2020）和Elhage等（2021）<sup>[3](https://transformer-circuits.pub/2021/framework/index.html)</sup><sup> • </sup><sup>[4](https://aclanthology.org/2025.conll-1.8.pdf)</sup> |
| 解释 | DLA衡量的是组件对残差流的直接贡献，而非其总因果效应——后者包含下游交互<sup>[1](https://loganthomson.com/Direct-Logit-Attribution/)</sup> |
| 标准工具 | TransformerLens的`direct_logit_attribution`支持组件、层和头三种粒度，默认使用最后一个词元位置<sup>[5](https://transformerlensorg.github.io/TransformerLens/_static/coverage/z_a0fb5fb066fd825f_direct_logit_attribution_py.html)</sup> |

## 残差流与分解

Transformer语言模型从一个logit向量（每个词表词元对应一个分量）产生下一词元的概率分布。在《Transformer电路的数学框架》的框架中，模型的计算被描述为一条共享的残差流：每个块从中读取，并通过加法向其写入。残差流从零开始，接收词元嵌入和位置嵌入，然后逐层累加每个注意力层和每个MLP层的输出。最终的logit为`Unembed(LayerNorm(final_residual_stream))`。<sup>[1](https://loganthomson.com/Direct-Logit-Attribution/)</sup><sup> • </sup><sup>[3](https://transformer-circuits.pub/2021/framework/index.html)</sup>

由于模型对残差流的操作只有加法，最终状态就是所有组件输出之和。反嵌入是一个线性映射，因此logit也是各贡献之和。每个注意力层的输出可进一步分解为其各注意力头输出之和，每个MLP层的输出可分解为其各神经元输出之和再加上一个偏置项。<sup>[1](https://loganthomson.com/Direct-Logit-Attribution/)</sup><sup> • </sup><sup>[6](https://learn.arena.education/chapter1_transformer_interp/21_ioi/2-logit-attribution/)</sup>

**处理归一化。** 读出阶段唯一的非线性来自最终的归一化。LayerNorm减去残差向量的均值并除以标准差；RMSNorm（用于Llama和Qwen等架构）则除以均方根。这两种统计量都是整条残差流的非线性函数。然而，若要分解某一次特定的前向传播，只需将统计量固定为实际计算出的值，而除以这个固定标量是线性的，因此逐组件分解对该次传播精确成立。归一化中学到的缩放和偏移参数线性地作用于每个贡献，而偏移项（LayerNorm中的beta项）作为单独的常数贡献附加，编码了一元词元统计量。<sup>[1](https://loganthomson.com/Direct-Logit-Attribution/)</sup>

于是算法为：运行一次前向传播，记录每个组件的输出和固定的范数统计量；对每个贡献进行中心化和缩放；应用反嵌入矩阵得到逐组件的logit贡献。由于反嵌入偏置`b_U`是每个词元的常数、不由任何组件产生，完整的分解重构的是`logit[token] - b_U[token]`而非原始logit。<sup>[1](https://loganthomson.com/Direct-Logit-Attribution/)</sup><sup> • </sup><sup>[5](https://transformerlensorg.github.io/TransformerLens/_static/coverage/z_a0fb5fb066fd825f_direct_logit_attribution_py.html)</sup>

## Logit差值指标

实践者通常归因的是正确回答词元与错误回答词元之间的**logit差值**，而非单个logit。模型以交叉熵损失训练，优化的是对数概率而非原始logit；给所有logit加上任意常数不改变概率。但对数概率之差等于logit之差，因为从每个logit中减去的logsumexp项在相减时抵消了。因此，归因两个反嵌入行之间的差值可以分离出所关心的能力，并抵消常数偏移。<sup>[6](https://learn.arena.education/chapter1_transformer_interp/21_ioi/2-logit-attribution/)</sup>

该指标还能控制词频伪影。在间接宾语识别（IOI）设置中，提示词会以两个候选名字互换的方式重复出现；这控制了一个事实：GPT-2 small的最终LayerNorm偏置使John相对于Mary的logit差值增加1，这是名字频率而非任务逻辑的效应。<sup>[6](https://learn.arena.education/chapter1_transformer_interp/21_ioi/2-logit-attribution/)</sup>

## 在电路分析中的使用

DLA通常是逆向工程一个电路的第一步：从输出出发，找出哪些组件显著地朝正确答案方向写入，然后用激活修补和注意力模式分析等互补技术回溯。<sup>[6](https://learn.arena.education/chapter1_transformer_interp/21_ioi/2-logit-attribution/)</sup><sup> • </sup><sup>[1](https://loganthomson.com/Direct-Logit-Attribution/)</sup>在对GPT-2 small的IOI研究中，Wang等（2023）对logit差值使用直接效应分析识别出名称移动头，并通过路径修补追踪到控制其注意力的S-抑制头；所得电路包含26个注意力头、分为7类，达到了完整模型logit差值的87%（忠实性差距为0.46，完整模型logit差值为3.56）。<sup>[7](https://arxiv.org/abs/2211.00593)</sup>

应用研究使用同样的分解。一项金融服务的可解释性研究使用DLA衡量GPT-2 small检测潜在公平借贷违规时各层和各头对logit差值的贡献，发现头10.2、10.7和11.3有正贡献，头9.6和10.6有负贡献。<sup>[8](https://arxiv.org/abs/2407.11215)</sup> nnsight库中一个Qwen3-8B的完整示例展示了分解重现了模型自身的巴黎减柏林logit差值（计算值7.03对实际值7.06，差距来自bfloat16精度），且答案写入较晚：第31层的注意力单独贡献2.6，第27至31层的MLP各贡献0.7至1.7，最后的MLP以−1.1和−1.7的贡献对抗答案。<sup>[2](https://nnsight.net/vllm/attribution/)</sup>

## 直接效应与因果效应

DLA只衡量组件输出对最终logit的直接贡献。它不衡量总因果效应：消融一个组件还会改变所有下游计算，包括后续组件所消费的范数统计量，而DLA的固定范数线性化只对描述实际发生的那次传播精确成立。因此，DLA最好理解为将logit分解为直接贡献，而非因果分析；DLA值小并不意味着组件不重要，因为其影响可能通过其他组件间接流动。<sup>[1](https://loganthomson.com/Direct-Logit-Attribution/)</sup><sup> • </sup><sup>[9](https://learnmechinterp.com/topics/direct-logit-attribution/)</sup>

实证结果印证了这一点。一项关于基于Transformer的符号回归的研究发现，DLA和[探针分类器](https://www.edgepedia.org/zh/probing-classifier)与模型实际用于因果分析的组件之间没有可靠的相关性。<sup>[10](https://pure.uva.nl/ws/files/296572627/2602.03506v1.pdf)</sup>在奖励模型上（其标量回归头取代了词表反嵌入），一份2026年的库报告发现线性归因完全无法预测因果修补效应，在Skywork上的平均Spearman相关系数为−0.256，在ArmoRM上为−0.027。<sup>[11](https://arxiv.org/abs/2604.26130)</sup>

## 实践要点与近期进展

实现细节很重要。TransformerLens的`direct_logit_attribution`提供三种分解粒度：`component`（嵌入加上每层的注意力和MLP输出）、`layer`（每个子层之后的累积残差流，即logit透镜轨迹）和`head`（每个注意力头单独加上一个余项）。它默认使用最终序列位置——下一词元归因的常用选择——并对Mamba或SSM等混合架构显式报错，因为分解尚不支持这些模型的块布局。<sup>[5](https://transformerlensorg.github.io/TransformerLens/_static/coverage/z_a0fb5fb066fd825f_direct_logit_attribution_py.html)</sup>

近似的主要来源是最终归一化。2025年的一系列工作通过微调从GPT-2系列中移除了所有LayerNorm层，GPT-2 XL的交叉熵损失仅增加+0.03；在这些无LN模型中，DLA给出各组件精确的直接效应，无需线性化步骤，尽管归因修补的准确率并未显著提高。<sup>[12](https://arxiv.org/abs/2507.02559)</sup>该方法还被扩展到原始组件之外：[微软研究院](https://www.edgepedia.org/zh/microsoft-research)的对比归因工作将错误输出词元与正确替代词元之间的logit差值归因到输入词元和内部状态，为长上下文失败分析构建跨层归因图。<sup>[13](https://www.microsoft.com/en-us/research/publication/contrastive-attribution-in-the-wild-an-interpretability-analysis-of-llm-failures-on-realistic-benchmarks/?lang=zh-cn)</sup>

## References

1. [Logan Thomson. Direct Logit Attribution.](https://loganthomson.com/Direct-Logit-Attribution/)
2. [Direct logit attribution, nnsight documentation (vLLM).](https://nnsight.net/vllm/attribution/)
3. [Elhage, N., Nanda, N., Olsson, C., et al. (2021). A Mathematical Framework for Transformer Circuits. Transformer Circuits Thread.](https://transformer-circuits.pub/2021/framework/index.html)
4. [The Transformer (tutorial chapter), Proceedings of CoNLL 2025.](https://aclanthology.org/2025.conll-1.8.pdf)
5. [TransformerLens source coverage: direct_logit_attribution.py.](https://transformerlensorg.github.io/TransformerLens/_static/coverage/z_a0fb5fb066fd825f_direct_logit_attribution_py.html)
6. [ARENA, Chapter 1: Transformer Interpretability, section 2: Logit Attribution.](https://learn.arena.education/chapter1_transformer_interp/21_ioi/2-logit-attribution/)
7. [Wang, K., Variengien, A., Conmy, A., Shlegeris, B., Steinhardt, J. (2023). Interpretability in the Wild: a Circuit for Indirect Object Identification in GPT-2 small. arXiv:2211.00593.](https://arxiv.org/abs/2211.00593)
8. [Golgoon, A., Filom, K., Kannan, A. R. (2024). Mechanistic interpretability of large language models with applications to the financial services industry. arXiv:2407.11215.](https://arxiv.org/abs/2407.11215)
9. [Learn Mechanistic Interpretability: Direct Logit Attribution.](https://learnmechinterp.com/topics/direct-logit-attribution/)
10. [Explaining the Explainer: Understanding the Inner Workings of Transformer-based Symbolic Regression Models, University of Amsterdam.](https://pure.uva.nl/ws/files/296572627/2602.03506v1.pdf)
11. [Nadaf, M. S. B. (2026). reward-lens: A Mechanistic Interpretability Library for Reward Models. arXiv:2604.26130.](https://arxiv.org/abs/2604.26130)
12. [Baroni, L., Khara, G., Schaeffer, J., Subkhankulov, M., Heimersheim, S. (2025). Transformers Don't Need LayerNorm at Inference Time. arXiv:2507.02559.](https://arxiv.org/abs/2507.02559)
13. [Tan, R., Zhang, J., Li, Z., Lin, Q., Rajmohan, S., Zhang, D. (2026). Contrastive Attribution in the Wild: An Interpretability Analysis of LLM Failures on Realistic Benchmarks. Microsoft Research.](https://www.microsoft.com/en-us/research/publication/contrastive-attribution-in-the-wild-an-interpretability-analysis-of-llm-failures-on-realistic-benchmarks/?lang=zh-cn)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 现代人工智能：基础模型、生成式人工智能与人工智能产业 › Foundation-model methods and training › Safety methods, interpretability, and red-teaming › Probing and attribution methods

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/logit-attribution

English original: https://www.edgepedia.org/logit-attribution
