Transformer Families: Encoder, Decoder, and Encoder-Decoder
About 38 min
Goal
By the end of this lesson, you can distinguish encoder-only, decoder-only, and encoder-decoder Transformers by the job they perform, the context each position may use, the kind of output they produce, and the training objective commonly paired with each design.
The Transformer machinery you just assembled is not limited to one kind of model. Before naming the families, start with three different jobs.
Job 1: understand a whole input
Suppose the input is already complete:
The battery lasts all day.
You may want a useful representation for every word, one representation for the whole sentence, a classification, or token-level labels.
For this job, there is usually no reason for the token battery to pretend that day has not been provided yet. The whole input is available at once.
A useful flow is:
complete input
→ contextualize the input using both left and right context
→ contextual representations
→ classification / embeddings / token-level understanding
Job 2: continue a prefix
Now suppose the text is incomplete:
The battery lasts
The task is to predict what could come next. If the training example already contains the answer to the right, an earlier position must not look at it.
The flow becomes:
visible prefix
→ causal Transformer
→ next-token distribution
→ choose or sample a token
→ append it and repeat
This is the path Level 6 will train.
Job 3: transform one sequence into another
Now the input and output are different sequences:
source: good morning
target: bonjour ...
The model should understand the complete source while generating the target one token at a time.
The flow becomes:
source input
→ source representations
→ target prefix consults those source representations
→ next target token
→ repeat until the target is complete
These three jobs motivate the three common Transformer families.
Compare what each Transformer family can see
Switch families and move the focused token. Watch which positions stay visible, where future target tokens are blocked, and where autoregressive generation happens.
Bidirectional self-attention over the provided input
Bidirectional context: the focused input can use tokens on both its left and right.
Encoder-only: represent the input
An encoder-only Transformer uses self-attention to contextualize the input it has been given.
For ordinary encoder self-attention, a token can use context on both sides:
input tokens
→ bidirectional contextualization
→ contextual representations
→ classification / embeddings / token-level understanding
Here bidirectional does not mean “read a future answer during generation.” There is no autoregressive target being generated in this architecture. It means the complete input is available, so a token representation may use provided input tokens to its left and right. Padding masks or task-specific masks may still hide positions for other reasons.
A common pretraining idea is masked-token prediction:
original: the cat sat down
corrupted: the [MASK] sat down
↑
predict the hidden token using surrounding context
This objective encourages representations that use both left and right context. BERT is a familiar encoder-style example, and masked-language modeling is strongly associated with BERT-style pretraining.
This connects forward to Level 9: its optional real-model RAG path uses sentence-transformers/all-MiniLM-L6-v2 to produce sentence embeddings from an encoder-style Transformer representation.
But architecture and objective are not synonyms. Not every encoder must be BERT, and not every encoder must be trained with exactly the original BERT masking recipe.
Decoder-only: generate from a prefix
A decoder-only Transformer uses causal self-attention. At decoder-input position t, it may use positions at or before t, but not later positions.
During next-token training, the hidden state at that visible prefix position is used to predict the following token. So the model may use the current prefix token, but it may not peek at the answer token that comes after it.
prefix
→ causal Transformer
→ next-token distribution
→ generation
For a four-token training row, the visibility pattern is triangular:
position 0 can use: 0
position 1 can use: 0 1
position 2 can use: 0 1 2
position 3 can use: 0 1 2 3
That rule supports next-token prediction:
visible prefix: the cat
target: sat
The model learns to score the next token from information that would actually exist during generation. GPT-style language models are familiar decoder-only examples. Many modern general-purpose language models use decoder-only Transformer backbones.
Again, “decoder-only” names an architecture and visibility pattern; “next-token prediction” names a training objective. They are commonly paired, but they are not the same definition.
Encoder-decoder: understand a source, then generate a target
An encoder-decoder Transformer separates the two roles.
The encoder reads the source input and produces source representations. The decoder generates the target causally, while an additional attention path lets the decoder consult the encoder output.
During training, the decoder receives the target prefix available before the next target token it must predict. This is the same no-peeking idea as decoder-only next-token prediction, with one extra information source: the encoder representations.
source input
→ encoder representations
→ decoder attends to source + generated target prefix
→ target output
The decoder therefore has two important information paths:
target self-attention: causal access to the target prefix
cross-attention: access to encoder source representations
This is a natural fit for translation and other sequence-to-sequence transformations. The original Transformer in Attention Is All You Need is an encoder-decoder architecture. T5 is a familiar encoder-decoder example with a text-to-text framing.
Encoder-decoder systems can use different pretraining and fine-tuning objectives. The defining architecture is the division between source encoding and target decoding with attention from the decoder to encoder representations.
Predict
Compare the families after the jobs are clear
| Family | Typical job | Self-attention visibility | Common training signal | Typical output |
|---|---|---|---|---|
| Encoder-only | understand or represent a whole input | bidirectional over the provided input | masked-token or other representation learning | contextual representations, labels, embeddings |
| Decoder-only | continue a prefix | causal over the target/prefix | next-token prediction | next-token distributions and autoregressive generation |
| Encoder-decoder | transform source into target | encoder source context + causal decoder target context | target-token prediction conditioned on source; pretraining varies | generated target sequence |
The table is a map, not a set of rigid identities. Real systems may vary their objectives, masks, normalization placement, and task heads.
Under the Hood
“BERT-style,” “GPT-style,” and “T5-style” are useful examples, not definitions of the three families. The transferable questions are: What is the model's job? Which positions may each self-attention layer use? Is there a separate source stream? Where does autoregressive generation happen? What training target supplies the learning signal?
Inspect the access rules in the Lab
Run the Lab unchanged. It prints small visibility matrices for all three families.
- For the encoder-only matrix, confirm that every input position can use every provided input position.
- For the decoder-only matrix, confirm that cells above the diagonal are blocked.
- For the encoder-decoder case, inspect two rules separately: the target self-attention matrix is causal, while every target position may read all encoder source representations through cross-attention.
- Find
focus_position = 2. Change it to1and predict which decoder target positions remain visible before rerunning. - Read the objective examples printed by the second cell: one masked-token question and several next-token questions. Explain why they provide different learning signals even though both can use Transformer blocks.
Loading lab…
The Lab is deliberately small. It tests information access and objective structure, not model quality.
Quick Check
Explain it back
For each family, answer four questions without naming a specific model first:
- What job is it doing?
- Which positions may self-attention use?
- What does the model output?
- What training signal is commonly paired with that design?
Then give BERT, GPT, and T5 only as examples.
Check your explanation
A strong answer mentions:
- Encoder-only (BERT-style): build contextual representations of the provided input, commonly with bidirectional self-attention and masked-token training.
- Decoder-only (GPT-style): predict next tokens using causal self-attention; output next-token scores for generation, commonly trained with next-token prediction.
- Encoder-decoder (T5-style): encode the source with source-wide attention and generate target text with a causal decoder and cross-attention, commonly trained on a source-to-target task.
Key Takeaways
- Transformer does not mean “GPT decoder.” Encoder-only, decoder-only, and encoder-decoder are three important families.
- Encoder-only self-attention commonly uses the complete provided input to build contextual representations.
- Decoder-only self-attention is causal because generation must not use future target tokens.
- Encoder-decoder models combine source representations with a causal target decoder through cross-attention.
- Masked-token prediction and next-token prediction are different training objectives, not alternative names for the same architecture.
- BERT-style, GPT-style, and T5-style systems are examples rather than family definitions.
Next Lesson
Next, L5.15 — Stacking Transformer Blocks returns to the decoder-only branch for the Level 5 build. You will stack causal blocks, then L5.16 will assemble the mini decoder that Level 6 trains with next-token prediction.
References
- Vaswani et al., Attention Is All You Need.
- Devlin et al., BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding.
- Radford et al., Language Models are Unsupervised Multitask Learners.
- Raffel et al., Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer.
Completion is stored locally on this device.