Sub topic 2
One Vocabulary for Many Scripts: Shared Subword Tokenization
This chapter assumes you know basic Python, have used the Hugging Face transformers library, and understand that a tokenizer converts text into model inputs. You should also know that a vocabulary is the set of symbols a model can recognize.
Why one vocabulary is useful
A multilingual model needs to process many languages. A separate word vocabulary for every language would be large and difficult to manage. It would also fail whenever text contains a new word, spelling variation, or name.
A shared subword tokenizer uses pieces that are smaller than words. A piece might be a whole word, part of a word, punctuation, or a character sequence. The tokenizer builds one vocabulary containing pieces from many languages and scripts.
For example, an English word may be stored as one piece, while a rare Hindi word may be split into several pieces. The model can still represent the rare word because it can combine smaller pieces.
| Input text | Possible token pieces | Main benefit |
|---|---|---|
| `playing` | `play`, `ing` | Handles related word forms |
| `unusualword` | `un`, `usual`, `word` | Handles unseen or rare words |
| `आज` | Several script-specific pieces | Supports another writing system |
The exact pieces depend on the tokenizer and its trained vocabulary. Do not assume that spaces, words, or characters always equal tokens.
Comparing languages with XLM-R
XLM-R, or XLM-RoBERTa, uses a SentencePiece-based vocabulary shared across many languages. SentencePiece is a tokenizer that learns subword pieces directly from text, including text without spaces.
Install the libraries if needed:
pip install transformers sentencepieceThe following program prints the pieces and their counts:
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("xlm-roberta-base")
texts = {
"English": "The weather is nice today.",
"German": "Das Wetter ist heute schön.",
"Hindi": "आज मौसम अच्छा है।",
}
for language, text in texts.items():
pieces = tokenizer.tokenize(text)
print(f"{language}: {len(pieces)} tokens")
print(pieces)
print()A token count is important because transformer models usually have a maximum input length measured in tokens, not words. If one language uses more pieces for the same message, it consumes more of that limit and may require more computation.
How the tokenizer handles scripts
A shared vocabulary can contain pieces from Latin, Cyrillic, Arabic, Devanagari, and other scripts. It does not require every language to use the same alphabet. Instead, the vocabulary contains pieces that let the model encode each script.
The tokenizer usually prefers frequent sequences. Frequent words or word parts receive efficient pieces. Rare sequences receive smaller pieces. This creates a compromise:
- Larger pieces make common text shorter and faster.
- Smaller pieces provide better coverage for rare words.
- A fixed vocabulary cannot give every language equally many useful pieces.
Tokenization disparity
Tokenization disparity means that equivalent content receives different numbers or qualities of tokens across languages. A language with little representation in the training data may be split into many short pieces. This can increase cost and reduce the amount of meaning represented in each context window.
Inspect tokenization before comparing model performance across languages. Report token counts, truncation rates, and results per language. A low score may reflect limited training data, poor token coverage, or both.
Practice
Exercise
Why might the same meaning require more tokens in one language than another?
Show answer
The shared vocabulary contains more frequent or longer pieces for some languages. A language with less suitable vocabulary coverage may be split into smaller pieces, producing more tokens.