Sub topic 14
Comparing BPE, WordPiece, and SentencePiece
This chapter assumes you know basic Python, dictionaries, loops, frequency counts, and the idea that a language model receives tokens rather than raw characters.
BPE, WordPiece, and SentencePiece all reduce the out-of-vocabulary problem by representing text with subword units. They differ mainly in how they learn a vocabulary and how much text preparation they require.
What each approach does
BPE starts with small units, often characters or bytes. It repeatedly merges the most frequent adjacent pair. The learned merge list determines how new text is encoded.
WordPiece also builds larger pieces from smaller ones, but its training procedure chooses vocabulary additions using a likelihood-style score rather than simply choosing the most frequent pair. During encoding, it usually applies greedy longest-match search. A continuation marker such as ## shows that a piece continues a word.
SentencePiece is a model-training and tokenisation framework. It accepts raw text instead of requiring whitespace-separated words. It can train either a BPE model or a unigram language model. It represents spaces explicitly with the ▁ marker, which helps make encoding reversible.
| Approach | Vocabulary learning | Typical encoding | Raw text input |
|---|---|---|---|
| BPE | Merge the most frequent adjacent pair | Apply learned merges in order | Usually needs pre-tokenisation |
| WordPiece | Select pieces using a likelihood-style score | Greedy longest match | Usually uses pre-tokenisation |
| SentencePiece | Train BPE or unigram models | Model-specific segmentation | Yes |
How to choose
Choose BPE when you want a simple, predictable algorithm that is easy to inspect and implement. It is common in decoder-only language models. Byte-level BPE is especially useful when the system must represent arbitrary text, because every byte can be covered without an unknown token.
Choose WordPiece when you are integrating with an ecosystem that already expects WordPiece conventions, such as models using [CLS], [SEP], and ## continuation pieces. Its longest-match encoder is straightforward, but its vocabulary depends on the training score and preprocessing choices.
Choose SentencePiece when your data is multilingual, does not have reliable whitespace, or must preserve the original text exactly. It is particularly useful for scripts where spaces do not separate words. Remember that SentencePiece is not one segmentation algorithm: a SentencePiece model may use BPE or a unigram language model.
Trade-offs and failure modes
Tokeniser choice cannot be separated from the training corpus. A vocabulary trained mostly on English may split names, code, or other scripts into many pieces. This increases sequence length and can reduce model capacity for useful context.
Vocabulary size creates another trade-off. A larger vocabulary often produces shorter sequences, but it requires a larger embedding table and may memorize many rare forms. A smaller vocabulary saves parameters but produces more pieces per sentence.
The tokenizer must be identical during training and inference. Differences in Unicode normalization, whitespace handling, special tokens, or vocabulary files can change token IDs. A model then receives inputs that do not mean what its embedding table expects.
> callout warning > Compare tokenisers using the same training and evaluation data. Measure fertility, meaning the average number of tokens per word or sentence, together with unknown-token rates and behavior on multilingual, numeric, and unusual text.
A practical decision process
- Define required coverage, languages, and normalization rules.
- Decide whether raw-text, language-independent training is important.
- Check the model architecture or pretrained checkpoint requirements.
- Train candidate tokenisers on representative data.
- Compare sequence length, unknown handling, memory cost, and downstream task quality.
- Freeze the tokenizer files and preprocessing configuration with the model.
Exercise
You need a multilingual model trained from raw text, including languages with inconsistent or absent whitespace. Which approach is the strongest starting point, and why?
Show answer
Start with SentencePiece. It accepts raw text, preserves whitespace with the ▁ marker, and can train either BPE or a unigram model without relying on language-specific pre-tokenisation.