Explain the difference between self-attention and cross-attention. When does each appear in a real architecture?
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.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Self-attention and cross-attention differ entirely in where Q, K, and V come from. That one routing fact determines which architectural family a model belongs to. Get the routing right and BERT, GPT, T5, and Whisper sort themselves out.
Mental model: count the towers. Same tower architectures use only self-attention. Two tower architectures use both self-attention within each tower and cross-attention between them.
The Q/K/V routing, explicitly
Self-attention
One input tensor x, three learned projections:
q = W_Q xk = W_K xv = W_V x
The attention pattern is T × T. Each token attends to other tokens in the same sequence, subject to a mask (bidirectional in BERT, causal in GPT/Llama).
Cross-attention
Two input tensors, asymmetric routing:
q = W_Q x_decfrom the decoder sidek = W_K x_encandv = W_V x_encfrom the encoder side
The attention pattern is T_dec × T_enc, non-square when the sequence lengths differ (which they almost always do).
Why the direction matters
The convention is fixed: Q is from the decoder, K and V are from the encoder. Reversing this (encoder-Q, decoder-K/V) would mean the encoder attends to the decoder. That is the wrong direction for grounding generation in the source.
The asymmetric routing is what turns 'attention' into 'cross-attention'. Same routing under both Q and K/V is plain self-attention with two streams concatenated.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
| 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 | Same 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-3, Llama 4 Maverick, Mistral Large 3, DeepSeek-V3: decoder-only with causal self-attention only.
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?
K and V in cross-attention are projections of the encoder output, which is computed once and then held fixed for the entire generation. Each decode step uses a new Q but the same cached K and V. Self-attention's cache must grow because each newly generated token contributes a new K and V row.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases 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.
60 second bullets to scan on the way to the call.
Q/K/V routing for self vs cross-attention
Direction of Q vs K/V flow in cross-attention
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.