Notes

Byte-Pair Encoding (BPE)

A tokeniser seems forced to choose between a vocabulary small enough to model and one large enough to cover the words people actually write. Byte-pair encoding refuses the choice: it learns a fixed set of word fragments that can be reassembled into anything.

The merge loop
Start with every word split into single characters. Count all adjacent pairs across the corpus, merge the most frequent pair into a new symbol, add it to the vocabulary, and repeat until the vocabulary reaches the size you asked for. Frequent words get merged all the way into one token; rare ones stop part-way, so tokenisation may arrive as token plus isation and an invented product name as five pieces. The ordered merge list is the model — encoding new text means replaying those merges in the order they were learned.

Why it matters
Sennrich, Haddow and Birch carried the idea over from data compression into neural machine translation, where rare and morphologically rich words were a standing failure point, and it retired the UNK token for a whole generation of models. Shared fragments also give the model something real to work with: happiness and unhappiness overlap in a way a word-level vocabulary cannot express. Vocabulary size becomes a genuine hyperparameter, trading sequence length against the size of the embedding table.

What it does not do
The merges are chosen by frequency, not by linguistics, so splits routinely cut across real morpheme boundaries. Pre-tokenisation and whitespace conventions are part of the scheme in practice, and differences there make two BPE vocabularies mutually unintelligible. Nothing in plain BPE guarantees a character outside its alphabet can be encoded at all — the gap that byte-level variants close.

Byte-pair encoding builds a subword vocabulary by starting from single characters and repeatedly merging the most frequent adjacent pair until a target vocabulary size is reached. Frequent words end up as single tokens while rare ones decompose into fragments, so an open vocabulary is covered without an UNK token and related word forms share pieces. Merges are chosen by frequency rather than linguistics, so the resulting splits need not align with real morphemes.

Think about how you cope with a word you have never seen before — unfriendliness, say. You do not give up. You spot un, friend and liness inside it and work the meaning out from the parts.

Byte-pair encoding teaches software the same habit. Before training begins, it reads a large pile of text and works out which letter groups turn up together most often, building itself a dictionary of useful fragments. Common words stay whole; unusual ones get broken into pieces. That way the system is never stumped by a word it has not met — it simply spells it out of smaller parts it already knows.