4.1 Tokenization
1. Learn
Section titled “1. Learn”- Karpathy, Let's build the GPT Tokenizer00:14:56–01:25:28 (Unicode and UTF-8, the BPE algorithm, training, decoding and encoding, regex pre-splitting, tiktoken, special tokens) and 01:51:41–02:10:20 (the quirks of LLM tokenization); video length 2:13:34
- Stanford CS336: Language Modeling from Scratchlecture 1 "Overview, tokenization" (April 1) and its lecture_01.py trace
2. Bridge
Section titled “2. Bridge”Why subwords. A language model reads a sequence of integers, so text has to be cut into pieces from a fixed vocabulary. Characters give short vocabularies but long sequences, and attention’s cost grows with length. Whole words give short sequences but an unbounded vocabulary that can never cover a new name or typo. Subword tokens sit in between: common words are one token, and rare ones fall apart into pieces (Karpathy, Let’s build the GPT Tokenizer).
Byte-pair encoding builds that vocabulary greedily. Encode the text as UTF-8 so that the base vocabulary is just the 256 byte values, which can represent any string in any script. Then count every adjacent pair of tokens, give the most frequent pair a new ID, replace it everywhere, and repeat until the vocabulary is the size you want. The list of merges, in order, is the tokenizer (Sennrich et al. 2016; Radford et al. 2019, §2.2).
Pre-tokenization. Run on raw text, BPE learns tokens like “dog.” and “dog!”, wasting vocabulary on combinations. GPT-2 first splits text with a regular expression into words (each keeping its leading space), numbers, punctuation and whitespace, and merges never cross those splits. GPT-4’s pattern also splits numbers into chunks of at most three digits, read from the left, so 1234 becomes 123|4. That caps how many number tokens the vocabulary needs, though the chunks don’t line up with place value (Karpathy, Let’s build the GPT Tokenizer, 01:11:38; GPT-2 §2.2).
Encoding and decoding. To encode new text, start from its bytes and apply the merges in the order they were learned: always the lowest-ranked merge available, not the most frequent pair in this text. Decoding looks up each token’s bytes, joins them, and decodes UTF-8 once at the end, since a single token can hold half of a multi-byte character (Karpathy, minbpe).
Consequences. Text that was rare in the tokenizer’s training data, such as other scripts, code or long numbers, costs more tokens per character. That means a shorter effective context and a higher price per word. Many odd model behaviours, such as miscounting letters in a word, come from the model never seeing the individual characters (Karpathy).
3. Explore
Section titled “3. Explore”stubs/m4_1_bpe.pyLoading starter file…
Prefer your own editor? Edit exercises/stubs/m4_1_bpe.py and run mlc check 4.1. The tests are identical; import the results on the progress page.
Then train on Tiny Shakespeare (locally; downloads about 1.1 MB once):
cd exercises && python projects/bpe_shakespeare.py 1024The reference solution learns 768 merges in about 12 seconds and averages 2.3 bytes per token on held-out text. Look at how it splits a modern sentence, a number, code and non-Latin text.
5. Check
Section titled “5. Check”- not yet: All tests pass (0/?)
- not yet: Checkpoint quiz ≥ 80%
Learner log: time spent and points of confusion
Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.