skip to content

Search

← The Language Machines

Part II — The Transformer Era (2017–2020)

Tokenization

The BPE merge algorithm, SentencePiece, glitch tokens, and why tokenizers cause weird failures.

2015–2024 13 min read 8 papers

It is August 2015. Neural machine translation systems can only handle the words they saw during training — everything else collapses into a single unknown-word symbol. A short paper from Edinburgh proposes borrowing a merge trick from data compression, and in doing so quietly fixes the input layer of every large language model that follows. This chapter covers the layer almost nobody sees: how raw text becomes the integer sequences a transformer actually reads.

Where We Were

Every neural language model needs a vocabulary: a fixed, finite list of symbols it can read and emit. Through 2015 there were two obvious choices, and both were bad.

The first was word-level vocabularies. Models like word2vec assigned one vector per word (chapter 2), and the encoder–decoder translation systems of 2014 (chapter 3) inherited the same design: pick the 30,000–80,000 most frequent words, map everything else to a single <unk> token. This failed in predictable ways. Natural language has a long tail — names, numbers, typos, new coinages, and the rich inflection systems of languages like German, Russian, Turkish, or Finnish, where a single stem can surface in dozens of forms. A word-level system that had seen Regierung (government) still treated Regierungsverantwortung (governmental responsibility) as an unknown word. Translation systems papered over this with back-off dictionaries and copy mechanisms, but the core problem remained: the model’s world was closed, and language is open.

The second choice was character-level modeling: give the model an alphabet of individual characters and let it learn everything else. This eliminates unknown words by construction — any string decomposes into characters — but at a steep price. Sequences become several times longer, recurrent networks of the era struggled to carry information across the resulting distances, and compute per sentence grows with sequence length. Characters are also a semantically impoverished unit: the model must relearn from scratch that c-a-t forms a meaningful whole.

What the field needed was the middle ground: a unit larger than a character, smaller than a word, chosen so that common strings stay whole while rare strings decompose gracefully. Between 2015 and 2019 that middle ground was found, standardized, and pushed all the way down to raw bytes.

From a compression trick to a fairness problem The same merge algorithm that fixed open vocabularies in 2015 later turned out to tax some languages far more than others. Grey monospace text is the paper, system or measurement for that step. 2015 BPE for text merge frequent symbol pairs Sennrich et al., 2015 2016 GNMT wordpieces production-scale subwords Google, September 2016 2018 SentencePiece probabilistic segmentation Kudo and Richardson, 2018 2019 Byte-level BPE 256-symbol base alphabet GPT-2, 50,257 tokens, 2019 2021 ByT5 token-free, byte to byte robust to spelling noise 2023 Tokenizer unfairness measured across languages up to 15x length gap the founding idea adopted in production a refinement a cost the recipe revealed later The tokenizer is trained separately from the model it feeds, on its own corpus, with its own objective; the last two cards are that seam showing.
The tokenizer is trained separately from the model it feeds, on its own corpus, with its own objective; the last two cards are that seam showing. PNG ↓

The Breakthrough

Byte Pair Encoding for text, introduced in “Neural Machine Translation of Rare Words with Subword Units” (Sennrich et al., 2015), adapted a byte-level data-compression algorithm into a vocabulary learner. The idea is disarmingly simple. Start with a vocabulary of individual characters. Scan the training corpus and count every pair of adjacent symbols. Merge the most frequent pair into a single new symbol, add it to the vocabulary, and repeat. Each merge is recorded as a rule; after a few thousand iterations the vocabulary contains characters, common fragments like ing and tion, and eventually whole frequent words. The number of merges is the tokenizer’s only real hyperparameter — it directly sets the vocabulary size.

BPE learns a vocabulary by merging, one frequent pair at a time The loop repeats until the vocabulary hits its target size; every merge becomes a rule replayed later on new text. Grey monospace text is what each step actually operates on. Corpus as characters the starting vocabulary one symbol per character Count adjacent pairs every symbol pair in the corpus frequency table Pick the most frequent a single winning pair greedy, no ties broken smartly Merge into one symbol added to the vocabulary rule recorded in order Target size reached? Final vocabulary characters, fragments, whole words plus the ordered merge list yes no, count again over the updated corpus the raw material one loop iteration the stopping check what training leaves behind
Every merge is greedy and local, but the recorded order is what lets the same procedure be replayed deterministically on text the trainer never saw. PNG ↓

At inference time the learned merge list is replayed deterministically on new text: split the input into characters, then apply the merges in the order they were learned. Any word the model has never seen decomposes into known subwords — in the worst case, into individual characters. The vocabulary is finite, but the set of representable strings is open. Sennrich et al. showed this was not just elegant but effective: on the WMT 15 translation tasks, subword models improved over a back-off dictionary baseline by 1.1 BLEU on English–German and 1.3 BLEU on English–Russian.

Google arrived at a closely related scheme independently. Its production translation system, GNMT (Wu et al., 2016), divided words “into a limited set of common sub-word units” — wordpieces — for both input and output, demonstrating in September 2016 that subword vocabularies worked at production scale, not just in research benchmarks. Wordpieces later became the tokenizer of BERT and its descendants (chapter 6).

The next conceptual step treated segmentation as a probabilistic modeling problem rather than a greedy procedure. Subword regularization (Kudo, 2018), from April 2018, observed that any given sentence admits many valid segmentations, and that committing to exactly one throws information away. Kudo proposed a segmentation algorithm based on a unigram language model: the vocabulary carries a probability for each token, a sentence’s segmentation score is the product of its token probabilities, and during training the model sees multiple segmentations sampled in proportion to their probability. The sampling acts as a regularizer — the same word arrives split differently across epochs, making the model robust to segmentation noise, with the largest gains in low-resource and out-of-domain settings.

Four months later came the tooling consolidation. SentencePiece (Kudo & Richardson, 2018) packaged both BPE and the unigram model into an open-source C++ and Python library with one decisive design change: it trains directly on raw sentences, with no language-specific pre-tokenization step. Earlier pipelines assumed text arrived pre-split into words — an assumption that quietly breaks for Japanese, Chinese, and Thai, which do not delimit words with spaces. SentencePiece instead treats the input as a raw character stream and escapes whitespace itself with the meta-symbol ▁ (U+2581), making tokenization fully lossless: detokenization is a simple string join followed by replacing ▁ with spaces, with no ambiguity about where spaces were. Kudo and Richardson validated the end-to-end approach on English–Japanese translation, showing accuracy comparable to pipelines that used a dedicated pre-tokenizer.

The final move went below characters entirely. GPT-2 (Radford et al., 2019), released in February 2019 (chapter 7), ran BPE over bytes rather than Unicode characters. A byte-level base alphabet has exactly 256 symbols and covers every possible input — every emoji, every script, every corrupted encoding — so an unknown token becomes structurally impossible. GPT-2’s vocabulary of 50,257 tokens (256 byte symbols, plus learned merges, plus one end-of-text token) became one of the most widely reused artifacts in the field, and byte-level BPE became the default recipe for the GPT lineage.

Everything in this chapter happens before the first attention layer Raw text becomes integers, then vectors, before a transformer ever runs. Grey monospace text is the concrete unit each stage works in. Raw text a sentence typed by a user Unicode string Byte or char stream the tokenizer's base alphabet 256 bytes or characters Apply learned merges in the order they were learned deterministic replay Token ids integers, not text indices into a fixed vocabulary Embedding lookup learned per token GPT-2: 768 dims Transformer layers chapter 5 what the tokenizer takes in the tokenizer's own work where the model takes over
The transformer itself never sees text; tokenizer defects are invisible in the architecture diagram because they live entirely in the three steps before it. PNG ↓

The transformer itself (chapter 5) never sees text. It sees a sequence of integers, each mapped to a learned embedding vector. Everything in this chapter happens before the first attention layer — which is precisely why tokenizer defects are invisible in the architecture diagram and yet surface everywhere in model behavior.

The Mathematics

BPE’s training loop is a greedy frequency maximization. At each step, the algorithm selects the pair of adjacent symbols to merge:

(x∗,y∗)=arg⁡max⁡(x,y) count(x,y)(x^{*}, y^{*}) = \arg\max_{(x, y)} \ \mathrm{count}(x, y)

The unigram language model of Kudo (2018) replaces greedy merging with an explicit probabilistic model. A segmentation of a sentence is scored as the product of its token probabilities, and decoding picks the best segmentation:

P(x)=∏i=1Mp(xi),x∗=arg⁡max⁡x∈S(X)P(x)P(\mathbf{x}) = \prod_{i=1}^{M} p(x_i), \qquad \mathbf{x}^{*} = \arg\max_{\mathbf{x} \in \mathcal{S}(X)} P(\mathbf{x})
SymbolMeaning
XXThe raw input sentence
S(X)\mathcal{S}(X)The set of all valid segmentations of XX
x=(x1,…,xM)\mathbf{x} = (x_1, \ldots, x_M)One candidate segmentation, a sequence of MM subword tokens
p(xi)p(x_i)Learned unigram probability of token xix_i
P(x)P(\mathbf{x})Score of the whole segmentation — product of its token probabilities
x∗\mathbf{x}^{*}The maximum-likelihood segmentation actually used

Finally, vocabulary size is not free. Every token in the vocabulary owns a row in the model’s embedding matrix (and, tied or not, in the output projection):

Nembed=∣V∣⋅dmodelN_{\mathrm{embed}} = |V| \cdot d_{\mathrm{model}}

The efficiency of a tokenizer on a given text is usually summarized as its fertility — average tokens produced per word — or its compression rate in bytes per token. Two models with identical architectures but different fertility on your language are not paying the same price per sentence, a fact that resurfaces below.

What It Enabled

Subword tokenization removed the open-vocabulary problem as a blocker, and everything in Part II is downstream of that. The pretrain-then-finetune wave (chapter 6) trained on web-scale corpora (chapter 11) precisely because no string in those corpora could crash the input layer. The GPT lineage (chapter 7) standardized on byte-level BPE. And the idea of a learned discrete vocabulary escaped text entirely: VQ-VAE codebooks give images their own “tokens” (image-generation chapter 5), the foundation of the multimodal models covered in chapter 18.

Word-level (pre-2015)Character-levelSubword (2015–)
Unknown wordsCollapse to <unk>Impossible by constructionImpossible by construction
Typical vocabulary30k–80k words per languageDozens to hundreds of symbols30k–250k shared units
Sequence lengthShortVery longModerate
Morphology and rare wordsLostLearnable but slowDecompose into meaningful parts
Multilingual sharingPoor — separate vocabulariesGoodGood — shared subwords across languages

The individual algorithms settled into stable niches:

SchemeYearSelection criterionNotable adopters
BPE (Sennrich et al., 2015)2015Greedy pair frequencyNMT systems; most GPT-family models
WordPiece (Wu et al., 2016)2016Corpus-likelihood-driven mergesGNMT, BERT family
Unigram LM (Kudo, 2018)2018Probabilistic segmentation, vocabulary pruningModels trained with subword regularization
SentencePiece (Kudo & Richardson, 2018)2018Framework — implements BPE and unigram on raw textStandard open-source tokenizer tooling
Byte-level BPE (Radford et al., 2019)2019BPE over a 256-byte base alphabetGPT-2 and successors

The failure modes

The chapter’s subtitle promise — why tokenizers cause weird failures — comes down to one structural fact: the tokenizer is trained separately from the model, on its own corpus, with its own objective. The seams show.

Glitch tokens. If a string is frequent in the tokenizer’s training corpus but rare or absent in the model’s, the vocabulary contains a token whose embedding is essentially untrained — a reserved seat nobody ever sat in. Prompting a model with such a token produces unpredictable behavior, because the model is being asked to interpret a vector it never learned. The best-known specimen is the token SolidGoldMagikarp, a Reddit username that survived into GPT-2’s vocabulary; the phenomenon was later studied systematically in “Fishing for Magikarp” (Land & Bartolo, 2024), which showed such under-trained tokens exist across essentially all major model families and developed automated methods — inspecting tokenizer behavior and model embedding weights — to detect them before users do.

Character blindness. A model that reads strawberry as one or two opaque tokens has no direct access to its letters, which is why letter-counting, spelling, rhyming, and arithmetic on long digit strings fail in ways that seem bizarre for systems that write fluent prose. The cleanest evidence is comparative: ByT5 (Xue et al., 2021), a token-free byte-to-byte model from May 2021, showed byte-level models are “significantly more robust to noise and perform better on tasks that are sensitive to spelling and pronunciation” — at the cost of much longer sequences, traded against parameter count, training FLOPs, and inference speed. Tokenization, in other words, is a compression choice, and what it compresses away is exactly what character-level tasks need.

Language inequity. Because merge rules are learned from a corpus, the tokenizer is fluent in whatever that corpus over-represents — overwhelmingly English. “Language Model Tokenizers Introduce Unfairness Between Languages” (Petrov et al., 2023) measured the damage in May 2023: the same text translated into different languages can differ in tokenized length by up to 15×, and even character- and byte-level models show over 4× differences for some language pairs. Since commercial APIs bill per token, generate token by token, and cap context windows in tokens (chapter 16), a speaker of a poorly tokenized language pays more, waits longer, and fits less into the same model — for identical content.

Key Papers

PaperAuthorsYearLinkWhy it matters
Neural Machine Translation of Rare Words with Subword UnitsSennrich et al.2015arXiv:1508.07909Adapted BPE from compression to text; solved open-vocabulary NMT with a 3-step merge loop
Google’s Neural Machine Translation SystemWu et al.2016arXiv:1609.08144Wordpieces at production scale; the tokenizer lineage BERT inherited
Subword RegularizationKudo2018arXiv:1804.10959Unigram LM segmentation; sampling multiple segmentations as a regularizer
SentencePieceKudo & Richardson2018arXiv:1808.06226Language-independent, lossless tokenization from raw text; the field’s standard tooling
Language Models are Unsupervised Multitask LearnersRadford et al.2019OpenAI reportByte-level BPE with the 50,257-token vocabulary reused across the GPT lineage
ByT5: Towards a token-free futureXue et al.2021arXiv:2105.13626Byte-to-byte models; quantified the spelling-robustness cost of tokenization
Language Model Tokenizers Introduce Unfairness Between LanguagesPetrov et al.2023arXiv:2305.15425Measured up to 15× tokenization-length gaps across languages — cost, latency, and context inequity
Fishing for MagikarpLand & Bartolo2024arXiv:2405.05417Systematic detection of under-trained glitch tokens across model families

PM Lens

Papers referenced