Tokenization: BPE vs WordPiece vs SentencePiece (and Unigram)
A tokenizer splits text into subwords and converts them to numbers the model understands. For chat models it also holds the dialogue format (chat template).
What is the difference between BPE, WordPiece and SentencePiece?
BPE and WordPiece both build a vocabulary by merging symbol pairs: BPE takes the most frequent pair, WordPiece the pair that occurs most often relative to its parts. SentencePiece is a library: it runs BPE or Unigram directly on raw text and stores the space as ▁, so it also works for languages without spaces.
Hugging Face LLM Course: Practical Transformers
9 practical lessons with Transformers: pipeline(), tokenizers, the Hub, Trainer, LoRA and a Gradio demo on Spaces. Each lesson has a task checked by AI and a short quiz.
The rest of this lesson opens after a free sign-up
Sign up with email or Google: the first two lessons of every course stay open for good, and the whole catalog is open for 7 days. No card needed.
Which algorithm, which models
The four names you meet in the Hugging Face docs, side by side. Three are algorithms; SentencePiece is the library that runs two of them on raw text.
| Name | How it builds tokens | Models (per Hugging Face docs) | What you see in the output |
|---|---|---|---|
| BPE | Starts from characters and merges the most frequent adjacent pair until the vocabulary reaches its target size | Llama, Gemma, Qwen2; GPT (40,478 tokens). GPT-2 uses byte-level BPE: 256 bytes + 50,000 merges + end-of-text = 50,257 | Any text maps to tokens; byte-level BPE needs no <unk> |
| WordPiece | Merges the pair with the highest score: freq(pair) ÷ (freq(a) × freq(b)) | BERT, DistilBERT, Electra | Word-internal pieces start with ##, e.g. play ##ing |
| Unigram | Starts from a large vocabulary and drops the tokens whose removal raises the loss least (10–20% per round); picks the most probable split | T5, BigBird, Pegasus | Can sample several splits of one word during training |
| SentencePiece | A library: applies BPE or Unigram to the raw character stream, with the space as a symbol | Checkpoints that ship a SentencePiece model file (for example spiece.model in T5) | ▁ marks a space; decoding turns ▁ back into spaces |


