Walk through a T5 decoder block in order, list the attention sub-layers and what each one's Q, K, V read from.
- 1Decoder self-attention: Q, K, V all projected from the decoder's own current hidden state, with a causal lower-triangular mask so position t only sees positions <= t.
- 2Feed-forward sub-layer (FFN): operates per token, no cross-token interaction. Runs on the residual stream and pushes its output back into the residual.
- 3Cross-attention: Q projected from the decoder hidden state, while K and V are projected from the final-layer encoder output. No causal mask on the encoder axis, the encoder is fully observed.
T5 decoder block order: causal self-attention first (Q, K, V from decoder), then cross-attention (Q from decoder, K, V from encoder), then position-wise FFN, each with residual and norm.
Think of a translator writing the next word of a translation. First the translator reviews what they have already written so far (causal self-attention: looking at their own previous work). Then the translator looks at the original sentence to see what still needs translating (cross-attention: consulting the encoder). Finally the translator polishes the wording for the next word individually (FFN: per-token refinement). The order matters because reviewing your own progress before consulting the source means you ask the source the right question.
Detailed answer & concept explanation~7 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.
6 min: T5 decoder block structure, three sub-layers in causal-self -> cross -> FFN order, projection sources per sub-layer, mask conventions, why causal then cross is the natural ordering, encoder K, V caching, modern survival of encoder-decoder in code, speech, multimodal.
| Sub-layer | Q from | K, V from | Mask |
|---|---|---|---|
| Decoder self-attention | Decoder hidden state | Decoder hidden state | Causal lower-triangular |
| Cross-attention | Decoder hidden state (post self attn) | Encoder final-layer output | None on encoder axis; optional padding mask |
| Position-wise FFN | N/A (no attention) | N/A (per-token MLP) | N/A |
Real products, models, and research that use this idea.
- T5 (Raffel et al. 2020) is the canonical text to text encoder-decoder model used as the architectural reference for this layout.
- T5-v1.1 and T5-1.1-XXL retain this exact block structure with GeGLU activations and relative position bias.
- Codestral and other 2026 code-generation models inherit the encoder-decoder pattern for source to target structured tasks.
- Whisper for speech recognition uses encoder-decoder with this block structure: audio encoder feeds K, V into text decoder cross-attention.
- BLIP-2's Q-Former and Flamingo's gated cross-attention extend the cross-attention sub-layer pattern to bridge vision and language, with similar self then cross ordering.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy does cross-attention K, V get cached across all decoder steps?
QHow does the cross-attention pattern in encoder-decoder differ from decoder-only models with system prompts?
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.
Putting cross-attention before self-attention. The decoder must process its own context first (causal self-attention) before forming a query against the encoder output (cross-attention).
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.