How byte pair encoding learns the pieces a language model reads, and why those pieces make letter counting hard.
A language model cannot read letters. Before it sees your text, a program called a tokenizer cuts the text into pieces called tokens and replaces each piece with a number. The model only ever sees the numbers. The way those pieces are chosen explains why models are bad at spelling, letter counting and some other tasks, and why the same text can cost different amounts in different languages. This article follows byte pair encoding (BPE), the method behind most tokenizers.
Neural Machine Translation of Rare Words with Subword Units. Rico Sennrich, Barry Haddow, Alexandra Birch (University of Edinburgh). ACL 2016. arXiv:1508.07909. This paper brought BPE to language tasks. It describes BPE as a compression method by Gage (1994) and adapts it.
Language Models are Unsupervised Multitask Learners (the GPT-2 paper), Radford et al., OpenAI, 2019. PDF. Section 2.2 describes the byte-level version of BPE.
Start with single characters, then again and again join the pair of neighbouring pieces that occurs most often, until the list of pieces is the size you want.
Common words end up as one piece. Rare words split into smaller pieces. Because every character is on the list, any word can be written, so nothing is ever "unknown".
Sennrich et al. wanted translation to handle any word, including rare names and long compound words such as the German Abwasserbehandlungsanlage. A list of whole words cannot cover every word. A list of single characters can, but the paper's counts show the cost: for their German training text, characters give 550 million tokens, against 100 million for whole words and 112 million for BPE.
| Splitting | Tokens in the text | Different pieces (types) |
|---|---|---|
| Whole words | 100 million | 1,750,000 |
| Characters | 550 million | 3,000 |
| BPE | 112 million | 63,000 |
BPE keeps the text nearly as short as whole words, with a small list of pieces.
The training text is first split into words, and each word is written as characters. The paper adds a special end-of-word symbol · so the original spacing can be restored. Pairs are not allowed to cross a word boundary. Then repeat:
The final list size is the starting characters plus the number of merges. The number of merges is the only setting of the algorithm.
Toy example (our own, checked by running it). Word counts: hug 10, pug 5, pun 12, bun 4, hugs 5. I leave out the end marker to keep it short.
Start with letters: h u g (10), p u g (5), p u n (12), b u n (4), h u g s (5). The pair u g appears 10+5+5=20 times, the most of any pair
The result is the ordered list of merges: u+g, u+n, h+ug, p+un.
To split a new word, write it as characters and apply the merges in the order they were learned. Do not recount anything.
bugs: b u g s. Merge 1 (u+g) applies: b ug s. No other merge matches. Tokens: b, ug, s.pun: p u n. Merge 1 does not match. Merge 2 (u+n): p un. Merge 4 (p+un): pun. One token.mug: m u g. Merge 1: m ug. Tokens: m, ug.The word mug was never in the training text, yet it is split into known pieces. The order matters. A later merge can only use pieces that earlier merges made.
All text is stored as bytes, which are numbers from 0 to 255. For example, the letter é is two bytes in UTF-8 (195 and 169). The GPT-2 paper points out that a base list of all Unicode characters would be over 130,000 symbols, which is too large compared to the 32,000 to 64,000 token lists usually used with BPE. A byte-level BPE needs a base list of only 256. Any text in any language can be written with it, so there is never an unknown symbol. The GPT-2 vocabulary has 50,257 tokens.
GPT-2 adds one rule. Plain BPE on bytes would learn many separate versions of a common word, such as dog., dog! and dog?. So the paper stops BPE from merging across character categories (letters, punctuation and so on), with an exception for spaces.
The model receives numbers for pieces. If a tokenizer cuts strawberry as straw + berry, the model gets two numbers. Those numbers do not contain the letters. To count the rs, the model would have to have learned, from its training text, how each piece is spelled. The exact split depends on the tokenizer, and some words are cut so that the model has to rely on that memory. This is a reading problem, not a counting problem.
Sennrich et al. compared BPE with a word-level model that, for unknown words, falls back to a dictionary (WDict). They tested English to German and English to Russian on WMT 2015 data. BLEU is an automatic translation score where higher is better. "Ens-8" means an ensemble of 8 models.
| English to German, newstest2015 | BLEU single | BLEU ens-8 | Unigram F1, rare words |
|---|---|---|---|
| WDict (baseline) | 22.0 | 24.2 | 36.8 |
| BPE, separate 60k lists | 21.5 | 24.5 | 40.9 |
| BPE, joint 90k list | 22.8 | 24.7 | 41.8 |
For English to Russian, ens-8 BLEU was 22.8 (WDict), 23.6 (BPE-60k) and 24.1 (joint BPE). The paper says the subword ensembles beat the baseline by 0.3 to 1.3 BLEU. Joint BPE means the merges are learned on the source and target text together, which makes the splits more consistent between the two languages.
The numbers show that BPE helps with rare words in translation. They do not show it is best everywhere. On words never seen in training, the dictionary baseline scored higher in German (F1 36.8 against 33.6 for joint BPE), and a character-pair model got the best BLEU (25.3) in one German setting.