Topic 2 of 6 · 5 min read
Byte-level BPE and pre-tokenization rules
Goal: Explain how a regex pre-tokenizer changes the tokens produced for source code, numbers and non-English text.
Byte-level BPE starts from the 256 possible byte values of UTF-8 text, so any string can be encoded with no unknown token, and a regex pre-tokenizer first cuts the text into chunks so that merges never cross the chunk edges. Two decisions, and the second one shapes the model's behaviour more than most people expect.
Check yourself · 1 of 3
Why does byte-level BPE have no unknown-token problem?
Why it matters
A character-based vocabulary has to list every symbol: emoji, rare Chinese characters, accents. Anything missing becomes an unknown token and the information is lost. Bytes cannot be missing. A character the vocabulary has never seen simply costs several byte tokens. This is what tiktoken and the GPT-2 family do, and what minbpe teaches.
How pre-tokenization works
Before any merging, a regex splits text into chunks. BPE then runs inside each chunk and never merges across a boundary. The GPT-2 pattern looks like this:
import regex # the third-party "regex" module, not the standard "re"
PAT = regex.compile(
r"""'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+"""
)
print(PAT.findall("Hello world 2024!"))
# ['Hello', ' world', ' 2024', '!']
Letters, digits and punctuation never share a chunk, and a single leading space stays attached to the word that follows. Newer tokenizers change the details. The one in tiktoken's cl100k_base encoding, for example, splits digit runs into groups of at most three, which you can read in its source.
What the rules decide
- Code. Because punctuation is split from letters,
foo(bar)can never become one token. Indentation is a run of spaces, so how whitespace is chunked decides whether four spaces cost one token or four. - Numbers. The digit rule fixes how "1234567" is cut, and the model has to do arithmetic on whatever pieces come out.
- Non-English text. Rules written with English in mind chunk other scripts less usefully, and byte fallback makes uncommon characters expensive.
Before and after
Weak mental model: "The tokenizer reads the string and picks the best tokens." Fixed: "The string is cut by a regex into chunks, each chunk is turned into bytes, and merges run inside each chunk only." That is why " world" and "world" are different tokens and why no token ever spans a word boundary plus punctuation.
What goes wrong
- Using the wrong regex module. Python's standard
redoes not support\p{L}, so a copied pattern fails or is rewritten wrongly. - Changing the pattern after training. A tokenizer trained with one pattern but run with another yields chunks the merges never saw.
- Assuming one byte is one character. Accented letters take two bytes, most CJK characters three, and many emoji four.
Examples
Which chunks does the pattern make?
Input: x = foo(42)
The GPT-2 style pattern gives x, =, foo, (, 42, ). The ( and 42 can never merge into one token, because they sit in different chunks. A pattern that allowed punctuation next to letters would allow tokens like foo( and would give the model different statistics.
One character, several bytes
The letter é is 2 bytes in UTF-8, the character 日 is 3, and most emoji are 4. A byte-level vocabulary with few merges for those scripts represents them as 2 to 4 tokens each, while an ASCII letter is usually 1 byte and often merges into a longer token. Same sentence length in characters, very different token counts.
Key points
- Byte-level BPE starts from 256 byte values, so no input can be unknown; a rare character just costs several tokens.
- A regex pre-tokenizer cuts text into chunks first, and merges never cross a chunk boundary.
- A leading space is attached to the next word, so " world" and "world" are different tokens.
- The regex decides how code punctuation, indentation and digit runs are split, which shapes model behaviour.
- The pre-tokenizer pattern is part of the tokenizer and must be identical at training and inference.
Common mistakes
- Pasting a Unicode regex such as
\p{L}into Python's standardremodule instead of theregexmodule. - Treating bytes and characters as the same thing when estimating token counts for non-English text.
- Training a tokenizer with one split pattern and serving it with another.
- Assuming merges can build tokens that span a space plus punctuation, when the pre-tokenizer forbids it.
Check yourself · 2 of 3
A GPT-2 style regex splits "Hello world 2024!" into Hello, ' world', ' 2024' and '!'. What does this guarantee?
Check yourself · 3 of 3
A teammate copies a tokenizer regex using \p{L} into Python and the standard re module raises an error. What is the cause and the fix?
Practice items, not counted towards your mastery.
Start here
- Byte-Pair Encoding tokenization (Hugging Face LLM course) (opens in a new tab)CourseFree
Walks through BPE training and encoding step by step on a tiny corpus, including the byte-level trick that removes unknown tokens.
- Andrej Karpathy: minbpe and the GPT tokenizer lecture (opens in a new tab)ToolFree
Short, readable Python for training, encoding and decoding BPE; the best code to step through next to the merge algorithm.
- Hugging Face LLM Course: The Tokenizers library (opens in a new tab)CourseFree
Hands-on chapters on training a tokenizer from an old one and on the BPE, WordPiece and Unigram algorithms.
More for this topic
- tiktoken (OpenAI) (opens in a new tab)ToolFree
The fast BPE tokenizer used by OpenAI models; shows regex pre-splitting, special-token handling and encoding names in real code.
- Hugging Face LLM Course: Normalization and pre-tokenization (opens in a new tab)CourseFree
Shows how real tokenizers split text before BPE runs, including the GPT-2 byte-level pre-tokenizer that marks spaces with a special symbol.