A byte pair encoder, first by hand and then in code. About sixty lines of Python are the whole of the understanding, and the awkward cases are where the understanding shows. You will also measure fertility across scripts and match four tokenisation symptoms to the downstream failures they cause.
Allow eight to ten hours. Chapter 5 covers everything the project needs.
Python and nothing else for the encoder. For the fertility stage, the
tiktoken library and a few snippets of text in different
scripts, such as English, Hindi, Tamil and source code.
These are the conventions of Chapter 5. State them to yourself before you start, because two correct implementations that differ on one of them will disagree.
A word is its characters followed by the end-of-word marker
</w>.
At each step count all adjacent pairs, weighted by word frequency, and merge the most frequent.
Tie break. If two pairs have equal counts, merge the lexicographically smallest tuple .
A merge is written as the two symbols separated by one space, as
in "e s".
A pair that occurs twice in a word counts twice, weighted by that word’s frequency.
Overlapping occurrences are consumed left to right. In
a a a one merge pass joins the first two and leaves the
third alone. Walk the symbols with an index that advances by two on a
match and by one otherwise.
If the corpus runs out of pairs before you have the merges you asked for, return fewer. Do not pad the list.
Hand simulation. Choose a mini corpus of six words with frequencies. Compute the first eight merges by hand, writing the pair counts and the ties at every step, before you write any code.
Check. Then do the same by hand on the corpus of Chapter 5, low 5,
lower 2, newest 6, widest 3. The first five
merges are e s, es t,
est </w>, l o,
lo w.
Implement the core. Write
bpe_train(word_frequencies, num_merges), which returns the
merges in learning order, and bpe_encode(word, merges),
which applies them in order and returns the tokens. Use no external
library.
Check. Your encoder should turn lowest into
low and est</w>, lowers into
low, e, r, s,
</w>, and nest into n and
est</w>. For the awkward cases, train on the single
word aaa with frequency
.
The merges are a a, a </w>,
aa a</w>. Work out by hand why the second is
a </w> and not aa a, and why training
stops after three merges.
Fertility across scripts. With the GPT-2 tokenizer compute the fertility, tokens per word, of an English snippet, a Hindi snippet, a Tamil snippet and a code snippet. Predict the order first. Then say which of four causes best explains the worst script: training data, word length, grammar or font.
Failure modes. Four tokenisation symptoms follow. Match each to the downstream failure it best illustrates, using each of arithmetic, character counting, fertility cost and code whitespace once.
a number split into inconsistent pieces,
a word whose letters cannot be counted reliably,
a language that costs several times as much per sentence,
a block of code whose indentation changes its meaning.
Break your own implementation on purpose by removing the tie break, and show on the corpus above which merges change.
Add the byte-level base vocabulary of Chapter 5 and show that no string is out of vocabulary.
Train on a corpus of your choice at several merge counts and plot fertility on held-out text against the count.
From Natural Language Processing: From Corpus Statistics to Grounded LLMs, by Ramaseshan Ramachandran.