{"notes":[{"id":"transformers/roberta","category":"transformers","legacyPath":"transformers/1907-roberta","legacySlug":"1907-roberta","legacyPaths":[],"title":"RoBERTa","subtitle":"A Guide to Training BERT","author":"Z. Yuan","paperDate":"2019-07-26","publishedOn":"2024-09-26","paperTitle":"RoBERTa: A Robustly Optimized BERT Pretraining Approach","paperUrl":"https://arxiv.org/abs/1907.11692","bibliography":{"title":"RoBERTa: A Robustly Optimized BERT Pretraining Approach","authors":["Yinhan Liu","Myle Ott","Naman Goyal","Jingfei Du","Mandar Joshi","Danqi Chen","Omer Levy","Mike Lewis","Luke Zettlemoyer","Veselin Stoyanov"],"url":"https://arxiv.org/abs/1907.11692","pdfUrl":"https://arxiv.org/pdf/1907.11692","codeUrl":null,"preprint":{"id":"1907.11692","submittedOn":"2019-07-26","url":"https://arxiv.org/abs/1907.11692"},"publication":null,"citation":"@misc{transformersroberta2019,\n  title = {{RoBERTa: A Robustly Optimized BERT Pretraining Approach}},\n  author = {Yinhan Liu and Myle Ott and Naman Goyal and Jingfei Du and Mandar Joshi and Danqi Chen and Omer Levy and Mike Lewis and Luke Zettlemoyer and Veselin Stoyanov},\n  year = {2019},\n  eprint = {1907.11692},\n  archivePrefix = {arXiv},\n  url = {https://arxiv.org/abs/1907.11692}\n}\n","citationPath":"/content/papers/citations/transformers/roberta.704870f708c1.bib"},"thumbnail":{"src":"/content/papers/assets/cbf2bba8a99ec32ddf52a845f831eada66fc53f6337cd22a4fff68e4fc6bb2e4.jpg","width":1064,"height":452},"locale":"en","path":"/content/papers/en/transformers/roberta.20a92ad248ef.md","headings":[{"id":"defining-the-problem","text":"Defining the Problem","depth":2},{"id":"solving-the-problem","text":"Solving the Problem","depth":2},{"id":"optimization-of-implementation-details","text":"Optimization of Implementation Details","depth":3},{"id":"optimizing-the-training-data","text":"Optimizing the Training Data","depth":3},{"id":"discussion","text":"Discussion","depth":2},{"id":"static-masking-vs-dynamic-masking","text":"Static Masking vs. Dynamic Masking","depth":3},{"id":"next-sentence-prediction-nsp","text":"Next Sentence Prediction (NSP)","depth":3},{"id":"large-batch-training","text":"Large Batch Training","depth":3},{"id":"tokenization","text":"Tokenization","depth":3},{"id":"training-data-size","text":"Training Data Size","depth":3},{"id":"conclusion","text":"Conclusion","depth":2}],"anchorAliases":{},"guide":null,"relatedReading":[{"id":"transformers/bert","relation":"background","topic":"Pretraining bidirectional language representations"}]},{"id":"transformers/bert","category":"transformers","legacyPath":"transformers/1810-bert","legacySlug":"1810-bert","legacyPaths":[],"title":"BERT","subtitle":"Twelve Layers of Encoders","author":"Z. Yuan","paperDate":"2018-10-11","publishedOn":"2024-05-12","paperTitle":"BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding","paperUrl":"https://arxiv.org/abs/1810.04805","bibliography":{"title":"BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding","authors":["Jacob Devlin","Ming-Wei Chang","Kenton Lee","Kristina Toutanova"],"url":"https://arxiv.org/abs/1810.04805","pdfUrl":"https://arxiv.org/pdf/1810.04805","codeUrl":null,"preprint":{"id":"1810.04805","submittedOn":"2018-10-11","url":"https://arxiv.org/abs/1810.04805"},"publication":{"venue":"NAACL-HLT","year":2019,"kind":"conference","url":"https://doi.org/10.18653/v1/n19-1423"},"citation":"@misc{transformersbert2018,\n  title = {{BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding}},\n  author = {Jacob Devlin and Ming-Wei Chang and Kenton Lee and Kristina Toutanova},\n  year = {2018},\n  eprint = {1810.04805},\n  archivePrefix = {arXiv},\n  url = {https://arxiv.org/abs/1810.04805}\n}\n","citationPath":"/content/papers/citations/transformers/bert.d9bf47a21c5f.bib"},"thumbnail":{"src":"/content/papers/assets/57ed95afec24d2732e290d4fcb5633e14a2bbe74b83c6b6cdee90ef44ac89411.jpg","width":1224,"height":396},"locale":"en","path":"/content/papers/en/transformers/bert.75ec0229da6b.md","headings":[{"id":"defining-the-problem","text":"Defining the Problem","depth":2},{"id":"unidirectionality","text":"Unidirectionality","depth":3},{"id":"solving-the-problem","text":"Solving the Problem","depth":2},{"id":"model-architecture","text":"Model Architecture","depth":3},{"id":"pre-training-strategy","text":"Pre-training Strategy","depth":3},{"id":"fine-tuning-strategy","text":"Fine-tuning Strategy","depth":3},{"id":"discussion","text":"Discussion","depth":2},{"id":"ablation-study","text":"Ablation Study","depth":3},{"id":"conclusion","text":"Conclusion","depth":2}],"anchorAliases":{},"guide":null,"relatedReading":[{"id":"transformers/transformer","relation":"background","topic":"Pretraining bidirectional language representations"},{"id":"transformers/roberta","relation":"next","topic":"Pretraining bidirectional language representations"},{"id":"transformers/albert","relation":"comparison","topic":"Training and compressing BERT"}]}],"text":"BERT’s success inspired a wave of subsequent research, and this paper revisits its design with a focus on optimization. Through a series of experiments, the authors discovered several areas where BERT could be improved significantly.\n\n## Defining the Problem\n\nLet’s begin by reviewing the key aspects of BERT's design:\n\nBERT's pretraining involves two main objectives: Masked Language Modeling (MLM) and Next Sentence Prediction (NSP).\n\nBERT first undergoes pretraining on large-scale unlabelled text, followed by fine-tuning on labelled data.\n\nBuilt on a Transformer architecture, BERT consists of L layers of Transformers, each with A self-attention heads, and hidden layers of dimension H.\n\nBERT’s pretraining objectives consist of:\n\n- **Masked Language Modeling (MLM)**:\n\n  - Randomly masking 15% of the tokens in the input sequence, where the objective is to predict the masked tokens. Of the selected tokens, 80% are replaced with a [MASK] token, 10% remain unchanged, and 10% are replaced with random tokens.\n\n- **Next Sentence Prediction (NSP)**:\n  - NSP is a binary classification task that predicts whether two segments of text are consecutive. Positive samples come from contiguous sentences, while negative samples are from different documents. Each type of sample represents 50% of the training data.\n\nBERT uses the Adam optimizer with the following hyperparameters:\n\n- $\\beta_1 = 0.9$, $\\beta_2 = 0.999$, and L2 regularization with a weight decay of 0.01.\n- The learning rate gradually increases to a peak of 1e-4 over the first 10,000 steps and then decays linearly.\n- Use 0.1 dropout on hidden states and attention probabilities, with GELU in the FFN. GELU is not applied directly to all attention weights.\n- Pretraining continues for 1,000,000 updates, with a batch size of 256 sequences, each with a maximum sequence length of 512 tokens.\n\nBERT was trained on a combination of the BOOKCORPUS and English WIKIPEDIA datasets, which together amount to 16GB of uncompressed text.\n\n---\n\nThe authors looked back at all these choices and thought: there is still a lot to improve. So they set out to change BERT's training recipe.\n\n## Solving the Problem\n\n### Optimization of Implementation Details\n\nMost hyperparameters follow the original BERT optimization settings, except for adjustments in the peak learning rate and the number of warm-up steps, which vary depending on the specific settings.\n\nThe authors found that training is highly sensitive to the epsilon value in the Adam optimizer, with adjustments potentially improving performance or stability. When using larger batch sizes, setting $\\beta_2 = 0.98$ also enhances stability.\n\nUnlike the earlier BERT training setup, the authors did not randomly insert short sequences or reduce sequence length for the first 90% of updates. They used full-length sequences, up to 512 tokens, throughout pre-training.\n\nTraining was conducted using mixed-precision floating-point computation on DGX-1 machines, each equipped with 8 × 32GB Nvidia V100 GPUs.\n\n### Optimizing the Training Data\n\nPretraining BERT-like models requires a large corpus of text data. Prior research showed that increasing the dataset size generally improves performance in downstream tasks.\n\nThough many studies used larger and more diverse datasets than the original BERT, not all of these additional datasets are publicly available. In their experiments, the authors collected as much data as possible to ensure high data quality and quantity.\n\nIn the end, the authors utilized five different English corpora of varying sizes and domains, totaling over 160GB of uncompressed text. The first list item groups two of the five corpora:\n\n1. **BOOKCORPUS** and **WIKIPEDIA**: The same datasets used in the original BERT training (16GB).\n2. **CC-NEWS**: English news articles from the CommonCrawl News dataset, spanning from September 2016 to February 2019, containing 63 million articles (after filtering, 76GB).\n\n3. **OPENWEBTEXT**: An open-source recreation of the WebText dataset described by Radford et al. (2019), containing web pages linked from Reddit posts with at least three upvotes (38GB).\n\n4. **STORIES**: A dataset introduced by Trinh and Le (2018), consisting of text from CommonCrawl that matches the Winograd schema story style (31GB).\n\n\n> [!TIP]\n> \n> We put in a lot of effort, used ten times more data, and got a better model!\n> \n> This is an internal joke, as the authors show that even without increasing data, modifying training strategies yields superior results.\n\n\n## Discussion\n\nIn the paper, the authors break down the key areas of improvement into several topics:\n\n### Static Masking vs. Dynamic Masking\n\n\n\n![BERT-Base static versus dynamic masking on SQuAD, MNLI and SST-2](/content/papers/assets/cbf2bba8a99ec32ddf52a845f831eada66fc53f6337cd22a4fff68e4fc6bb2e4.jpg \"Paper Table 1; source: arXiv:1907.11692v1\")\n\n\n\nTable: Paper [Table 1](https://arxiv.org/pdf/1907.11692v1). The static/dynamic rows belong to the authors' BERT-Base reimplementation; the reference row is from another reported result.\n\nThe original BERT implementation used static masking, where the masks were applied once during preprocessing and reused for every epoch. To avoid training on the same masked patterns, the data was duplicated 10 times, allowing each sequence to be masked in 10 different ways over 40 epochs, with each masked version being seen four times.\n\nDynamic masking, on the other hand, generates new masking patterns each time the sequence is fed into the model, making it particularly useful for longer training steps or when larger datasets are used for pretraining.\n\nResults are mixed. Relative to static masking, dynamic masking changes SQuAD 2.0 from **78.3 to 78.7 F1** and SST-2 from **92.5 to 92.9%**, but MNLI-m from **84.3 to 84.0%**. The authors treat the methods as broadly comparable and use dynamic masking for its fresh samples at each read, not because it wins every task.\n\n### Next Sentence Prediction (NSP)\n\n![BERT-Base downstream results for four input formats with or without NSP](/content/papers/assets/64f4d750632789b23d7794c48b711c6dfa6e1770ccceb912f7cfe24b2c4ed304.jpg \"Paper Table 2; source: arXiv:1907.11692v1\")\n\nTable: Paper [Table 2](https://arxiv.org/pdf/1907.11692v1). The no-NSP configurations also change how sentences are packed, so these rows do not isolate the NSP loss alone.\n\nOriginal NSP uses adjacent segments from the same document as positives and randomly drawn segments from another document as negatives. Its target follows this sampling scheme, not a general test of whether any two segments share a source.\n\nSome research had questioned the necessity of NSP, suggesting that removing it might not negatively affect performance.\n\nTo evaluate this, the authors compared several input formats:\n\n- **SEGMENT-PAIR+NSP**: The same as original BERT, retaining the NSP loss, with input consisting of two segments.\n- **SENTENCE-PAIR+NSP**: Input consists of two natural sentences, retaining NSP loss. These inputs are much shorter than 512 tokens, so the batch size is increased to keep the total token count similar.\n- **FULL-SENTENCES**: Input packs consecutive full sentences, NSP loss is removed, and sequences may cross document boundaries, with an extra separator when they do.\n- **DOC-SENTENCES**: Similar to FULL-SENTENCES, but sequences do not cross document boundaries, and NSP loss is removed. Near a document's end, the batch size is adjusted to keep a similar total token count.\n\nComparing SEGMENT-PAIR+NSP with DOC-SENTENCES, SQuAD 2.0 rises from **78.7 to 79.7 F1**, MNLI-m from **84.0 to 84.7%**, and RACE from **64.2 to 65.6%**, while SST-2 falls from **92.9 to 92.7%**. The no-NSP configuration matches or improves most tasks here, but sentence packing changes too; Table 2 cannot assign the entire difference to NSP removal.\n\nDOC-SENTENCES does slightly better than FULL-SENTENCES, but the authors use FULL-SENTENCES later because its batch size is easier to keep fixed for comparisons.\n\n### Large Batch Training\n\n\n\n![Perplexity and task accuracy for batch sizes 256, 2K and 8K at roughly matched example counts](/content/papers/assets/4e006daaf348ac16e03edf1c05d264438854dccd02ba33620446647604ec9e76.jpg \"Paper Table 3; source: arXiv:1907.11692v1\")\n\n\n\nTable: Paper [Table 3](https://arxiv.org/pdf/1907.11692v1). Step counts and learning rates also change with batch size; **2K**, not **8K**, gives the best perplexity and MNLI-m in this table.\n\nBatch size must be compared together with learning rate and total data exposure. The reference uses BERT-Base with batch 256 and one million updates; the comparison adjusts batch size and steps to preserve the total number of examples seen.\n\nAt batch sizes **256/2K/8K**, perplexity is **3.99/3.68/3.77**, MNLI-m is **84.7/85.2/84.6%**, and SST-2 is **92.7/92.9/92.8%**. The 2K row is best on all three, so gains are not monotonic. Larger batches facilitate distributed data parallelism, and the authors choose **8K** for later runs. Since learning rate and update count change as well, this is not a single-variable batch ablation.\n\n### Tokenization\n\nByte-Pair Encoding (BPE) can handle large vocabularies in natural language. BPE treats subword units as the basic unit of text.\n\nOriginal BERT uses about 30K WordPiece entries after heuristic preprocessing; its tokens are not all individual characters. RoBERTa switches to about 50K byte-level BPE entries to reduce reliance on language-specific preprocessing.\n\nInitial experiments showed little difference in performance between the two, with byte-level BPE performing slightly worse in some tasks. However, due to its broader applicability, the authors used byte-level BPE in subsequent experiments.\n\n### Training Data Size\n\n![RoBERTa corpus-size and pretraining-step comparisons for SQuAD, MNLI and SST-2](/content/papers/assets/a5ecbc74409ef7cc74de8b085d8b88bcf61ee5e9cf6c45f3fcab303cc1204248.jpg \"Paper Table 4 with legacy red annotations; source: arXiv:1907.11692v1\")\n\nTable: The imported crop of paper [Table 4](https://arxiv.org/pdf/1907.11692v1) includes **red boxes and an English arrow added in the earlier note**; they are not original paper annotations. Its first RoBERTa row uses **16GB**, while the published BERT-Large baseline uses **13GB**, not identical data volume.\n\nThe model shares BERT-Large’s layer and hidden configuration (L = 24, H = 1024, A = 16). The larger vocabulary brings RoBERTa-Large to about 355M parameters, compared with original BERT-Large’s 340M. The first run uses BOOKCORPUS and WIKIPEDIA for 100K steps.\n\nUsing 1024 V100 GPUs, training took roughly one day.\n\nThe first RoBERTa row uses **16GB, an 8K batch and 100K steps**, giving **87.3 F1** on SQuAD 2.0 and **89.0%** on MNLI-m. The published BERT-Large row uses about **13GB, a 256 batch and 1M steps**, giving **81.8/86.6**. Training choices matter, but this is not a single-variable comparison. RoBERTa's own rows are clearer: raising the corpus from 16GB to 160GB at 8K/100K steps changes SQuAD 2.0 **87.3→87.7** and MNLI-m **89.0→89.3**; extending the 160GB run to 500K steps brings them to **89.4/90.2**. These rows accumulate changes.\n\n\n> [!TIP]\n> \n> 1024 V100 GPUs. (？？？)\n\n\n## Conclusion\n\nKey areas of improvement highlighted in the paper:\n\n1. Train the model for longer.\n2. Use larger batches and train on more data.\n3. Remove the \"NSP\" objective.\n4. Train with longer input sequences.\n5. Use dynamic masking on the training data.\n\nRoBERTa reached then-best results on GLUE, RACE and SQuAD, without multi-task fine-tuning for GLUE or extra training data for SQuAD. Its GLUE test submission did ensemble single-task models, and some tasks used specific fine-tuning steps. The results show how much these overlooked training choices can matter.\n\nThe same idea carries over to other architectures: a good training recipe can bring out more of a model's performance. When making a comparison, we still have to keep the corpus, training compute and fine-tuning procedure in view.\n","imageSizes":{"/content/papers/assets/cbf2bba8a99ec32ddf52a845f831eada66fc53f6337cd22a4fff68e4fc6bb2e4.jpg":{"width":1064,"height":452},"/content/papers/assets/64f4d750632789b23d7794c48b711c6dfa6e1770ccceb912f7cfe24b2c4ed304.jpg":{"width":1386,"height":646},"/content/papers/assets/4e006daaf348ac16e03edf1c05d264438854dccd02ba33620446647604ec9e76.jpg":{"width":858,"height":316},"/content/papers/assets/a5ecbc74409ef7cc74de8b085d8b88bcf61ee5e9cf6c45f3fcab303cc1204248.jpg":{"width":3854,"height":1595}}}
