Word-level tokenization
The simplest tokenizer splits text at spaces and punctuation and gives every distinct word an ID. Any word that was not in the training text maps to a single unknown token, usually written<unk>. The model cannot tell one unknown word from another.
Code
Code
<unk> although “runs” is in the vocabulary. A word-level vocabulary does not record that the words run, runs and running share a stem, so each form needs its own entry and its own training examples. Covering a real corpus this way takes hundreds of thousands of entries, and new names, misspellings and technical terms still fall outside it.
Character-level tokenization
At the other extreme, every character is a token. The vocabulary is small and nothing is ever unknown. The price is sequence length.Code
Code
Byte pair encoding
Byte pair encoding (BPE) was a text compression method before Sennrich et al. adapted it to build subword vocabularies for machine translation (Sennrich et al., 2015). Training starts with every word split into characters, plus an end-of-word marker, which is written_ below. Then it repeats two steps:
- Count every pair of adjacent symbols across the corpus, weighted by how often each word occurs.
- Merge the most frequent pair into a new symbol, add it to the vocabulary and record the merge.
Code
Code
s h, h e, s e and e _ all occur 3 times, and the tie is broken by the order in which the pairs were first seen. Once s and h merge into sh, the pair h e survives only in “the”, so its count drops from 3 to 1 and it never wins. Counting the pairs once on the starting corpus and merging them in that order gives a different and incorrect sequence. The whole word she_ appears only at merge 9.
After ten merges, the corpus words look like this:
Code
Code
Code
Code
Byte-level BPE in GPT-2
A character-level starting vocabulary has a gap: a character that never appeared in training, such as an emoji or a letter from another alphabet, still has no ID. GPT-2 closes it by running BPE over the UTF-8 bytes of the text instead of its characters (Radford et al., 2019). The starting vocabulary is the 256 possible byte values, so every string can be encoded. On top of them sit 50,000 learned merges and one special end-of-text token, 50,257 entries in all. GPT-3 uses the same tokenizer.Code
Code
Ġ in front of most tokens is a space. Before training, GPT-2 maps each of the 256 byte values to a printable Unicode character, so that the vocabulary and merge files can be stored and read as ordinary text. The 188 bytes that already print as a visible character keep it. The remaining 68, which include space, newline, tab and the other control characters, are assigned the code points from 256 upward in byte order. Space is the 33rd of them, so it lands on code point 288, which is Ġ. The function below rebuilds the table.
Code
Code
dog. and dog! out of the vocabulary.
Code
Code
Code
Code
WordPiece
BERT uses WordPiece, which builds its vocabulary by merging pairs like BPE but chooses the pair differently (Wu et al., 2016). Instead of the most frequent pair, it merges the pair with the highest score which favors pairs whose parts rarely occur apart. Encoding is also different. WordPiece does not replay merges. It takes the longest vocabulary entry that matches the start of the word, then the longest entry that matches what remains, and so on. Pieces that continue a word carry the prefix##, which tells the decoder to attach them to the previous piece without a space.
Code
Code
bert-base-uncased tokenizer lowercases its input before anything else, so “GPU” is looked up as “gpu”. That word is not in the vocabulary of 30,522 entries, and the longest match from the left is gp, followed by ##u.
Unigram and SentencePiece
The Unigram tokenizer of Kudo works in the opposite direction. It starts from a large candidate vocabulary and assigns each token a probability. It then repeatedly removes the tokens whose removal reduces the likelihood of the training corpus the least, until the vocabulary has the target size. To tokenize a word, it picks the segmentation with the highest probability. SentencePiece is a library that trains BPE or Unigram tokenizers directly on raw text. It treats the space as an ordinary symbol, written▁, so it needs no language-specific rules for splitting words. That is useful for languages such as Japanese and Chinese, which do not separate words with spaces. T5 uses a SentencePiece Unigram model, and Llama 2 a SentencePiece BPE model.
Code
Code
Vocabulary size and sequence length
The tiktokenizer app runs several tokenizers in the browser and colors each token. Paste the following program into it and switch between thegpt2 and cl100k_base tokenizers.
Code
Code
The comparison below uses three OpenAI tokenizers:
gpt2, cl100k_base (GPT-3.5 and GPT-4) and o200k_base (GPT-4o). For each one it counts the tokens of the program, the tokens that contain only whitespace, and the tokens left once the indentation is removed.
Code
Code
gpt2 needs: it spends 40 tokens on pieces that hold only spaces or newlines, against 17 for cl100k_base. The newer tokenizer has merges for runs of spaces, so the indentation of a line costs one token instead of one per space. It also has merges that join a newline to the punctuation before it, such as .\n, and to other newlines. One line of the docstring shows both.
Code
Code
cl100k_base to o200k_base, saves nothing on this program. The extra entries pay off on other languages, as the Greek sentence below shows.
Code
Code
Code
Code
cl100k_base vocabulary would add another 38 million. For a model the size of GPT-3, the same matrix is well under one percent of the parameters. At that scale the extra parameters cost little compared with the shorter sequences they buy, which is one reason recent large models use vocabularies of 100,000 to 200,000 entries.
References
- Radford, A., Wu, J., Child, R., Luan, D., Amodei, D., et al. (2019). Language models are unsupervised multitask learners. OpenAI Blog. Link
- Sennrich, R., Haddow, B., Birch, A. (2015). Neural Machine Translation of Rare Words with Subword Units. arXiv [cs.CL]. Link
- Wu, Y., Schuster, M., Chen, Z., Le, Q., Norouzi, M., et al. (2016). Google’s Neural Machine Translation System: Bridging the Gap between Human and Machine Translation. arXiv [cs.CL]. Link
Found an error on this page? Report an issue.

