BPE’s merge criterion — raw pair frequency — is simple but not the only reasonable one.
Two related algorithms modify the criterion, one still bottom-up like BPE and one working top-down instead.
WordPiece. WordPiece was introduced for a Japanese and Korean speech system by Schuster and Nakajima (2012) and later adopted, with the same core algorithm, as the tokenizer for BERT (Devlin et al., 2019). Its training procedure has the same bottom-up merge structure as BPE — start from a character alphabet, repeatedly merge a pair of adjacent symbols, record the merge, stop at a target vocabulary size — but it replaces BPE’s arg-max-frequency criterion with an arg-max-likelihood criterion. The motivation is a specific weakness of raw frequency: a pair can be extremely frequent purely because and are each individually extremely frequent symbols, even if their co-occurrence carries little extra information beyond what their separate frequencies already predict under an independence assumption. Consider a simple unigram model over the current symbol vocabulary in which each symbol has estimated probability , with the total symbol count in the corpus at that stage. Under an assumption that adjacent symbols are drawn independently, the “surprise” of observing the specific pair next to each other, relative to what independence would predict, is the log-likelihood ratio
Merging throughout the corpus increases the training data’s log-likelihood under the (very crude, but tractable) unigram model by an amount that grows with this ratio, weighted by how often the pair actually occurs. WordPiece’s published training heuristic drops the occurrence-count weighting and the constant , and ranks merge candidates purely by the ratio itself,
merging, at each step, the pair with the highest score rather than the highest raw count. The practical consequence is a different, and often better, ordering of merges: a moderately frequent pair whose two constituents are individually rare (so their co-occurrence is far more predictable than chance would suggest — high mutual information) can outrank an extremely frequent pair whose constituents are individually extremely frequent letters or common short words (whose co-occurrence, while common in absolute terms, is closer to what independence would already predict, hence lower mutual information). BPE, driven by raw frequency, will greedily grab the highest-count pair regardless of this distinction; WordPiece’s likelihood-based score is a targeted correction for exactly the cases where raw frequency and “informative merge” diverge. At encoding time, WordPiece applies the learned vocabulary via a longest-match-first greedy segmentation of each word (rather than replaying merge rules in learned order, as BPE does), conventionally marking non-initial subword pieces with a continuation prefix (## in BERT’s implementation) so that, for example, “tokenization” decodes unambiguously as “token” followed by a continuation “##ization” rather than being confusable with the two independent words “token” and “ization.”
Unigram language model tokenization. The Unigram LM algorithm, introduced by Kudo (2018) and packaged together with a BPE implementation in the language-independent SentencePiece toolkit (Kudo and Richardson, 2018), inverts the entire strategy: instead of building a vocabulary bottom-up by merging, it starts from a large candidate vocabulary and prunes it down. Concretely, seed a large candidate vocabulary (in practice, often all substrings up to some length that occur with reasonable frequency, or the output of a preliminary BPE-like pass run to a large intermediate size) — large enough, crucially, to already contain the full base character alphabet as a floor, exactly as in BPE.
The Unigram LM defines a probabilistic segmentation model directly. For a string and a candidate segmentation into pieces drawn from the current vocabulary, assume the pieces are generated independently under a unigram distribution with per-piece probabilities , so that the segmentation’s probability is , and the string’s total probability marginalizes over every segmentation consistent with it:
where is the (generally large) set of all ways to partition into pieces that are members of the current vocabulary. Given a training corpus of strings, the parameters are fit by maximizing total corpus log-likelihood, , via the expectation-maximization (EM) algorithm: the E-step computes (or, for efficiency, approximates with the single best segmentation found by a Viterbi-style dynamic program) the expected count of each piece across all segmentations weighted by their probability under the current parameters, and the M-step re-estimates each as its expected relative frequency, exactly analogous to fitting the maximum-likelihood n-gram parameters discussed in the companion book, but with the added complication that the segmentation itself — not just the piece identities — is latent.
Once is fit for the current vocabulary , pruning proceeds by asking, for each piece other than the base characters, how much total corpus log-likelihood would be lost if were removed from the vocabulary and every string currently using it in its best segmentation had to fall back to its next-best segmentation without . This quantity is a direct measure of how much unique modeling value contributes beyond what the rest of the vocabulary can already express. The pieces contributing the least are removed — typically the bottom 10–20% by this criterion in one round, not merely the single worst piece, for computational efficiency — and the EM fit is repeated on the smaller vocabulary. This build-fit-prune cycle repeats until reaches the target size, with the base character alphabet always protected from removal so that, exactly as with BPE and WordPiece, no string is ever unrepresentable.
At encoding time, the Unigram model tokenizes a new string by finding the segmentation that maximizes (equivalently minimizes ) via the same Viterbi-style dynamic program used during EM training — a well-defined, globally optimal segmentation under the fitted model, in contrast to BPE and WordPiece’s greedy, locally-applied merge rules. Kudo and Richardson (2018) package both the BPE and Unigram LM trainers into a single library, SentencePiece, whose second major contribution is orthogonal to the choice of algorithm: it treats the input as a raw, un-pre-tokenized Unicode stream rather than assuming whitespace-delimited words, replacing every space with an explicit meta-symbol (conventionally rendered as a low, underscore-like block, Unicode U+2581) so that whitespace becomes an ordinary vocabulary symbol like any other. This makes both tokenization and detokenization fully lossless and reversible — the original string, including its exact whitespace, can always be reconstructed by concatenating token surface forms — and makes the same trainer directly applicable to languages such as Japanese, Chinese, and Thai that do not delimit words with whitespace at all, without any language-specific pre-tokenizer.
The Unigram model has one further advantage BPE and WordPiece lack structurally: because it defines a proper probability distribution over segmentations rather than a single deterministic greedy rule, it supports subword regularization — sampling from the (approximate) posterior over segmentations during training, so the same word is occasionally tokenized differently across training examples, exposing the model to alternative valid segmentations of the same text as a form of data augmentation. This is the specific technique Kudo (2018) introduces the Unigram LM algorithm to enable; production tokenizers used purely for deterministic inference typically disable sampling and always take the Viterbi-optimal segmentation, but the capability remains a structural feature that BPE’s rule-replay and WordPiece’s greedy longest-match do not offer without separate modification.
Continue with the book
Explore the full treatment, worked examples, and exercises in LLMs Derived.
Companion article for LLMs Derived, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.
