Transformer architectures: encoder, encoder–decoder, and decoder
Three ways to organize sequence processing
A Transformer can use an encoder, a decoder, or both. This choice determines which positions can exchange information and how the model processes a sequence. It does not, by itself, determine the training objective or the task being performed.
- Encoder-only: contextualizes the input with generally bidirectional attention; BERT and RoBERTa are examples.
- Encoder–decoder: the encoder represents the input; the decoder generates the output with causal self-attention and cross-attention to the encoder; T5 is an example.
- Decoder-only: uses causal attention to extend a prefix; GPT is the central teaching example here.
- Three distinct concepts: architecture (how computation is organized), training objective (what is optimized), and task (what the system must do).
Quick question: can you explain which context each architecture can access and why a prediction objective does not by itself define the architecture?
This comparison is optional: to follow the main route, move directly from the LLM panorama to generation.
You will be able to: - compare encoder-only, encoder–decoder, and decoder-only architectures by context visibility and information flow; - distinguish bidirectional self-attention, causal self-attention, and cross-attention; - separate architecture, training objective, and task without treating typical pairings as rules.
At a glance
On a phone, the comparison retains all six dimensions: scroll the region below horizontally to read the remaining columns. The region is keyboard accessible.
| Family | Information flow | Masking and attention | Typical objectives | Common uses | Examples |
|---|---|---|---|---|---|
| Encoder-only | The encoder transforms an entire input sequence into contextual representations. | Generally bidirectional self-attention: each position can use information from other positions in the input. | Masked language modeling, among other possible objectives. | Classification, semantic search, extraction, and text representation. | BERT, RoBERTa |
| Encoder–decoder | The encoder represents the input; the decoder produces a target sequence using that representation. | Encoder attention over the input; causal self-attention in the decoder; cross-attention to encoder states. | Reconstruction, denoising, or text-to-text learning, depending on the model. | Translation, summarization, and sequence-to-sequence transformation. | Original Transformer, T5 |
| Decoder-only | The decoder extends a prefix by producing representations used to predict the next token. | Causal self-attention: a position cannot attend to future tokens. | Autoregressive next-token prediction in classic GPT models. | Prompt-conditioned continuation and generation. | GPT (2018), GPT-2 |
Scroll horizontally to read all six columns.
Links point to model cards or original publications. Research papers: BERT, RoBERTa, T5, GPT, and GPT-2. RoBERTa remains an encoder-only model: it retains BERT’s architectural basis while mainly changing the pretraining recipe; it is not a fourth configuration.
The three architectures in one diagram
In an encoder–decoder architecture, the decoder reads representations produced by the encoder through cross-attention. The dedicated lesson explains the computation; it is not required to understand the broad architecture comparison.
Encoder-only: contextualize an input
The encoder processes the input sequence as a whole. With bidirectional attention, each position can use tokens both before and after it. This is useful for representing or classifying a complete text.
BERT’s best-known pretraining objective predicts masked tokens from their left and right context. RoBERTa keeps this encoder-only configuration while optimizing, among other things, the pretraining setup. Masked prediction is associated with this architecture; it does not define every encoder.
Encoder–decoder: transform one sequence into another
The encoder builds representations of the input. The decoder then produces the target sequence, usually autoregressively, combining its generated context with information from the encoder through cross-attention.
This organization suits tasks where input and output play distinct roles, such as translation, summarization, or text transformation. The original 2017 Transformer and T5 are examples. The cross-attention lesson explains the mechanism and the respective origins of Q, K, and V.
Decoder-only: extend a prefix
The decoder uses a causal mask: a position cannot attend to future tokens. During training, each position is used to predict the next token. At inference, the model repeats this computation and appends each selected token to the context; Generating text explains this loop.
This is the configuration used by GPT (2018), GPT-2, and LLM Lab’s small-GPT build. It makes generation easy to inspect step by step, but not every LLM is decoder-only.
Architecture, objective, and task are different concepts
These terms are often conflated, but they answer different questions:
- Architecture: which operations and connections organize the computation?
- Training objective: what quantity does the model learn to predict or optimize? Examples include masked tokens, next tokens, or denoised sequences.
- Use task: what problem should the system solve? Examples include classification, translation, summarization, or generation.
One family can be trained with several objectives; similar objectives can be implemented in different architectures. The table shows common associations, not mandatory pairings.
Beyond the three configurations
These three families describe the main Transformer configurations, not every language-model architecture. State-space models, recurrent architectures, and hybrids offer other trade-offs. MoE (Mixture of Experts) is a sparse expert-activation strategy: it can be combined with a Transformer, so it is not by itself a mutually exclusive fourth family.
Nor is Transformer synonymous with LLM: architecture alone does not determine scale, data, or model capabilities. For an example of a hybrid architecture combining several mechanisms, see the Nemotron case study. For the historical development of these families, see the history of Transformers.
At fixed model width, dense attention performs \(O(T^2 d)\) arithmetic work per layer, or \(O(T^2)\) when dimension \(d\) is treated as constant. Pairwise position interactions can be parallelized within a layer during training and prefill: quadratic work therefore does not imply position-by-position execution. A recurrence with fixed state size can reduce total work to \(O(T)\), but a naïve recurrence retains a sequential dependency chain across states. Some structures support a parallel scan and reduce this depth. Finally, in standard autoregressive generation, each generated token depends on the previous one. These orders describe different costs under specific assumptions; they are not sufficient to compare runtime. The guide to computational complexity and parallelism examines these compute, memory, and dependency trade-offs. The internal computation of the project’s decoder-only GPT is explained in GPT and Transformer.
Observe the three families in practice
The conceptual comparison is sufficient to locate the architectures. To run one representative example of each, install dependencies, and interpret the outputs, continue to the hands-on activity: compare BERT, GPT-2, and T5. This optional activity belongs to practice, not to the conceptual route or the small-GPT build.
Practice
The questions and exercises for this lesson are collected on its dedicated practice page.