Explain when an architecture uses cross-attention vs self-attention. Give concrete examples.
Explain the difference between self-attention and cross-attention in terms of where Q, K, V come from. When does each appear in a real architecture? Give concrete examples (BERT, GPT, T5).
Self-attention: Q, K, V from the same sequence (BERT, GPT, Llama). Cross-attention: Q from one sequence, K/V from another (T5, BART decoders). Decoder-only LLMs skip cross-attention because they have no separate encoder.
Imagine taking notes during a lecture. Self-attention is like glancing back at your own notes as you write the next line: one notebook, one stream. Cross-attention is like a translator at a desk: they look at a textbook open on their left while writing on a fresh page on their right. The pen on the right page is the Query; the textbook on the left is what they're keying and valuing against. Decoder-only models like GPT and Llama are the note-taker with one notebook. T5 and BART are the translator with two documents in play.
Detailed answer & concept explanation~5 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
Walk the Q/K/V routing distinction with explicit equations, map BERT/GPT/T5/Whisper to their families, describe the T5 decoder block's three sublayers, explain the same tower vs two tower production analogy, and cover Flamingo-style cross-modal injection as the modern use case.
| Aspect | Self-attention | Cross-attention |
|---|---|---|
| Q source | Same sequence x | Decoder sequence x_dec |
| K, V source | Same sequence x | Encoder sequence x_enc |
| Attention matrix shape | T × T (square) | T_dec × T_enc (non-square) |
| KV cache during decoding | Grows one row per token | Fixed once encoder runs |
| Production deployment | One-tower, one cache layout | Two tower, dual cache regimes |
| Used in | BERT, GPT, Llama, all encoders | T5/BART decoder, Whisper, Flamingo |
Real products, models, and research that use this idea.
- BERT, RoBERTa, DeBERTa: encoder-only with bidirectional self-attention. No cross-attention anywhere.
- GPT-5.5, Claude Opus 4.7, Llama 4 Maverick, Gemini 3.1 Pro, DeepSeek V4: decoder-only with causal self-attention only.
- T5 and BART: encoder decoder where the encoder uses bidirectional self-attention and each decoder block has causal self-attention, cross-attention to the encoder, and an MLP.
- Whisper: encoder decoder for speech to text where the text decoder cross-attends to the audio encoder output.
- Flamingo and modern VLMs: gated cross-attention layers inserted into a frozen LM to inject vision features without retraining the language backbone.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy doesn't the cross-attention KV cache grow across decoding steps the way self-attention's does?
QWhy is the cross-attention matrix non-square as T_dec × T_enc?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Reversing the direction of cross-attention. In encoder decoder cross-attention, Q is from the decoder and K/V are from the encoder, not the other way around.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.