Put the BPE training steps in the correct order, from raw corpus to final vocabulary.
- 1Initialize vocabulary with individual characters (or bytes) present in the corpus
- 2Repeat counting and merging until the vocabulary reaches the target size
- 3Select the most frequent adjacent pair and add it as a new merged symbol
- 4Count bigram frequencies across all words in the current vocabulary representation
- 5Replace all occurrences of the selected pair in the corpus with the new merged symbol
BPE starts from single characters or bytes, then loops: count adjacent pairs, merge the most frequent, update the corpus, repeat until the target vocabulary size.
Imagine inventing a shorthand for a thick book. You begin with only the alphabet. Then you scan the whole book and find the two letters that sit next to each other most often, maybe 't' and 'h'. You invent one new symbol for 'th' and rewrite every 'th' in the book with it. Now you scan again. The counts have changed, because 'th' is one symbol now, so a new most-common pair wins next, maybe 'th' plus 'e'. You keep gluing the most frequent neighbors and rewriting the book after each glue, until your shorthand has as many symbols as you wanted. That step by step gluing, always recounting after each merge, is exactly how BPE builds a tokenizer.
Detailed answer & concept explanation~3 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
2 min: byte/char init + count, select, replace loop + why the corpus rewrite matters + recount on updated stream + incremental-count optimization.
Real products, models, and research that use this idea.
- GPT-2 onward and OpenAI's cl100k_base initialize from the 256 bytes, so byte-level BPE never emits an unknown token.
- HuggingFace tokenizers' BPE trainer maintains incremental pair counts to avoid rescanning the whole corpus after each merge.
- SentencePiece can train BPE directly on raw text, folding the merge loop into one pipeline used by Llama-family tokenizers.
What an interviewer would ask next. Try answering before peeking at the approach.
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Forgetting that the corpus is rewritten after every merge. Counts must be recomputed on the updated representation, not on the original characters.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.