Sub topic 3
Pretraining on Many Languages: mBERT and XLM-R
This chapter assumes that you understand Transformer encoders, masked language modeling, and basic Hugging Face fine-tuning. You should also know Python and be comfortable interpreting token IDs and model outputs.
The multilingual pretraining idea
A multilingual model uses one Transformer to process several languages. During pretraining, the model sees text from many languages and learns to predict tokens that have been hidden from the input.
For example:
- Input:
The cat is [MASK] - Target:
sleeping
The same process can use German, Hindi, Arabic, or other languages. The model does not need a separate encoder for each language. Its parameters are shared across all training examples.
This design can replace many monolingual models. One model is easier to deploy and can transfer knowledge from a high-resource language to a lower-resource language. The cost is competition for vocabulary space, model capacity, and training data.
mBERT
Multilingual BERT, usually called mBERT, extends BERT to many languages. It was pretrained on Wikipedia text from more than one hundred languages.
mBERT uses:
- A shared WordPiece vocabulary.
- The BERT Transformer architecture.
- Masked language modeling.
- Next sentence prediction in its original pretraining setup.
The training data is not usually made of translated sentence pairs. English and German examples can appear in the same training run without being translations of one another. Cross-lingual behavior emerges because the languages use the same model parameters and vocabulary, and because some languages share words, scripts, sentence patterns, or meanings.
A shared vocabulary does not mean that every language receives equal treatment. Frequent English fragments may occupy many vocabulary entries, while words in a low-resource script may be split into several pieces.
XLM-R
XLM-R is a multilingual version of the RoBERTa encoder. It was trained on the larger CC100 collection, which was extracted from Common Crawl and filtered by language.
Compared with mBERT, XLM-R uses:
- SentencePiece tokenization instead of BERT WordPiece.
- More multilingual web data.
- RoBERTa-style masked language model training.
- More careful language sampling to reduce the dominance of high-resource languages.
- A larger vocabulary and model family.
XLM-R does not require parallel translations to learn useful cross-lingual representations. Its advantage comes mainly from scale, data coverage, architecture choices, and sampling. It often transfers better than mBERT, but it still performs unevenly across languages.
| Model | Main training source | Tokenization | Typical strength |
|---|---|---|---|
| mBERT | Multilingual Wikipedia | WordPiece | Broad early multilingual baseline |
| XLM-R | Filtered Common Crawl | SentencePiece | Stronger coverage and transfer at larger scale |
Inspecting shared tokenization
The tokenizer determines how text becomes model input. Run this example to compare token boundaries across scripts.
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("xlm-roberta-base")
texts = {
"en": "The weather is nice today.",
"de": "Das Wetter ist heute schön.",
"hi": "आज मौसम अच्छा है।",
}
for language, text in texts.items():
pieces = tokenizer.tokenize(text)
print(language, len(pieces), pieces)A language that produces more pieces for the same amount of text requires more computation and may lose useful word-level information. Token counts are therefore one practical clue about multilingual model quality.
What the model actually learns
During training, the encoder builds contextual representations. A word's representation depends on nearby words, its language, and the task context. Shared parameters encourage reusable patterns, such as named entities, negation, sentence roles, and semantic relationships.
However, the representations are not perfectly aligned. Languages with similar scripts or abundant pretraining data often transfer more easily. Differences in word order, morphology, script, and domain can reduce transfer.
Exercise
Why can XLM-R transfer to a language on which it was not fine-tuned?
Show answer
XLM-R learned shared parameters and contextual representations from multilingual pretraining. Those representations can encode patterns that are useful in the new language, even without task labels in that language.