What is cross-attention?
Cross-attention is an attention operation in which the queries come from one sequence and the keys and values come from another. Each token of the first sequence, such as a word the decoder is generating, scores every token of the second sequence, such as the encoded source sentence, and takes a weighted average of its values. It is the same scaled dot-product attention as self-attention; only the source of K and V changes.
Cross-attention connects the two halves of encoder-decoder models for translation, summarization, and speech recognition, including T5 and Whisper. It is also how text-to-image diffusion models such as Stable Diffusion let the text prompt steer each part of the image, and how some vision-language models let a language model read image features. Any time one stream of data has to look up information in another, cross-attention is a standard choice.
Key ideas
- Cross-attention takes queries from one sequence and keys and values from another.
- The math is the same as self-attention: softmax(Q · Kᵀ / √d_k) · V.
- The score matrix is m × n, so the two sequences can have different lengths.
- Encoder keys and values are computed once and reused at every decoding step.
- Cross-attention weights act as a soft alignment between the two inputs.
- It links text to images in diffusion models and vision-language models.
Common questions
How is cross-attention different from concatenating the two inputs?
A decoder-only model can join both inputs into one sequence and use self-attention over all of it, as many vision-language models do with image tokens. Cross-attention instead keeps the streams separate, so only one side is updated and the other is read. That saves compute when the source is long and lets the source encoder be frozen or reused.
Why do the keys and values come from the encoder?
Keys decide what can be found and values decide what is returned, so both have to describe the information being looked up. The query describes what the current position needs. Swapping the roles would make the source sentence ask questions of the output instead.
How much compute does cross-attention need?
For m queries and n source tokens it computes m × n scores per head, which is linear in each length. Since the source keys and values are fixed, they are projected once and cached during generation. The cost grows with source length, which matters for long documents and high-resolution images.
What is the Perceiver?
The Perceiver (Jaegle et al., 2021) uses cross-attention from a small, fixed set of learned latent vectors to a very large input, such as all the pixels of an image. The cost is then proportional to the number of latents times the input size, not the input size squared. Self-attention then runs only among the latents.
Related explainers
- How Self-Attention Works — Query, key, value — the core of self-attention
- The Transformer Architecture — A visual guide to 'Attention Is All You Need'
- Seq2Seq & Beam Search — Encode an input sequence and decode an output — the architecture behind translation
- Multi-Head Attention — Parallel attention heads learning different patterns
- Diffusion Models — Generate images by learning to reverse a gradual noising process
- Decoder-Only Transformers — The architecture behind GPT and modern LLMs
References
- Vaswani et al. (2017), "Attention Is All You Need", NeurIPS
- Rombach et al. (2022), "High-Resolution Image Synthesis with Latent Diffusion Models", CVPR
- Alayrac et al. (2022), "Flamingo: a Visual Language Model for Few-Shot Learning"
- Hertz et al. (2022), "Prompt-to-Prompt Image Editing with Cross Attention Control"
- Jaegle et al. (2021), "Perceiver: General Perception with Iterative Attention"
Last updated