Byte-pair encoding

14 merges·409 → 409 tokensAll algorithms

AlgoLab runs no language model. Every merge here is counted from eight sentences written for this page, which you can read in full in the Corpus tab. A real tokeniser learns its merges from billions of words.

the·reader·sees·the·text·before·the·tool·sees·the·tokensthere·are·other·ways·to·split·a·sentence·into·piecesthen·the·thing·that·happens·is·that·the·pieces·get·longereach·merge·is·another·rule·in·the·tablethe·theory·is·thin·but·the·practice·is·thoroughthese·three·sentences·share·the·same·short·patternsanother·reader·in·another·room·reads·the·other·halfthe·point·of·the·exercise·is·that·the·pieces·are·learned12345678
Whole corpus

Every sentence starts as single characters: 409 tokens across 8 sentences.

Every sentence starts as single characters: 409 tokens across 8 sentences.
  1. 1Split every sentence into single characters.
  2. 2Do the following once for each merge you want to learn.
  3. 3Count every pair of neighbouring tokens, one sentence at a time.
  4. 4Take the pair that occurs most often, keeping the earlier one if two tie.
  5. 5Replace every occurrence of that pair with one new token, and write the rule down.
  6. 6The rules, in the order they were learned, are the tokeniser.

Pairs counted so far

The tally is built while the sentences are counted.
PairCount
—
—
—
—
—
—
—
—
Pairs counted
0
Merges learned
0
Pairs replaced
0

Complexity

BestO(m · n)
AverageO(m · n)
WorstO(m · n)
Extra spaceO(n + m)

Byte-pair encoding

AlgoLab runs no language model. Every merge here is counted from eight sentences written for this page, which you can read in full in the Corpus tab. A real tokeniser learns its merges from billions of words.

Byte-pair encoding turns text into a vocabulary it was not given. It starts with every sentence split into single characters, counts every pair of neighbouring tokens, replaces every occurrence of the most frequent pair with one new token, and writes that substitution down as a rule. Repeat, and the tokens grow from characters into the chunks that actually recur in the text. The list of rules, in the order they were learned, is the tokeniser.

When it is the right choice

It is the standard way a language model gets its vocabulary, and that is the honest reason to understand it: every prompt anyone writes is turned into tokens by a table built exactly like this one, which is why models count letters badly and why an unusual word costs more than a common one. Outside that, it is a real general-purpose compressor for text with repeated substructure, and it is the wrong choice when the units are already known — a fixed dictionary is faster and more predictable than a learned one, and a learned vocabulary carries whatever biases its corpus had.

How it works

Worth noticing

Where the text comes from

The tool counts pairs in eight sentences that a person wrote for this page, held attools/bpe/src/data/corpus.v1.json and shown in full in the tool’s Corpus tab. They are 409 characters altogether, all lower case, and nothing in them is quoted. The lower case is a decision rather than an oversight: byte-pair encoding treatsT and t as different symbols, so capitals would spend the early merges on case variants and teach nothing.

The sentences were picked so that the first twenty merges are ones a reader recognises —th, the·, er·, other·,read — and two of those are four generations deep. That is not how a real tokeniser’s training corpus is chosen. Those are enormous and are not curated sentence by sentence, and the merges they produce are mostly not legible. Four hundred characters can be counted by hand and checked against the tool; billions cannot. The reason this page can promise its arithmetic is verifiable is the same reason it cannot promise to show you a real tokeniser.

A space is drawn as · throughout, because an empty cell in a row of characters reads as a missing character rather than as the space it is, and after a mergethe· would otherwise be indistinguishable from the. They are different tokens.

Cost

Time
O(m · n)
Extra space
O(n + m)
Most merges
24

m is the number of merges to learn and n the number of tokens in the corpus, which shrinks as the run proceeds. The ceiling is twenty-four merges, and it is not a cost limit — the whole run generates in under a millisecond. It is the point at which the algorithm stops making a frequency decision: the winning pair occurs 30 times at merge 1, 3 times at merge 24, twice by merge 30 and once from merge 48 on. Past that, “the most frequent pair” is settled entirely by the tie-break, and the tool would be teaching its own tie-break rather than the algorithm.

Worth comparing

All algorithms