Pair counts and the merge loop
Agent-Assisted Engineering Check
Prerequisites
Complete Day 38. If blocked, use the Day 35 recovery link for probability review.
Learning Objective
Trace the BPE training loop — count adjacent pairs, merge the most frequent pair into a fresh id, repeat — and reproduce a verified merge table by hand.
Instructions
Start from the byte ids of 'aaabdaaabac': [97,97,97,98,100,97,97,97,98,97,99]. Required work: each round, count every adjacent pair; the most frequent pair receives the next free id (256, then 257, and so on); ties break by first occurrence, matching Python's max() over an insertion-ordered dict. An empty string '' has no pairs and returns empty immediately; Unicode strings decompose into UTF-8 bytes and follow the identical merge loop. Write the merge trace, then verify with a small script. Optional work: continue to a fourth merge and name the token it creates.
Practice & Prediction Log
Hand-trace 3 merges on 'aaabdaaabac': write the pair counts each round, the chosen pair, and the new sequence. Predict the final token count and vocabulary size before checking.
Round 1: (97,97) occurs 4 times → id 256; sequence becomes [256,97,98,100,256,97,98,97,99]. Round 2: (256,97) and (97,98) tie at 2 — first occurrence (256,97) wins → 257. Round 3: (257,98) occurs 2 times → 258.
Verified trace: merges {(97,97)→256 ('aa'), (256,97)→257 ('aaa'), (257,98)→258 ('aaab')}; final ids [258,100,258,97,99] read as 'aaab','d','aaab','a','c' — 11 bytes compressed to 5 tokens (2.2×), vocabulary 256+3=259.
Your handwritten trace reproduces all three merges, the first-occurrence tie-break, the final token list, and the 259 vocabulary size without consulting the reference.
Authoritative Source Resources & Provenance
minbpe: Minimal Byte-Pair Encoding tokenizer (Let's build the GPT Tokenizer)
Relevant Section / Range: minbpe/base.py: get_stats pair counting and merge loop; minbpe/basic.py BasicTokenizer
Verified Version & Date: commit 1acefe8 (Verified: 2026-10-30)
The reference minimal implementation whose pair-count and merge loop the lesson traces by hand.
Basic Python and Day 37 bytes/code points
The lesson re-implements the merge loop locally and prints a verified merge trace.
Compared Candidate Sources & Selection Analysis (3)
Production BPE with Rust core and regex pre-tokenization; selected as the production contrast source rather than the primary teaching implementation.
High-quality BPE coverage but frames tokenization through library APIs; minbpe exposes the raw merge loop for hand tracing.
Industrial tokenizer trainer supporting BPE and unigram models; C++ core hides the teaching mechanics.
Neural Machine Translation of Rare Words with Subword Units
Relevant Section / Range: Section 3: byte-pair encoding adapted from compression to subword segmentation
Verified Version & Date: ACL 2016 (Verified: 2026-10-30)
Primary source that introduced BPE as a subword segmentation algorithm for neural sequence models.
Basic probability and text processing
The lesson teaches the complete merge algorithm with worked traces independent of the paper text.
Compared Candidate Sources & Selection Analysis (2)
Related likelihood-scored variant used in BERT; paywalled and less aligned with frequency-greedy teaching merges.
Strong probabilistic alternative segmentation; deferred because deterministic BPE merges are more hand-traceable at this stage.