Tokenisation and BPE: cutting text into tokens
Byte-pair encoding (BPE) turns text into reusable token pieces. How should a language model split words before calculating with them?
What comes out
The digit reader ended with ten output neurons, one for each digit, and the strongest one was its prediction. A language model ends the same way, with far more neurons: one for every piece of text it knows. Given what came before, which piece comes next?
What should go in?
The digit reader was handed numbers: a grid of 28 × 28 pixel values. A sentence is not numbers.
So before anything else, text has to become numbers.
Cut the text into pieces
There are two obvious ways to cut text: one letter at a time, or one word at a time. Both go wrong.
Each piece is called a token. Subword tokens let common pieces have their own entries while unfamiliar words can be assembled from smaller pieces. A token can be a word, part of a word, punctuation, or bytes; its boundaries need not match the words we see.
Let the text choose the tokens
Press Join the top pair below. It finds the most frequent neighbouring pair and joins it into a new token.
Repeat. Which pieces become single tokens first?
Within a few joins, frequent pieces such as “the” can become single tokens, while rarer pieces remain split. The vocabulary contains the starting symbols, the new merged symbols and any special tokens, so the number of merges is only one part of its final size.
This is byte-pair encoding (BPE). Real tokenisers learn merges from much larger collections of text, with their own preprocessing rules and special tokens.
Byte-level versions can represent any UTF-8 text, including unfamiliar words and emoji. The demonstration starts with letters to make the merges easy to follow.
Give every token a number
The tokens we learned are the model’s vocabulary. Number them: a token’s position in the list is its id. That list does a second job. The output layer has one neuron for every token, so neuron 12 is the one that predicts “the”.
The same ids can stand for text on the way in. Cut the text into tokens, and replace each token by its id.
How big should the vocabulary be?
We chose when to stop joining, so we chose the size of the vocabulary. It is a trade-off. Here is the same sentence in a small vocabulary and in a big one.
There is a quieter cost too. Small pieces occur in many different settings, while larger pieces can capture more specific patterns. Neither choice gives the model knowledge by itself: the network still has to learn how those pieces are used.
Are ids enough?
Any text can now be turned into a list of ids. Can we feed them into the network? Not quite: an id says nothing about what a token means, and a network will read meaning into any number it is given. So what should we give it instead?
Sources and assumptions
- Sennrich, Haddow and Birch: subword units for neural translation (2015/2016), for the learned merge procedure.
- GPT-2 report (2019), for byte-level BPE.
The small vocabulary, merge sequence and token IDs in the figures are teaching examples, not the vocabulary of a released model. Byte-level BPE can represent arbitrary UTF-8 text; preprocessing, special tokens and exact segmentation vary by tokeniser.
BPE originated as a compression method in 1994 and was adapted to neural text models by Sennrich and colleagues in 2015. GPT-2 popularised byte-level BPE for this model family.