Tokenization
A tokenizer turns text into a sequence of IDs that a model can accept. The choice of vocabulary affects how much text fits into a context window and which units the model learns to predict.
Open this chapter with experiments and audio
How does a word become a row of numbers?
Start with a vocabulary containing three characters. Give a the ID 0, b the ID 1 and n the ID 2. Reading “banana” from left to right now produces [1, 0, 2, 0, 2, 0]. To get the text back, look up the character for each ID and concatenate the results. Repeated occurrences of a character receive the same ID.
The ID is an address. ID 2 does not mean that n is twice as important as b. In the next chapter the model will use that address to retrieve a vector. At this stage the operation is a reproducible encoding of the text.
Which vocabulary will also work tomorrow?
A vocabulary built from a single word cannot encode the new letter s. A production tokenizer uses a vocabulary and rules saved alongside the model. Depending on the tokenizer, unknown pieces can map to a special token or be composed from bytes. Changing the meaning of ID 1 after training would make every occurrence point to the wrong learned vector.
text = "banana"
vocab = sorted(set(text))
encode = {ch: i for i, ch in enumerate(vocab)}
ids = [encode[ch] for ch in text]
print(ids)
print("".join(vocab[i] for i in ids))Why combine frequent pieces?
A character tokenizer gives short vocabularies and long sequences. A whole-word tokenizer often needs a large dictionary. Subword methods choose pieces between these extremes. In a simplified byte-pair encoding example, split “banana” into b a n a n a. The pair a n occurs twice, as does n a. If training chooses a n in this tie, a new token an gives b an an a. Six positions have become four.
Apply the example rules one at a time. Switch the word and check whether the same adjacent pieces are available for the next merge.
For “banana” the first merge can act in two places at once. The second merge sees the new pieces. A different word can skip that second step because its remaining neighbours differ.
Training records merge rules in order of priority. When encoding a new document the tokenizer reuses those rules. It need not relearn them from the document. Leading spaces, capitalization, punctuation and a language’s spelling can affect the segmentation. Counting words is therefore an unreliable way to measure the number of model positions.
Which part starts to learn language?
Given a sequence of IDs, the model can be trained to predict the next one. Two words that get very different IDs may occur in similar contexts. The training objective can then give their vectors useful shared structure. The tokenizer created the units on which that learning operates. The learned representation develops in the network.
Would every tokenizer split “square meters” the same way?
No. Its saved vocabulary, preprocessing and segmentation rules determine the result. Use the tokenizer belonging to the actual checkpoint to count tokens.
What changes when one word takes two positions?
Imagine a tokenizer whose vocabulary contains the pieces thermo and meter. It represents “thermometer” using two positions. A second tokenizer has a single entry for the whole word. Both can reconstruct the same text. The first model must learn how the two pieces combine. The second can look up a dedicated vector using one position.
Ten repetitions cost twenty versus ten content positions in this example, ignoring separators. A longer sequence occupies more of the context window and creates more keys and values to cache. Adding a token also adds a row to the embedding table. Its vector must receive enough training signal to become useful.
What if two tokenizers happen to assign the same ID to different strings?
The checkpoint retrieves the vector for the meaning its own tokenizer assigned during training. Passing IDs from the other tokenizer silently substitutes pieces. A valid integer can still be the wrong input.
Replay the merges and count the remaining pieces →
Sources · Sennrich et al., 2015