Topic 1 of 6 · 5 min read
Byte-pair merge algorithm step by step
Goal: Run three BPE merges by hand on a short corpus and encode a new word with the learned merge list.
Byte-pair encoding (BPE) builds a vocabulary by repeatedly merging the most frequent pair of neighbouring symbols into one new symbol, and the ordered list of merges is the tokenizer. Nothing else is learned.
Check yourself · 1 of 3
In one BPE training step, what exactly gets merged?
Why it matters
A model cannot read text, only integer ids. If every character is its own id, sequences get long and the model spends attention on spelling. If every word is its own id, the vocabulary explodes and rare words are unknown. BPE sits between the two: common strings become single tokens, rare strings fall back to smaller pieces. Every training and inference bug you meet later in this skill starts here.
The algorithm
- Split the training text into words and count each word. Write every word as a sequence of single symbols (bytes in real systems, letters here).
- Count every adjacent pair of symbols across the corpus, weighted by word count.
- Pick the most frequent pair. Break ties the same way every time, for example by first occurrence.
- Replace that pair with one new symbol everywhere, and append the merge to the merge list.
- Repeat until the vocabulary reaches the target size. Final size is the starting symbols plus one entry per merge.
To encode new text, split it into starting symbols and apply the merges in the order they were learned, not by frequency or length.
Worked example
Corpus: low x5, lower x2, newest x6, widest x3. Counting pairs, "e s" appears 9 times (6 in newest, 3 in widest), as does "s t". With a first-occurrence tie-break, merge 1 is e+s into "es". Now "es t" appears 9 times: merge 2 is es+t into "est". After that, "l o" and "o w" each appear 7 times and nothing else beats 7, so merge 3 is l+o into "lo".
Encode the unseen word "lowest": start with l o w e s t. Merge 1 gives l o w es t. Merge 2 gives l o w est. Merge 3 gives lo w est. The result is three tokens even though "lowest" never appeared in training.
Before and after
Weak: "BPE picks the longest common substrings." Wrong: it never looks at length, only at pair counts, and one merge adds exactly one new symbol. Fixed: "BPE merges the most frequent adjacent pair, one pair per step, and replays those merges in order at encode time."
What goes wrong
- Encoding by longest match. Real BPE replays merges by rank. A greedy longest-match encoder gives different tokens from the trained one.
- Unstable tie-breaks. If ties resolve differently between runs, two trainings on the same data give different tokenizers.
- Counting characters instead of words. Pair counts must be weighted by how often each word occurs, or rare words distort the merges.
- Forgetting the order matters. Shuffle the merge list and the same text encodes differently.
Examples
Three merges on a tiny corpus
Corpus: low x5, lower x2, newest x6, widest x3.
| Step | Most frequent pair | Count | New symbol |
|---|---|---|---|
| 1 | e + s | 9 | es |
| 2 | es + t | 9 | est |
| 3 | l + o | 7 | lo |
Encoding "lowest" replays the three merges and gives: lo, w, est.
Same word, different merge order
Take merges (l,o), (o,w), then (lo,w). Encoding "low" gives l o w, then lo w, then low. Reverse the list so (lo,w) comes first and it never fires, because "lo" does not exist yet: the result is lo w. Order changes the tokens, so a saved tokenizer must store merges as an ordered list.
Key points
- BPE trains by merging the most frequent adjacent pair into a new symbol, one pair per step.
- The tokenizer is the ordered merge list plus the starting symbols; the order is part of it.
- Encoding replays the merges in the order they were learned, not longest match first.
- Final vocabulary size equals the starting symbols plus the number of merges.
- Pair counts are weighted by word frequency, and ties need a fixed rule so training is repeatable.
Common mistakes
- Encoding new text by longest-match against the vocabulary instead of replaying merges in rank order.
- Treating the merge list as an unordered set when saving or loading a tokenizer.
- Counting pairs once per distinct word and ignoring how often each word occurs.
- Leaving tie-breaking undefined, so two training runs give different vocabularies.
Check yourself · 2 of 3
A tokenizer has three learned merges: (e,s), then (es,t), then (l,o). How is the new word "lowest" encoded?
Check yourself · 3 of 3
Why does the order of the merge list matter when encoding?
Practice items, not counted towards your mastery.
Start here
- Byte-Pair Encoding tokenization (Hugging Face LLM course) (opens in a new tab)CourseFree
Walks through BPE training and encoding step by step on a tiny corpus, including the byte-level trick that removes unknown tokens.
- Andrej Karpathy: minbpe and the GPT tokenizer lecture (opens in a new tab)ToolFree
Short, readable Python for training, encoding and decoding BPE; the best code to step through next to the merge algorithm.
- Hugging Face LLM Course: The Tokenizers library (opens in a new tab)CourseFree
Hands-on chapters on training a tokenizer from an old one and on the BPE, WordPiece and Unigram algorithms.
More for this topic
- Neural Machine Translation of Rare Words with Subword Units (opens in a new tab)PaperFree
The paper that brought byte-pair merging to neural models, with the small worked merge example.