WordPiece is a subword tokenization algorithm that selects merge rules by maximizing the likelihood of a language model on the training corpus. Unlike Byte-Pair Encoding (BPE), which merges the most frequent adjacent token pair, WordPiece evaluates potential merges by calculating which pair most increases the training data's probability. This likelihood-based criterion, introduced by Schuster and Nakajima for Japanese and Korean voice search, was later adopted by Google for the BERT model.
Glossary
WordPiece

What is WordPiece?
WordPiece is a data-driven subword tokenization algorithm that builds a vocabulary by iteratively merging characters or subword units based on maximizing the likelihood of the training data, rather than raw frequency.
The algorithm begins with a base vocabulary of individual characters and iteratively adds new tokens formed by merging existing ones. For each candidate merge, WordPiece computes the likelihood gain—essentially, how much better the model predicts the corpus if the pair is treated as a single unit. The pair with the highest score is added to the vocabulary. This approach efficiently handles out-of-vocabulary (OOV) words by decomposing them into known subword units, balancing vocabulary size with the ability to represent rare and unseen terms.
Key Characteristics of WordPiece
WordPiece is a data-driven subword tokenization algorithm that builds its vocabulary by selecting merges that maximize the likelihood of the training data, rather than simply merging the most frequent pairs.
Likelihood-Based Merge Criterion
Unlike BPE which merges based on raw frequency, WordPiece selects the pair of symbols whose merge maximizes the training data likelihood. It computes a score for each potential merge: score = count(pair) / (count(first) * count(second)). The pair with the highest score is merged, prioritizing combinations where the individual symbols are rare but their co-occurrence is frequent. This statistically-driven approach produces a vocabulary that better captures meaningful linguistic units.
BERT's Native Tokenizer
WordPiece was developed specifically for BERT (Bidirectional Encoder Representations from Transformers) and remains the default tokenizer for BERT-family models. The original BERT vocabulary consists of approximately 30,000 tokens. A distinctive feature is the use of the ## prefix to denote subword continuations—for example, 'playing' might be tokenized as ['play', '##ing']. This prefix convention allows the model to distinguish between word-initial and word-internal occurrences of the same character sequence.
Greedy Longest-Match-First Encoding
During tokenization, WordPiece applies a greedy longest-match-first algorithm. Starting from the beginning of a word, it iteratively finds the longest substring present in the vocabulary. If no match is found for the entire remaining substring, it falls back to unknown token handling. This deterministic encoding ensures that every input text is segmented consistently, producing the same token sequence for identical inputs across different runs.
Special Token Architecture
WordPiece vocabularies reserve several special tokens with fixed semantic functions:
- [CLS]: Prepended to every sequence; its final hidden state is used for classification tasks
- [SEP]: Separates sentence pairs and marks sequence boundaries
- [MASK]: Used during pre-training for masked language modeling
- [PAD]: Ensures uniform sequence length within batches
- [UNK]: Represents characters or sequences not found in the vocabulary
Training Corpus Dependence
The quality of a WordPiece vocabulary is directly tied to the representativeness of the training corpus. The algorithm learns merge rules that reflect the statistical properties of the text it was trained on. A vocabulary trained on general-domain text will perform poorly on highly specialized domains like biomedical literature or legal documents. This necessitates domain-specific vocabulary training for optimal performance in specialized applications.
Comparison with BPE
While both are subword algorithms, key differences exist:
- Merge Criterion: BPE uses raw frequency; WordPiece uses a likelihood ratio
- Encoding: BPE applies merge rules sequentially; WordPiece uses longest-match-first
- Continuation Markers: WordPiece uses
##prefixes; BPE typically uses@@or</w>suffixes - Vocabulary Construction: BPE builds bottom-up from characters; WordPiece starts with all characters and iteratively merges WordPiece generally produces more linguistically motivated segmentations.
WordPiece vs. Byte-Pair Encoding (BPE)
A technical comparison of the two dominant subword tokenization algorithms, highlighting their distinct merge strategies, training objectives, and practical implications for modern language models.
| Feature | WordPiece | Byte-Pair Encoding (BPE) | Unigram LM |
|---|---|---|---|
Merge Selection Criterion | Maximizes training data likelihood | Maximizes raw co-occurrence frequency | Maximizes training data likelihood |
Training Direction | Bottom-up (starts with characters) | Bottom-up (starts with characters) | Top-down (starts with large vocabulary) |
Scoring Metric | Mutual information gain | Pair frequency count | Token survival probability |
Handles Unknown Characters | |||
Lossless Decoding | |||
Primary Model Adoption | BERT, DistilBERT | GPT series, RoBERTa | XLNet, ALBERT, T5 |
Subword Regularization Support | |||
Vocabulary Size Typical Range | 30,000-32,000 | 30,000-50,000 | 32,000-64,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 and rationale behind the subword tokenization algorithm that powers BERT and other encoder-only transformer models.
WordPiece is a data-driven subword tokenization algorithm that iteratively builds a vocabulary by selecting merges that maximize the likelihood of the training data. Unlike Byte-Pair Encoding (BPE), which merges the most frequent adjacent symbol pair, WordPiece evaluates potential merges using a probabilistic language model. It calculates the likelihood gain of adding a new token to the vocabulary and selects the merge that most increases the probability of the training corpus. This greedy, likelihood-based optimization ensures that the resulting vocabulary efficiently represents common words as single tokens while decomposing rare and morphologically complex words into meaningful subword units like stems and suffixes. The algorithm begins with a base vocabulary of individual characters and iteratively adds new tokens until a predefined vocabulary size is reached, typically 30,000 tokens for models like BERT.
Related Terms
Explore the algorithms and concepts that form the foundation of modern subword tokenization, directly related to the WordPiece methodology used in BERT.
Vocabulary Size
A critical hyperparameter defining the total number of tokens a model recognizes. WordPiece vocabularies for BERT typically range from 30,000 to 120,000 tokens, balancing encoding efficiency against model embedding parameters.
- Trade-off: Larger vocabularies reduce sequence length but increase embedding matrix size
- BERT-base: 30,522 tokens
- Impact: Directly affects memory footprint and inference speed
Special Tokens
Reserved vocabulary entries with specific control functions essential to WordPiece-based models like BERT. These include [CLS] for sequence classification, [SEP] for sentence separation, and [MASK] for masked language modeling.
- [CLS]: Aggregates sequence-level representation
- [SEP]: Delimits sentence boundaries in input pairs
- [MASK]: Placeholder for masked token prediction during pre-training
- [UNK]: Represents unknown tokens (rarely used with subword tokenization)
Subword Regularization
A technique that stochastically samples different token segmentations during training to improve model robustness. While WordPiece uses a deterministic longest-match-first segmentation, applying BPE-Dropout or Unigram sampling can expose models to tokenization variance.
- Purpose: Reduces sensitivity to segmentation ambiguity
- Method: Randomly drops merges during training
- Benefit: Improved generalization to rare and misspelled words

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