Foundations to Frontier
Tests
E. Deep Learning › E12 Transformer Architectures

RoBERTa

RoBERTa (Liu and colleagues, 2019) showed that BERT was undertrained. Keeping the architecture, it trained longer on ten times more text (about 160 GB) with much larger batches, dropped next-sentence prediction, used full-length sequences, switched to a 50 K byte-level BPE vocabulary and drew a fresh mask every time a sequence is seen (dynamic masking). It matched or beat every model published after BERT, showing how much training choices matter. This page covers each change and what it buys. In the exercises you implement byte-pair encoding and count RoBERTa-base's 124,645,632 parameters. You then compare static and dynamic masking on a small fixed dataset: with 64 training sequences static masks are memorised (training-mask accuracy 1.000, new data 0.223) while dynamic masking reaches 0.874; with 256 sequences the two tie.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Same model, better training

RoBERTa used exactly BERT’s architecture and simply trained it better:

  • more text, bigger batches and longer training;
  • hiding a different set of words each time a sentence comes round, instead of always the same ones;
  • dropping a side task that turned out not to help.

The result beat newer, fancier models. The lesson for engineers: before changing the architecture, check the training recipe. The demo shows BERT-style bidirectional attention.

Transformer lab
Computed pairs: 136 of 256 for 16 tokens. Sparse patterns grow linearly with length instead of quadratically.
Why engineers care

Many reported model improvements are really training improvements. Careful baselines with good recipes save teams from chasing architectural changes that do not matter.

📝 Quick check (Level 0)
0/3 answered
  1. 1. RoBERTa changed BERT's…
  2. 2. Dynamic masking means…
  3. 3. RoBERTa dropped…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Data: BookCorpus and Wikipedia (16 GB), CC-News (76 GB), OpenWebText (38 GB) and Stories (31 GB): about 160 GB, ten times BERT’s.
  • Batches: 8,192 sequences of up to 512 tokens is 4.2 M tokens per batch, 32 times BERT’s 131 K; RoBERTa trained up to 500 K steps.
  • Masking:
    • BERT duplicated its data 10 times with different static masks over 40 epochs, so each mask was seen 4 times;
    • RoBERTa draws a new mask every time.
  • Inputs:
    • no next-sentence prediction;
    • sequences are packed with contiguous full sentences up to 512 tokens, possibly crossing documents.
  • Vocabulary: byte-level BPE with 50,265 tokens. It needs no unknown token, since any byte string can be encoded.
  • Size: RoBERTa-base has 124,645,632 parameters, 31% of them in the embedding table (BERT-base: 21%); RoBERTa-large has 355 M.
  • BPE: start from characters or bytes and repeatedly merge the most frequent adjacent pair. Frequent words become single tokens; rare ones split into pieces.
📝 Quick check (Level 1)
0/3 answered
  1. 1. RoBERTa's training text was about…
  2. 2. Times BERT saw each static mask:
  3. 3. Byte-level BPE needs no…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

RoBERTa in code

Exercise 1.

  • BPE on a toy corpus (low, lower, newest, widest) merged t + ⟨/w⟩ first (three pairs tied at 9 occurrences).
  • After 10 merges “newest” was one token, “lowest” split into low + est, and an unseen word fell back to characters.

Practice.

  • Hugging Face RobertaForMaskedLM with RobertaTokenizerFast (byte-level BPE). RoBERTa has no token-type embeddings in practice (a single segment).
  • Dynamic masking is what DataCollatorForLanguageModeling does by default: masks are drawn when each batch is formed.
  • Large batches need a scaled learning rate and warmup. RoBERTa used peak rates around 6 × 10⁻⁴ (base) with Adam β₂ = 0.98.
collator = DataCollatorForLanguageModeling(tokenizer=tok, mlm_probability=0.15)   # dynamic masks per batch
trainer = Trainer(model=RobertaForMaskedLM(config), data_collator=collator, train_dataset=ds,
                  args=TrainingArguments(per_device_train_batch_size=256, learning_rate=6e-4, warmup_steps=24000))
🐍 Exercise 1: byte-pair encoding
Learn BPE merges from word counts and encode words with them.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Hugging Face's MLM collator masks…
  2. 2. RoBERTa's vocabulary size:
  3. 3. Large batches usually need…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why dynamic masking can matter.

  • With static masks, each sequence always asks the same questions. Over many epochs a model can memorise the answers to those specific masked positions without learning general relations.
  • Dynamic masking turns one sequence into many different training examples, a form of data augmentation.
  • Liu et al. found dynamic masking comparable or slightly better on their huge corpus, where each sequence is seen only a few times. Exercise 2 shows it matters much more when data is small and repeated.

Next-sentence prediction. RoBERTa’s ablations found that removing NSP and packing full sentences matched or improved downstream results. The NSP task was too easy: random sentences usually differ in topic.

Scale of training. Accuracy kept improving with more data and steps, a precursor of the scaling-law view (E12-10): BERT’s architecture was far from saturated.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Static masks risk…
  2. 2. Dynamic masking acts as…
  3. 3. NSP was too easy because random sentences…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

When does dynamic masking matter?

History.

  • Y. Liu, M. Ott, N. Goyal, J. Du, M. Joshi, D. Chen, O. Levy, M. Lewis, L. Zettlemoyer and V. Stoyanov, “RoBERTa: a robustly optimized BERT pretraining approach” (2019).
  • RoBERTa led GLUE, RACE and SQuAD on release.
  • Its byte-level BPE came from GPT-2 (Radford et al., 2019). BPE for subwords came from Sennrich, Haddow and Birch (2016).

Exercise 2.

  • Data: mirrored sequences as in E12-4 (16 tokens, vocabulary 8), with fixed training sets of 64 or 256 sequences.
  • Training: a one-layer encoder for 400 steps of batch 32 (200 or 50 epochs), with static masks (drawn once per sequence) or dynamic masks (fresh each time), both at 15%.
  • Measurement: accuracy on masked tokens of new sequences, and on the training sequences with their static masks.
training set masking new-data accuracy training-mask accuracy
64 static 0.223 1.000
64 dynamic 0.874 0.938
256 static 0.861 0.920
256 dynamic 0.860 0.835

A second seed agreed: 0.192 against 0.898 with 64 sequences, and 0.860 against 0.889 with 256.

  • With little, repeated data, static masks are memorised. The model answers every training mask perfectly but has not learned the mirror rule (0.22, near chance at 0.125).
  • Dynamic masking prevents this. The same 64 sequences give enough varied questions to learn the rule (0.87, at the 0.869 ceiling).
  • With 4 times more data the difference vanishes, consistent with RoBERTa’s finding that, on a huge corpus, dynamic masking is only comparable or slightly better.
🐍 Exercise 2 (research): when does dynamic masking matter?
Train masked LMs with static or dynamic masks on 64 or 256 sequences and compare memorisation and generalisation.

RoBERTa in AI, robotics and aerospace

  • Domain pre-training: with limited in-domain text (maintenance logs, mission reports), use dynamic masking and many epochs carefully, and watch for memorisation.
  • Strong baselines: a well-trained RoBERTa-style encoder is a hard-to-beat baseline for text classification and retrieval.
  • Tokenisation: byte-level BPE handles part numbers, codes and mixed scripts without unknown tokens.

References

  1. Y. Liu et al. (2019). “RoBERTa: a robustly optimized BERT pretraining approach.” arXiv:1907.11692.
  2. R. Sennrich, B. Haddow, A. Birch (2016). “Neural machine translation of rare words with subword units.” Proc. ACL.
  3. A. Radford et al. (2019). “Language models are unsupervised multitask learners.” OpenAI report.