What GPT-2's tokenizer does
A language model reads integers, not text. Its tokenizer cuts the text into entries of a fixed vocabulary and gives each its id, which picks a row of the model's embedding table. Whole words cannot cover every word; single characters can, but make sequences long. Byte-pair encoding (BPE) sits between: a frequent string such as " numbers" is one token, and a rarer one is spelled from smaller entries.
GPT-2's tokenizer is BPE over bytes. Every text is a sequence of UTF-8 bytes and all 256 bytes are in the vocabulary, so any string encodes, decodes back exactly, and nothing is ever unknown. The vocabulary has 50,257 entries: the 256 byte tokens, 50,000 merges and <|endoftext|>. The toolbar follows one text through the stages the board's columns show: the English sentence is 37 characters, 37 UTF-8 bytes, 6 pieces and 7 tokens.
Bytes as characters
UTF-8 writes a character as one to four bytes: an ASCII letter as one, ï as C3 AF, each character of 東京は晴れ as three and 🤖 as four (F0 9F A4 96). A vocabulary built on Unicode characters would need over 130,000 base symbols before its first merge, the GPT-2 paper notes; one built on bytes needs 256.
GPT-2 stores its vocabulary and merges as text, so each byte is first given a printable character. The 188 printable bytes keep their own; the 68 others, the space among them, are moved to U+0100 and up. A space becomes Ġ and a newline Ċ, so " turns" is stored as Ġturns and ï as ï. The Text → bytes → characters panel shows each character, its bytes and their characters, and the Byte → character table shows all 256, the moved ones tinted.
The pre-tokenizer
Before any merge, a regular expression cuts the text into pieces, and merges never cross from one piece into the next. It tries six alternatives in order and keeps the first that matches:
's|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+
The Pre-tokenizer column names them contraction, letters, digits, symbols, spaces before a word and spaces, and counts the pieces each cut; the Pieces chips are underlined in their colour. Letters, digits and other symbols never share a piece, and a word takes the one space before it. The GPT-2 paper gives the reason: run on raw bytes, BPE spent vocabulary entries on variants such as dog. dog! dog?, so merges were kept from crossing character categories, with an exception for the space before a word.
Runs of spaces show a detail. In Spaces and a number, \s+(?!\S) stops one space short of the x, so the four spaces become a piece of three and the piece " x". No pair of spaces is in merges.txt, so those three spaces stay three tokens, each id 220.
Learning the merges
The merges come from training. Sennrich et al.'s algorithm starts with every piece of a training corpus split into single symbols, here byte characters, and repeats one step: count every adjacent pair across the corpus, weighting each piece by how often it occurs, and merge the most frequent pair everywhere into a new symbol.
Here is how often piece occurs in the training text and how often symbol is followed by in its current split. The pair chosen in round becomes line of merges.txt, its rank. GPT-2 stopped after 50,000 rounds and numbers the token that merge makes as id . Rank 0, the first merge and so the most frequent pair of byte characters, is Ġ + t: a space followed by t.
Encoding: the BPE loop
Encoding replays the training order without counting. Inside each piece, start from the byte characters, merge the adjacent pair with the lowest rank at every place it occurs, and repeat until no adjacent pair is in merges.txt:
The BPE merges column shows one state of the loop: the symbols, every adjacent pair's rank, and the pair that merges next outlined. The BPE loop strip plays the whole film of the selected piece. In the English sentence, "Tokenization" takes 10 merges, from o + n at rank 5 to T + oken at rank 30,386, and ends as Token and ization, because no merge joins those two. Ranks only rise along the way: a merge can only use symbols that earlier merges made. A word the vocabulary lacks falls apart into pieces it has, so "unbelievably" becomes un, bel, iev and ably after 8 merges.
Ids and the exact decode
The Tokens → ids column looks each final symbol up in vocab.json: Token is id 30,642, which is 256 + 30,386. GPT-2 adds no start or end token, and each id picks a row of the model's 50,257 × 768 embedding table, which the vocabulary popup draws.
Decoding maps the tokens' characters back to bytes and reads them as UTF-8; the Decode panel shows every example coming back exactly. A token need not hold a whole character. The emoji's four bytes become three tokens, ĠðŁ (the space and the first two bytes), ¤ and ĸ, and the 15 bytes of 東京は晴れ become 9 tokens of which only は and れ are whole characters. Joined, the bytes make the characters whole again.
The page records the drawer's plain-Python code run on the released vocab.json and merges.txt; its ids equal GPT2TokenizerFast's and tiktoken's gpt2 encoding on all 325 check strings: 4,212 tokens for 12,473 bytes.
Where it came from
BPE began as Philip Gage's compression method (1994), which replaces the most frequent pair of bytes with an unused byte. Sennrich, Haddow and Birch (2016) used its merge loop to build subword vocabularies for neural translation, over characters with an end-of-word mark, so that rare words could be translated piece by piece. GPT-2 (Radford et al. 2019) moved it to bytes and added the regex that keeps character categories apart; Wang, Cho and Gu studied byte-level BPE for translation in the same year. GPT-3 kept GPT-2's tokenization unchanged. Zouhar et al. later framed BPE training as an optimisation problem and bounded how far its greedy choice can fall from the best merge sequence.
The o200k tab shows GPT-4o's tokenizer: the same byte-level BPE, a vocabulary about four times larger and a new regex. BERT uses WordPiece, T5 a Unigram model, and ByT5 reads the bytes themselves.
Papers
- Neural Machine Translation of Rare Words with Subword Units Turns Gage's byte-pair compression into a way to learn a subword vocabulary: merge the most frequent adjacent pair, again and again.
- Language Models are Unsupervised Multitask Learners GPT-2, whose tokenizer this tab records: BPE over bytes, with merges kept from crossing character categories (section 2.2).
- Neural Machine Translation with Byte-Level Subwords Byte-level BPE for translation: no unknown tokens, and results comparable to BPE over characters with a vocabulary an eighth the size.
- Language Models are Few-Shot Learners GPT-3, which kept GPT-2's reversible tokenization and so this tab's vocabulary.
- A Formal Perspective on Byte-Pair Encoding BPE training as an optimisation problem: how close the greedy merge choice comes to the best merge sequence, and a faster encoder.
- Between words and characters: A Brief History of Open-Vocabulary Modeling and Tokenization in NLP Survey of word, subword, character and byte vocabularies, with BPE, WordPiece and Unigram side by side.