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.
Glossary
Unigram Language Model

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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
| Feature | Unigram LM | BPE | WordPiece |
|---|---|---|---|
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 |
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
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.
Related Terms
Explore the foundational concepts and sibling algorithms that contextualize the Unigram Language Model within modern subword tokenization pipelines.
Subword Tokenization
The broader category of segmentation strategies that the Unigram model belongs to. Instead of treating whole words or single characters as units, subword tokenization breaks text into meaning-bearing fragments like morphemes or frequent character sequences. This balances vocabulary size with the ability to represent rare, misspelled, or unseen words by composing them from known subword units.
Subword Regularization
A training technique that leverages the probabilistic nature of the Unigram model. By stochastically sampling different segmentations for the same input text during training, the model learns to be robust to tokenization variance. This is implemented via BPE-Dropout or by exploiting the multiple segmentation candidates that the Unigram model naturally provides, improving generalization.
Vocabulary Size
A critical hyperparameter directly controlled by the Unigram algorithm's pruning process. The model starts with a massive seed vocabulary and iteratively removes tokens that least impact the corpus likelihood until the target size is reached. This represents a trade-off: a larger vocabulary captures more semantics per token but increases the model's embedding matrix, while a smaller vocabulary forces more compositional encoding.
Lossless Tokenization
A property guaranteed by the Unigram model when implemented within frameworks like SentencePiece. Because the tokenizer operates on a complete probability distribution over all possible segmentations and the vocabulary is derived directly from the corpus, the original input text can be perfectly reconstructed from the token sequence. This eliminates the information loss common in heuristic normalization pipelines.

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.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us