Inferensys

Glossary

Unigram Language Model

A probabilistic subword tokenization method that initializes a large seed vocabulary and iteratively prunes tokens whose removal minimally impacts the overall likelihood of the training corpus, optimizing for the most efficient encoding.
ML engineer managing model training cluster on laptop, GPU utilization visible, technical deep learning setup.
TOKENIZATION STRATEGY

What is a Unigram Language Model?

A probabilistic subword tokenization method that starts with a large vocabulary and iteratively removes tokens that least increase the overall likelihood of the training corpus.

A Unigram Language Model is a subword tokenization algorithm that assumes each token occurs independently and selects an optimal vocabulary by maximizing the probability of the training data. Unlike Byte-Pair Encoding (BPE), which builds a vocabulary from the bottom up by merging frequent pairs, the Unigram model starts with a large seed vocabulary and prunes it iteratively, removing tokens whose elimination causes the smallest decrease in the corpus likelihood.

At each pruning step, the algorithm computes the loss over the corpus for every token, retaining only those that are essential for high-probability segmentations. This process is often coupled with subword regularization, where the tokenizer samples different segmentations probabilistically during training to improve model robustness. The Unigram approach is the foundation of the SentencePiece library and is used in models like T5 and mBART, offering a principled, likelihood-driven alternative to frequency-based tokenization.

Probabilistic Subword Segmentation

Key Characteristics of Unigram Tokenization

The Unigram Language Model tokenization algorithm takes a fundamentally different approach from BPE and WordPiece. Instead of building a vocabulary from the bottom up by merging characters, it starts with a large seed vocabulary and iteratively prunes tokens that contribute the least to the overall likelihood of the training corpus.

01

Top-Down Pruning Strategy

Unlike BPE's bottom-up merge operations, Unigram begins with a heuristic-based large vocabulary—often all possible substrings or characters—and then applies a lossy pruning process. During each iteration, the algorithm calculates the loss in corpus likelihood if a specific token were removed. Tokens with the lowest impact on the overall probability are discarded. This continues until the target vocabulary size is reached, ensuring only the most statistically useful subword units survive.

02

Probabilistic Segmentation via the Viterbi Algorithm

A core innovation of Unigram is that it does not enforce a deterministic segmentation during training. Given a trained model, a word can have multiple valid segmentations, each with an associated probability. The most likely segmentation is found using the Viterbi algorithm, which efficiently computes the path through the token lattice that maximizes the product of token probabilities. This probabilistic foundation directly enables subword regularization.

03

Loss Function: Maximizing Log-Likelihood

The optimization objective is to maximize the log-likelihood of the training corpus. For a given vocabulary, the probability of a sentence is the product of the probabilities of its constituent tokens. The pruning step estimates the marginal loss for each token by calculating how much the overall log-likelihood would decrease if that token were removed and its occurrences were replaced by the next-best segmentation. Tokens with the smallest marginal loss are pruned.

04

Subword Regularization via Stochastic Sampling

Because Unigram models the probability distribution over all possible segmentations, it can be used for subword regularization during training. Instead of always using the Viterbi-best segmentation, tokens can be stochastically sampled from the distribution. This exposes the model to multiple tokenization variants of the same word, acting as a form of data augmentation that improves robustness to spelling variations and rare morphological forms.

05

Implementation in SentencePiece

The Unigram algorithm is natively supported in the SentencePiece library, which treats the input as a raw Unicode stream without requiring language-specific pre-tokenization. SentencePiece's implementation pairs Unigram with a lossless encoding scheme by escaping whitespace into a meta-character, ensuring the original text can be perfectly reconstructed from the token sequence. This makes it a popular choice for multilingual models.

06

Comparison to BPE and WordPiece

Unigram differs fundamentally from frequency-based methods:

  • BPE: Merges the most frequent pair of tokens iteratively.
  • WordPiece: Merges the pair that maximizes training data likelihood.
  • Unigram: Starts with all tokens and prunes the least useful ones. This top-down approach often yields a more statistically principled vocabulary that better captures rare morphological patterns, though it can be computationally more expensive to train than BPE.
SUBWORD TOKENIZATION ALGORITHMS

Unigram vs. BPE vs. WordPiece

A technical comparison of the three dominant subword tokenization strategies, contrasting their training objectives, segmentation behavior, and practical trade-offs.

FeatureUnigram LMBPEWordPiece

Training Objective

Maximize corpus likelihood via token removal

Maximize frequency of adjacent symbol pairs

Maximize corpus likelihood via greedy merging

Direction

Pruning (top-down)

Merging (bottom-up)

Merging (bottom-up)

Initial State

Large over-segmented vocabulary

Base character vocabulary

Base character vocabulary

Selection Criterion

Remove token that least reduces P(corpus)

Merge most frequent adjacent pair

Merge pair with highest likelihood gain

Probabilistic Segmentation

Subword Regularization Support

Lossless Decoding

Typical Vocabulary Size

8,000–32,000

30,000–50,000

30,000–110,000

UNIGRAM LANGUAGE MODEL

Frequently Asked Questions

Explore the mechanics of the Unigram Language Model, a probabilistic subword tokenization algorithm that starts with a large vocabulary and iteratively prunes it to find the optimal set of tokens for a given corpus.

A Unigram Language Model is a probabilistic subword tokenization algorithm that assumes each token in a sequence is generated independently of its neighbors. Unlike Byte-Pair Encoding (BPE), which builds a vocabulary from the bottom up by merging frequent pairs, the Unigram model starts with a massive initial vocabulary—often including all possible substrings—and iteratively removes tokens. At each step, it calculates the overall likelihood of the training corpus given the current vocabulary and removes a percentage of tokens (typically 10-20%) whose removal causes the least decrease in this likelihood. This top-down pruning continues until the desired vocabulary size is reached, resulting in a vocabulary optimized for the probabilistic model.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.