Decoder Architecture
Masked self-attention, cross-attention, and feed-forward, stacked and repeated one token at a time
Masked self-attention, cross-attention, and feed-forward, stacked and repeated one token at a time
One Transformer decoder block stacks masked self-attention (look only at the past), cross-attention (look at the full encoder output), and a feed-forward network — each wrapped in residual + layer norm — and the whole stack finishes with a linear projection and softmax over the vocabulary to predict the next token, one step at a time.
This is the architecture behind every sequence-generation model built on an encoder-decoder Transformer (machine translation, summarization) — and, with the cross-attention sublayer simply removed, it's also the backbone of decoder-only LLMs like the GPT family.
A decoder generates output autoregressively: one token at a time, where each new token is conditioned on every token generated so far, plus — in an encoder-decoder setup — the entire input sequence. Masked self-attention handles "what have I generated so far," cross-attention handles "what does the input actually say," and the feed-forward layer processes each position's combined information further. Matlab, decoder apne hi output ko dekhte hue, aur encoder ke diye hue context ko dekhte hue, ek-ek token aage badhta hai.
Referring to the diagram, each decoder block runs (and this block is repeated N times):
i can only mix value vectors from positions ≤ i. This is needed because at generation time
future tokens genuinely don't exist yet, and during training (teacher forcing) the model must be
prevented from peeking at the very tokens it's being trained to predict.LayerNorm(x + Sublayer(x)).Q come from the decoder's own (masked-attention) output;
keys and values K, V come from the encoder's output. This lets the decoder "look up" whatever
part of the input is relevant to the token it's about to generate.N stacked blocks, a final linear projection maps each position's output to
vocabulary-sized logits, and a softmax turns those into a probability distribution over the
next token.Masked self-attention uses the same core formula as self-attention, with a mask added to the scores before the softmax:
scores = QK^T/√d_k + M, where M_ij = −∞ for j > i (future positions) and M_ij = 0 otherwise,
then Attention(Q,K,V) = softmax(scores) V.
Cross-attention uses the identical formula, softmax(QK^T/√d_k)V, but Q is derived from the
decoder sequence while K and V are derived from the encoder's output — no masking is needed here
since the full input is already known.
Each sublayer is wrapped as LayerNorm(x + Sublayer(x)) — the residual keeps gradients flowing
through the deep stack, and layer norm stabilizes the activation scale going into the next sublayer.
Translating French "Je vais bien" to English, generating one token per step:
<BOS>. Masked self-attention has only itself to attend to.
Cross-attention looks across the whole encoded French sentence. Output: "I".<BOS, I>. Masked self-attention can now use both positions (but not any
future one). Cross-attention again looks at the full French input. Output: "am".<BOS, I, am>. Output: "fine".At every step the decoder re-runs cross-attention against the same, full encoder output — it never loses access to any part of the input, no matter how many tokens it has already generated.
−∞ to disallowed positions), not after.A decoder block = masked self-attention (see only the past) + cross-attention (see the whole input)
N times, finished with a softmax
over the vocabulary — one new token per pass through the autoregressive loop.