GPT-2 · RoBERTa Lab · Part III — The BERT family
11

Encoder vs decoder:
the showdown

One line of code, and all the consequences that descend from it. The chapter that ties the whole course together.

Chapter 11 of 15 Prerequisite: chapters 04 and 10

1 The line

We have reached the point the course had been preparing for since chapter 00. Here it is, in full, the architectural difference between GPT-2 and RoBERTa:

if causal:
    att = att.masked_fill(futuro, float("-inf"))

That's it. Everything else is the same or almost the same: the same Transformer block, the same attention equation, the same tokenization algorithm (byte-level BPE), the same GELU, the same LayerNorm, practically identical sizes (124 M vs 125 M, both with 12 layers, 768 dimensions and 12 heads).

gpt2 and roberta-base are structural twins that do opposite things. It is the perfect controlled experiment, and that is why studying them together teaches more than studying either one alone.

2 The experimental proof

This is not an argument — it is a measurement. In lab 04 you run two sentences that are identical up to the middle and different at the end, and measure how much the representation at each position changed:

A: "The cat sat on the mat and slept   peacefully all afternoon"
B: "The cat sat on the mat and screamed furiously  all afternoon"
                                 ^^^^^^^^^^^^^^^^ only this changes

GPT-2 (causal)                    RoBERTa (bidirectional)
 pos token      |diff|             pos token      |diff|
   0 'The'    0.00e+00               0 '<s>'     3.31e-01
   1 'cat'    0.00e+00               1 'The'     3.66e-01
   2 'sat'    0.00e+00               2 'cat'     2.88e-01
   3 'on'     0.00e+00               3 'sat'     3.91e-01
   4 'the'    0.00e+00               4 'on'      3.40e-01
   5 'mat'    0.00e+00               5 'the'     3.01e-01
   6 'and'    0.00e+00               6 'mat'     2.59e-01
   7 'slept'  7.86e+00  <- changes   7 'and'     3.36e-01
   8 ...      1.30e+01               8 'slept'   3.97e-01
                                     ...
                                    12 '</s>'    3.27e-01

ZERO up to the swapped word.       EVERYTHING changes, even <s>.
The past is untouchable.           One word rewrites the sentence.

It is not approximately zero. It is exactly 0.00e+00, bit for bit. The future does not exist for the past in a causal model. And in RoBERTa, swapping one word at the end alters even the opening token.

3 The tree of consequences

Everything that follows descends from that line. Follow the chain:

Branch A — the decoder

  1. The mask prevents seeing the future.
  2. → Therefore, predicting the next token is a legitimate task, with no leakage.
  3. → Therefore, all n positions generate a training signal. Maximum efficiency.
  4. → Therefore, the model learns P(next | previous), which is exactly what is needed to generate.
  5. → Therefore, the past never changes when a token is appended.
  6. → Therefore, the KV cache is possible, and generation drops from O(n²) to O(n) per token.
  7. → Therefore, scaling is feasible, and scale brought in-context learning.

Branch B — the encoder

  1. Without the mask, each token sees the whole sentence.
  2. → Therefore, predicting the next token would be copying. Unviable objective.
  3. → Therefore, another objective is needed: MLM, masking randomly drawn words.
  4. → Therefore, only 15% of positions generate signal. 85% of the compute is wasted.
  5. → Therefore, training is less efficient per token processed.
  6. → But each representation is built with context from both sides, and is richer for understanding.
  7. → And appending a token changes everything, so there is nothing stable to cache. Autoregressive generation is impossible.
The sentence to take away

The decoder trades understanding for generation. The encoder trades generation for understanding. There is no free lunch: it is the same architecture, with a minus-infinity sign deciding which way it leans.

4 The practical decision table

When to use each one. The rule: if the output is free text, decoder; if the output is a label or a vector, encoder.
TaskBest familyWhy
Writing, completing, conversingdecoderonly it generates
Classifying (spam, sentiment, topic)encoderbidirectional is more accurate; much cheaper
Semantic search / embeddingsencoderquality sentence vector, a single pass
Extracting entities (NER)encoderlabel per token, context from both sides
Extractive answering (finding the span)encodermark start and end in the text
Generative answering (writing the answer)decoderneeds to produce new text
Translation, summarizationencoder-decoderT5, BART — read everything and write everything
Detecting machine textencoderit is classification — our chapter 12
A note on 2026: the encoder did not die

With the success of ChatGPT, it became common sense that "decoders won". That is half true, and the other half matters to anyone building systems.

For classifying, a 125 M RoBERTa-base is still the right choice in production: it runs in milliseconds on a CPU, costs almost nothing, and frequently beats a giant LLM on the specific task it was fine-tuned for. Calling a 70-billion-parameter model to decide whether an e-mail is spam is waste by several orders of magnitude.

And for semantic search, RAG systems commonly use an encoder to produce the embeddings, often derived from BERT or RoBERTa. The decoder writes the answer; the encoder finds what to read.

The practical division, today: encoders find and classify, decoders write.

5 The third family: encoder-decoder

There is a third arrangement, worth knowing to complete the map. The original 2017 Transformer was like this, and T5 and BART follow the same design:

It is the best of both worlds, at the cost of roughly twice the model. Ideal when input and output are distinct objects — translating from Portuguese to English, summarizing an article. Less useful for open dialogue, where the "input" and the "output" are the same continuous stream of conversation — which helps explain why the world of assistants converged on pure decoders.

6 The lineage, on one page

                    Transformer (2017)
                    encoder + decoder
                           |
              +------------+------------+
              |                         |
        DECODER only              ENCODER only
     (with causal mask)        (without causal mask)
              |                         |
         GPT-1 (2018)             BERT (2018)
         117M, BooksCorpus        MLM + NSP, WordPiece
              |                         |
         GPT-2 (2019)             RoBERTa (2019)
         124M-1.5B, WebText       dynamic MLM, no NSP,
         Pre-LN, byte BPE         GPT-2's byte BPE, 160GB
              |                         |
         GPT-3 (2020)                   +--> roberta-*-openai-detector
         175B, few-shot                      binary classifier
              |                             trained on GPT-2's outputs
         InstructGPT / ChatGPT                        ^
         fine-tuning + RLHF                           |
              |                                       |
         modern LLMs  -------- generate the text -----+
                                the detector judges

Note how the drawing closes: the two branches, separated in 2018 by one line of code, meet again in the detector. OpenAI built the hunter with the architecture opposite to the prey's. That is chapter 12.

7 In the lab

.venv\Scripts\python.exe lab\04_multihead_mask.py   (section 3 — the proof)
.venv\Scripts\python.exe lab\10_roberta_mlm.py         (the comparison)

Exercise 11 — the synthesis exercise

Answer without looking anything up. If you get stuck on one, go back to the chapter indicated.

  1. Why can't an encoder use a KV cache? (chapter 04)
  2. Why can't a decoder use MLM as its objective? It actually could — what would be lost? (chapters 08 and 10)
  3. roberta-base has 125 M and gpt2 has 124 M. Where does that 1 million difference come from? (chapters 01 and 02)
  4. If you removed the causal mask from an already-trained GPT-2, without retraining, what would happen to the output? Why?
  5. Hard: why can a 175 B decoder classify reasonably well via prompt, if the encoder is the "right" architecture for that? What does scale compensate for?
References

The primary sources for the concepts in this chapter. The numbers in the text were measured in the lab; the ideas come from here.

Technical terms here carry their machine-learning meaning. The course glossary shows that meaning next to the others the same words have in IT.

  1. Devlin, J., Chang, M.-W., Lee, K. and Toutanova, K. (2019). BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding. NAACL 2019. arXiv:1810.04805
  2. Radford, A., Wu, J., Child, R., Luan, D., Amodei, D. and Sutskever, I. (2019). Language Models are Unsupervised Multitask Learners. Technical report, OpenAI. cdn.openai.com
  3. Raffel, C. et al. (2020). Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer. JMLR, 21(140). arXiv:1910.10683
  4. Lewis, M. et al. (2020). BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension. ACL 2020. arXiv:1910.13461
  5. Ouyang, L. et al. (2022). Training Language Models to Follow Instructions with Human Feedback. NeurIPS 2022. arXiv:2203.02155

Chapter 12 closes Part III with the most curious model in the collection: the detector.
And it brings an uncomfortable answer to question 3 of Exercise 0.