# Encoder-Only vs Decoder-Only vs Encoder-Decoder?

> Encoder-only, decoder-only and encoder-decoder models: what each one reads and writes, and which design fits which job, explained for your AI interview.

**In short:** An encoder-only model reads text to label or score it, like BERT. A decoder-only model writes text one word at a time, like the GPT family. An encoder-decoder reads an input first and then writes an output, like the original Transformer or T5.

AI and LLM interview question Q049, Transformer architecture. Intermediate, very common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/encoder-only-vs-decoder-only-vs-encoder-decoder

## Say it in 30 seconds

The three designs wire the Transformer differently. An encoder-only model, like BERT, reads everything at once and labels it, such as tagging a complaint email. A decoder-only model, like GPT, writes one word at a time, seeing only the words before it. An encoder-decoder, like the original Transformer or T5, reads an input and writes an output, which suits translation. I pick by the job: a label, free writing, or a rewrite of one text into another.

## Say it in 2 minutes

A Transformer is the network design behind these models, and it builds on attention. The three versions differ in two choices: which parts you keep, and whether each word may look at the words after it. An encoder reads the whole input and builds a rich picture of every word, with each word looking both ways. A decoder writes output one token (roughly a word) at a time, and it may only look at the words before.

Encoder-only models, like BERT, use the reading side alone. BERT's name says it: Bidirectional Encoder Representations from Transformers, so it reads in both directions. It is good at labelling and scoring, such as tagging each complaint email in one pass. It is not built to write free text.

Decoder-only models use the writing side alone. The GPT family is trained to predict the next word from the words before it, and it runs on a prompt. Encoder-decoder models, like the original Transformer and T5, read the input first and then write the output. That suits translation, a task that needs the writing side.

In an interview, start with the job. Labels and scoring need reading. Free writing needs decoding. Rewriting one text into another needs both. Then name the trade-off: OpenAI's own GPT-3 paper says its one-way design may cost quality on tasks that gain from reading both ways.

## What are encoder-only, decoder-only and encoder-decoder models, in plain words?

A Transformer is the network design behind these models. The 2017 paper that introduced it builds on attention alone: each word weighs the other words around it.

Think of an insurance claims team. One clerk reads the whole claim file before writing a one-line decision. That is an encoder: it takes in everything first. A second clerk drafts a letter to the customer one sentence at a time, looking only at what is already on the page. That is a decoder. A third clerk reads the adjuster's notes and then writes the letter from them. That is the encoder-decoder pattern.

The same three roles show up in AI. A bank's complaint tool reads each email whole and tags it. A reply drafter writes its answer word by word. A translator for a machine maker reads the English manual, then writes the German one.

- **Encoder:** The part that reads the input and builds a rich picture of each word.
- **Decoder:** The part that writes output one token at a time.
- **Token:** A chunk of text the model works on, often a word or part of a word.
- **Masking:** A rule that hides the words a position is not allowed to look at. BERT's training also hides words, but that is a training trick, not this rule.

## How does each design read and write text?

The designs differ on two points: whether a word may see words on both sides of it, and whether the model reads, writes or does both. The chart puts each design in its own column.

*Diagram, Three designs and how text flows through them: Each column is one design, read top to bottom. The encoder-only design reads everything and returns a label. The decoder-only design writes one word at a time. The encoder-decoder reads a source first, then writes a target.*
1. The whole input goes in at once, so every word can see every other word.
2. The model returns a label or a span as its answer.
3. The words so far go in, and the decoder may not look ahead.
4. It picks one next word, which is added and read again.
5. The source text goes in whole.
6. The encoder hands its reading across to the decoder.
7. The decoder writes the target one token at a time.

Encoder-only models use the reading side alone. BERT's name says it: Bidirectional Encoder Representations from Transformers. It reads context from both sides in every layer. It is first trained on large amounts of plain, unlabelled text, then given one extra output layer for a task such as question answering.

BERT learns by hiding some words and guessing them from the words around them. The T5 paper calls BERT an encoder-only model, and its training goal is to rebuild the hidden tokens at the encoder's output. So it is built to fill in and classify.

Decoder-only models use the writing side alone. OpenAI's GPT-2 post says the model was trained simply to predict the next word in 40GB of Internet text. Its decoder is masked, so each word can look only backwards. The original Transformer paper says the rule exists to preserve the auto-regressive property, which means each new word depends only on the words already written.

Encoder-decoder models use both sides. The original Transformer was an encoder-decoder built for sequence-to-sequence tasks. The encoder reads the whole input. The decoder writes the output one token at a time, and each decoder layer has attention over the encoder's output.

Here is the masking rule in plain Python, on one refund note. Each line is one word, and a Y means that word may read the word in that column.

```python
# A refund note, read by two designs. Which words may each position look at?
words = ["Refund", "approved", "for", "the", "duplicate", "charge"]

def show(title, can_see):
    print(title)
    for i, word in enumerate(words):
        row = "".join("Y" if can_see(i, j) else "." for j in range(len(words)))
        print(f"{word:<10} {row}")

show("Encoder: every word reads the whole note", lambda i, j: True)
show("Decoder: each word reads only itself and the words before", lambda i, j: j <= i)
```

Output:

```text
Encoder: every word reads the whole note
Refund     YYYYYY
approved   YYYYYY
for        YYYYYY
the        YYYYYY
duplicate  YYYYYY
charge     YYYYYY
Decoder: each word reads only itself and the words before
Refund     Y.....
approved   YY....
for        YYY...
the        YYYY..
duplicate  YYYYY.
charge     YYYYYY
```

In the decoder block, Refund may read only itself, and each later word may read everything up to its own position. That one rule stops the model from peeking ahead while it writes.

## Which design fits which job?

- **Encoder-only: Tag or score.** A bank's complaint tool reads each email whole and tags it as a fee dispute, a lost card or a branch complaint.
- **Decoder-only: Draft the reply.** A claims assistant writes a letter to a policyholder from the adjuster's notes, one word at a time.
- **Encoder-decoder: Map one text to another.** A machine maker translates 2,000 pages of safety manuals from English into German and Polish.

> "are designed to produce a single prediction per input token or a single prediction for an entire input sequence. This makes them applicable for classification or span prediction tasks but not for generative tasks like translation or abstractive summarization." (T5 paper, Raffel et al. (2019))
>
> "Thus our design decision comes at the cost of potentially worse performance on tasks which empirically benefit from bidirectionality." (GPT-3 paper, Brown et al. (2020))

The trade-off goes both ways. Reading in both directions helps tasks like tagging, and the GPT-3 paper admits its one-way design may cost quality on tasks that gain from both directions. An encoder-decoder has two stacks to run.

> **Tip:** Say the job first, then the design. A label from one input usually points to encoder-only, though a prompted decoder-only model can return a label too, so name the trade-off. Free-form writing points to decoder-only. Turning one long text into another points to encoder-decoder.

### How would you choose a design for a new bank product?

A bank wants one tool that tags complaints and drafts replies. Here is how I would split the choice.

1. **Name the output:** A label, score or span from the input means encoder-only. Free text means decoder-only. Turning a long input into a new text means encoder-decoder.
2. **Check what labelled data you have:** Past complaints with labels suit a small encoder you can train further on your own examples. A decoder can start from a prompt alone. GPT-3 was applied that way, with no retraining.
3. **Test on your own emails:** Measure each design on real complaints from your own bank, with the costly wrong answers marked, before you trust any public test score.

My answer would be two models with two jobs: an encoder that tags complaints, and a decoder that drafts replies, which a person checks before anything is sent.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] The encoder reads the whole input and labels it. The decoder writes one token at a time.
- [ ] BERT is encoder-only. GPT-style models use only the decoder. The original Transformer and T5 are encoder-decoder.
- [ ] The key difference is attention direction: an encoder word reads both sides, a decoder word reads only the words before it, and masking enforces that.
- [ ] Pick by the job: a label, free writing, or one text rewritten into another.
- [ ] Name the trade-off, and don't call any one design the best for everything.

**Common traps**

- Trap: "BERT can write a reply too, just more slowly." Better: BERT is built to read and label. Its training fills in hidden words, and T5's authors say encoder-only models are not for generative tasks.
- Trap: "Decoder-only is always the best choice." Better: It depends on the job. OpenAI's GPT-3 paper admits the one-way design may cost quality on tasks that need both directions.
- Trap: "Encoder-decoder is two models glued together." Better: It is one network with two parts. Each decoder layer looks back at the encoder's output while it writes.

## Can you spot the wrong design?

**Spot the mismatch:** Which of these four pairings of design and job is wrong?

A. An encoder-only model tags each complaint email as fee dispute or lost card.
B. A decoder-only model drafts a reply letter from the adjuster's notes.
C. An encoder-only model drafts the reply letter to the policyholder.
D. An encoder-decoder model translates the English safety manual into German.

**Answer:** C. An encoder-only model reads and labels. It is not built for generative tasks, so drafting a letter needs a decoder. The other three match their design.

**Quick check:** A hospital wants each discharge note tagged 'follow-up needed' or 'no follow-up'. Which design is built for this job, with no free text in the output?

A. Decoder-only, writing a summary of each note first
B. Encoder-only, reading the whole note and returning one of two labels
C. Encoder-decoder, rewriting each note into a new one
D. Decoder-only, writing its reasoning and then the label

**Answer:** B. The output is one label from one input, which is the encoder's job. The other three write free text first, which adds words to check when the answer is a choice of two.

## Can you answer these from memory?

**Flash cards**

- **Encoder-only model?** Reads the whole input and labels it. BERT is the classic example.
- **Decoder-only model?** Writes one token at a time, seeing only earlier words. GPT-style models.
- **Encoder-decoder model?** Reads an input, then writes an output. The original Transformer and T5.
- **What is masking?** A rule that hides later words from a position, so the decoder can't look ahead.
- **Design for translation?** Encoder-decoder: read the source, then write the target.
- **Design for complaint tags?** Encoder-only: read the whole email and return a label.

## Likely follow-up questions

- Q050 What is causal (masked) attention? https://designervenkat.online/interview/ai/what-is-causal-masked-attention
- Q051 What is cross-attention? https://designervenkat.online/interview/ai/what-is-cross-attention
- Q055 What is the KV cache, and why does it speed up generation? https://designervenkat.online/interview/ai/what-is-the-kv-cache
- Q062 How does BERT differ from GPT? https://designervenkat.online/interview/ai/how-does-bert-differ-from-gpt
- Q063 Masked vs causal language modelling? https://designervenkat.online/interview/ai/masked-vs-causal-language-modelling
- Q073 Walk through one decoder block's forward pass. https://designervenkat.online/interview/ai/walk-through-one-decoder-block-forward-pass

## Learn it in depth

- [Transformers](https://designervenkat.online/learn/machine-learning/transformers)
