Summary
Self-attention computes where are all derived from the same sequence. Cross-attention uses from one sequence and from another. Same kernel, different routing.
This input choice separates encoder-only models (BERT), decoder-only models (GPT), and encoder-decoder models (T5, the original Transformer, and Whisper). Multimodal architectures also use cross-attention to connect image, text, or audio representations.
If you can write the matrix multiplications and explain why a layer uses one form, you understand the attention structure of common transformer architectures.
Self-attention
Inputs: a single sequence .
Each token attends to every other token (subject to masking). Used in:
- BERT encoder layers: bidirectional self-attention.
- GPT decoder layers: causal self-attention. The mask zeros out positions so token cannot attend to future tokens.
Cross-attention
Inputs: a query sequence and a key-value sequence .
Same softmax, same scaling. The shape of the attention matrix is now .
Used wherever the model needs to “look up” information from a different source:
- Encoder-decoder transformers: decoder layers cross-attend to the encoder output. Translation, summarization, speech-to-text.
- Diffusion models with text conditioning: image-side latents cross-attend to text embeddings. Stable Diffusion, DiT.
- Perceiver / Q-Former: a small set of learned latent queries cross-attend to a large input (image patches, audio frames) to compress it.
- RAG architectures with separate memory: model states can cross-attend to retrieved document representations. Many decoder-only RAG systems instead concatenate retrieved text into the prompt and use self-attention.
Learning objective
Which sequence supplies Q, K, and V?
X fans out to Q, K, and V, so three query tokens scored against three key tokens form a square 3 × 3 matrix. In cross-attention, X supplies two queries while Y supplies three key-value pairs, so the same kernel forms a rectangular 2 × 3 matrix and returns one row per X query. Source labels, arrows, and matrix dimensions carry the distinction without color. Original schematic, checked against Attention Is All You Need.Where each lives in a transformer block
Encoder block (BERT, T5 encoder):
- Self-attention.
- FFN.
Decoder block (GPT):
- Causal self-attention.
- FFN.
Encoder-decoder block (T5 decoder, original Transformer decoder):
- Causal self-attention.
- Cross-attention to encoder output.
- FFN.
The decoder reads its own past tokens (self-attention) and the encoder’s output (cross-attention) at every layer.
Tradeoffs
- Compute: self-attention is . Cross-attention is , which can be much cheaper if is small (compressed conditioning) or much larger (cross-attending to a long context).
- KV-cache: at encoder-decoder inference, the decoder can cache both forms. Cross-attention K/V are computed once from the fixed encoder output and reused for every decoded token; self-attention K/V grow with the decoded sequence.
Variants
- Masked cross-attention: padding masks hide invalid K/V positions. A triangular mask is meaningful only when query and K/V positions share an ordered alignment; separate source and target sequences do not imply one automatically.
- Cross-attention with caching: precompute from a fixed conditioning sequence (system prompt, retrieved docs) and reuse across decoding steps.
- Asymmetric cross-attention: in Perceiver, the queries are a small learned set (e.g. 256), the K/V are massive (e.g. all image patches). The model compresses high-dim input into a fixed-size latent.
Common pitfalls
- Calling decoder self-attention “cross-attention.” They are different. Self-attention reads from the same sequence (the previously generated tokens); cross-attention reads from another sequence (encoder output, retrieved docs).
- Forgetting that decoder-only LLMs do not use cross-attention. Their conditioning is the prompt prefix, attended via self-attention, not a separate cross-attention path.
- Conflating attention masks with attention types. The mask shape differs (causal mask is square and triangular; cross-attention is rectangular and usually unmasked) but the operation is the same.