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.
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.
The T5 decoder block is the canonical encoder-decoder transformer building block, descended directly from the 2017 Transformer paper and used in T5, BART, Marian, Whisper, Voicebox, and most encoder-decoder models in the 2026 production stack. Its three sub layer structure (causal self-attention, cross-attention, FFN) and the fixed ordering of those sub-layers encode a deep insight about how a decoder should integrate information.
This card walks the three sub-layers in order, explains what each one's Q, K, V read from and why, justifies the ordering choice with a concrete linguistic argument, covers the caching efficiency that makes cross-attention practical, and surveys where encoder-decoder architectures still dominate in 2026 despite the rise of decoder-only LLMs.
Sub-layer 1: causal self-attention
The first sub-layer in every T5 decoder block is decoder self-attention with a causal mask. This is what gives the decoder its autoregressive property and lets it integrate information about its own previously-generated tokens.
Projections
Q_dec = H_dec . W_QK_dec = H_dec . W_KV_dec = H_dec . W_V
All three are linear projections of the decoder's current hidden state stack H_dec. Different weight matrices, but all reading from the same source.
The causal mask
A lower-triangular mask ensures position t attends only to positions 0, 1, ..., t. The upper triangle holds -inf (or the framework's negative-infinity sentinel) and is added to the raw scores before softmax. This prevents position t from peeking at future tokens during training; at inference time, future tokens do not exist yet, so the mask is implicit.
What this sub-layer accomplishes
At the moment the decoder is generating token t, this sub-layer lets that position pull information from every previously-generated token (and its own position via the diagonal). The decoder's hidden state at position t after this sub-layer encodes "what have I generated so far, and how does the current position fit into that?"
KV cache during inference
During autoregressive decoding, the decoder self-attention's K and V grow by one row per decode step. Production stacks cache these incremental K and V additions in a per-request KV cache to avoid recomputing them from scratch every step. This is the standard KV cache that PagedAttention and continuous batching manage.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
| 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.
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?
The encoder runs once over the source input and produces a fixed final-layer hidden state. K and V for cross-attention are linear projections of that fixed output, so they are also fixed across the entire decoding process. Computing them once and reusing them across hundreds of decoder steps is one of the major efficiency wins of encoder-decoder architectures. The only K, V that change per decoder step are those from the decoder's own self-attention, which still need a per-step KV cache.
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.
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).
60 second bullets to scan on the way to the call.
Three sub-layers per T5 decoder block: causal self-attn, cross-attn, FFN
Decoder self-attention with causal lower-triangular mask
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.