Unigram tokenization is a subword segmentation method that treats every token in a vocabulary as an independent unit with its own probability and splits text into the most probable sequence of tokens under that model. In language models, it converts raw text into the integer IDs a network can process, and it is the default algorithm of the SentencePiece library used by models such as T5, ALBERT, XLNet and mBART.
The method was introduced by Taku Kudo in 2018 together with subword regularization, a training technique that samples alternative segmentations of the same sentence. Unlike byte-pair encoding, which builds a vocabulary by merging pairs upward, the unigram language model tokenizer starts with a very large candidate vocabulary and prunes it downward until it reaches the target size.
How unigram tokenization segments text
A unigram tokenizer segments a word by searching all possible ways to cover it with vocabulary pieces and selecting the segmentation with the highest total probability. Because tokens are assumed independent, the probability of a segmentation is simply the product of the token probabilities, which in practice is computed as a sum of log probabilities to avoid numerical underflow.
The search uses the Viterbi algorithm, a dynamic programming procedure that walks through the string character by character and keeps, for each position, the best-scoring segmentation that ends there. Its cost grows roughly with the length of the input times the maximum token length, so encoding remains fast even with vocabularies of 32,000 to 256,000 entries.
The table below traces how the model scores competing segmentations of one word, using illustrative log probabilities.
| Candidate segmentation | Tokens | Sum of log probabilities | Result |
|---|---|---|---|
| un + happiness | 2 | -9.1 | Selected |
| un + happi + ness | 3 | -13.4 | Rejected |
| unhapp + iness | 2 | -15.8 | Rejected |
| u + n + happiness | 3 | -16.2 | Rejected |
Longer, frequent pieces usually win because each extra token adds another negative log probability. This gives unigram tokenization a natural preference for fewer, meaningful tokens, although the final choice always depends on the learned probabilities rather than on a fixed rule.
Training the vocabulary by pruning
Training a unigram vocabulary is a top-down pruning process driven by the expectation-maximization (EM) algorithm. The trainer first builds an oversized seed vocabulary, often several times the target size, from frequent substrings of the corpus, and then repeatedly removes the pieces that contribute least to the corpus likelihood.
- Build a seed vocabulary from frequent substrings, typically found with a suffix array.
- Run the EM algorithm to estimate each token probability from expected counts over all segmentations.
- Compute the loss in likelihood that would follow if each token were removed.
- Drop the lowest-impact tokens, commonly the bottom 20 percent per iteration, while always keeping single characters.
- Repeat until the vocabulary reaches the target size.
Keeping every single character in the vocabulary guarantees that any input can be segmented, which avoids unknown tokens for covered scripts. The main training settings and their effects are summarized below.
| Parameter | Typical value | What it affects |
|---|---|---|
| Vocabulary size | 8,000 to 256,000 | Sequence length and embedding size |
| Seed vocabulary size | About 1,000,000 pieces | Coverage of candidate substrings |
| Shrinking factor | 0.75 | Speed and stability of pruning |
| Character coverage | 0.9995 to 1.0 | Handling of rare characters |
| EM sub-iterations | 2 per round | Precision of probability estimates |
Unigram compared with other subword algorithms
Unigram tokenization differs from byte-pair encoding (BPE) and WordPiece mainly in how the vocabulary is built and how text is encoded. BPE and WordPiece apply deterministic, greedy rules, while the unigram model holds a full probability distribution over segmentations, which allows it to score and sample alternatives.
| Property | Unigram | BPE | WordPiece |
|---|---|---|---|
| Vocabulary construction | Prunes a large seed set | Merges frequent pairs | Merges by likelihood gain |
| Encoding | Viterbi, most probable path | Applies learned merges | Greedy longest match |
| Multiple segmentations | Yes, with probabilities | Only with BPE-dropout | No |
| Typical library | SentencePiece | tiktoken, Hugging Face | Hugging Face |
| Example models | T5, ALBERT, mBART | GPT family, Llama | BERT, DistilBERT |
A frequent misconception is that SentencePiece and unigram are the same thing. SentencePiece is a library that supports both unigram and BPE; it also treats input as a raw character stream and marks spaces with a visible meta symbol, which makes it language-independent, including for scripts without whitespace such as Japanese or Thai.
Subword regularization and sampling
The probabilistic design makes subword regularization possible: during training, the tokenizer samples a different segmentation of the same text on each pass instead of always using the single best one. The model therefore sees words split in several plausible ways, which improves robustness to spelling variation, noise and rare words.
Sampling is controlled by two settings. The sampling size limits how many top candidates are considered, and the smoothing parameter alpha flattens or sharpens the distribution; values around 0.1 to 0.3 are common in machine translation experiments. At inference time, sampling is switched off and the deterministic Viterbi segmentation is used so that outputs remain reproducible.
Choosing and configuring a unigram tokenizer
Unigram tokenization is a strong choice when a model must handle many languages or unsegmented scripts, or when robustness gained from regularization matters more than raw encoding speed. The decision table below reflects common engineering practice.
| Situation | Recommended choice | Reason |
|---|---|---|
| Multilingual corpus with varied scripts | Unigram via SentencePiece | No pre-tokenization by spaces needed |
| Noisy user text or small data | Unigram with regularization | Sampling improves generalization |
| Compatibility with a GPT-style model | Byte-level BPE | Must match the pretrained vocabulary |
| Strict byte coverage required | Unigram with byte fallback | Avoids unknown tokens for rare characters |
The most important rule is that a tokenizer cannot be swapped after pretraining. The embedding matrix and output layer are tied to specific token IDs, so changing the algorithm or vocabulary requires retraining or careful vocabulary adaptation.
Limitations and common pitfalls
- The independence assumption ignores context, so segmentation of a word never depends on its neighbors.
- Training is slower and more memory-intensive than BPE because EM runs over many candidate segmentations.
- Low character coverage can produce unknown tokens unless byte fallback is enabled.
- Vocabularies trained on one domain may fragment specialized terms such as chemical names or code identifiers.
- Uneven segmentation across languages raises token costs for underrepresented languages.
These issues are usually managed by training on a representative corpus, enabling byte fallback, and checking average tokens per word for each target language before the vocabulary is frozen.
The short version
A computer cannot read words directly. It needs text cut into small pieces called tokens. Unigram tokenization decides where to cut. It keeps a list of known pieces, and each piece has a score for how common it is.
For every word, it tries the possible ways to cut it. Then it keeps the cut with the best total score. Common words stay whole. Rare words break into familiar parts, much like reading an unknown word by spotting its prefix and root. The list itself is made by starting big and throwing out the least useful pieces.
Where it makes a difference
A team building a translation model for 20 languages, including Japanese, Hindi and Swahili, needs one shared vocabulary. Splitting text by spaces fails for Japanese, which has no spaces between words. The engineers train a unigram tokenizer with SentencePiece on a balanced multilingual corpus, with a vocabulary of 64,000 pieces, character coverage of 0.9995 and byte fallback enabled.
During training, they enable subword regularization with alpha set to 0.1, so the model sees slightly different splits of the same sentences. When they evaluate on informal social media text with typos, the regularized model loses noticeably less translation quality than a baseline trained with a single fixed segmentation. The team also checks tokens per word in each language and enlarges the Swahili share of the corpus to reduce fragmentation before freezing the vocabulary.