BPE learned 50,000 merge rules during training. What is a merge rule, and where does it live at inference?
A merge rule is one entry in BPE's ordered learned list: 't' + 'h' = 'th'. Encoders replay merges greedily in learned order. Storage: merges.txt, tokenizer.json, or the tiktoken binary.
Imagine a cookbook where every recipe just says 'combine X with Y to make Z'. The cookbook has hundreds of recipes in a specific order. You start with a kitchen full of plain ingredients. To cook anything, you flip through the recipes in order and every time you see an ingredient pair you have in the kitchen, you combine them as the recipe says. Then you keep flipping. By the end, your kitchen has a few big assembled dishes instead of many small ingredients. BPE merges work the same way. The training process produced an ordered cookbook of pair-combine rules. The encoder reads the cookbook in order and combines every applicable pair in the input. Different tokenizer libraries print the cookbook in different file formats, but the cookbook itself is the same kind of object.
Detailed answer & concept explanation~7 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.
3 min: define a merge rule as one entry in BPE's ordered list, walk through how encoding replays the list greedily, explain why the order matters, and list the storage formats in three or four major tokenizer libraries.
Real products, models, and research that use this idea.
- GPT-2's original tokenizer ships with merges.txt and vocab.json files in the model directory; you can open merges.txt and read the BPE training history line by line.
- Llama 3's tokenizer ships as tokenizer.json in the model repository on Hugging Face; the merges are nested inside the model.merges field of the JSON.
- tiktoken's cl100k_base and o200k_base are binary encoder files included in the tiktoken package; you cannot inspect the merges directly without reverse-engineering the binary.
- SentencePiece-BPE models for Mistral ship as tokenizer.model files (the SentencePiece protobuf); the merges and vocabulary live in the same file.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy is the order of the merge list critical and what would happen if you shuffled it?
QHow does BPE encoding scale with merge-list length?
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.
Thinking BPE encoding picks the highest-scoring merge at each step at inference time. Encoding is not re-running BPE training; it is replaying the learned merges in their training order, greedily, until no more apply.
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.