ModelRefs / SentencePiece — AI Glossary
SentencePiece — AI Glossary
A language-agnostic subword tokenizer supporting BPE and unigram LM algorithms, used in T5, LLaMA, and Gemma. Also called SPM.
Overview
SentencePiece (Kudo & Richardson, Google 2018) tokenizes directly from raw text without pre-tokenization, making it language-agnostic. It supports both BPE and unigram language model algorithms. Used in T5, LLaMA 1/2/3, Gemma, and multilingual models. Packaged as a standalone C++ library with Python bindings.
Reference details
| Topic | architecture |
|---|---|
| Also known as | SPM |
| Last reviewed | 2026-06-24 |
Related terms
How it works
- Training operates on raw text with no pre-tokenisation step. Whitespace is encoded as a visible meta symbol, so the tokenizer learns word boundaries rather than being handed them — which is what makes it work on languages that do not separate words with spaces.
- Two segmentation algorithms are offered. BPE merges the most frequent adjacent pair repeatedly until the vocabulary target is reached. The unigram language model starts from a large candidate set and prunes the pieces that cost the least likelihood, which gives it a probabilistic segmentation lattice rather than a fixed merge order.
- The vocabulary size is a hyperparameter fixed before training and frozen into the model, because the embedding matrix and output head are sized by it.
- Encoding maps text to piece ids deterministically, and decoding is exactly lossless: the meta symbol for whitespace reconstructs the original string, including spacing, without a detokenisation heuristic.
- Byte fallback handles characters absent from the vocabulary by emitting their raw bytes as pieces, which removes the unknown-token class entirely at the cost of long encodings for rare scripts.
- The trained model is a single self-contained file holding the vocabulary and the segmentation parameters, which is why it ships with the model weights and must be versioned alongside them.
Example: Why a vocabulary tuned for one language taxes another
A tokenizer trained predominantly on English learns long pieces for common English words, so a 100-word English paragraph of roughly 600 characters might encode to about 130 tokens — around 4.6 characters per token. The same meaning in a language poorly represented in the training mixture may fall back to short pieces or raw bytes, encoding to 400 or more tokens for similar content — closer to 1.5 characters per token. The ratio matters in three places at once: cost, since billing is per token, so the same request costs 400 / 130 ≈ 3.1x more; context, since a 128,000-token window holds about three times less text; and latency, since generation time scales with tokens emitted. This is the concrete reason multilingual models train the tokenizer on a balanced mixture rather than adding language data later — the vocabulary is frozen at training time, so a tokenizer tax cannot be fixed by fine-tuning.
Commonly confused with
SentencePiece is a tokenizer trainer and runtime, not a tokenisation algorithm — it implements BPE and unigram, so 'SentencePiece versus BPE' is a category error. The more consequential confusion is with tiktoken: both tokenise, but tiktoken is a fast byte-level BPE runtime shipped with a fixed set of published vocabularies, while SentencePiece trains a vocabulary for your corpus and produces a portable model file. Counting tokens for one model's pricing with another model's tokenizer produces a number that is wrong by tens of percent, and the direction of the error depends on the language.
When to use it
Reach for it when:
- Training a tokenizer for a new model, particularly a multilingual one, where the balanced-mixture decision is yours to make.
- Languages without whitespace word boundaries, where pre-tokenisation assumptions break.
- Domain-specific corpora — code, chemistry, clinical text — where a general vocabulary fragments the terms that matter most.
- Any pipeline needing exactly reversible encoding, since decoding reconstructs the original string including whitespace.
Reach for something else when:
- Counting tokens for a hosted model that uses a different tokenizer. Use the tokenizer that model ships with, or the count is wrong.
- As a drop-in replacement for the tokenizer a pretrained model was trained with. The embedding matrix is indexed by piece id; a different vocabulary makes the weights meaningless.
- Expecting a vocabulary change to be adoptable by fine-tuning. Vocabulary size is frozen into the architecture.
- When a published vocabulary already matches your domain and language mixture, where training your own adds a versioned artefact for no measured gain.
Continue your research
Use these connected ModelRefs sections to compare alternatives, inspect implementation paths, and review the evidence and governance boundaries relevant to SentencePiece — AI Glossary.
Frequently asked questions
What is SentencePiece?
A language-agnostic subword tokenizer supporting BPE and unigram LM algorithms, used in T5, LLaMA, and Gemma.
Is SentencePiece the same as SPM?
Yes — SPM are common aliases for SentencePiece.
What concepts are related to SentencePiece?
Closely related concepts include byte pair encoding, tiktoken, tokenizer.