Cross-Attention

An attention pattern where queries come from one stream while keys and values come from a different memory source.

Cross-attention is the attention pattern where queries come from one stream while keys and values come from a separate memory source, so the active sequence can read information stored outside itself instead of only from its own hidden states.

At a glance

Released

June 2017

Authors

Ashish Vaswani, Noam Shazeer, Niki Parmar, et al.

Optimizes

  • Cross Source Conditioning
  • Multistream Context Fusion
  • Encoder Decoder Context Bridging

What It Is

Cross-attention is an attention variant in which the query vectors come from one stream, but the key and value vectors come from somewhere else. The model still performs a weighted lookup, but it no longer reads only from the same sequence that produced the query.

Why It Exists

Self-attention is enough when one sequence only needs to mix information inside itself. Cross-attention solves the different problem of conditioning one stream on information stored elsewhere, such as a decoder reading an encoder memory or a text stack reading image features.

How It Works

A target stream produces queries, while a separate source stream produces keys and values. Each query scores the external memory slots, turns those scores into weights with softmax, and blends the matching value vectors into a context vector for the target stream. The critical difference from self-attention is the memory source: the target tokens ask, but a different representation answers.
Cross-attention keeps the query on the active target stream while keys and values stay on a separate memory source, unlike self-attention where all three come from the same sequence.

Math Or Compute Schema

The formulas below contrast self-attention with cross-attention. The weighted lookup is still scaled dot-product attention, but cross-attention changes which hidden states create the queries versus the keys and values.
Self-attention
Attention(Q(X),K(X),V(X))=softmax ⁣(Q(X)K(X)⊤dk)V(X)\mathrm{Attention}(Q(X), K(X), V(X)) = \mathrm{softmax}\!\left(\frac{Q(X) K(X)^{\top}}{\sqrt{d_k}}\right) V(X)
XX
Hidden states from the same source sequence.
Q(X)Q(X)
Query projection of X.
K(X)K(X)
Key projection of X.
V(X)V(X)
Value projection of X.
dkd_k
Key dimension per head.
Cross-attention
Attention(Q(Y),K(X),V(X))=softmax ⁣(Q(Y)K(X)⊤dk)V(X)\mathrm{Attention}(Q(Y), K(X), V(X)) = \mathrm{softmax}\!\left(\frac{Q(Y) K(X)^{\top}}{\sqrt{d_k}}\right) V(X)
XX
Hidden states from the external memory source.
YY
Hidden states from the active target stream.
Q(Y)Q(Y)
Query projection of Y.
K(X)K(X)
Key projection of X.
V(X)V(X)
Value projection of X.
dkd_k
Key dimension per head.

Compared To Nearby Modules

Compared with self-attention, cross-attention changes where keys and values come from: self-attention builds queries, keys, and values from the same stream, while cross-attention keeps queries on the target stream but reads keys and values from a separate memory source. Compared with multi-head attention, the difference is not the memory layout: multi-head attention is the head-parallel execution pattern that splits attention across multiple heads and can run inside either self-attention or cross-attention blocks. Compared with causal attention, the key difference is masking and allowed positions, not the memory source: causal attention usually still reads the same growing sequence but blocks future positions, while cross-attention can read a different memory and may appear inside a causal decoder block. Compared with bidirectional attention, cross-attention again changes the memory source rather than simply opening left and right context inside one sequence.
Comparison dimensionCross-AttentionAttention OverviewMulti-Head AttentionCausal AttentionBidirectional Attention
Where queries come fromThe active target stream that needs outside informationDepends on the attention pattern being discussedDepends on the parent block: the same stream in self-attention or the target stream in cross-attentionThe active generated prefix up to the current tokenThe visible sequence being encoded
Where keys and values come fromA different source sequence or memory bankMay be the same sequence or a separate memory depending on the designDepends on the parent block: the same stream in self-attention or the external memory in cross-attention, split across parallel headsThe same growing sequence, but future positions are masked outThe same visible sequence with left and right context available
Main use contextConditioning one stream on another, such as decoder-on-encoder or text-on-imageGeneral weighted lookup across Transformer modulesHead-parallel attention execution that can wrap either self-attention or cross-attention lookupsAutoregressive decoding where each token predicts the next one without seeing future tokensEncoder-style full-context understanding

Example Architectures

Cross-attention appears in encoder-decoder Transformers where decoder states read encoder outputs, in multimodal models where text queries image or audio features, and in retrieval-style systems where the active stream reads an external memory representation.

Limitations And Tradeoffs

Cross-attention adds another memory interface, which means extra projections, more tensors to keep available, and more places where weak source representations can hurt the result. If the external memory is noisy or badly aligned with the target stream, the lookup can pull in the wrong evidence.

Why It Still Matters

Many important model designs depend on one stream reading another without collapsing both into one shared sequence. A dedicated cross-attention page makes that bridge mechanism clear instead of leaving it implied inside larger architecture diagrams.

Tags

References

  1. Vaswani, Ashish, et al. "Attention Is All You Need." arXiv, 2017, https://arxiv.org/abs/1706.03762.