# 注意力蒸馏

注意力蒸馏是一种知识蒸馏方法，它训练一个较小的学生模型，通过额外添加的损失项来复现一个较大的教师模型（通常是 [Transformer](https://www.edgepedia.org/zh/transformer)）的注意力图或注意力权重。它属于知识蒸馏这一更大的方法家族——通过从教师模型迁移知识来改进学生模型的训练——并且有别于较早的卷积神经网络技术“注意力迁移”，后者蒸馏的是空间激活强度而非注意力图。<sup>[1](https://openreview.net/references/pdf?id=ryNuSVpex)</sup><sup> • </sup><sup>[2](https://proceedings.neurips.cc/paper_files/paper/2024/file/ceb45c48e29e7f49b0b47edb98e43691-Paper-Conference.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 迁移内容 | 注意力矩阵（softmax 前或 softmax 后）、自注意力分布，或由查询、键、值计算出的值关系<sup>[3](https://aclanthology.org/2020.findings-emnlp.372/)</sup><sup> • </sup><sup>[4](https://proceedings.neurips.cc/paper_files/paper/2020/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf)</sup> |
| 常用损失 | softmax 前矩阵上的均方误差、softmax 后分布上的 KL 散度、注意力图之间的交叉熵<sup>[3](https://aclanthology.org/2020.findings-emnlp.372/)</sup><sup> • </sup><sup>[5](https://ar5iv.labs.arxiv.org/html/2305.15032)</sup><sup> • </sup><sup>[2](https://proceedings.neurips.cc/paper_files/paper/2024/file/ceb45c48e29e7f49b0b47edb98e43691-Paper-Conference.pdf)</sup> |
| 层选择 | 仅最后一层（MiniLM）、均匀层映射（TinyBERT）、大教师模型取中上层（MiniLMv2），或搜索得到的映射<sup>[4](https://proceedings.neurips.cc/paper_files/paper/2020/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf)</sup><sup> • </sup><sup>[6](https://aclanthology.org/2021.findings-acl.188.pdf)</sup><sup> • </sup><sup>[7](https://ar5iv.labs.arxiv.org/html/2012.06153)</sup> |
| 头数不匹配 | 通过关系头、二分匹配、插值或线性挤头来处理<sup>[6](https://aclanthology.org/2021.findings-acl.188.pdf)</sup><sup> • </sup><sup>[2](https://proceedings.neurips.cc/paper_files/paper/2024/file/ceb45c48e29e7f49b0b47edb98e43691-Paper-Conference.pdf)</sup><sup> • </sup><sup>[8](https://arxiv.org/html/2502.07436)</sup> |
| 报告收益 | 在 6 层任务无关 BERT 蒸馏中，Att-MSE 在 GLUE 开发集上平均 81.3，而普通 KD 为 79.3；TinyBERT4 达到 BERT-Base GLUE 性能的 96.8% 以上，同时体积小 7.5 倍、速度快 9.4 倍<sup>[5](https://ar5iv.labs.arxiv.org/html/2305.15032)</sup><sup> • </sup><sup>[3](https://aclanthology.org/2020.findings-emnlp.372/)</sup> |
| 主要用途 | BERT 类语言模型压缩、视觉 Transformer、自监督 ViT、检测，以及扩散模型编辑<sup>[3](https://aclanthology.org/2020.findings-emnlp.372/)</sup><sup> • </sup><sup>[9](http://arxiv.org/pdf/2210.00944v1)</sup><sup> • </sup><sup>[10](https://openaccess.thecvf.com/content/CVPR2025/papers/Zhou_Attention_Distillation_A_Unified_Approach_to_Visual_Characteristics_Transfer_CVPR_2025_paper.pdf)</sup> |

## 工作原理

在 Transformer 中，每个头从查询和键计算出一个注意力矩阵，这个矩阵决定了信息如何在词元之间流动。注意力蒸馏添加一个损失，使学生模型的注意力矩阵与教师模型的相匹配，从而让学生学到教师模型词元间的路由方式，而不仅仅是最终输出。这正是它与辛顿式知识蒸馏（匹配最终输出）的既定区别。<sup>[2](https://proceedings.neurips.cc/paper_files/paper/2024/file/ceb45c48e29e7f49b0b47edb98e43691-Paper-Conference.pdf)</sup>

匹配目标因方法而异。TinyBERT 拟合第 \( i \) 个头的未归一化注意力矩阵 \( A_i \in \mathbb{R}^{l \times l} \)，而非其 softmax 输出 \( \mathrm{softmax}(A_i) \)，因为实验表明这样收敛更快、性能更好。<sup>[3](https://aclanthology.org/2020.findings-emnlp.372/)</sup> [MiniLM](https://www.edgepedia.org/zh/minilm) 最小化教师与学生自注意力分布之间的 KL 散度（该分布由查询和键的缩放点积计算），并额外迁移值关系（由值之间的多头缩放点积计算）。<sup>[4](https://proceedings.neurips.cc/paper_files/paper/2020/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf)</sup> NeurIPS 2024 的公式使用学生与教师注意力图之间的交叉熵，

\[ L_{\mathrm{dist}} = \langle H_h\, \mathrm{softmax}(Q_s \cdot K_s^{\top}),\ \mathrm{softmax}(Q_t \cdot K_t^{\top}) \rangle, \]

其中 \( H \) 计算交叉熵，对所有施加蒸馏的头和层求和，并由超参数 \( \lambda \) 加权，以与任务损失平衡。<sup>[2](https://proceedings.neurips.cc/paper_files/paper/2024/file/ceb45c48e29e7f49b0b47edb98e43691-Paper-Conference.pdf)</sup>

损失的选择很重要。在从 BERT-Base 蒸馏 6 层学生的任务无关蒸馏中，softmax 前矩阵上的 Att-MSE 优于 softmax 后分布上的 Att-KL，后者与普通 KD 表现相近；作者假设 MSE 比 KL 散度提供了更直接的匹配。<sup>[5](https://ar5iv.labs.arxiv.org/html/2305.15032)</sup> 在 BERT-Base 的量化感知训练中，顺序则相反：KL 散度注意力图损失优于先前在注意力分数上取 MSE 的技术。<sup>[11](https://ar5iv.labs.arxiv.org/html/2211.11014)</sup>

## 具体做法

按照 TinyBERT 的典型方案，通过层映射函数将每个学生层 \( m \) 与教师层 \( g(m) \) 配对，嵌入层索引为 0，预测层索引为 \( M+1 \)。嵌入层用嵌入损失匹配，中间层结合注意力损失和隐藏状态损失，预测层匹配教师的 logits。隐藏状态损失为 \( L_{\mathrm{hidn}} = \mathrm{MSE}(H^S \cdot W_h, H^T) \)，其中 \( W_h \) 是将学生隐藏状态映射到教师空间的可学习线性变换。<sup>[3](https://aclanthology.org/2020.findings-emnlp.372/)</sup>

TinyBERT 的框架分两个阶段：以预训练 BERT_BASE 为教师，在英文维基百科上进行通用蒸馏；然后以微调后的 BERT 为教师模型，在数据增强下进行任务特定蒸馏。<sup>[3](https://aclanthology.org/2020.findings-emnlp.372/)</sup> 初始化很重要：在任务特定蒸馏中，从教师较低层初始化学生，使普通 KD 在 QNLI 上从 68.1% 提升到 85.9%，而注意力迁移在不同初始化设置下表现一致。<sup>[5](https://ar5iv.labs.arxiv.org/html/2305.15032)</sup> 层映射本身可以搜索：在映射 \( g(m) \) 上使用遗传算法，配合逐层损失 \( L_{\mathrm{KD}} = \sum_x \sum_m \lambda_m \cdot L_{\mathrm{layer}}(S_m(x), T_{g(m)}(x)) \)，始终优于均匀策略和仅最后一层策略。<sup>[7](https://ar5iv.labs.arxiv.org/html/2012.06153)</sup>

## 起源

注意力迁移论文提及了知识蒸馏的来源。<sup>[1](https://openreview.net/references/pdf?id=ryNuSVpex)</sup> ConvNet 的注意力迁移将注意力定义为空间激活图，并通过损失 \( L_{\mathrm{AT}} = L(W_S, x) + \sum_{j \in I} \lVert Q_j^S - Q_j^T \rVert \) 在 \( \ell_2 \) 归一化图上进行迁移；文中强调，注意力图的归一化对学生训练的成功很重要，并且注意力迁移与辛顿式蒸馏结合几乎不增加额外成本。<sup>[1](https://openreview.net/references/pdf?id=ryNuSVpex)</sup>

Transformer 时代的形式化伴随 TinyBERT（焦等人，2019，arXiv）而来，它在隐藏状态损失和预测损失之外加入了注意力矩阵匹配，<sup>[12](https://doi.org/10.48550/arxiv.1909.10351)</sup> 以及 MiniLM（王等人，2020，NeurIPS），它引入了对教师最后一层的深层自注意力蒸馏。<sup>[4](https://proceedings.neurips.cc/paper_files/paper/2020/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf)</sup> [MobileBERT](https://www.edgepedia.org/zh/mobilebert)（孙等人，2019）将注意力图蒸馏与瓶颈结构和教师的层数相结合。

## 变体

**激活迁移与注意力图迁移。** “注意力迁移”这一名称此前用于蒸馏 ConvNet 中的空间激活强度，与注意力图蒸馏是相当不同的方法；对于 ConvNet，注意力并非显式计算，因此需要额外计算和注意力定义。<sup>[2](https://proceedings.neurips.cc/paper_files/paper/2024/file/ceb45c48e29e7f49b0b47edb98e43691-Paper-Conference.pdf)</sup><sup> • </sup><sup>[9](http://arxiv.org/pdf/2210.00944v1)</sup>

**TinyBERT** 用按头平均的 MSE 匹配未归一化注意力矩阵。<sup>[3](https://aclanthology.org/2020.findings-emnlp.372/)</sup> **MiniLM** 用 KL 散度蒸馏最后一层的注意力分布和值关系。<sup>[4](https://proceedings.neurips.cc/paper_files/paper/2020/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf)</sup> **MiniLMv2**（王等人，2021）将其推广到由查询、键、值两两之间缩放点积计算的多头自注意力关系，消除了教师与学生注意力头数必须相同的限制；它将头向量拼接后重新拆分为所需数量的关系头。<sup>[6](https://aclanthology.org/2021.findings-acl.188.pdf)</sup>

**注意力复制与注意力蒸馏。** 注意力复制将教师的注意力图直接复制粘贴到随机初始化的学生模型中；注意力蒸馏则训练学生在蒸馏损失下计算自己的注意力图，训练后不再需要教师模型。<sup>[2](https://proceedings.neurips.cc/paper_files/paper/2024/file/ceb45c48e29e7f49b0b47edb98e43691-Paper-Conference.pdf)</sup> **AttnDistill** 将类别词元上的投影器对齐损失与注意力引导的 KL 损失相结合，\( L = L_c + \lambda \cdot L_a \)，用于自监督视觉 Transformer。<sup>[9](http://arxiv.org/pdf/2210.00944v1)</sup> **挤头蒸馏** 通过高效线性近似将多头教师注意力图压缩到更少的学生头中，使得不同头数的模型之间无需投影器或架构改动即可迁移。<sup>[8](https://arxiv.org/html/2502.07436)</sup>

## 应用

主要应用是 BERT 类语言模型的任务无关和任务特定压缩、视觉 Transformer 压缩、自监督 ViT 蒸馏以及稠密预测。4 层的 TinyBERT4 达到 BERT-Base GLUE 性能的 96.8% 以上，同时体积小 7.5 倍、推理速度快 9.4 倍；TinyBERT6 与其教师表现相当。<sup>[3](https://aclanthology.org/2020.findings-emnlp.372/)</sup> 对于从 BERT-Base [蒸馏](https://www.edgepedia.org/zh/distillation)的 6 层任务无关学生，Att-MSE 在 GLUE 开发集上平均 81.3，而普通 KD 为 79.3。<sup>[5](https://ar5iv.labs.arxiv.org/html/2305.15032)</sup> MiniLM 的单语模型在 SQuAD 2.0 和若干 GLUE 任务上保持 99% 以上的准确率，仅使用教师 Transformer 参数和计算量的 50%。<sup>[4](https://proceedings.neurips.cc/paper_files/paper/2020/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf)</sup> 对于视觉 Transformer，注意力蒸馏在 ImageNet-1K 微调上达到 85.7 的准确率，与从 MAE 微调 ViT-L 权重相当；在 448×448 输入的 ViTDet COCO 检测中，它恢复了预训练的大部分收益。<sup>[2](https://proceedings.neurips.cc/paper_files/paper/2024/file/ceb45c48e29e7f49b0b47edb98e43691-Paper-Conference.pdf)</sup> 一篇 CVPR 2025 论文将注意力蒸馏损失应用于 [Stable Diffusion](https://www.edgepedia.org/zh/stable-diffusion) 的 UNet 自注意力模块，以在图像之间迁移视觉特征。<sup>[13](https://doi.org/10.48550/arxiv.2502.20235)</sup>

## 局限与替代方案

**架构不匹配是主要的失败模式。** 在来自 11 个家族的 20 个教师 ViT 基准上，注意力迁移对 7 个家族成功，但对 4 个家族持续失败，最多比从头训练的无迁移基线低 5.1%；这种失败在同一家族的不同规模模型中一致出现，并在更长训练、不同迁移数据集和分布外评估下持续存在。主要机制是教师与学生之间的架构不匹配：将教师原生架构组件以随机初始化状态加入学生后，所有 4 个家族的失败完全逆转。<sup>[14](https://arxiv.org/html/2605.07191v1)</sup>

**头数和分辨率不匹配** 需要特殊处理。TinyBERT 和 MobileBERT 要求教师与学生头数匹配，这是一道对齐障碍。<sup>[8](https://arxiv.org/html/2502.07436)</sup> AttnDistill 通过双三次插值教师图再重新归一化使其和为 1，来处理头数相同但图像块数不同的情况；通过头注意力的对数求和聚合再 softmax，来处理图像块数相同但头数不同的情况。<sup>[9](http://arxiv.org/pdf/2210.00944v1)</sup> NeurIPS 2024 的工作通过对对称 Jensen-Shannon 散度进行二分匹配来配对跨模型的头，因为头顺序在不同模型间并不对齐。<sup>[2](https://proceedings.neurips.cc/paper_files/paper/2024/file/ceb45c48e29e7f49b0b47edb98e43691-Paper-Conference.pdf)</sup>

**层选择也很重要。** MiniLM 发现蒸馏教师最后一层优于逐层蒸馏，并缓解了层映射的困难，<sup>[4](https://proceedings.neurips.cc/paper_files/paper/2020/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf)</sup> 而 MiniLMv2 发现对于大教师模型，中上层效果最好（BERT_LARGE 用第 21 层，RoBERTa_LARGE 和 XLM-R_LARGE 用第 19 层），对于基础规模的教师模型则用最后一层。<sup>[6](https://aclanthology.org/2021.findings-acl.188.pdf)</sup>

与量化感知训练相比，注意力图蒸馏作为一个组件发挥作用：对于 BERT-Base 的量化感知训练，KL 注意力图损失优于在注意力分数上的 MSE。<sup>[11](https://ar5iv.labs.arxiv.org/html/2211.11014)</sup>

## References

1. [Paying More Attention to Attention: Improving the Performance of Convolutional Neural Networks via Attention Transfer (ICLR 2017)](https://openreview.net/references/pdf?id=ryNuSVpex)
2. [On the Surprising Effectiveness of Attention Transfer for Vision Transformers (NeurIPS 2024)](https://proceedings.neurips.cc/paper_files/paper/2024/file/ceb45c48e29e7f49b0b47edb98e43691-Paper-Conference.pdf)
3. [TinyBERT: Distilling BERT for Natural Language Understanding (Findings of EMNLP 2020)](https://aclanthology.org/2020.findings-emnlp.372/)
4. [MiniLM: Deep Self-Attention Distillation for Task-Agnostic Compression of Pre-Trained Transformers (NeurIPS 2020)](https://proceedings.neurips.cc/paper_files/paper/2020/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf)
5. [How to Distill your BERT: An Empirical Study on the Impact of Weight Initialisation and Distillation Objectives](https://ar5iv.labs.arxiv.org/html/2305.15032)
6. [MiniLMv2: Multi-Head Self-Attention Relation Distillation for Compressing Pretrained Transformers (Findings of ACL 2021)](https://aclanthology.org/2021.findings-acl.188.pdf)
7. [Improving Task-Agnostic BERT Distillation with Layer Mapping Search](https://ar5iv.labs.arxiv.org/html/2012.06153)
8. [Optimizing Knowledge Distillation in Transformers: Enabling Multi-Head Attention without Alignment Barriers (Squeezing-Heads Distillation)](https://arxiv.org/html/2502.07436)
9. [AttnDistill: Knowledge Distillation of Self-Supervised Vision Transformers](http://arxiv.org/pdf/2210.00944v1)
10. [Attention Distillation: A Unified Approach to Visual Characteristics Transfer (CVPR 2025)](https://openaccess.thecvf.com/content/CVPR2025/papers/Zhou_Attention_Distillation_A_Unified_Approach_to_Visual_Characteristics_Transfer_CVPR_2025_paper.pdf)
11. [Understanding and Improving Knowledge Distillation for Quantization-Aware Training of Large Transformer Encoders (EMNLP 2022)](https://ar5iv.labs.arxiv.org/html/2211.11014)
12. [Jiao, Xiaoqi and colleagues (2019). TinyBERT: Distilling BERT for Natural Language Understanding. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1909.10351)
13. [Zhou, Yang and colleagues (2025). Attention Distillation: A Unified Approach to Visual Characteristics Transfer. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.2502.20235)
14. [Attention Transfer Is Not Universally Effective for Vision Transformers](https://arxiv.org/html/2605.07191v1)

---
*Initially written Sep 29, 2026 · Edited: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Machine learning and neural computation › Neural networks and deep learning › Attention and transformer training topics

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/attention-distillation

English original: https://www.edgepedia.org/attention-distillation
