1. Start with the task and the available context
A Transformer block is a reusable component. The architecture around it determines which sequences it processes and which positions can exchange information.
Compare two tasks:
- Classify a review: read “I expected more, but the film disappointed me” and predict a sentiment label.
- Continue a story: read “Once there was a kitten that loved…” and generate what follows.
Read and classify
“I expected more, but the film disappointed me.”
Complete input → contextual representations → label
Continue a prefix
“Once there was a kitten that loved…”
Available prefix → next token → longer prefix
For classification, the complete supplied review is available. A representation at one position can use information before and after it. For ordinary next-token generation, the continuation has not been written yet; prediction must use the available prefix.
“Left” and “right” refer to positions relative to a particular query position, not to the sequence as a whole. In “cat / chases / the / ball”, the query at “chases” might access all four positions or only “cat” and “chases”, depending on its mask.
These are useful distinctions, not exclusive categories of intelligence. A causal model can classify a review after reading its complete prefix. An encoder can support generation when paired with suitable output components or decoding procedures.
2. What do encoder and decoder mean?
An encoder maps a supplied input sequence into contextual representations. In a standard Transformer encoder, each non-padding position can attend to the other supplied positions.
A decoder produces representations used to predict output tokens. A standard autoregressive decoder masks future target positions. In an encoder-decoder architecture, it additionally attends to the encoder's source representations.
The names describe roles and connectivity. They do not mean that an encoder compresses a whole sentence into exactly one vector, or that a decoder simply reverses a token embedding lookup.
Decoder-only models omit the separate encoder and its cross-attention. They put the prompt and generated continuation into one causal stream.
3. The encoder: contextualise the supplied input
Consider “I thought the film would be good, but it disappointed me”. Information near the end can change how we interpret the earlier words. Bidirectional self-attention lets a position use both sides of the supplied sequence.
Input tokens
Bidirectional self-attention + FFN, residual paths and normalisation
Contextual rows → classification, span or token-prediction head
An encoder returns a contextual row for each position. A classification head can use a pooled or designated representation; token-label heads produce outputs per position; a span-prediction head can select positions in the input.
BERT is a familiar encoder-only example. Its masked-language-model objective corrupts selected tokens and trains the model to predict the original tokens using the supplied context. “Paris is the [MASK] of France” can draw on words on both sides of the gap.
A simplified masked prediction loss is:
where contains the selected prediction positions and is the corrupted input. Not every selected position must literally contain a mask marker; the corruption policy is part of the training design. Original BERT also used an additional sentence-pair objective.
BERT's standard objective is not ordinary left-to-right continuation. Saying “BERT cannot generate text” is still too absolute: iterative masked predictions or an added decoder can generate text, but those procedures differ from conventional GPT-style generation.
Bidirectional access is also subject to practical masks: padding positions are usually excluded, and some applications introduce other attention restrictions.
4. The decoder: predict without seeing the future
For causal self-attention, query position may access key position when:
The current position is included. Its representation is used to predict the next token, not to recover its own already visible input token.
A mask added to the attention scores expresses the rule:
| Position | Input | Visible input prefix | Target |
|---|---|---|---|
| 0 | <BOS> | <BOS> | I |
| 1 | I | <BOS> I | love |
| 2 | love | <BOS> I love | cats |
| 3 | cats | <BOS> I love cats | <EOS> |
The same forward pass can calculate these training predictions for all supplied positions. Future positions exist in the training tensor, but the mask prevents earlier queries from accessing them through causal self-attention.
The autoregressive factorisation is:
and a corresponding training loss is:
During generation, the next token is selected from the predicted distribution, appended to the prefix and used for the following step. A key-value cache can reuse previous computations, but the dependency on selected tokens remains.
GPT-style models use this causal pattern. It suits continuation, dialogue and code generation. It does not imply that the model can only perform those tasks: labels, translations and answers can also be generated as text.
| Query / key | cat | chases | the | ball |
|---|---|---|---|---|
| 0 · cat | ✓ | — | — | — |
| 1 · chases | ✓ | ✓ | — | — |
| 2 · the | ✓ | ✓ | ✓ | — |
| 3 · ball | ✓ | ✓ | ✓ | ✓ |
Query 1 (chases) can access: cat, chases.
This shows attention permissions, not learned weights or predictions. Cross-attention keys come from the complete source sequence; target self-attention remains causal. Padding and other masks are omitted.
The experiment shows permissions, not attention weights. An allowed key can still receive a small learned weight. Selecting a different row shows how the available prefix grows under the causal mask.
5. Compare the three architecture families
The third family combines an encoder and a decoder. The encoder reads a complete source sequence, such as a sentence to translate. The decoder writes a target sequence while consulting the source representations.
Its standard block contains three main sublayers:
- Causal self-attention over the target prefix.
- Cross-attention to encoder output.
- A position-wise feed-forward network.
Residual paths and normalisation surround these sublayers as part of the block.
Source: J’aime les chats
Keys and values from H
Target prefix: BOS, I, love
Queries from S
Cross-attention → FFN → vocabulary logits → next target token
In cross-attention, decoder representations supply queries, while encoder representations supply keys and values. The score table is rectangular when source and target lengths differ:
All known source positions can normally be consulted. This does not reveal future target tokens: source access and causal target self-attention are separate operations.
The target distribution is conditional on the source :
Encoder-only · BERT
Bidirectional input self-attention
Contextual representations; classification, retrieval and token/span tasks
Decoder-only · GPT-style
Causal self-attention over prompt and continuation
Autoregressive continuations, including answers and task labels
Encoder-decoder · Original Transformer / T5
Bidirectional source + causal target + source cross-attention
Conditional text generation, including translation and summarisation
The original Transformer used the encoder-decoder arrangement. T5 is another example: it casts tasks into a text-to-text format with a source sequence and a generated target.
The usual task preferences are useful guides, rather than hard boundaries. Encoder-decoder models can generate task labels as well as translations, while decoder-only models can produce classifications and handle filling tasks using suitable prompts or training.
6. Why is causal generation a useful common interface?
Many tasks can be expressed as completing a prompt:
| Task | Supplied text | Desired continuation | | --- | --- | --- | | Sentiment | “Classify this review: … Answer:” | A label such as “negative”. | | Translation | “Translate this sentence into English: …” | The translated sentence. | | Question answering | “What is the capital of France? Answer:” | “Paris”. | | Code generation | A request and a function signature | An implementation. |
A causal language model can use the same token-generation interface for all of them. Large-scale text training, a reusable architecture and this flexible interface help explain the appeal of decoder-only designs. They do not establish that one family is best for every workload.
Encoders remain useful when the required output is a representation, similarity score or task label. Encoder-decoder models provide an explicit separation between source processing and target generation. Latency, memory, data, training objectives and output requirements all affect a practical choice.
A prompt can also contain examples of a task. If a model uses those examples to perform a new case without an optimiser updating its parameters, that is in-context learning. It is different from fine-tuning.
This capability is learned and varies across models and tasks; it is not guaranteed merely by adding a causal mask. Text-to-text task interfaces are not unique to decoder-only models, as T5 illustrates.
7. What you have discovered
- Encoder self-attention contextualises a known input using bidirectional access.
- Causal decoder self-attention includes the current position and earlier positions, while excluding future target positions.
- Shifted targets make next-token training compatible with parallel calculations over the known training sequence.
- Encoder-decoder cross-attention connects decoder queries to encoder keys and values.
- Architecture, objective and output head jointly shape capabilities; “understanding” and “generation” are not exclusive abilities assigned to separate families.
Next, we will examine residual connections and layer normalisation, and how they support training stacked blocks.