# 交叉注意力网络

交叉注意力网络是一种神经网络架构，它通过注意力层将两个输入序列融合或对齐，其中一路流提供查询，另一路流提供键和值，于是第一路流的每个元素都从第二路流中检索并聚合信息。这与自[注意力](https://www.edgepedia.org/zh/attention)不同：自注意力从单一序列中提取查询、键和值；而在交叉注意力中，查询来自序列 A，键和值来自序列 B，输出的长度与 A 相同，同时聚合来自 B 的信息。<sup>[1](https://d2l.smola.org/chapter_attention/multihead-attention.html)</sup> 由于查询、键和值张量可以来自两个或更多不同的模态或序列，这一设计成为多模态与序列对任务的标准融合机制，例如视觉-语言建模、机器翻译以及生成模型的文本条件化。<sup>[2](https://dl.acm.org/doi/10.1145/3649447)</sup>

| 关键事实 | 细节 |
|---|---|
| 核心计算 | \( O = \text{softmax}(Q \cdot K^{T}/\sqrt{d}) \cdot V \)，其中查询来自一个输入，键/值来自另一个输入<sup>[3](https://arxiv.org/html/2502.02406v3)</sup> |
| 输出形状 | 长度等于查询序列的长度，与键/值序列的长度无关<sup>[1](https://d2l.smola.org/chapter_attention/multihead-attention.html)</sup> |
| 开销 | 计算相似度矩阵的开销为 \( O(N \cdot M) \)；当潜在长度 \( M \ll N \) 固定时，这是线性的 \( O(N) \)，而自注意力为 \( O(N^{2}) \)<sup>[4](https://proceedings.neurips.cc/paper_files/paper/2024/file/ab1ee157f7804a13f980414b644a9460-Paper-Conference.pdf)</sup> |
| 典型接线 | 在 Transformer 解码器中，查询来自解码器流，键和值来自编码器输出<sup>[5](https://proceedings.neurips.cc/paper_files/paper/2017/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf)</sup> |
| 效率收益 | 一种分布式交叉注意力实现报告称，在视觉输入较大的交叉注意力多模态大语言模型上，端到端加速最高可达 10.62 倍，同时指出交叉注意力层本身可能成为训练和推理的关键瓶颈<sup>[3](https://arxiv.org/html/2502.02406v3)</sup> |
| 精度权衡 | 在 Flickr8k 上使用预对齐的 CLIP 特征时，拼接在所有测试规模上均比交叉注意力高出 4.1–5.1 个百分点<sup>[6](https://arxiv.org/html/2606.01207v1)</sup> |

## 工作原理

注意力函数将一个查询和一组键-值对映射为一个输出，输出是值的加权和，每个权重来自查询与对应键的兼容性函数。<sup>[5](https://proceedings.neurips.cc/paper_files/paper/2017/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf)</sup> 在交叉注意力层所使用的缩放点积形式中，输出为 \( O = \text{softmax}(Q \cdot K^{T}/\sqrt{d}) \cdot V \)：查询与键的点积除以 \( \sqrt{d} \)，softmax 将其转化为权重，权重再对值进行混合。<sup>[3](https://arxiv.org/html/2502.02406v3)</sup><sup> • </sup><sup>[5](https://proceedings.neurips.cc/paper_files/paper/2017/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf)</sup>

[多头注意力](https://www.edgepedia.org/zh/multi-head-attention)并行运行该操作 \( h \) 次：查询、键和值分别通过学到的投影线性投影 \( h \) 次，投影到维度 \( d_{k} \)、\( d_{k} \) 和 \( d_{v} \)，各头的输出被拼接，结果再经过 \( W^{O} \) 投影，使模型能够关注来自不同表示子空间的信息。<sup>[5](https://proceedings.neurips.cc/paper_files/paper/2017/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf)</sup><sup> • </sup><sup>[7](https://docs.pytorch.org/docs/stable/generated/torch.nn.modules.activation.MultiheadAttention.md)</sup> 当每头宽度为 \( d/h \) 时，参数数量和 FLOP 数与头数无关，因此实现时可以将头轴折叠进批次轴。<sup>[1](https://d2l.smola.org/chapter_attention/multihead-attention.html)</sup>

开销直接由两个序列的长度决定。计算长度为 \( N \) 的目标与长度为 \( M \) 的源之间的缩放成对相似度矩阵，开销为 \( O(N \cdot M) \)；当两者相同（如自注意力中那样）时，这变为 \( O(N^{2}) \)，而具有固定 \( M \ll N \) 的交叉注意力对 \( N \) 是线性的。<sup>[4](https://proceedings.neurips.cc/paper_files/paper/2024/file/ab1ee157f7804a13f980414b644a9460-Paper-Conference.pdf)</sup> 由于 \( Q \) 的形状为 \( n \times d \)，而 \( K \) 和 \( V \) 的形状为 \( m \times d \)，沿长度维度的逐元素操作（例如 XCiT 式注意力中使用的交叉协方差）并不适用。<sup>[8](https://proceedings.mlr.press/v202/zhang23r/zhang23r.pdf)</sup>

## 实现方式

接线决策的关键在于哪一路流提供查询。在最初的编码器-解码器 [Transformer](https://www.edgepedia.org/zh/transformer) 中，双向编码器读取源序列，因果解码器生成目标，同时交叉关注编码器的输出：查询来自目标流，键和值来自源，因此每个解码器位置都关注所有输入位置。<sup>[5](https://proceedings.neurips.cc/paper_files/paper/2017/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf)</sup><sup> • </sup><sup>[9](https://d2l.smola.org/chapter_transformers/encoders-decoders.html)</sup> 在典型的多模态融合模块中，查询从一个模态投影而来，\( Q = W_{q} \cdot x^{t} \)，而键和值来自另一个模态，\( K = W_{k} \cdot x^{v} \) 和 \( V = W_{v} \cdot x^{v} \)，随后计算 \( \alpha = \text{softmax}(Q \cdot K^{T}/\sqrt{d_{k}}) \) 和 \( h_{\text{cross}} = \alpha \cdot V \)。<sup>[6](https://arxiv.org/html/2606.01207v1)</sup>

在代码中，两个输入通过条件分支接线：在 Hugging Face Diffusers 的实现中，当提供 `encoder_hidden_states` 时，键和值从它投影而来，而查询来自隐藏状态；否则该层回退为对相同状态的自注意力。<sup>[10](https://github.com/huggingface/diffusers/blob/4125756e88e82370c197fecf28e9f0b4d7eee6c3/src/diffusers/models/cross_attention.py)</sup> 注意力分数计算为查询与转置键的批量矩阵乘法，乘以缩放因子，并可选地加上注意力掩码，随后经过 softmax 并与值相乘。<sup>[10](https://github.com/huggingface/diffusers/blob/4125756e88e82370c197fecf28e9f0b4d7eee6c3/src/diffusers/models/cross_attention.py)</sup> PyTorch 的 `torch.nn.MultiheadAttention` 以任意的查询、键和值输入暴露相同的操作。<sup>[7](https://docs.pytorch.org/docs/stable/generated/torch.nn.modules.activation.MultiheadAttention.md)</sup>

## 起源

最早被广泛使用的两个序列间的注意力是 Bahdanau、Cho 和 Bengio（2014）用于神经机器翻译的编码器-解码器注意力，其中权重 \( \alpha_{ij} \) 反映了在生成 \( y_{i} \) 时编码器标注 \( h_{j} \) 相对于解码器前一隐藏状态的重要性；这一机制是交叉注意力层所推广的前身。<sup>[11](https://doi.org/10.48550/arxiv.1409.0473)</sup> 上述缩放点积和多头公式即典型的交叉注意力层，实现为解码器到编码器的注意力。<sup>[5](https://proceedings.neurips.cc/paper_files/paper/2017/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf)</sup>

对于对称地融合两个序列，注意力综述将这一概念归功于协同注意力（co-attention），它让视觉问答模型同时关注图像和问题，使图像注意力和问题注意力相互引导。<sup>[12](https://ar5iv.labs.arxiv.org/html/1904.02874)</sup><sup> • </sup><sup>[13](https://ar5iv.labs.arxiv.org/html/2203.14263)</sup> 同年，Xiong、Zhong 和 Socher 在他们用于问答的动态协同注意力网络中提出了同时关注问题和文档的协同注意力机制，这是对两个文本序列的早期应用。<sup>[14](https://arxiv.org/pdf/1611.01604)</sup>

## 变体

协同注意力本身分为几种接线方式。粗粒度协同注意力使用一个特征矩阵的紧凑表示作为另一个的查询；细粒度协同注意力使用一个输入的所有特征向量作为查询；交替协同注意力将一个注意力模块的上下文向量作为另一个的查询，这是顺序的、不可并行化；而交互式协同注意力使用键向量的不加权平均作为查询，在两个特征矩阵上并行计算注意力。<sup>[13](https://ar5iv.labs.arxiv.org/html/2203.14263)</sup>

若干命名架构围绕该机制构建。Jaegle 及其同事（2021）的 Perceiver 使用交叉注意力模块将高维输入数组投影到固定的潜在瓶颈上，然后用自注意力处理潜在表示，并迭代地重新查询输入；Perceiver IO（2021）增加了一个解码器，使用输出查询，并以潜在表示作为键和值，因此输出形状由查询设定。<sup>[15](https://doi.org/10.48550/arxiv.2103.03206)</sup><sup> • </sup><sup>[16](https://doi.org/10.48550/arxiv.2107.14795)</sup> [DETR](https://www.edgepedia.org/zh/detr) 使用约一百个学到的对象查询交叉关注图像特征，每个产生一个检测。<sup>[9](https://d2l.smola.org/chapter_transformers/encoders-decoders.html)</sup> Flamingo（2022）通过插入在现有层之间的门控交叉注意力层，以视觉输入对冻结的大语言模型进行条件化，学到的 tanh 门初始化为零，因此在初始化时保留了文本能力。<sup>[17](https://doi.org/10.48550/arxiv.2204.14198)</sup><sup> • </sup><sup>[18](https://raw.githubusercontent.com/rohitg00/ai-engineering-from-scratch/main/phases/12-multimodal-ai/04-flamingo-gated-cross-attention/docs/en.md)</sup> CrossViT 用交叉注意力融合两个视觉 Transformer 分支，其中一个分支只有 CLS 令牌查询另一分支的图像块令牌，使注意力图在计算和内存上都是线性的。<sup>[19](https://arxiv.org/pdf/2103.14899.pdf)</sup> BiXT 用双向交叉注意力取代 Perceiver 的迭代注意力，其中输入令牌和潜在表示同时相互关注。<sup>[4](https://proceedings.neurips.cc/paper_files/paper/2024/file/ab1ee157f7804a13f980414b644a9460-Paper-Conference.pdf)</sup> DeepCrossAttention（DCA）重构了残差流本身，在增加可忽略不计的参数量的同时，以最高 3 倍的速度获得相同的模型质量。<sup>[20](https://proceedings.mlr.press/v267/heddes25a.html)</sup>

## 应用

交叉注意力是两路流必须交互时的标准做法。它应用于机器翻译、数据到文本生成和知识增强模型，在这些模型中它跨序列整合特征，而自注意力在单个序列内构建上下文特征。<sup>[8](https://proceedings.mlr.press/v202/zhang23r/zhang23r.pdf)</sup> 在语音领域，Whisper 的编码器双向读取整个音频片段，而文本解码器交叉关注它。<sup>[9](https://d2l.smola.org/chapter_transformers/encoders-decoders.html)</sup> 在多模态大语言模型中，交叉注意力层在 Flamingo、Otter、IDEFICS 和 NVLM-X 中交错于大语言模型块之间。<sup>[3](https://arxiv.org/html/2502.02406v3)</sup> Perceiver 通过相同的交叉注意力接口处理图像、点云、音频和视频，在没有二维卷积的情况下在 ImageNet 上表现与 ResNet-50 和 ViT 相当。<sup>[15](https://doi.org/10.48550/arxiv.2103.03206)</sup> 在文本到图像扩散模型中，交叉注意力以文本嵌入对 U-Net 进行条件化：\( C_{c}^{t} = \text{softmax}(Q_{z}^{t} \cdot K_{c}^{\top}/\sqrt{d}) \cdot V_{c} \)，其中 \( Q_{z}^{t} \) 投影含噪潜在表示，\( K_{c} \)、\( V_{c} \) 投影文本嵌入。<sup>[21](https://arxiv.org/html/2404.02747v1)</sup>

## 局限与替代方案

主要的开销局限在于两个序列长度乘积的二次增长。在 Llama 3-V 中，对 2048 令牌的文本序列和以 1 fps 计的 20 分钟视频应用交叉注意力，即使使用内存高效的注意力，也需要超过 234 GB 的内存。<sup>[3](https://arxiv.org/html/2502.02406v3)</sup> 交叉注意力还带来样本复杂度惩罚：学习其双线性注意力权重需要 \( O(d_{v} \cdot d_{t}) \) 个样本，而拼接需要 \( O(d_{v} + d_{t}) \)，对于 512 维 CLIP 特征而言相差 256 倍。<sup>[6](https://arxiv.org/html/2606.01207v1)</sup>

交叉注意力与拼接的比较取决于特征对齐，已发表的结果相互矛盾。一项在 Flickr8k 上的受控研究发现，使用预对齐的 CLIP ViT-B/32 特征时，拼接在所有测试规模上均比交叉注意力高出 4.1–5.1 个百分点，并且随着对齐退化，拼接的优势从 1.3% 增长到 2.8%。<sup>[6](https://arxiv.org/html/2606.01207v1)</sup> 另一项在相同流程中训练的视觉-语言模型研究发现，在大规模上模式相反：普通交叉注意力的表现非常接近令牌插入融合，平均下降 1.5%，仅在 ChartQA 和 InfographicVQA 上仍存在显著差距。<sup>[22](https://arxiv.org/pdf/2512.19535)</sup> 两项结果在规模、骨干和任务上不同，因此都不能解决这一普遍问题。

自 2023 年以来，大多数最先进的视觉-语言模型已从交叉注意力融合转向将视觉嵌入插入语言模型的输入流，其开销随图像令牌数量增长；与此同时，交叉注意力作为流式处理长多模态序列的天然高效替代方案重新受到关注，因为图像令牌从不进入令牌流、[KV 缓存](https://www.edgepedia.org/zh/kv-cache)（生成过程中复用的存储键/值向量）或 FFN 更新。<sup>[22](https://arxiv.org/pdf/2512.19535)</sup> 高效变体直接针对开销：BiXT 需要的 FLOPs 少 28%，比完整 Transformer 变体快最高 8.4 倍，同时在序列建模和文档检索上表现相当；<sup>[4](https://proceedings.neurips.cc/paper_files/paper/2024/file/ab1ee157f7804a13f980414b644a9460-Paper-Conference.pdf)</sup> TGATE 是一种用于扩散模型的无需训练的时间门，利用交叉注意力输出在最初几个去噪步骤后收敛到不动点的观察，相比 SDXL 基线减少约 50% 的延迟；<sup>[21](https://arxiv.org/html/2404.02747v1)</sup> DCA 以可忽略的参数成本将精度与大小的权衡提升最高 3 倍。<sup>[20](https://proceedings.mlr.press/v267/heddes25a.html)</sup> 在理论方面，在多模态数据的潜在因子模型下，单层线性自注意力可证明无法恢复贝叶斯最优估计器，而多层交叉注意力在多模态上下文学习中被证明是最优的。<sup>[23](https://arxiv.org/html/2602.04872v2)</sup>

## References

1. [Dive into Deep Learning 10.3: Multi-Head and Cross-Attention](https://d2l.smola.org/chapter_attention/multihead-attention.html)
2. [Deep Multimodal Data Fusion (ACM Computing Surveys)](https://dl.acm.org/doi/10.1145/3649447)
3. [LV-XAttn: Distributed Cross-Attention for Long Visual Inputs in Multimodal Large Language Models](https://arxiv.org/html/2502.02406v3)
4. [Perceiving Longer Sequences With Bi-Directional Cross-Attention Transformers (BiXT)](https://proceedings.neurips.cc/paper_files/paper/2024/file/ab1ee157f7804a13f980414b644a9460-Paper-Conference.pdf)
5. [Attention Is All You Need (Vaswani et al., NeurIPS 2017)](https://proceedings.neurips.cc/paper_files/paper/2017/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf)
6. [Feature Alignment Determines Fusion Strategy: A Comparative Study of Cross-Attention and Concatenation in Multimodal Learning](https://arxiv.org/html/2606.01207v1)
7. [PyTorch documentation: torch.nn.MultiheadAttention](https://docs.pytorch.org/docs/stable/generated/torch.nn.modules.activation.MultiheadAttention.md)
8. [CAB: Comprehensive Attention Benchmarking on Long Sequence Modeling](https://proceedings.mlr.press/v202/zhang23r/zhang23r.pdf)
9. [Dive into Deep Learning 11.4: Encoders, Decoders, and Cross-Attention](https://d2l.smola.org/chapter_transformers/encoders-decoders.html)
10. [Hugging Face Diffusers: cross_attention.py implementation](https://github.com/huggingface/diffusers/blob/4125756e88e82370c197fecf28e9f0b4d7eee6c3/src/diffusers/models/cross_attention.py)
11. [Bahdanau, Dzmitry, Cho, Kyunghyun, Bengio, Yoshua (2014). Neural Machine Translation by Jointly Learning to Align and Translate. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1409.0473)
12. [An Attentive Survey of Attention Models](https://ar5iv.labs.arxiv.org/html/1904.02874)
13. [A General Survey on Attention Mechanisms in Deep Learning](https://ar5iv.labs.arxiv.org/html/2203.14263)
14. [Dynamic Coattention Networks For Question Answering (Xiong, Zhong, Socher, 2016)](https://arxiv.org/pdf/1611.01604)
15. [Jaegle, Andrew and colleagues (2021). Perceiver: General Perception with Iterative Attention. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.2103.03206)
16. [Jaegle, Andrew and colleagues (2021). Perceiver IO: A General Architecture for Structured Inputs & Outputs. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.2107.14795)
17. [Alayrac, Jean-Baptiste and colleagues (2022). Flamingo: a Visual Language Model for Few-Shot Learning. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.2204.14198)
18. [Flamingo gated cross-attention (engineering lesson)](https://raw.githubusercontent.com/rohitg00/ai-engineering-from-scratch/main/phases/12-multimodal-ai/04-flamingo-gated-cross-attention/docs/en.md)
19. [CrossViT: Cross-Attention Multi-Scale Vision Transformer for Image Classification](https://arxiv.org/pdf/2103.14899.pdf)
20. [DeepCrossAttention: Supercharging Transformer Residual Connections (ICML 2025, PMLR v267)](https://proceedings.mlr.press/v267/heddes25a.html)
21. [Cross-Attention Makes Inference Cumbersome in Text-to-Image Diffusion Models (TGATE)](https://arxiv.org/html/2404.02747v1)
22. [CASA: Cross-Attention over Self-Attention for Efficient Vision-Language Fusion](https://arxiv.org/pdf/2512.19535)
23. [Multi-layer Cross-Attention is Provably Optimal for Multi-modal In-context Learning](https://arxiv.org/html/2602.04872v2)

---
*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/cross-attention-network

English original: https://www.edgepedia.org/cross-attention-network
