Explain the Unigram LM tokenization algorithm and how its training differs fundamentally from BPE. What does stochastic tokenization enable?
Compare the Unigram Language Model tokenizer (used in SentencePiece) to BPE in terms of: (1) training direction (additive vs. subtractive), (2) the optimization objective, and (3) the key capability that Unigram's probabilistic output enables during model training.
BPE merges up from characters by frequency; Unigram LM prunes down from a large vocabulary by corpus likelihood, and its probabilistic output enables stochastic tokenization as training-time augmentation.
Imagine two ways to settle on a useful set of word-pieces. One way starts with single letters and keeps gluing the most popular pair together, like 'th' then 'the'. The other starts with a huge pile of every chunk imaginable and keeps asking, which chunk would I miss the least, then throws it out, until the pile is the right size. The second way, Unigram LM, has a bonus. It remembers that a word can be cut in several valid ways and even how likely each cut is. During training it uses different cuts on different passes, which teaches the model to handle the same word no matter how it is sliced. The first way always cuts the same way every time.
Detailed answer & concept explanation~6 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
5 min: additive vs subtractive + likelihood objective over segmentations + E-step forward-backward + M-step prune + stochastic tokenization + alpha and inference determinism.
import sentencepiece as spm
sp = spm.SentencePieceProcessor()
sp.load("llama_tokenizer.model") # a Unigram model
text = "tokenization"
# Deterministic Viterbi path: same result every call
print("viterbi:", sp.encode(text, out_type=str))
# Stochastic sampling: different valid splits across calls
for _ in range(4):
print("sample:", sp.encode(text, out_type=str,
enable_sampling=True, alpha=0.2, nbest_size=-1))Real products, models, and research that use this idea.
- The Llama-family SentencePiece tokenizers use Unigram, whose probabilistic segmentation supports stochastic tokenization during pretraining.
- Google's T5 and mT5 use SentencePiece Unigram, where likelihood-based pruning balances vocabulary coverage across many languages.
- For agglutinative languages like Finnish and Turkish, sampling different splits of a shared root builds representations robust to unseen derived forms, an edge over deterministic BPE.
What an interviewer would ask next. Try answering before peeking at the approach.
QDerive the M-step probability update for a token given expected counts from the E-step.
QHow does the per-iteration pruning fraction affect final vocabulary quality, say 50% versus 10%?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Reversing the directions. BPE is additive and merges up; Unigram LM is subtractive and prunes down. They reach the same vocabulary size from opposite ends.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.