1pairs = count_pairs(words)2best = max(pairs, key=pairs.get)3words = merge_all(words, best)4merges.append(best)0–910111213×5llooloww×2lloolowwe×6rnewe×3essestwtideessesttlloolowweessestt123…1,024low+est=lowest01…111213…GPT-2: 50,257 × 7681311
Khaki tiles: symbols of the corpus; neighbours fuse into one tile at each mergeBlue cells: the vocabulary, token and ID · teal block: the embedding tableGold chips: token IDs, the only thing the model seesOn the left, a tokenizer is trained with byte pair encoding: the training loop on the Python card, and the corpus of four words split into letter tiles, each with its count. Each pass fuses the most frequent neighbouring pair into one tile and adds it to the vocabulary at the back right, so IDs 10 to 13 fill in: es, est, lo, low. On the right, the new word lowest is split by the same merges into low and est, which become the IDs 13 and 11; they enter the context window, pick their rows of the embedding table, and are decoded back into text in front. Real tokenizers such as GPT-2's start from the 256 possible bytes, not letters, so any text, emoji included, can be tokenized. A space is part of the token that follows it.

Training a tokenizer

  1. A language model does arithmetic, so it cannot read letters. Before any text goes in, a tokenizer cuts it into pieces called tokens and gives each piece a number, its ID. The model only ever sees those IDs.

Using it

Inside the model

What happens

;; step 1/10 · why
text  ->  tokenizer  ->  tokens  ->  IDs  ->  model

token  a piece of text: a word, part of a word,
       a letter or a punctuation mark
ID     the number the tokenizer gives each token

Real tokenizers such as GPT-2's start from the 256 possible bytes, not letters, so any text, emoji included, can be tokenized. A space is part of the token that follows it. The model's answer in the last step is an illustration of decoding, not the output of a real model.