Tokenization
A tokenizer turns a string into a sequence of token indices from a fixed vocabulary of entries. It works in two steps:
- Parse: split the string into pieces that appear in the vocabulary.
- Index: replace each piece by its position in the vocabulary, a number from to .
The pieces can be characters, words or something in between:
- Characters: the vocabulary is small and covers every string. The network must learn how letters combine into meaning, and sequences get long.
- Words: each token carries meaning. Unseen words such as names have no token, and related forms like “walk” and “walked” get unrelated tokens.
- Subwords: common words get one token, and rare words split into common fragments. Single characters stay in the vocabulary, so every string can still be written.
Byte pair encoding (BPE) builds a subword vocabulary from a corpus. It starts with one token per character and repeats two steps until the vocabulary reaches a chosen size:
- Count every pair of adjacent tokens in the corpus.
- Add the most frequent pair to the vocabulary as a new token, and merge every occurrence of that pair.
The tokenizer is built before the network trains and never changes during training. No gradient reaches it. Its vocabulary size sets the size of the embedding table.
Intuition
A deep network needs vectors, and text is a sequence of characters. Using each character as a token would work, but a single letter carries little meaning. The Spring 2023 lecture 21 notes use “dog” as the example. As three character tokens, the network must learn that these three vectors together mean one animal. A single token for “dog” does that work for the network.
Whole words have the opposite problem. A word vocabulary misses names, typos and new words. It also needs separate tokens for “walk”, “walks” and “walked”, with no sign that they are related. Subwords sit in between. Frequent words become one token. A rare word such as “unhappiness” splits into pieces such as “un”, “happi” and “ness”.
BPE chooses the subwords by frequency alone. Strings that occur together often tend to form a unit of meaning, so they get their own token. The same rule makes text shorter, which is why BPE first appeared as a compression method. The Spring 2023 lecture 21 notes compare it to Huffman coding. Huffman coding gives frequent symbols short codes of different lengths. BPE goes the other way and gives frequent strings of different lengths one fixed-size code, a single index.
Properties
(Unique Parses) The string-to-index table must split every string in exactly one way.
The Spring 2023 lecture 21 notes raise the problem with the tokens “a” and “an”. The input “an” could be one token or two. One fix is a rule, such as always taking the longest match. Another is a prefix-free vocabulary, in which no token is the beginning of another. BPE tokenizers in practice apply their learned merges to new text in the order they were learned. This also gives every string one parse.
(No Unknown Words) A BPE vocabulary keeps all single characters, so any string over the alphabet can be tokenized.
A word that never appeared in the corpus still splits into known pieces. In the worst case it splits into single characters. Prince notes that punctuation and capital letters are treated as separate input characters. The Spring 2023 lecture 21 version of BPE drops a character once every pair containing it has its own token. Prince’s version keeps every character.
(Each Merge Shortens the Corpus) Merging a pair that occurs times without overlap removes exactly tokens from the corpus.
Each occurrence of the pair turns two tokens into one. BPE picks the most frequent pair, so each step shortens the corpus as much as any single merge can. Overlap happens only for a pair of two equal tokens, such as “a” “a” inside “aaa”. The Hard example works out this case.
(Vocabulary Size Is a Trade-off) A larger vocabulary gives shorter sequences but a larger embedding table and fewer examples of each token.
With more merges, each token covers more text. A document then needs fewer tokens, and the cost of attention falls. The embedding table and the output layer grow linearly with . Each token also appears less often in the corpus, so its vector gets fewer updates. Prince quotes tokens as typical, and the lecture uses . GPT-3 uses .
(Tokenizers Come From Outside the Model) Most models use an existing tokenizer, so the vocabulary is a design choice made by someone else.
The Spring 2023 lecture 21 notes mention off-the-shelf tokenizers such as OpenAI’s tiktoken. The tokenizer runs on the CPU and passes indices to the network on the GPU. A team that needs a different vocabulary size or a different alphabet must build its own tokenizer. A model trained with one tokenizer cannot use another without retraining its tables.
Examples
(Easy) Longest-Match Parsing
A vocabulary has the tokens : “a”, : “n”, : “t”, : “s”, : “an” and : “ant”.
Tokenize "ants" and "tan" with the longest-match rule. . For "tan", the only token that starts with "t" is "t". The rest is "an", whose longest match is the token "an". The result is . Without the rule, "an" could also be read as "a" and "n", which gives .
For “ants”, the longest token at the start is “ant”, then “s” remains. The result is
(Medium) Three BPE Merges
A corpus has the words “cat” times, “cats” times, “hat” times and “chat” times. BPE starts from the characters a, c, h, s and t.
Run three merges. Count the tokens in the corpus after each one, and tokenize the new word "chats". tokens. The pair counts are (a, t) , (c, a) , (h, a) , (t, s) and (c, h) .
The corpus starts with
Merge adds “at” and removes tokens, leaving . The words are now c·at, c·at·s, h·at and c·h·at. The counts are (c, at) , (h, at) , (at, s) and (c, h) .
Merge adds “cat” and leaves tokens. The pair (h, at) now has the top count of . Merge adds “hat” and leaves tokens. The words are cat, cat·s, hat and c·hat, and the vocabulary has tokens.
To tokenize “chats”, start from c·h·a·t·s and apply the merges in order. Merge gives c·h·at·s. Merge needs “c” next to “at”, which does not occur. Merge gives c·hat·s. The unseen word becomes three known tokens.
(Hard) Overlapping Pairs
A corpus is the single string “aaaaa”, five copies of “a”. BPE counts adjacent pairs and merges occurrences from left to right.
What count does the pair (a, a) get? How many tokens does merging it remove? State the general rule for a run of equal tokens. The pair occurs at positions -, -, - and -, so its count is . Merging from the left gives aa·aa·a. That is tokens instead of , so the merge removed , not . The occurrences overlap. Once positions - merge, position cannot also pair with position .
In a run of equal tokens, the pair count is , but only merges happen. The run shrinks from to tokens. For pairs of two different tokens no overlap is possible, so a count of always removes tokens. A careful BPE counts non-overlapping occurrences for pairs like (a, a), so that its counts match the actual savings.
See Also
- 7.3. Token Embeddings, which turns token indices into vectors.
- 7.3.2. Word2vec, which learns vectors for whole words.
- 4.2 Huffman Coding, the compression method that the lecture compares BPE to.
- 7.1. Attention, whose cost depends on the number of tokens.