4.2.3 · D1Tokenization & Language Modeling

Foundations — WordPiece and SentencePiece

3,292 words15 min readBack to topic

This page assumes you have seen the parent note's symbols flash by — , , , PMI, ##, , and the training loop called Expectation–Maximization — and felt lost. Here we build every one of them from the ground up, in an order where each brick rests on the one before it. Nothing is used before it is drawn.


1. A "token" and a "vocabulary"

Picture it. Think of a box of alphabet-stamps. Each stamp is a token; the whole box is the vocabulary. To print any sentence you must build it out of stamps you actually own — you can't invent a stamp mid-sentence. Figure s01 draws this box beside the two extremes we reject.

Figure — WordPiece and SentencePiece
Figure s01 — the subword box (centre) has a small, fixed size yet can still spell unbreakable; the word-level box on the upper right is infinite, the char-level box on the lower right is tiny but forces very long print jobs.

Why the topic needs it. Word-level stamps mean an infinite box (every rare word needs its own stamp). Character-level stamps mean a tiny box but very long print jobs. Subword stamps are the compromise — that compromise is the topic.

The symbol (two vertical bars around ) just means "how many things are in the set " — the count of stamps in the box. Bars-around-a-thing always means "size of".


2. Splitting a word: the segmentation

Picture it. Take the word dogs. You could cut it as dog | s, or do | g | s, or leave it whole as dogs. Each set of scissor-cuts is a different segmentation. The word is a strip of paper; a segmentation is a choice of where to snip. Figure s02 lines up four different cuts of the very same word.

Figure — WordPiece and SentencePiece
Figure s02 — four valid segmentations of dogs, from all-characters (top) to the whole word (bottom). Every row rebuilds the identical strip of paper; the algorithms differ only in which row they prefer.

Why the topic needs it. Every algorithm here is secretly answering "of all the ways to cut this word, which cut do we prefer?". WordPiece answers greedily (one hard cut); SentencePiece-Unigram answers softly (a probability over all cuts, using the training loop of §9). You cannot understand either without first seeing that many cuts exist.


3. Probability of a token:

Picture it. Imagine a spinner (a pie chart). Each token gets a slice; the slice's area is . Frequent tokens get fat slices, rare tokens get thin slivers. Spin once — the slice you land on is the token you "drew".

Why the topic needs it. Both algorithms score a cut by multiplying the probabilities of its pieces. So we need a per-piece probability first. The little is just a placeholder name for "some token" — like using a blank to write "".


4. Probability of a whole segmentation: the product

The tall symbol (capital Greek "Pi") means "multiply all of these together", exactly as (capital Sigma) means "add all of these together". So

Here is how many pieces this cut has, and is the -th piece. The little under is a counter that walks .

Picture it. Spin the spinner once per piece. The chance of getting exactly this sequence of pieces is each slice-area multiplied together (independent events multiply). More pieces → more multiplications by small numbers → smaller product. This is why long, over-chopped cuts score badly.

Why the topic needs it. This single product is the scoring rule inside SentencePiece-Unigram's E-step (parent's Worked Example 3: dog×s = 0.40×0.20 = 0.08). "Independent" is doing real work here — it is exactly what lets us multiply, and exactly what "no context" means in the parent note.


5. Why we take

Picture it. Plot : it climbs steeply near and flattens for large inputs. A probability of becomes — a comfortable negative number instead of a vanishing decimal. Because probabilities are , their logs are always ; a bigger (closer to zero) log-likelihood means a more probable corpus. Figure s03 plots this squashing.

So the product from §4 becomes the sum the parent writes:

The script letter is just a name — "the log-likelihood", a single number scoring how well our vocabulary + probabilities explain the whole corpus. is the total number of token-slots in the corpus.

Figure — WordPiece and SentencePiece
Figure s03 — the log curve. Three marked points show how probabilities as small as (pink dot) become the tame value ; every log sits at or below the horizontal line because .

Why the topic needs it. "Maximize the likelihood" is the shared goal of both algorithms. Every merge or prune is judged by how much it moves . Logs make that arithmetic possible and additive.


6. A change in likelihood:

Picture it. is your height on a hill. Take one step (merge a pair, prune a token). is how much higher or lower you now stand. WordPiece always takes the step that climbs most.

Why the topic needs it. WordPiece's merge rule is literally "pick the pair with the biggest ". A positive = a good merge; negative = a bad one. This is also how Unigram decides which tokens to prune (remove the ones whose loss hurts least).


7. From to PMI

First two pieces of counting notation we are about to use:

Merging pair into one token replaces with everywhere that pair occurs — that is, times. Subtracting, and using :

The fraction inside the log is exactly Pointwise Mutual Information (PMI).

Picture it. is the "if they were strangers" expectation; is what really happens. PMI is the surprise that they show up as a couple.

Why the topic needs it. This is the exact reason the parent stresses WordPiece is not frequency-based. A pair like the+cat can be frequent yet low-PMI (both words are common on their own, so meeting is no surprise). Raw-frequency Byte-Pair Encoding (BPE) would merge them; PMI-weighted WordPiece won't.


8. The markers ## and

Picture it. ## is a left-facing hook linking a fragment back onto its neighbour. is a visible ghost of a space that rides along inside a token (▁w = "space then w").

Why the topic needs it. These markers are how each system recovers word boundaries. WordPiece needs whitespace pre-split, so it marks interior pieces with ##. SentencePiece refuses to assume spaces exist (Chinese, Thai, Japanese don't use them), so it encodes every space as and stays perfectly reversible — vital for T5 and mT5. This directly addresses the Out-of-Vocabulary Problem and mirrors how humans read morphemes.


9. The training loop: Expectation–Maximization (EM)

Let a word have possible segmentations (the set from §2). We need two new symbols.

Now the two steps, each one equation:

Why the topic needs it. SentencePiece-Unigram is this loop plus pruning. See Expectation Maximization for the general machinery.


10. Prerequisite map

Token and Vocabulary size

Segmentation many cuts

P of x per token probability

Product over pieces unigram

Log turns product into sum

Likelihood script L

Change in likelihood Delta L

PMI meaningful pairs

EM soft counts and pruning

Markers hashhash and underblock

WordPiece and SentencePiece topic

Two tributaries feed the topic: the likelihood chain (token → probability → product → log → → PMI) and the bookkeeping markers. PMI (§7) is what WordPiece maximizes; EM soft counts and pruning (§9) are what SentencePiece-Unigram runs. Both need Tokenization Fundamentals and both attack the Out-of-Vocabulary Problem.


Equipment checklist

Cover the right side and answer aloud before moving to the parent note.

What does mean, in words?
The number of tokens (stamps) in the vocabulary — its size.
How is the vocabulary seeded before any merge/prune?
All single characters (+ special tokens for WordPiece; a large over-complete substring set for Unigram) so every word is spellable.
What is a segmentation of a word?
One valid way of cutting the word into tokens that glue back to the original.
What is and what do all the sum to?
The probability of drawing token ; they all sum to .
How is the very first estimated?
Relative frequency: count of divided by the grand total of all token counts.
Why can't we let ?
Because is undefined; we keep all characters and smooth tiny probabilities so every .
What does tell you to do, and why is it valid here?
Multiply the pieces' probabilities; valid because the unigram assumption makes pieces independent.
Why do we take of the probabilities?
It turns a tiny fragile product into a stable sum via .
What is a token-slot, and what is ?
One filled position in the segmented corpus; is the total number of token-occurrences (with repeats), not .
What single quantity does measure?
The total log-likelihood — how well the whole vocabulary + probabilities explain the corpus.
What is and how do you get ?
The tally of immediately followed by ; divide it by , the same denominator used for and .
What does represent?
The change in the score caused by one merge or prune.
In one sentence, what does PMI capture?
How much more (or less) two pieces co-occur than they would by pure chance.
Why is high PMI, not high frequency, WordPiece's merge criterion?
A frequent pair can be frequent only because both pieces are common; PMI rewards genuine togetherness (meaningful units).
What is the responsibility ?
The probability that word was cut as segmentation : its product-score divided by , the sum of all cuts' scores.
What is a soft count ?
The expected (usually fractional) number of times appears — sum of from every cut containing , weighted by word frequency.
What are the E-step and M-step formulas?
E-step computes and accumulates ; M-step sets .
What do ## and each signal?
## = "no space before me, I attach left"; = "there was a space before me" (kept for exact reversibility).

Ready? If every reveal above felt easy, jump back to WordPiece and SentencePiece and the symbols will read like sentences.