Tokenizers, Unicode, and Byte-Pair Encoding
1. Tokenizer
A tokenizer breaks a string into chunks and maps them to integer IDs. Those IDs can be used to reconstruct the string.
2. Unicode and UTF-8
Unicode assigns code points to characters. UTF-8 defines how to represent those code points as sequences of bytes.
For A, the Unicode code point is U+0041, and its UTF-8 representation is 01000001.
A byte has eight bits and 256 possible values, from 0 through 255.
3. Byte-Pair Encoding
Byte-level BPE constructs a vocabulary by repeatedly merging frequent adjacent byte sequences until the target vocabulary size is reached. This reduces the number of tokens needed to represent the text while retaining the ability to encode arbitrary UTF-8 input.
Training the tokenizer on raw text produces a vocabulary tailored to that data.
4. A BPE Example
The string abab is encoded as the UTF-8 bytes 61 62 61 62, written in hexadecimal. Two merge rules combine these pieces:
| Priority | Merge rule | New token ID |
|---|---|---|
| 1 | a + b → ab | 256 |
| 2 | ab + ab → abab | 257 |
After the first merge, the pieces are ab and ab. After the second, the result is one token, abab, representing four bytes. Looking it up in this example’s vocabulary gives the token ID 257.
Notebooks
Four notebooks inspect these representations. Each page is the notebook, and the file beside it is the notebook itself.