Inferensys

Glossary

WordPiece

A subword tokenization algorithm, developed for the BERT model, that selects merge rules based on maximizing the likelihood of the training data rather than raw frequency.
Data scientist building training data pipeline on laptop, data preprocessing visible, technical workspace.
Subword Tokenization Algorithm

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.

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.

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.

Algorithm Mechanics

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.

01

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.

02

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.

03

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.

04

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
05

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.

06

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.
SUBWORD TOKENIZATION COMPARISON

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.

FeatureWordPieceByte-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

WordPiece Tokenization

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.

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.