Byte Pair Encoding (BPE)
When training a language model, how do you keep the vocabulary size manageable while still representing every word, even rare or new ones? Byte Pair Encoding (BPE) addresses this by creating a flexible vocabulary of subword units, allowing models to process diverse text efficiently.
Limitations of Basic Tokenization
Traditional tokenization methods often face significant challenges in modern Natural Language Processing (NLP). Word-level tokenization, which treats each word as a distinct unit, leads to enormous vocabularies, especially with large, diverse text corpora. This approach struggles with out-of-vocabulary (OOV) words—words not seen during training—forcing models to assign them a generic "unknown" token and lose their specific meaning.
Conversely, character-level tokenization breaks text down into individual characters. While this eliminates OOV issues and keeps the vocabulary small, it results in very long input sequences for models. Processing these long sequences is computationally expensive, and the model must learn semantic meaning from scratch, as individual characters carry little inherent meaning.
| Feature | Word-level | Character-level | Subword-level (like BPE) |
|---|---|---|---|
| Vocabulary Size | Very Large | Very Small | Moderate |
| Handling OOV Words | Poor (uses | Excellent (no OOV) | Good (breaks into known subwords) |
| Sequence Length/Semantic Meaning Preservation | Short sequences, good semantic units | Very long sequences, poor semantic units | Moderate sequences, good semantic units |
Introducing Byte Pair Encoding (BPE)
Byte Pair Encoding (BPE) offers an elegant solution to the tokenization dilemma by finding a "sweet spot" between word-level and character-level approaches. Its core idea is to iteratively merge the most frequent adjacent character or subword pairs in a text corpus. This process effectively compresses the text by replacing common sequences with new, longer tokens.
BPE starts with an initial vocabulary of all unique characters in the text. It then repeatedly identifies the most frequently occurring pair of adjacent tokens and merges them into a single new token. This new token is added to the vocabulary, and the text is updated to reflect the merge. This iterative merging continues until a predefined vocabulary size is reached or no more merges can be performed, creating a vocabulary of common subword units.
BPE in Action: A Step-by-Step Example
Let's walk through a simplified example to see how BPE builds its vocabulary. Consider a small corpus containing the words "low", "lower", "lowest", "new", and "newer". We'll track the vocabulary and the tokenized representation of the text as merges occur.
l o w l o w e r l o w e s t n e w n e w e r
Initial Vocabulary: {'l', 'o', 'w', 'e', 'r', 's', 't', 'n'}l o appears 3 times, making it the most frequent. We merge l o into a new token lo.
Corpus (updated): lo w lo w e r lo w e s t n e w n e w e r
Vocabulary (updated): {'l', 'o', 'w', 'e', 'r', 's', 't', 'n', 'lo'}lo w is the most frequent pair, appearing 3 times. We merge lo w into low.
Corpus (updated): low low e r low e s t n e w n e w e r
Vocabulary (updated): {'l', 'o', 'w', 'e', 'r', 's', 't', 'n', 'lo', 'low'}e r is a frequent pair (appears 2 times). We merge e r into er.
Corpus (updated): low low er low e s t n e w n e w er
Vocabulary (updated): {'l', 'o', 'w', 'e', 'r', 's', 't', 'n', 'lo', 'low', 'er'}['p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g'] and the most frequent pair m m, what would be the next step?Applying BPE: Tokenizing New Text
Once the BPE algorithm has been run on a large corpus and a vocabulary of subword units (and the sequence of merges that created them) is established, this learned model can tokenize new, unseen text. The tokenization process for new input is deterministic: it greedily applies the longest possible learned subword units from the vocabulary.
This means that when tokenizing a new word, the algorithm attempts to match the longest subword from its vocabulary first. If a match is found, that subword becomes a token, and the process continues on the remainder of the word. If no subword from the vocabulary matches a segment, it falls back to individual characters, ensuring that every word can be represented, even if it's entirely new (an OOV word).
word_to_tokenize variable in the code above to "programming" and observe how it's broken down. Then, try a completely new word like "dataclass" to see how BPE handles words not explicitly in its subword vocabulary.Benefits and Practicalities of BPE
BPE offers several significant advantages for NLP tasks. It effectively handles out-of-vocabulary (OOV) words by breaking them down into known subword units, ensuring that no information is lost due to unknown tokens. This is particularly beneficial for languages with rich morphology or when dealing with specialized domains and evolving terminology.
Furthermore, BPE strikes an optimal balance in vocabulary size. It creates a much smaller vocabulary than word-level tokenization, reducing model complexity and memory requirements, while still preserving more semantic meaning than character-level tokenization. The resulting token sequences are also shorter than character-level sequences, making them more efficient for neural networks to process. This balance allows models to generalize better to new words and maintain reasonable sequence lengths.
When implementing or using BPE, setting a target vocabulary size or a maximum number of merges is important. This controls the granularity of the subword units and prevents the vocabulary from growing too large. The initial vocabulary can be all unique characters, or it can be pre-tokenized words, which influences the types of subwords learned. Starting with pre-tokenized words (e.g., splitting by spaces) is common in many BPE variants used in large language models.
Byte Pair Encoding (BPE) is an iterative subword tokenization algorithm.
It builds a vocabulary by repeatedly merging the most frequent adjacent character or subword pairs in a text corpus.
BPE effectively handles out-of-vocabulary (OOV) words by breaking them into known subword units.
It balances vocabulary size and sequence length, offering a middle ground between word-level and character-level tokenization.
The process creates a flexible vocabulary that allows language models to represent diverse text efficiently, addressing the initial challenge of managing vocabulary size.
Try it yourself
Tokenization
A real byte-pair tokenizer, trained live on a small corpus. Type “strawberry” and see why a model that writes essays cannot count the r’s in it.
Open the lab