Rúben GalvãoThe Rabbit Hole

The Rabbit Hole / AI and machine learning / Language models / Inside a language model

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 comes out of a language modelThe text "The rabbit eats the" goes into a language model. Out comes a vote: carrot 62 per cent, bread 12, grass 8, cake 5, the 1, and the rest of the vocabulary shares the remaining 12 per cent.The output: a vote on the next pieceThe rabbit eats the?language modela probability for every piece of textcarrot62%bread12%grass8%cake5%the1%…all the rest share 12%
What to notice: the model does not write a sentence. For the text so far, it gives every piece of text it knows a probability of coming next, and one is chosen. The probabilities here are made up, to show the shape of the output.

What should go in?

The digit reader was handed numbers: a grid of 28 × 28 pixel values. A sentence is not numbers.

What should go into a language model?Top: the digit reader. A picture of a 7, as a 28 by 28 grid of pixel values, goes into the network, which ends in ten neurons, one for each digit. Bottom: a language model. The text "The rabbit eats the" goes through a question mark before it reaches the network, because nobody has yet said how to turn text into numbers. The network ends in one neuron for each token.The digit reader28 × 28 pixel valuesnetwork0123456789one neuron per digitA language modelThe rabbiteats the?networktheharerabbit⋮zooone neuron per token
What to notice: Pixels already arrive as numbers. Text does not. Before the network can process a sentence, we need a numerical representation.

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.

Three ways to cut text into stepsThe text "the hare is unfriendly" cut into 22 letters, into 4 whole words, and into 6 subword tokens: the, hare, is, un, friend, ly.Letters: 22 stepsthe·hare·is·unfriendlyWhole words: 4 stepsthehareisunfriendlySubword tokens: 6 stepsthehareisunfriendly
What to notice: Compare sequence length with vocabulary size. Letters need many steps; whole words need many entries. These splits illustrate the trade-off, not a real tokeniser.

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?

Join the most common pair, again and again

Interactive
Tokens in the text Vocabulary

The outlined pair is joined next. _ marks a space.

Neighbouring pairs, counted

New tokens so far

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 vocabulary, its ids, and the output neuronsA list of tokens from the vocabulary, each with its id: 0 is a space, 1 is a, 2 is d, 11 is th, 12 is the, 13 is a space and the, 17 is a space and hare, 22 is a space and on. Each row is joined to the output neuron with the same number.VocabularyOutput neurons0_01a12d2⋮⋮⋮11th1112the1213_the1317_hare17⋮⋮⋮22_on22same number, same token
What to notice: The same vocabulary index identifies an input token and its output score. The list shown is shortened and illustrative.

The same ids can stand for text on the way in. Cut the text into tokens, and replace each token by its id.

From text to idsThe text "the hare sat on" is cut into four tokens: the, space-hare, space-sat, space-on. Each token is looked up in the vocabulary and replaced by its id: 12, 17, 20, 22.the hare sat oncut intotokensthe12_hare17_sat20_on22look upeach id
What to notice: Look up each token in the vocabulary. This example turns “the hare sat on” into IDs 12, 17, 20 and 22.

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.

The trade-off in the size of the vocabularyLeft, a small vocabulary: a small table, and the sentence "the hare sat" cut into eight tokens. Good: small embedding table, few output neurons, every token seen often. Bad: more tokens to process, and a token like _h carries little meaning. Right, a big vocabulary: a big table, and the same sentence cut into three tokens. Good: fewer tokens to process, and a token like _hare can carry meaning. Bad: big embedding table, many output neurons, rare tokens seen rarely.Small vocabularyembedding table“the hare sat” as tokensthe_hare_sat+small embedding table+few output neurons+every token seen often−more tokens to process−“_h” carries little meaningBig vocabularyembedding table“the hare sat” as tokensthe_hare_sat+fewer tokens to process+“_hare” can carry meaning−big embedding table−many output neurons−rare tokens seen rarely
What to notice: Same sentence, different cuts. Compare the number of vocabulary entries with the number of tokens needed to write the sentence.

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

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.

← Browse all subjectsBack to the beginning ↑