Training a tokenizer
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.
To build a tokenizer, take a training corpus, a sample of text: here four words and how often each appears. Split every word into single characters. These 10 characters are the first vocabulary, IDs 0 to 9.
Byte pair encoding (BPE) counts every pair of neighbouring symbols, weighted by how often its word appears. "e s" is in newest (6 times) and widest (3): 9 in total. "s t" also scores 9; the first one found wins.
The winning pair becomes one new symbol, "es", with the next free ID, 10. Every "e s" in the corpus is joined into "es". The merge is written down: the ordered list of merges is what the tokenizer learns.
Count again, merge again. "es t" scores 9, so "est" gets ID 11. Then "l o" (7) becomes "lo", ID 12, and "lo w" (7) becomes "low", ID 13. We stop after 4 merges; GPT-2's tokenizer learned 50,000.
Using it
"lowest" was never in the corpus. Split it into letters, then apply the 4 merges in the order they were learned: es, est, lo, low. Two tokens are left: "low" and "est". An unseen word still splits into known pieces.
Each token is looked up in the vocabulary: "low" is 13 and "est" is 11. So the 6 letters of "lowest" become 2 numbers, [13, 11]. Frequent pieces get their own token; rare words take several.
Inside the model
The model reads a sequence of IDs, up to a fixed length called the context window. GPT-2's was 1,024 tokens. Limits and prices are counted in tokens, not words: English text averages about 4 characters per token.
Inside the model, each ID selects one row of a learned table, the embedding table. The row is a vector, a list of numbers the transformer layers work on. GPT-2 small: 50,257 rows of 768 numbers each.
The model answers one token ID at a time. Decoding runs the tokenizer backwards: look up each ID and join the pieces. If it outputs 13 then 11, the text is "low" + "est" = "lowest".
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.