Explain when an architecture uses cross-attention vs self-attention. Give concrete examples.
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
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.
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.
What an interviewer would ask next. Try answering before peeking at the approach.
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.