GPT Tokenizers
PremiumGPT-2/GPT-4 tokenization, regex pre-tokenization, and the tiktoken library
Get code accessGPT-2 Tokenization
In the previous chapter we built a basic BPE. OpenAI did not use naive BPE directly in GPT-2; they made an important improvement: regex pre-tokenization.
Problems With Naive BPE
Consider a word like “dog” that appears with different punctuation:
dog.
dog!
dog?
dog,Naive BPE may merge each into separate tokens:
token_123 = "dog."
token_124 = "dog!"
token_125 = "dog?"
token_126 = "dog,"This causes two problems:
Vocabulary waste: the same word with different punctuation consumes multiple token slots, wasting limited vocab capacity.
Semantic entanglement: word meaning and punctuation meaning are mixed, forcing the model to learn their relationship.
Regex Pre-tokenization Solution
GPT-2’s approach: split text into chunks with regex before BPE.
Core idea:
Prevent cross-category merges: disallow BPE merges across boundaries such as:
- letters and digits
- letters and punctuation
- spaces and non-space characters
GPT-2 regex (simplified):
import regex as re
# GPT-2 split pattern
gpt2_pattern = r"""'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+"""
text = "Hello, world! It's 2024."
chunks = re.findall(gpt2_pattern, text)
print(chunks)
# Output: ['Hello', ',', ' world', '!', ' It', "'s", ' 2024', '.']Intuitive explanation:
|means “or”,?means “optional once”,+means “one or more”\p{L}is Unicode letters,\p{N}is Unicode digits,\sis whitespace[^...]means “not in this set”- The leading
?is an optional space; GPT-2 folds leading spaces into the token
Breakdown:
's|'t|'re|...: match common contractions so they do not split into'ands?\p{L}+: optional space + letters (a word)?\p{N}+: optional space + digits (a number chunk)?[^\s\p{L}\p{N}]+: optional space + punctuation/symbols\s+(?!\S)|\s+: whitespace (including end-of-line)
Workflow
With regex pre-tokenization, BPE becomes:
- Split: use regex to split text into chunks
- Encode each chunk: apply BPE to each chunk independently
- Concatenate: join all chunk tokens
Log in to continue reading
This is premium content. Please log in to access the full article.
CookLLM Docs