Medium6 min readUpdated 2026-08-12

Explain WordPiece and SentencePiece.

WordPiece and SentencePiece explained: how subword tokenizers build vocabularies, split words, and affect LLM inputs. Learn the core algorithms, key differences, and practical tradeoffs for training and inference.

Hand-drawn diagram comparing WordPiece and SentencePiece tokenization flow with labeled boxes and arrows.
TL;DR
  • WordPiece and SentencePiece are subword tokenizers used to split text into manageable tokens for LLMs.
  • WordPiece builds a compact vocabulary by greedily adding subwords to improve likelihood; SentencePiece offers two modes: BPE merges and a probabilistic unigram model and runs without pretokenization.
  • Differences matter for multilingual text, unknown bytes, and whether you want deterministic greedy splits or a probabilistic segmentation. Key tradeoffs: vocabulary size versus OOV handling, deterministic splits versus probabilistic flexibility, and training complexity versus inference speed.

In this question, we will learn the practical and theoretical differences between WordPiece and SentencePiece so you can explain why a model uses one or the other and how they affect tokenization and training.

We will cover the following:

  • The direct answer and short summary
  • The intuition that makes subword tokenization sensible
  • How each algorithm actually works with a concrete example
  • Practical differences, deployment implications, and a comparison table
  • Tradeoffs and failure modes

WordPiece and SentencePiece are both subword tokenization methods that reduce out of vocabulary issues and control vocabulary size; WordPiece uses a greedy likelihood-based expansion on words with explicit continuation markers, while SentencePiece can train either BPE merges or a probabilistic unigram model and tokenizes raw text without pretokenization. In practice, WordPiece is common in BERT-style pipelines and SentencePiece is flexible for multilingual and byte-level needs.

The intuition (an analogy that makes it click)

Think of building a toolbox for words. If you start with only single letters, many long words are expensive to represent. Subword tokenizers pick useful pieces to store in the toolbox so you can assemble words cheaply. WordPiece picks pieces by testing which additions best explain your training set in a greedy way. SentencePiece either repeatedly merges frequent pairs like a craftsman combining parts, or keeps a large set and prunes unlikely pieces based on a probabilistic model. Both reduce the need to store every full word and both let models generalize to rare or new words.

How it actually works

We will show a compact worked example with the word "playing" and a tiny toy corpus: "I am playing" and "She was playing". Start with characters and space markers.

WordPiece (conceptual steps)

  1. Initialize vocabulary with characters and a special unknown token.
  2. Consider candidate merges or subwords and pick the one that most increases corpus likelihood under a simple model.
  3. Repeat until you reach desired vocabulary size.

SentencePiece has two modes. The BPE-like mode merges frequent adjacent symbol pairs greedily. The Unigram mode starts with a large candidate vocabulary and removes tokens that reduce the marginal likelihood the least. The Unigram model optimizes the sum of log marginal probabilities over training sentences. If we denote a sentence as ss and a segmentation as zz with tokens wtw_t, the marginal sentence probability is

p(s)=ztp(wt)p(s)=\sum_{z}\prod_{t}p(w_t)

Training maximizes the sum of logp(s)\log p(s) over the corpus by pruning or updating p(w)p(w).

Worked example tokens for "playing":

AlgorithmExample tokenizationNotes
WordPieceplay + ##inguses "##" to mark continuation; vocabulary includes "play", "ing"
SentencePiece BPE_play + ingmay use leading underscore to mark word start; result similar to BPE merges
SentencePiece Unigram_pla + y + ing (probabilistic)segmentation chosen by scoring with token probabilities

Here _play\_play denotes a token that includes a leading space marker. WordPiece typically relies on white-space-tokenized words first then subword split while SentencePiece treats raw text uniformly.

Practical differences and deployment implications

Vocabulary training

  • WordPiece: often trained on pretokenized words. It greedily adds subwords by increasing a likelihood or frequency objective.
  • SentencePiece: trains on raw text. It can use BPE merges or the Unigram language model. Unigram is probabilistic and can produce alternate segmentations.

Markers and byte handling

  • WordPiece commonly uses continuation markers like "##" to show a subword is not a word start.
  • SentencePiece often represents spaces as an explicit symbol like leading underscore, or uses byte-level encoding so any input is representable without unknown tokens.

Determinism and sampling

  • WordPiece tokenization is deterministic given the vocabulary and greedy longest-match rules.
  • SentencePiece Unigram can provide probabilistic sampling of segmentations which can help data augmentation during pretraining.

Performance

  • Inference speed is similar when using greedy longest-match tokenization. Unigram sampling adds overhead if used at runtime.
  • Training complexity: Unigram optimization is heavier than greedy merges but produces a compact vocabulary with a principled likelihood objective.

Tradeoffs and failure modes

  • If you need robust byte-level handling for many scripts or noisy text, SentencePiece byte-level mode is safer.
  • If you prefer simple deterministic splits and reuse of existing white-space tokenization, WordPiece is straightforward.
  • Probabilistic segmentation helps cover unseen morphological variants but can complicate deterministic decoding.
If you choose a subword algorithm without matching it to your data, you may get poor coverage or very long token sequences. For example, training on monolingual clean text and then applying to social media or code will increase out of vocabulary stress and hurt model performance.

Questions the interviewer might ask

Some follow-up questions you might get:

How does Byte-Pair Encoding differ from WordPiece? BPE is a greedy pair-merging algorithm that merges the most frequent adjacent symbol pairs. WordPiece is similar conceptually but the original WordPiece uses a likelihood-based or frequency-informed greedy expansion tailored to words and often uses continuation markers.

Why use a leading underscore in SentencePiece? The leading underscore encodes a space or word boundary so tokenization preserves where words start. That helps models learn when a token is a word-initial piece versus a continuation piece.

What is the Unigram model objective in SentencePiece? It maximizes corpus log-likelihood by marginalizing over possible segmentations: sum over sentences of logp(s)\log p(s) with p(s)=ztp(wt)p(s)=\sum_z \prod_t p(w_t). This probabilistic view allows pruning less useful tokens.

When does probabilistic segmentation help? During pretraining, sampling alternative segmentations can act as data augmentation and help models generalize to morphological variation. At inference, deterministic splits are typical for stable behavior.

How do these choices affect model size and performance? Smaller vocabularies reduce embedding parameters but increase sequence lengths. Larger vocabularies save sequence length but increase embedding table size. Choose a balance based on compute, memory, and language morphology.

Some things to note:

  • Vocabulary markers differ across toolkits; make sure token-to-text conversion matches training.
  • Byte-level handling reduces unknowns but can increase token count for languages with long multi-byte characters.

What the interviewer is really testing

They want to see that you understand subword tokenization tradeoffs: algorithmic differences, how vocabularies are trained, and the impact on model inputs and performance. Showing familiarity with Unigram marginal likelihood, BPE greedy merging, and practical issues like markers and byte handling demonstrates both theoretical and deployment-ready knowledge.

Related questions

#tokenization#subword-units#nlp#llm

How would you rate the quality of this article?

Prepare for your AI engineering interview

This is one of many detailed questions and explainers on StudyAIDesign. Browse the full set, work through the curriculum, and walk into your interview ready.

Follow along for new questions and explainers:Instagram