Choosing an encoder, decoder, or encoder-decoder architecture

← Back to Blog & Articles

Encoder block. An encoder block, in the style of BERT (Devlin et al., 2019) or the encoder half of the original Transformer, contains exactly two sublayers: bidirectional self-attention (no causal mask — every position may attend to every other position, including positions to its right) and the position-wise feedforward network. In pre-norm form,

With the feedforward sublayer, the pre-norm placement, and the multi-head attention module from the companion book all in hand, a full Transformer block is just a specific composition of these pieces, repeated LL times. The three architectural families introduced conceptually at the end of the companion book — encoder-only, encoder-decoder, and decoder-only — differ only in which sublayers a block contains and whether its self-attention is masked, not in any of the underlying mathematics.

Encoder block. An encoder block, in the style of BERT (Devlin et al., 2019) or the encoder half of the original Transformer, contains exactly two sublayers: bidirectional self-attention (no causal mask — every position may attend to every other position, including positions to its right) and the position-wise feedforward network. In pre-norm form,

𝐳(l)=𝐡(l−1)+SelfAttn(Norm(𝐡(l−1))),𝐡(l)=𝐳(l)+FFN(Norm(𝐳(l))), \mathbf{z}^{(l)} = \mathbf{h}^{(l-1)} + \text{SelfAttn}\big(\text{Norm}(\mathbf{h}^{(l-1)})\big), \qquad \mathbf{h}^{(l)} = \mathbf{z}^{(l)} + \text{FFN}\big(\text{Norm}(\mathbf{z}^{(l)})\big),

where SelfAttn\text{SelfAttn} is exactly the companion book’s multi-head self-attention module with causal=False\text{causal}=\text{False}. Stacking LL of these blocks and applying a final Norm\text{Norm} to the last block’s output produces Encoder(𝐗)∈ℝT×dmodel\text{Encoder}(\mathbf{X}) \in \mathbb{R}^{T\times d_{\text{model}}}, a set of contextualized representations in which every position’s output has, in principle, been informed by every other position in the same sequence — the bidirectional context an encoder is built to provide, and that a causally-masked model, described below, structurally cannot.

Encoder-decoder decoder block. An encoder-decoder architecture — the original Transformer, or T5 (Raffel et al., 2020) — pairs an encoder stack with bidirectional attention with a separate decoder stack whose blocks contain three sublayers rather than two: causally-masked self-attention over the target sequence generated so far, cross-attention (the companion book) whose queries come from the decoder’s own stream but whose keys and values come from the encoder’s output, and the feedforward network. In pre-norm form,

𝐳1(l)=𝐡(l−1)+CausalSelfAttn(Norm(𝐡(l−1))),𝐳2(l)=𝐳1(l)+CrossAttn(Norm(𝐳1(l));𝐊,𝐕=Encoder(𝐗)),𝐡(l)=𝐳2(l)+FFN(Norm(𝐳2(l))), \begin{aligned} \mathbf{z}_1^{(l)} &= \mathbf{h}^{(l-1)} + \text{CausalSelfAttn}\big(\text{Norm}(\mathbf{h}^{(l-1)})\big), \\ \mathbf{z}_2^{(l)} &= \mathbf{z}_1^{(l)} + \text{CrossAttn}\big(\text{Norm}(\mathbf{z}_1^{(l)});\ \mathbf{K},\mathbf{V} = \text{Encoder}(\mathbf{X})\big), \\ \mathbf{h}^{(l)} &= \mathbf{z}_2^{(l)} + \text{FFN}\big(\text{Norm}(\mathbf{z}_2^{(l)})\big), \end{aligned}

where CrossAttn(𝐘;𝐊,𝐕)\text{CrossAttn}(\mathbf{Y}; \mathbf{K},\mathbf{V}) denotes the companion book’s general (non-self) attention formalism: queries are the linear projection of Norm(𝐳1(l))\text{Norm}(\mathbf{z}_1^{(l)}), while keys and values are linear projections of the encoder’s fixed output, which does not change across decoder layers or across generation steps within one forward pass — the entire source sequence was available before decoding began, so no causal mask is applied inside CrossAttn\text{CrossAttn} at all. This is the mechanism by which a translation or summarization model conditions every generated token on the full input sequence: the decoder’s causal mask restricts what the decoder can see of the sequence it is generating, but cross-attention’s keys and values are never subject to that restriction, since they come from an already-complete encoder pass rather than from the autoregressive process itself.

Decoder-only block. A decoder-only architecture — GPT and its successors, LLaMA, PaLM — has no encoder stack and therefore no cross-attention anywhere in the model. Its block is the two-sublayer pre-norm form from earlier in this section, but with the self-attention causally masked:

𝐳(l)=𝐡(l−1)+CausalSelfAttn(Norm(𝐡(l−1))),𝐡(l)=𝐳(l)+FFN(Norm(𝐳(l))).\begin{equation} \mathbf{z}^{(l)} = \mathbf{h}^{(l-1)} + \text{CausalSelfAttn}\big(\text{Norm}(\mathbf{h}^{(l-1)})\big), \qquad \mathbf{h}^{(l)} = \mathbf{z}^{(l)} + \text{FFN}\big(\text{Norm}(\mathbf{z}^{(l)})\big). \end{equation}

This is, structurally, the same block as the encoder above, with exactly one change — the additive causal mask from the companion book inside CausalSelfAttn\text{CausalSelfAttn} — and it is the block this book’s Python listings build directly on, since it is what nearly every general-purpose LLM in current production actually is: LL copies of this one block, stacked, followed by a final Norm\text{Norm} and the unembedding projection from the companion book.

A property shared by all three block types is worth naming explicitly, because it is what makes stacking LL of them to arbitrary depth possible at all: every block, whatever sublayers it contains, is a function from ℝT×dmodel\mathbb{R}^{T\times d_{\text{model}}} to ℝT×dmodel\mathbb{R}^{T\times d_{\text{model}}} — same sequence length in and out, same feature dimension in and out. This is a direct consequence of the companion book’s parameter accounting: the output projection 𝐖O\mathbf{W}^O of multi-head attention maps the concatenated per-head outputs back to exactly dmodeld_{\text{model}}, and the feedforward sublayer’s second matrix 𝐖2\mathbf{W}_2 does the same. Nothing about either sublayer changes the shape of the residual stream it operates on, which is precisely what allows 𝐡(l)\mathbf{h}^{(l)} and 𝐡(l−1)\mathbf{h}^{(l-1)} to be added together at every layer in the first place — a residual connection is only well-defined when the sublayer’s output has the exact shape of its input, and every block in this section was built, by construction, to satisfy that constraint. Cross-attention is the one place this needs a small qualification rather than an exception: its queries come from the decoder’s stream at length TtgtT_{\text{tgt}}, while its keys and values come from the encoder’s fixed output at length TsrcT_{\text{src}}, and the companion book’s general (non-self) attention formalism already permits Ttgt≠TsrcT_{\text{tgt}} \neq T_{\text{src}} — cross-attention’s output, the quantity actually added back into the decoder’s residual stream, is still shaped (Ttgt,dmodel)(T_{\text{tgt}}, d_{\text{model}}), matching the decoder stream it is added to, regardless of how long the source sequence being attended over happens to be.

The dominance of the decoder-only design over the other two is not an accident of fashion; it follows from a genuine architectural simplification with a correspondingly genuine cost, and both are worth stating precisely rather than asserting only the conclusion. The simplification: a decoder-only model uses one kind of block, applied to one undifferentiated sequence that mixes what an encoder-decoder model would call “prompt” and “generated continuation” into a single stream of tokens, with no cross-attention sublayer and no separate encoder stack to train, parallelize, or reason about. Because the same causal self-attention mechanism processes a prompt and generates a continuation with no architectural seam between the two, a decoder-only model can be adapted to essentially any task an encoder-decoder model handles — translation, summarization, question answering — simply by placing the source material as a prefix in the causally-masked sequence and letting generation continue from there; every later token’s self-attention can still see the entire prefix, since causal masking only restricts attention to the future, never the past. This is what “everything an encoder-decoder use case needs, via prompting” means concretely: no information the encoder-decoder split would have preserved is architecturally unavailable to a sufficiently capable decoder-only model, because the whole prompt sits, unmasked with respect to everything after it, inside the same causal stream that generation reads from.

The cost is equally precise. An encoder block’s self-attention lets every position attend to every other position at every layer, including positions to its right — a token’s representation at position ii is built from the entire input, forward and backward, at every one of the encoder’s LL layers. A decoder-only model’s causal self-attention never permits this: a token’s representation at position ii, at any layer, is built only from positions ≤i\le i, regardless of whether position ii sits inside what a user would think of as “the prompt” or “the generated text.” By the time generation actually begins — after the whole prompt has been processed token by token — every position’s key and value vectors have long since been computed and cached, so the model’s output is conditioned on the full prompt in an ordinary, practical sense. But the intermediate representation built for an early prompt token was never allowed to look at later prompt tokens the way an encoder’s would, which is the qualified sense in which decoder-only architectures trade away bidirectional context: not “the model cannot use the whole prompt,” which is false, but “no single token’s representation is built with a genuinely bidirectional view of its own sequence,” which is true and is exactly the property BERT-style encoders provide and decoder-only models do not.

Continue with the book

Explore the full treatment, worked examples, and exercises in LLMs Derived.


Companion article for LLMs Derived, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.

Scroll to Top