BPE TRAINING / THE MERGE LOOP
START FROM BYTES · MERGE UPWARD
1
Count pairs
every adjacent token pair
2
Merge the top pair
most frequent → one new token
3
Assign next id
starts at 256, then +1…
↻ REPEAT → |vocab| = target

LEARNED MERGES

( t · h )th256
( th · e )the257
( the · ␣ )the␣258
Rules apply in this order at encode time.
Equal-frequency pairs break by a fixed order — training is deterministic.
Startraw bytes
th e the then
12
tokens
+256merge t·h
the the then
9
tokens
+257merge th·e
the the then
6
tokens
+258merge the·␣
the␣258 the␣258 the257 n
4
tokens
THE PAYOFF Frequent sequences collapse to one token · rare words fall back to subwords · nothing is ever UNK. Same loop, 50,000 merges over real text = GPT-2's vocabulary.
FIG. 2 — 3 MERGES: 12 → 4 TOKENS, VOCAB +3 · GPT-2 DOES THIS 50,000×
SOURCE: STANFORD CS336 L1 · DIAGRAM: @SUSHANT_P18