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:

  1. Count every pair of adjacent tokens in the corpus.
  2. 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”.

(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.

(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.

See Also