Skip to content

The previous section gave every tokenizer one shape, but the only trained tokenizer so far is the char tokenizer. With one id per code point, a 10M-parameter model on TinyStories spends most of its context window on spelling, and any character it never saw is <unk>. The byte tokenizer never fails, but it costs one token per byte. Byte-level BPE sits between the two: it starts from the 256 bytes, so nothing is ever unknown, and it learns merges until frequent words are a single token.

Concepts from other sections (13)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

It is also what the real models in this book use. SmolLM2, the model loaded in the Llama-family model and the one the agents run on, ships a GPT-2-style byte-level BPE in tokenizer.json, and the engine cannot answer a single request until the tokenizer turns text into exactly its ids. This section builds two things: a trainer for the capstone’s own vocabulary, and a loader for the real files, verified id for id against Hugging Face.

You will need the protocol and check_ids from the tokenizer protocol; the GPT-2 byte map from Injective, surjective, bijective: the GPT-2 byte map; and the trie (insert, longest_prefix) from Trees and tries, longest-prefix match, both in Mathematics. The trainer’s heap is the binary heap with lazy deletion from Programming and Algorithms.

The solution is in pretok.py (pretokenize_gpt2, split_digits, is_space, is_letter, is_number) and bpe.py (BPETokenizer with train, from_gpt2, from_hf_json, encode, encode_batch, decode, save, load). The contracts are bpe.pyi and pretok.pyi; the file format is the tokenizer.json subset in tokenizer.md, with schema tokenizer-json.schema.json. The entry point cli_tok.py exposes the work as two verbs: tok train --algo bpe --vocab V --in FILE --out DIR [--special S] [--min-freq M] trains on the lines of FILE and saves DIR/tokenizer.json, and tok encode --tokenizer tokenizer.json --in FILE loads any file in the subset through from_hf_json and reports the ids and bytes per token. With them, the finished system trains a vocabulary with tok train --algo bpe and encodes with SmolLM2’s real file, matching Hugging Face’s ids.

Learning objectives

  • Transcribe the GPT-2 pre-tokenizer regex into a hand-written scanner and explain why Unicode White_Space is not str.isspace.
  • Train BPE by hand, including the frequency weighting, the tie rule, and min_freq.
  • Encode by replaying merges in rank order, and explain why this differs from merging left to right.
  • Explain why decoding must join bytes before UTF-8 decoding.
  • Load a real tokenizer.json faithfully, refusing every field the implementation does not support.

The notation for this section:

SymbolMeaningType / shape
β(b)\beta(b)the byte map: byte bb as one printable code pointstr of length 1
wwa pre-token: a piece of text no merge may crossstr
f(w)f(w)how often pre-token ww occurs in the training textsint
s=(s1,…,sn)s = (s_1, \dots, s_n)the current symbols of one pre-token (each a token string)list[str]
(a,b)(a, b)an adjacent pair of symbolstuple[str, str]
c(a,b)c(a, b)the pair count, ∑wf(w)⋅#{i:si=a,si+1=b}\sum_w f(w) \cdot \#\{i : s_i = a, s_{i+1} = b\}int
r(a,b)r(a, b)the rank of merge (a,b)(a, b): its position in mergesint
VVthe target vocabulary sizeint
mmmin_freq: the smallest count still worth a mergeint

Before any merging, GPT-2 splits text into pre-tokens with one regular expression, and merges never cross a pre-token boundary. Without that split, “dog.” and “dog!” would learn different merges, and a frequent pair could fuse the end of one word with the start of the next. The expression is:

's|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+

At each position the engine tries the alternatives in order and takes the first that matches:

  1. one of the seven lowercase contractions, 's 't 're 've 'm 'll 'd;
  2. an optional single space U+0020, then a run of letters (\p{L}: categories Lu, Ll, Lt, Lm, Lo);
  3. an optional single space, then a run of numbers (\p{N}: Nd, Nl, No);
  4. an optional single space, then a run of anything that is not white space, a letter, or a number;
  5. \s+(?!\S): a run of white space not followed by a non-space. When the run is followed by a word, the engine backtracks one character, so the run gives up its last space, which then starts the next pre-token via the optional space of alternatives 2 to 4;
  6. \s+: the white space that alternative 5 rejected (a single white-space character right before a word, such as a tab).

The subtle part is \s. It means the Unicode White_Space property: tab, newline, U+000B to U+000D, the space, U+0085, U+00A0, U+1680, U+2000 to U+200A, U+2028, U+2029, U+202F, U+205F, U+3000. Python’s str.isspace() also accepts U+001C to U+001F, which are not White_Space, so it gives wrong pre-tokens on text containing those control characters. The solution therefore writes pretokenize_gpt2 over unicodedata.category and this explicit set, with no regex module. Its result always joins back to the text and has no empty piece. Only an exact transcription matches the real ids.

SmolLM2 adds one step. Its file declares a Sequence of two pre-tokenizers: first Digits with individual_digits, which makes every number code point its own piece, then the GPT-2 split on each piece. That is split_digits, and it is why SmolLM2 spells 2024 as four tokens.

Once text is in pre-tokens, each pre-token is turned into its UTF-8 bytes and each byte into β(b)\beta(b). A pre-token thus becomes an ordinary string over 256 printable symbols: the space is Ġ, the newline Ċ, and é (bytes c3 a9) is é. Because the vocabulary starts with these 256 symbols, every text is encodable, there is no <unk>, and a token can be part of a character. That last fact will matter when we decode.

Training starts every pre-token as its sequence of byte symbols and counts each adjacent pair over all pre-tokens, weighting by f(w)f(w). Then it repeats until the vocabulary has VV entries:

  1. pick the pair (a,b)(a, b) with the largest count c(a,b)c(a, b); on a tie, the pair whose ids are smallest, compared as (left id, right id);
  2. stop if that count is below mm;
  3. add the token abab with the next id and record the merge (a,b)(a, b) with the next rank;
  4. replace every non-overlapping occurrence of a,ba, b (left to right) in every pre-token that contains it, and update the counts of the neighboring pairs that changed.

Ids are assigned in a fixed order: the specials first (ids 0, 1, …), then the 256 byte symbols in code point order (so ! is the first one, and Ġ is 220 when there are no specials), then one token per merge. The tie rule and this order are exactly what the Hugging Face BpeTrainer does, which is why the solution’s merges can be compared with its merges one for one.

The naive loop recounts every pair after every merge, which costs a pass over the corpus per merge. The trainer instead keeps a max-heap of (count, pair) with lazy updates (pop a stale entry, push it back with its current count) and a map from each pair to the pre-tokens that contain it, so a merge costs only the pre-tokens it touches. One more rule: specials are cut out of the training texts before counting, because <|endoftext|> between documents must never become the merges <|, end, and so on.

Encoding is not training run again. It begins by splitting out the added tokens, by leftmost-longest match through the trie: SmolLM2’s <|im_start|> is one id, and the regex would otherwise cut it into <|, im, _, start, |>. Then it pre-tokenizes the rest and maps bytes. Inside each pre-token it repeatedly merges the adjacent pair with the lowest rank among those present; when the lowest rank occurs twice, it merges the leftmost occurrence. It stops when no adjacent pair has a rank and looks each symbol up in the vocabulary. Caching the result per pre-token makes encoding real text fast, because words repeat.

Note that lowest rank first, leftmost on a tie, is not the same as merging left to right. That distinction is the single most common bug in BPE encoders.

Decoding concatenates the bytes of every token (β−1\beta^{-1} of each symbol, or the UTF-8 of an added token’s text) and decodes the whole byte string as UTF-8 with replacement, once. Decoding token by token breaks every character whose bytes landed in two tokens.

Two loaders read the real models’ files. from_gpt2(vocab.json, merges.txt) reads the original GPT-2 release (merges.txt has a #version first line) and makes <|endoftext|> an added special token. from_hf_json(tokenizer.json) reads the subset described in tokenizer.md: it honors the declared pre-tokenizer sequence and added tokens, and it raises ValueError naming the field for anything it does not implement (a normalizer, dropout, an unknown token, byte fallback, a template post-processor). Silently ignoring such a field would encode different ids than the model was trained on, with no error anywhere.

There is one quirk to reproduce. SmolLM2’s vocabulary has no symbol for six control bytes (0x04 among them) and no unknown token, and Hugging Face drops such a byte, so the solution drops it too.

Start with the pre-tokenizer on "I'm here!!\n" (three spaces before here). The table traces which alternative fires at each position:

PositionMatchesPre-token
Ialternative 2, a letter runI
'malternative 1, a contraction'm
three spacesalternative 5: the run minus its last space, because h followstwo spaces
herealternative 2 with the leading space here
!!alternative 4!!
\nalternative 5 at the end of the text\n

So the pre-tokens are ["I", "'m", " ", " here", "!!", "\n"]. Likewise "don't 2024 café" is ["don", "'t", " 2024", " café"].

Now train on the texts "hug", "hug", "pug", "hugs", with m=1m = 1 and V=259V = 259 (three merges after the 256 byte symbols). Each text is one pre-token, so f(hug)=2f(\text{hug}) = 2, f(pug)=1f(\text{pug}) = 1, f(hugs)=1f(\text{hugs}) = 1. The byte symbols involved have ids g 70, h 71, p 79, s 82, u 84. The three steps:

StepSymbolsPair countsMerge (new id)
1h u g (x2), p u g, h u g s(u, g) 4, (h, u) 3, (p, u) 1, (g, s) 1(u, g) = ug (256)
2h ug (x2), p ug, h ug s(h, ug) 3, (p, ug) 1, (ug, s) 1(h, ug) = hug (257)
3hug (x2), p ug, hug s(p, ug) 1, (hug, s) 1(p, ug) = pug (258)

At step 3 two pairs tie at 1. Their ids are (79, 256) and (257, 82); compared as tuples, (79, 256) is smaller, so pug wins. With the default m=2m = 2 training stops after step 2, because the best count is then 1.

Encode "hugs pug" with these merges. It pre-tokenizes to "hugs" and " pug", and the space becomes Ġ (id 220). In h u g s, the ranked pairs present start with (u, g) at rank 0: merge, giving h ug s; then (h, ug) at rank 1, giving hug s; (hug, s) has no rank, so stop with [257, 82]. In Ġ p u g, (u, g) merges first, then (p, ug) at rank 2, giving [220, 258]. The ids are [257, 82, 220, 258], and decoding them gives back "hugs pug".

Two tiny cases show that rank, not position, decides. With merges ranked (b, c) before (a, b), "abc" is a bc, although (a, b) is the leftmost pair. With the single merge (a, a), "aaa" is aa a: both occurrences have rank 0, and the leftmost merges first.

The interface spans two modules:

python/tinyllm/tok/pretok.py
def is_space(ch: str) -> bool; def is_letter(ch: str) -> bool; def is_number(ch: str) -> bool
def pretokenize_gpt2(text: str) -> list[str]
def split_digits(text: str, individual: bool = True) -> list[str]
# python/tinyllm/tok/bpe.py
class BPETokenizer(Tokenizer):
vocab: dict[str, int]; merges: list[tuple[str, str]]; added: dict[str, int]; pre_tokenizer: dict
def __init__(self, vocab, merges, added_tokens=(), pre_tokenizer=None) -> None
@classmethod
def train(cls, texts: Iterable[str], vocab_size: int, specials: Sequence[str] = (), min_freq: int = 2) -> "BPETokenizer"
@classmethod
def from_gpt2(cls, vocab_json: str, merges_txt: str) -> "BPETokenizer"
@classmethod
def from_hf_json(cls, tokenizer_json: str) -> "BPETokenizer"
def encode_batch(self, texts: Sequence[str]) -> list[list[int]]

added_tokens are (content, id, special) triples; special_ids holds the special ones, unk_id is None, and vocab_size is the largest id plus one, with no gap allowed. save writes tokenizer.json in the subset (Hugging Face loads it), and load reads it back through from_hf_json.

The model test suite, test_bpe.py, imports only names from the two contracts. Its names and docstrings are a compact summary of the definition:

def test_hand_example_merges_and_tie():
"""hug hug pug hugs, min_freq 1, 259 ids: merges (u,g), (h,ug), (p,ug)."""
def test_count_equal_to_min_freq_still_merges():
"""min_freq 3 keeps the (h, ug) merge, whose count is exactly 3."""
def test_lowest_rank_merges_first():
"""With (b,c) ranked before (a,b), "abc" encodes as [a, bc]."""
def test_equal_ranks_merge_leftmost():
"""With the one merge (a,a), "aaa" encodes as [aa, a]."""
def test_pretokenizer_examples():
"""Contractions, white-space runs, U+001C, U+00A0, a leading tab, and ½."""
def test_split_digits_both_modes():
"""individual: one piece per digit; otherwise one piece per run."""
def test_character_split_across_tokens_decodes_together():
"""The three bytes of a CJK character decode to it only together."""
def test_special_token_is_one_id_and_never_trained_on():
"""A special is id 0, matched in text, skipped on request, never merged."""
def test_save_load_keeps_specials():
"""load(save(t)) has the same merges, specials, and ids."""
def test_bad_arguments_raise():
"""vocab_size below 256 + specials, or min_freq 0, is ValueError."""
def test_round_trip_any_text():
"""decode(encode(x)) == x for text with emoji, accents, tabs, and runs of spaces."""

Eight observations lead to the solution.

  1. Ids must match the real models exactly, so every stage must be a faithful transcription: the regex alternatives in order, White_Space rather than isspace, the byte map, and merges applied by rank. Any approximation produces plausible but different ids.
  2. A regex with backtracking can be written as a scanner. Each alternative of the GPT-2 regex matches a run of one character class, optionally preceded by one space; the only lookahead is (?!\S), which amounts to “a white-space run gives back its last character when a non-space follows”. A hand-written loop over unicodedata reproduces this without a regex engine that supports \p{L}.
  3. Training counts pairs inside pre-tokens, weighted by frequency. Merging is defined per pre-token, so the natural data structure is a counter of distinct pre-tokens, each a list of ids, with its count. Real corpora have far fewer distinct pre-tokens than occurrences.
  4. Each merge changes only a few counts. Replacing a,ba, b by abab in a word with neighbors xx and yy removes (x,a)(x, a) and (b,y)(b, y) and adds (x,ab)(x, ab) and (ab,y)(ab, y). So maintain counts incrementally and keep a map from pair to the words containing it; then a merge costs only the words it touches.
  5. The most frequent pair needs a priority queue, and counts change under it. A max-heap keyed by (count, left id, right id) gives the tie rule for free. Instead of updating entries in place, pop, compare with the current count, and requeue if stale: lazy deletion.
  6. Encoding must replay training’s priorities, not its procedure. Training merged the earliest-learned pair first everywhere, so at encode time, inside one pre-token, the pair with the lowest rank must merge first, leftmost on a tie. Merging left to right gives different ids.
  7. Added tokens are matched before anything else, because the pre-tokenizer would split them. Leftmost-longest through a trie handles overlapping specials (<|im versus <|im_start|>).
  8. Decoding must work on bytes, because a token can hold part of a UTF-8 character. Join all bytes, then decode once with replacement.

The pre-tokenizer comes first in the code because every id downstream depends on it; then training, then encode with merges by rank, then the loaders.

The loop tries the alternatives in the regex’s order at each position.

WHITE_SPACE = frozenset(
"\t\n\x0b\x0c\r \x85\xa0 "
+ "".join(chr(c) for c in range(0x2000, 0x200B))
+ "

   "
) # Unicode White_Space: not str.isspace(), which also accepts U+001C to U+001F
def pretokenize_gpt2(text: str) -> list[str]:
out: list[str] = []
n = len(text)
i = 0
while i < n:
ch = text[i]
# 1. contractions, lowercase only: 's 't 're 've 'm 'll 'd
if ch == "'":
suf = next((s for s in CONTRACTIONS if text.startswith(s, i + 1)), None)
if suf is not None:
out.append(text[i : i + 1 + len(suf)])
i += 1 + len(suf)
continue
# 2 to 4. an optional single U+0020, then a run of one class (L, N, or O)
j = i + 1 if ch == " " and i + 1 < n else i
k = _class(text[j]) # "L", "N", "S" (white space), or "O"
if k != "S":
end = j + 1
while end < n and _class(text[end]) == k:
end += 1
out.append(text[i:end])
i = end
continue
# 5. \s+(?!\S): a white-space run, minus its last character when a
# non-space follows (that character starts the next pre-token).
end = i
while end < n and is_space(text[end]):
end += 1
if end < n and end - i >= 2:
end -= 1
# 6. \s+: a single white-space character before a non-space.
out.append(text[i:end])
i = end
return out

Trace the three spaces before here from the hand example. At the first space, text[j] is the second space, class S, so alternatives 2 to 4 fail. The run is three long and h follows, so it gives back one character: the piece is two spaces. The third space then starts here through alternative 2. Only the U+0020 space is optional in alternatives 2 to 4 (ch == " "), which is why a tab never leads a word.

_merge_word rewrites one word and reports exactly the count changes around each occurrence.

def _merge_word(w: list[int], a: int, b: int, new: int) -> list[tuple[tuple[int, int], int]]:
"""Merge every non-overlapping (a, b) in w, left to right, in place.
Returns the pair count changes the merge causes around each occurrence
(Hugging Face `Word::merge`): the merged pair itself is not decremented."""
changes: list[tuple[tuple[int, int], int]] = []
i = 0
while i < len(w):
if w[i] == a and i + 1 < len(w) and w[i + 1] == b:
if i > 0:
changes.append(((w[i - 1], a), -1)) # left neighbour loses (x, a)
changes.append(((w[i - 1], new), 1)) # and gains (x, ab)
w[i : i + 2] = [new]
if i < len(w) - 1:
changes.append(((b, w[i + 1]), -1)) # right neighbour loses (b, y)
changes.append(((new, w[i + 1]), 1)) # and gains (ab, y)
i += 1 # after a merge, w[i] is `new`, so overlapping (a, b) cannot re-match
return changes

The merged pair is not decremented because the trainer never looks at it again: it was just popped, and its count is irrelevant once its token exists.

The trainer builds word counts, initial ids, and pair counts, then runs the lazy heap loop.

@classmethod
def train(cls, texts, vocab_size, specials=(), min_freq=2) -> "BPETokenizer":
specials = list(dict.fromkeys(specials)) # dedupe, keep order
# ... ValueError for vocab_size < 256 + len(specials) or min_freq < 1
b2u = bytes_to_unicode()
trie = Trie()
for k, s in enumerate(specials):
trie.insert(s, k)
# 1. word counts: pre-tokens in the byte alphabet, specials removed
wc: Counter[str] = Counter()
for text in texts:
for seg, sid in _split_added(trie, text) if specials else [(text, None)]:
if sid is None: # special text is cut out
for p in pretokenize_gpt2(seg):
wc["".join(b2u[b] for b in p.encode("utf-8"))] += 1
# 2. the initial vocabulary: specials, then the 256 symbols by code point
id_to_tok = list(specials) + sorted(b2u.values(), key=ord)
tok_to_id = {t: i for i, t in enumerate(id_to_tok)}
words = [[tok_to_id[c] for c in w] for w in wc] # one entry per distinct pre-token
counts = list(wc.values()) # f(w)
# 3. pair counts and where each pair occurs
pair_counts: dict[tuple[int, int], int] = {}
where: dict[tuple[int, int], set[int]] = {}
for wi, w in enumerate(words):
for pair in zip(w, w[1:]):
pair_counts[pair] = pair_counts.get(pair, 0) + counts[wi]
where.setdefault(pair, set()).add(wi)
# heap entries: [-count, left id, right id, seq, words]: a min-heap on this
# key pops the largest count, then the smallest (left, right) ids
# ... build and heapify
merges: list[tuple[str, str]] = []
# 4. merge the most frequent pair until the vocabulary is full
while len(id_to_tok) < vocab_size and heap:
top = heapq.heappop(heap)
pair = (top[1], top[2])
cur = pair_counts.get(pair, 0)
if -top[0] != cur: # stale: requeue with the current count
top[0] = -cur
heapq.heappush(heap, top)
continue
if cur < 1 or cur < min_freq: # equal to min_freq still merges
break
a, b = id_to_tok[pair[0]], id_to_tok[pair[1]]
new_tok = a + b
# ... new_id: the existing id if new_tok is already a token, else the next id
merges.append((a, b))
update: dict[tuple[int, int], set[int]] = {}
for wi in sorted(top[4]): # only the words that contain the pair
for p, d in _merge_word(words[wi], pair[0], pair[1], new_id):
pair_counts[p] = pair_counts.get(p, 0) + d * counts[wi]
if d > 0:
update.setdefault(p, set()).add(wi)
for p, pos in update.items(): # new pairs enter the heap
if pair_counts[p] > 0:
heapq.heappush(heap, [-pair_counts[p], p[0], p[1], seq, pos])
seq += 1
# ... vocab from id_to_tok; specials become added tokens with ids 0, 1, ...

Run it on the hand example. wc is {hug: 2, pug: 1, hugs: 1}. The heap’s top is (u, g) with count 4. Merging it touches all three words and pushes (h, ug) with 3, (p, ug) with 1, and (ug, s) with 1. At step 3, (p, ug) is [-1, 79, 256, ...] and (hug, s) is [-1, 257, 82, ...]; the heap pops the smaller list, (p, ug).

Why is lazy deletion correct? A popped entry whose count went down (a neighbor merged away part of it) is pushed back with its real count. An entry whose count is current is the true maximum: a pair whose count fell still has its old, higher entry, which pops first and gets corrected, and a pair whose count rose was pushed fresh in update.

The rank loop, with the cache and the dropped-byte rule:

def _bpe(self, word: str) -> list[int]:
hit = self._cache.get(word)
if hit is not None:
return hit
# A byte symbol the vocab lacks is dropped, as Hugging Face does when
# unk_token is null (SmolLM2 has no symbol for six control bytes).
syms = [c for c in word if c in self.vocab]
while len(syms) > 1:
best_rank, best_k = None, -1
for k in range(len(syms) - 1):
r = self.ranks.get((syms[k], syms[k + 1]))
# strict <: on equal ranks the first (leftmost) k is kept
if r is not None and (best_rank is None or r < best_rank):
best_rank, best_k = r, k
if best_rank is None:
break # no ranked pair left
syms[best_k : best_k + 2] = [syms[best_k] + syms[best_k + 1]]
ids = [self.vocab[s] for s in syms] # (a KeyError here means a broken file: ValueError)
self._cache[word] = ids
return ids
def encode(self, text: str, add_special: bool = False) -> list[int]:
ids: list[int] = []
segs = _split_added(self._trie, text) if self.added else [(text, None)]
for seg, aid in segs:
if aid is not None:
ids.append(aid) # an added token: one id, no BPE
continue
for p in self._pieces(seg): # the declared steps: Digits, then ByteLevel
ids += self._bpe("".join(self._b2u[b] for b in p.encode("utf-8")))
return ids

self.ranks is built with reversed(list(enumerate(merges))) so that if a file lists the same merge twice, the first (lowest) rank wins.

Bytes first, UTF-8 once:

def decode(self, ids: Sequence[int], skip_special: bool = False) -> str:
buf = bytearray()
for i in check_ids(ids, self.vocab_size):
if i in self._added_by_id:
content = self._added_by_id[i]
if not (skip_special and content in self.special_ids):
buf += content.encode("utf-8")
else:
buf += bytes(self._u2b[c] for c in self._id_to_tok[i]) # symbol -> byte (M05.2)
return buf.decode("utf-8", "replace")

The loader is a list of refusals followed by one constructor call.

@classmethod
def from_hf_json(cls, tokenizer_json: str) -> "BPETokenizer":
# ... json.load; version must be "1.0"
if doc.get("normalizer") is not None:
raise ValueError("normalizer: a byte-level BPE file has none (null)")
model = doc.get("model") or {}
# Older files (GPT-2's) omit model.type; a BPE model is the one with merges.
mtype = model.get("type", "BPE" if "merges" in model else None)
# ... must be "BPE"
for key, ok in (
("dropout", (None,)),
("unk_token", (None,)),
("continuing_subword_prefix", (None, "")),
("end_of_word_suffix", (None, "")),
("fuse_unk", (False, None)),
("byte_fallback", (False, None)),
("ignore_merges", (False, None)),
):
if model.get(key) not in ok:
raise ValueError(f"model.{key}: {model.get(key)!r} is outside the subset")
# ... merges as "a b" strings or [a, b] lists; added tokens with single_word,
# lstrip, rstrip all false; post_processor and decoder ByteLevel or null
return cls(model["vocab"], merges, added, doc.get("pre_tokenizer"))

The constructor then validates the pre-tokenizer (_check_pre_tokenizer: Digits and ByteLevel only, ByteLevel last with use_regex true) and checks that ids are contiguous. save writes the same subset, so load is from_hf_json on the saved file.

The full solution is in pretok.py (which also has split_digits) and bpe.py (which also has from_gpt2, _pieces, _split_added, and save).

Consider cost first. Training is O(N)O(N) to count pre-tokens over NN characters, then per merge O(words touched⋅word length+log⁡H)O(\text{words touched} \cdot \text{word length} + \log H) for a heap of HH entries, instead of the naive O(all words)O(\text{all words}) recount per merge. Encoding a pre-token of LL symbols is O(L2)O(L^2) in the worst case (each merge rescans), but pre-tokens are short and the cache makes repeated words O(1)O(1).

The alternatives are each worse in a specific way. The regex module would handle \p{L} directly, but it adds a dependency and hides the (?!\S) backtracking that the scanner makes explicit; the Rust port has the same problem, and a scanner ports line for line. An eager heap with decrease-key needs an indexed heap; the lazy version is simpler and amortizes to the same cost. A rank-free encoder that merges greedily left to right is faster to write and wrong. Ignoring unknown fields in tokenizer.json would load more files and silently produce wrong ids for them.

Two tradeoffs are accepted. The cache is unbounded, which is fine for a training or evaluation process but not for a long-lived server; the Rust version bounds it. And add_special is accepted for protocol compatibility but adds nothing, since the subset has no template post-processor.

A correct solution is held to a long list of checks. Beyond the hand-worked merges, encoding, and pre-tokens, min_freq values of 2, 3, and 4 must give 2, 2, and 1 merges on the hand example (a count equal to min_freq still merges). Five training configurations on a 3 KB story must give Hugging Face’s merges in order, so the capstone vocabulary is the standard algorithm. <|endoftext|> must be id 0 and never part of a merge; a vocab_size below 256 plus specials, or a min_freq of 0, must be ValueError. On 300 random texts, pre-token pieces must join back with none empty and obey the (?!\S) rule, and both the individual and run modes of Digits must work.

The golden checks compare against real files. With GPT-2’s tokenizer.json, ids on 300 strings must equal Hugging Face’s and tiktoken’s, and from_gpt2 on vocab.json and merges.txt (derived from the tokenizer.json) must give the same ids. With SmolLM2’s file (the Digits sequence, 17 added tokens, 300 strings) ids must equal Hugging Face’s, and "a\x04b" must encode to a, b. Decoding every oracle case, with and without skip_special, must match. The three bytes of 日 must decode together, while one alone is U+FFFD. Added tokens split first, leftmost longest: <|im_start|> is id 1, <|im is text, and GPT-2’s end-of-text is 50256. Swapping two ranks must change "abc"; "aaa" is aa a and "aaaa" is aa aa, which the Rust port must reproduce id for id. A trained tokenizer must round-trip 200 random texts, load(save(t)) must keep merges, specials, and ids, encode_batch must equal encode per text (empty texts included), a vocabulary with a gap is ValueError, and each of a normalizer, dropout, an unknown token, byte fallback, and a template is ValueError.

Warning: Merging left to right, or picking the rightmost of equal ranks, gives ids that differ from GPT-2’s on most words longer than three bytes. _bpe picks the minimum rank with strict <, so the leftmost wins ties.

The other classic mistakes:

PitfallSymptomHow the solution avoids it
str.isspace or \s of another regex flavor; a white-space run taken whole before a wordpre-tokens differ on control characters and runs of spaces; here loses its spaceWHITE_SPACE is the Unicode property; the run gives back its last character before a non-space
decoding token by tokenevery character split across tokens becomes U+FFFDdecode builds one bytearray and decodes it once
ignoring a field the loader does not implementa normalizer or the declared Digits step is skipped and ids are silently wrongfrom_hf_json raises for every field outside the subset; _pieces runs the declared steps
training on special-token textmerges such as <| and endoftext waste the vocabulary_split_added cuts specials out before pre-tokenizing
treating a byte the vocabulary lacks as an errorSmolLM2 raises on a control byte where Hugging Face drops it_bpe filters symbols not in the vocabulary
ties broken by the largest ids, or counting each pre-token oncemerges diverge from Hugging Face’s after a few stepsheap key (-count, left, right); every count is weighted by counts[wi]
Latin-1 instead of the byte mapé and every non-ASCII text get the wrong symbolsbytes go through bytes_to_unicode() in training and encoding

This tokenizer is load-bearing for much of the book. The Rust fast BPE reimplements encode and decode as tl-tok, and its differential tests compare its ids with this solution’s on the fixtures and a TinyStories sample. Tokenizer metrics measures GPT-2 and SmolLM2, loaded by from_hf_json, against Hugging Face’s numbers. Tokenize and pack in Data Engineering tokenizes corpus files through the Rust tokenizer process and shared file fixtures. The Llama-family model and the engine load SmolLM2 with it; in KV cache and generate, IncrementalDecoder streams byte-level ids through Tokenizer.decode, holding back text that ends in a partial UTF-8 character; the TinyStories capstone trains its 4096-token vocabulary with this trainer; and Whisper decoding consumes this implementation.

Each piece has a production counterpart:

  • train with a lazy heap corresponds to the Hugging Face BpeTrainer, which adds parallel word counting and progress bars around the same heap with a Word::merge delta (tokenizers/src/models/bpe/trainer.rs).
  • encode by rank corresponds to tiktoken, a rank-only encoder over a byte-pair map written in Rust with a regex pre-split (tiktoken/src/lib.rs, byte_pair_merge).
  • pretokenize_gpt2 corresponds to the fancy-regex / onig pre-tokenizer, the real regex engine, plus cl100k-style splits with numbers of 1 to 3 digits (tokenizers/src/pre_tokenizers/byte_level.rs).
  • from_hf_json corresponds to tokenizers’ Tokenizer::from_file, with the full pipeline of normalizers, offsets, truncation, and padding (tokenizers/src/tokenizer/serialization.rs).
  • The per-pre-token merge cache corresponds to the tokenizers BPE cache, bounded and shared across threads (tokenizers/src/utils/cache.rs); the Rust section builds its own version.
  • Sennrich, Haddow, and Birch, Neural Machine Translation of Rare Words with Subword Units (2016).
  • Radford et al., Language Models are Unsupervised Multitask Learners (GPT-2, 2019), section 2.2.
  • Pre-token: a piece of text produced by the pre-tokenizer; no merge may cross its boundary.
  • Byte-level: the base alphabet is the 256 bytes (through the byte map), so every text encodes and there is no unknown token.
  • Merge rank: a merge’s position in the learned merges list; lower ranks were learned earlier and apply first when encoding.
  • Lazy deletion: keeping stale heap entries and correcting them when popped instead of updating them in place.
  • Added token: a vocabulary entry matched in text before pre-tokenization, such as <|im_start|>.
  • White_Space: the Unicode property that \s means in the GPT-2 regex, which differs from str.isspace.
  • BPE training is a loop: count adjacent pairs inside pre-tokens, weighted by how often each pre-token occurs, merge the most frequent pair, and update only the counts the merge touched; ties go to the pair with the smaller ids.
  • Encoding replays the learned merges by rank, lowest rank first and leftmost on a tie, which is not the same as merging left to right.
  • Byte-level means the alphabet is the 256 bytes, so every text encodes with no unknown token, and decoding joins token bytes before UTF-8 decoding.
  • The GPT-2 pre-tokenizer regex can be written by hand over unicodedata, and only an exact transcription matches the real ids: Unicode White_Space is not str.isspace.
  • The loader reads GPT-2’s and SmolLM2’s real tokenizer.json unchanged, reproduces Hugging Face’s ids on 300 hard strings, and refuses fields it does not implement.
  1. Why does the scanner treat only U+0020 as the optional leading space, and not any white space?
    Answer The regex alternatives 2 to 4 begin with ` ?`, a literal space, not `\s?`. A tab or newline before a word is therefore its own pre-token (alternative 5 or 6), and the word starts without it. Accepting any white space would merge `\tword` into one pre-token and change the ids.
  2. In _merge_word, why is the change for the merged pair (a,b)(a, b) itself not recorded?
    Answer That pair was just popped from the heap and turned into a token; the trainer never needs its count again. The changes that matter are the neighboring pairs whose counts move, $(x, a) \to (x, ab)$ and $(b, y) \to (ab, y)$. This matches Hugging Face's `Word::merge`, which is necessary for the merges to agree one for one.
  3. Why is the heap key (-count, left id, right id), and what would break with (-count, right id, left id)?
    Answer Python's heap is a min-heap, so `-count` pops the largest count first, and the next two fields break ties by the smallest ids compared as (left, right), which is the Hugging Face rule. Swapping them would choose a different pair on ties; in the hand example, `(hug, s)` with right id 82 would beat `(p, ug)` with right id 256, and every later merge would diverge.
  4. Why must encoding merge by lowest rank and not left to right?
    Answer Training applied merges in rank order across the whole corpus: by the time a later merge was learned, every earlier one had already been applied everywhere. Replaying by rank reproduces the segmentation training produced. Left to right gives `ab c` for `"abc"` when `(b, c)` was learned first, which training would never produce.
  5. What goes wrong if decode converted each token to text separately and joined the strings?
    Answer A byte-level token can end in the middle of a multi-byte character. Decoding its bytes alone yields U+FFFD, and so does the next token's half, so every character split across tokens is replaced. Joining all bytes first and decoding once gives the original character.