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

RoBERTa:
BERT done right

Masked Language Modeling, dynamic masking, and the lesson that sometimes the advance is training better what already existed.

Chapter 10 of 15 Prerequisite: chapters 03, 04 and 08

1 The other branch of the tree

In October 2018, four months after GPT-1, Google published BERT. It started from the same Transformer block and made one different decision:

remove the causal mask.

Without the mask, each token sees the whole sentence — past and future. The representation of "banco" in "sentei no banco da praça" (Portuguese: "I sat on the bench in the square") is now built with "praça" available from the very first block.

The gain in comprehension is enormous and immediate. BERT broke records on eleven tasks at once. But there is a price, and it is fatal for one specific thing.

2 The problem bidirectionality creates

If each token sees all the others, the objective of predicting the next word becomes trivial. To predict word 5, just look at word 5. The model would learn to copy, and nothing else.

Note that this is exactly the label leakage the causal mask existed to prevent (chapter 04). By removing the mask, BERT lost the right to use GPT's objective. It needed another training objective.

The solution: mask the target, not the future

BERT's idea is elegant: instead of hiding the future, hide randomly drawn words, scattered through the text, and ask the model to reconstruct them using everything left on both sides.

Original text:
  The cat sat on the mat and slept peacefully

Model input (15% of tokens masked):
  The cat <mask> on the mat and <mask> peacefully
                                    ^
Target: reconstruct 'sat' and 'slept'.

To get 'sat' right, the model uses 'cat' (on the left) AND 'on the mat'
(on the right). That is what a BIDIRECTIONAL task means.

This is called Masked Language Modeling (MLM). In plain terms: the fill-in-the-blank task, the sentence-completion exercise you did at school. The model is forced to understand the whole context to fill the gap.

The odd 80/10/10 detail

Of the 15% of tokens selected for masking, BERT does not replace all of them with <mask>. It does:

FractionWhat it doesWhy
80%replaces with <mask>the main task
10%replaces with a random tokenforces the model to distrust every token, not just the masked ones. Without this, it would learn to blindly copy any unmasked token.
10%leaves the original tokenprevents the model from learning "I only need to think when I see <mask>". It needs to keep a good representation of every token, always.
The big disadvantage of MLM

Compare the efficiency. In GPT-2, a sequence of n tokens generates n predictions — every position is charged. In BERT, only the 15% masked ones generate a training signal. 85% of the computational work is wasted.

That is why MLM models need many more passes over the data to reach the same learning — and it is one of the reasons the decoder family ended up dominating once scale became the decisive factor.

There is also a mismatch between training and use: the <mask> token appears in training and never in real applications. The 80/10/10 is precisely a patch to reduce that mismatch.

3 What RoBERTa changed (2019)

RoBERTa is not a new architecture. It is the same BERT, trained properly. The name is the acronym of Robustly Optimized BERT Approach, and the paper from the Facebook AI authors has an implicit subtitle that is almost a provocation: BERT was significantly undertrained.

The changes, all in the training procedure. The architecture is identical.
ChangeBERTRoBERTa
Maskingstatic — the masks are drawn once, in preprocessing, and the model always sees the same pattern for each sentencedynamic — redrawn every epoch. The same sentence is seen with different gaps.
NSP taskpredicted whether two sentences were consecutiveremoved — the authors showed it hurt
Data16 GB160 GB (10×)
Batches256 sequences8,000 sequences
TokenizerWordPiece, 30,000GPT-2's byte-level BPE, 50,265
Training1 M steps500 K steps with much larger batches

Why dynamic masking matters

In the original BERT, the sentence "O gato sentou no tapete" ("The cat sat on the mat") had its masks decided a single time. If the draw picked "sentou", the model would always see that same gap, epoch after epoch — and could simply memorize that specific pair.

In RoBERTa, every epoch redraws. In one epoch the gap is "sentou", in another it is "tapete", in another it is "gato". Each sentence becomes multiple different exercises, and the model is forced to build a representation that serves to reconstruct any part. It is cheaper than collecting new data, and it works.

Why removing NSP was the brave decision

BERT had a second objective: given a pair of sentences, predict whether the second followed the first in the original document (Next Sentence Prediction). The intuition was to teach relations between sentences, useful for tasks like textual inference.

The RoBERTa authors tested it and showed that NSP did not help — and in several tasks, hurt. The task was too easy: telling two sentences from the same document apart from two sentences from random documents is almost always a matter of topic, not coherence. The model learned to detect a change of subject, a shallow signal, and spent capacity on it.

The lesson worth more than the model

RoBERTa invented nothing. It took an existing architecture, questioned every training decision, measured, and removed what did not work. It gained several points on benchmarks and became the foundation of a good part of the NLP industry.

In a field that tends to value new architectures, this paper is an important reminder: a considerable share of what gets attributed to "better architecture" is, in fact, better training — and it is almost never measured separately. It is worth being suspicious of this whenever you read a paper announcing a new architecture.

4 The structural differences you will run into

GPT-2RoBERTa
Causal maskyesno
Training objectivepredict the next tokenreconstruct masked tokens
Signal per sequence100% of positions15% of positions
Special tokens<|endoftext|><s> </s> <pad> <unk> <mask>
Wraps the sentence?no<s> … </s>
Vocabulary50,25750,265
Context1024512
Generates text?yesno
KV cache possible?yesno

RoBERTa's 8 extra tokens (50,265 − 50,257) are exactly the special ones it needs and GPT-2 does not. <s> deserves special attention: it is the first token of every sequence and, because it sees everything (bidirectionally), it ends up working as a summary of the whole sentence. Classification comes out of it — and that is the subject of chapter 12.

5 In the lab

.venv\Scripts\python.exe lab\10_roberta_mlm.py
An honest limitation of our models

The two RoBERTa models we downloaded are *-openai-detector: they were fine-tuned for classification, and in the process the MLM head was discarded and replaced with a binary classification head.

This means we cannot fill gaps with them — the necessary head is not in the weights. What we can do, and what the lab does: show the bidirectional body working (which is what matters conceptually), measure the difference in representation against GPT-2, and use the classification head they actually have. It is a real restriction of this set of models, and the lab records it instead of making the script look more complete than it is.

  1. Shows RoBERTa's tokenizer, with <s>/</s> wrapping the sentence and <mask> available in the vocabulary.
  2. Simulates the 80/10/10 masking on a sentence, several times, so you can see dynamic masking in action — the same sentence becoming different exercises.
  3. Compares the representation of an ambiguous word ("bank" in two contexts) in GPT-2 and RoBERTa, measuring how much each one separates the two senses.
  4. Redoes the causality proof from lab 04, now explaining the result from the encoder's point of view.
  5. Measures the waste of MLM: how many positions generate signal in each family.

Exercise 10 — predict before running

  1. Why does RoBERTa have a context of 512 and GPT-2 of 1024? Is that an architectural limitation or a training choice? (Think about the quadratic cost and what each one needs to do.)
  2. If MLM uses only 15% of positions, why not mask 50%? Where is the limit? (Hint: what is left to provide context?)
  3. RoBERTa adopted GPT-2's tokenizer — a competing model, from another lab. Why is that not strange?
  4. Hard: an encoder cannot generate text autoregressively. But there is a way to generate with MLM (filling gaps iteratively, as text diffusion models do). Why is that slower and usually worse?
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. Liu, Y. et al. (2019). RoBERTa: A Robustly Optimized BERT Pretraining Approach. Preprint. arXiv:1907.11692

You have seen the two branches separately. Chapter 11 puts them face to face
and shows that all the differences descend from a single line of code.