B.5 Pass 3: Tokens and Data
A model sees only token ids, and every id comes from a tokenizer and a corpus. If a single id differs from what a published checkpoint expects, the model reads the wrong embedding row; if the corpus is not reproducible, auditable, and free of evaluation text, no later evaluation number can be trusted. This pass builds both sides carefully: tokenizers that match GPT-2 and SmolLM2 exactly in Python and in an independent Rust implementation, and a corpus pipeline that fetches, filters, deduplicates, decontaminates, scrubs, shards, licenses, and tokenizes a corpus deterministically.
Concepts from other sections (39)
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.
- n-gram model: a language model that conditions each token only on the previous tokens. Read more: 8.1 n-gram language model with interpolated modified Kneser-Ney
- Byte-level: the base alphabet is the 256 bytes (through the byte map), so every text encodes and there is no unknown token. Read more: 7.2 Byte-level BPE
- GPT-2 byte map: the bijection from the 256 bytes to printable code points that gives byte tokens their strings. Read more: 7.1 The tokenizer protocol and the char tokenizer
- Bijection: a function that is both injective and surjective, and so has an inverse. Read more: 11.3.4 Relations, functions, and validation in Mathematics
- Bits per byte (bpb): , total information divided by the UTF-8 length; comparable across tokenizers. Read more: 17.2 Perplexity, bits per byte, NLL accumulator in Mathematics
- Byte map: GPT-2’s fixed bijection from the 256 byte values to 256 visible, non-whitespace characters. Read more: 11.2 Injective, surjective, bijective: the GPT-2 byte map in Mathematics
- Trie: a rooted tree whose edges carry characters so that each node spells the string on its path. Read more: 12.2 Trees and tries, longest-prefix match in Mathematics
- Longest-prefix match: finding the longest key that is a prefix of the text at a given position. Read more: 12.2 Trees and tries, longest-prefix match in Mathematics
- Perplexity: , the geometric mean of ; the effective number of choices per token. Read more: 17.2 Perplexity, bits per byte, NLL accumulator in Mathematics
- Negative log-likelihood (NLL): , the surprise of the true token under the model, in nats. Read more: 17.2 Perplexity, bits per byte, NLL accumulator in Mathematics
- Maximum-likelihood estimate (MLE): the distribution that maximizes the log-likelihood; for counts, the frequency . Read more: 13.3 MLE, Laplace, absolute discounting in Mathematics
- Laplace smoothing: adding one pseudo-count per vocabulary entry, . Read more: 13.10 Probability problem set, part b: joint distributions, covariance, MLE in Mathematics
- Absolute discounting: subtracting a fixed from each seen count and giving the freed mass to a backoff distribution. Read more: 13.3 MLE, Laplace, absolute discounting in Mathematics
- Tokenizer protocol: the six-member structural interface (
vocab_size,special_ids,unk_id,encode/decode,token_to_id/id_to_token,save/load) every tokenizer satisfies. Read more: 7.1 The tokenizer protocol and the char tokenizer - Basic tokenizer: BERT’s normalizer and pre-tokenizer: clean, space CJK, strip accents, lowercase, split on white space and punctuation. Read more: 7.3 WordPiece
- Greedy longest match: segmenting a word by repeatedly taking the longest vocabulary entry that matches at the current position. Read more: 7.3 WordPiece
- Viterbi: the dynamic program for the highest-scoring path through the lattice. Read more: 7.4 The Unigram language model tokenizer
- Backward-shift deletion: moving the following run back one slot after a removal, instead of leaving a tombstone. Read more: 15.5 A Robin Hood hash map in Programming and Algorithms
- Tombstone: a DELETED control byte that a probe must walk past. Read more: 15.2 A Swiss table in Programming and Algorithms
- Binary heap: a complete binary tree in an array in which no child comes out before its parent. Read more: 15.6 A binary heap with lazy deletion in Programming and Algorithms
- Lazy deletion: marking an item dead and discarding it only when it reaches the root. Read more: 15.6 A binary heap with lazy deletion in Programming and Algorithms
- Streaming decoder: a decoder that emits only complete UTF-8 after each token and holds back up to 3 bytes of an unfinished character. Read more: 7.5 Fast BPE in Rust
- PII policy: the per-source rule from the ledger:
scrub,drop, ornone. Read more: 5.5 PII scrub with typed placeholders and audit spans in Data Engineering - Bloom filter: a bit array with hashes per key that never misses an inserted key and has a tunable false-positive rate. Read more: 12.5 Discrete math 2 problem set, part b: trees, birthday bounds, MinHash and LSH, Bloom filters, recurrences in Mathematics
- semaphore: a counter of permits that bounds how many coroutines are inside a section at once. Read more: 1.8 Python asyncio: event loop, tasks, cancellation, bounded concurrency in Programming and Algorithms
- Sort-merge confirm: sorting candidate occurrences so equal digests form runs, keeping the first of each run. Read more: 5.3 Exact dedup: paragraph hashes, a Bloom screen, and a sort-merge confirm in Data Engineering
- MinHash: estimating Jaccard similarity by how often the minima of random permutations agree. Read more: 12.5 Discrete math 2 problem set, part b: trees, birthday bounds, MinHash and LSH, Bloom filters, recurrences in Mathematics
- Union-find: a disjoint-set forest that merges confirmed pairs into clusters; here rooted at the smallest id. Read more: 5.4 Near-duplicate dedup: MinHash, LSH, union-find, and decontamination in Data Engineering
- Typed placeholder: a token such as
<EMAIL>that replaces a match and names its kind. Read more: 5.5 PII scrub with typed placeholders and audit spans in Data Engineering - Audit span: a record of kind and start and end offsets into the original text, never the matched text. Read more: 5.5 PII scrub with typed placeholders and audit spans in Data Engineering
- Cosine similarity: the inner product divided by both lengths; depends only on direction. Read more: 9.6 Inner products, projections, cosine similarity, and top-k in Mathematics
- Inner product: . Read more: 9.6 Inner products, projections, cosine similarity, and top-k in Mathematics
- Mutual information: , the nats that knowing one variable saves about the other. Read more: 17.3 Mutual information, PMI, and PPMI in Mathematics
- Pointwise mutual information (PMI): for a single pair; positive above chance, for unseen pairs. Read more: 17.3 Mutual information, PMI, and PPMI in Mathematics
- PPMI: positive pointwise mutual information, . Read more: 8.3 word2vec skip-gram with negative sampling, and the PPMI-SVD baseline
- modified Kneser-Ney: Kneser-Ney with three discounts per order (, , ) estimated from counts of counts. Read more: 8.1 n-gram language model with interpolated modified Kneser-Ney
- continuation count: , the number of distinct symbols seen immediately before . Read more: 8.1 n-gram language model with interpolated modified Kneser-Ney
- skip-gram: a model that predicts the context words around each center word. Read more: 8.3 word2vec skip-gram with negative sampling, and the PPMI-SVD baseline
- negative sampling: replacing the softmax with logistic classification of the real context against words drawn from a noise distribution. Read more: 8.3 word2vec skip-gram with negative sampling, and the PPMI-SVD baseline
With tokens and data in place, the pass ends with the first real language models beyond the bigram: a Kneser-Ney n-gram model, the strongest count-based baseline, and Bengio’s neural probabilistic language model, the first model whose embeddings and hidden layer are learned. The engine keeps serving the bigram throughout.
Learning objectives
- Explain how byte-level BPE, WordPiece, and Unigram tokenizers train and encode, and why the GPT-2 byte map is a bijection.
- Explain how the Rust tokenizer reproduces the Python one exactly and why batching speeds it up.
- Explain each stage of the corpus pipeline, from fetch to packed token files, and why its output hash is independent of the worker count.
- Explain how licensing, PII, and decontamination are recorded and enforced.
- Explain how the n-gram and neural language models are trained and scored in bits per byte.
Before you begin
Section titled “Before you begin”This pass needs Pass 2, whose autograd trains the neural language model and whose token stream reads the files this pass writes.
In this pass
Section titled “In this pass”The pass opens with the discrete mathematics and probability the tokenizers and models need, then builds the tokenizers, the data structures and tests behind them, the corpus pipeline, and finally the language models:
- Injective, surjective, bijective: the GPT-2 byte map. Afterwards you can explain why the GPT-2 byte map is a bijection and how it maps bytes to printable characters.
- Trees and tries, longest-prefix match. Afterwards you can explain how a trie finds the longest-prefix match in one pass.
- Discrete math 2 problem set, part b: trees, birthday bounds, MinHash and LSH, Bloom filters, recurrences. Afterwards you can work every problem in the set and explain each answer.
- Perplexity, bits per byte, NLL accumulator. Afterwards you can explain how perplexity and bits per byte follow from the summed NLL and why the accumulator counts bytes, not tokens.
- Categorical sampling: inverse CDF, Gumbel-max, alias method. Afterwards you can explain how inverse-CDF, Gumbel-max, and the alias method each draw from a categorical distribution and what each costs.
- MLE, Laplace, absolute discounting. Afterwards you can explain how MLE, Laplace smoothing, and absolute discounting assign probability to unseen events.
- Probability problem set, part b: joint distributions, covariance, MLE. Afterwards you can work every problem in the set and explain each answer.
- The tokenizer protocol and the char tokenizer. Afterwards you can explain the tokenizer protocol and how the char tokenizer round-trips text.
- Byte-level BPE. Afterwards you can trace how byte-level BPE pre-tokenizes, trains merges, and loads a Hugging Face tokenizer to match GPT-2.
- WordPiece. Afterwards you can explain how WordPiece’s basic tokenizer splits text and how greedy longest match picks subwords.
- The Unigram language model tokenizer. Afterwards you can explain how the Unigram LM trains with EM, segments with Viterbi, and samples subwords.
- Tokenizer metrics. Afterwards you can explain what each tokenizer metric measures and how it is computed.
- A Robin Hood hash map. Afterwards you can explain how Robin Hood hashing bounds probe lengths and why backward-shift deletion needs no tombstones.
- A binary heap with lazy deletion. Afterwards you can explain how a binary heap with lazy deletion skips stale entries.
- Fast BPE in Rust. Afterwards you can explain how the Rust fast BPE applies merges, how the byte tokenizer works, and how the streaming decoder holds back incomplete UTF-8.
- Property-based tests with Hypothesis, proptest, rapid, and ol_prop.h. Afterwards you can explain which properties the tokenizers and data structures satisfy and how a property-based test shrinks a failing case.
- Data licensing and the ledger. Afterwards you can explain what the licensing ledger records for each source and why.
- Privacy and PII policy. Afterwards you can explain the privacy and PII policy and which data it lets the pipeline keep.
- A Bloom filter. Afterwards you can derive a Bloom filter’s false-positive rate and explain how the number of hashes is chosen.
- Python asyncio: event loop, tasks, cancellation, bounded concurrency. Afterwards you can explain how asyncio schedules tasks, how cancellation propagates, and how a semaphore bounds concurrency.
- Async fetch with resume, checksums, and license capture. Afterwards you can explain how the async fetcher resumes downloads, verifies checksums, and captures licenses.
- Extract, normalize, and quality filters. Afterwards you can trace how the generator stages extract, normalize, and quality-filter documents.
- Exact dedup: paragraph hashes, a Bloom screen, and a sort-merge confirm. Afterwards you can explain how paragraph hashes, a Bloom screen, and a sort-merge confirm find exact duplicates.
- Near-duplicate dedup: MinHash, LSH, union-find, and decontamination. Afterwards you can explain how MinHash, LSH, and union-find group near-duplicates and how decontamination removes eval overlap.
- PII scrub with typed placeholders and audit spans. Afterwards you can explain how the PII scrub substitutes typed placeholders and records audit spans.
- Parquet shards, the manifest, and the document-hash split. Afterwards you can explain how Parquet shards and the manifest are written and why the split is decided by document hash.
- Tokenize and pack to llm.c .bin. Afterwards you can explain how documents are tokenized and packed into llm.c .bin shards.
- Licensing ledger verification and the datasheet. Afterwards you can explain how the ledger is verified against the corpus and what the datasheet reports.
- SVD, Eckart-Young low rank, and least squares. Afterwards you can explain why truncated SVD is the best low-rank approximation (Eckart-Young) and how it solves least squares.
- Inner products, projections, cosine similarity, and top-k. Afterwards you can explain how projections and cosine similarity relate to inner products and how top-k is selected.
- Linear algebra problem set, part b: inner products, SVD and low rank, matmul accounting and the roofline. Afterwards you can work every problem in the set and explain each answer.
- Mutual information, PMI, and PPMI. Afterwards you can explain how mutual information, PMI, and PPMI are computed from co-occurrence counts.
- Information theory problem set, part b: coding and Huffman, mutual information, maximum entropy, rate-distortion. Afterwards you can work every problem in the set and explain each answer.
- n-gram language model with interpolated modified Kneser-Ney. Afterwards you can explain how interpolated modified Kneser-Ney uses continuation counts and three discounts.
- Bengio’s neural probabilistic language model. Afterwards you can explain how Bengio’s NPLM turns a context window of embeddings into a next-token distribution.
- word2vec skip-gram with negative sampling, and the PPMI-SVD baseline. Afterwards you can explain how skip-gram with negative sampling trains embeddings and how the PPMI-SVD baseline relates to it.
- What the System Does After Pass 3. Afterwards you can explain how the corpus pipeline builds a licensed, deduplicated corpus and how the tokenizers and n-gram and neural language models run on it through the CLI.
The stages are also in this pass’s manifest, path.tsv. Pass 4 adds models that carry state across a whole sequence.
Stages
Section titled “Stages”Generated from path.tsv.
| Stage | Read | Done when |
|---|---|---|
| 1 | Injective, surjective, bijective; the GPT-2 byte map 11.2 Injective, surjective, bijective: the GPT-2 byte map | You can explain why the GPT-2 byte map is a bijection and how it maps bytes to printable characters (M05.2). |
| 2 | Trees and tries, longest-prefix match 12.2 Trees and tries, longest-prefix match | You can explain how a trie finds the longest-prefix match in one pass (M06.2). |
| 3 | Solve set: trees, birthday bounds, Jaccard/MinHash/LSH S-curve, Bloom FP rate, recurrences 12.5 Discrete math 2 problem set, part b: trees, birthday bounds, MinHash and LSH, Bloom filters, recurrences | You can work every S-M06b problem and explain each answer. |
| 4 | Perplexity, bits per byte, NLL accumulator 17.2 Perplexity, bits per byte, NLL accumulator | You can explain how perplexity and bits per byte follow from the summed NLL and why the accumulator counts bytes, not tokens (M11.2). |
| 5 | Categorical sampling: inverse CDF, Gumbel-max, alias method 13.2 Categorical sampling: inverse CDF, Gumbel-max, alias method | You can explain how inverse-CDF, Gumbel-max, and the alias method each draw from a categorical distribution and what each costs (M07.1). |
| 6 | MLE, Laplace, absolute discounting 13.3 MLE, Laplace, absolute discounting | You can explain how MLE, Laplace smoothing, and absolute discounting assign probability to unseen events (M07.2). |
| 7 | Solve set: joint and covariance, MLE 13.10 Probability problem set, part b: joint distributions, covariance, MLE | You can work every S-M07b problem and explain each answer. |
| 8 | Tokenizer protocol, char tokenizer 7.1 The tokenizer protocol and the char tokenizer | You can explain the tokenizer protocol and how the char tokenizer round-trips text (L1.1). |
| 9 | Byte-level BPE (GPT-2 compatible): pre-tokenizer, trainer, HF loader 7.2 Byte-level BPE | You can trace how byte-level BPE pre-tokenizes, trains merges, and loads a Hugging Face tokenizer to match GPT-2 (L1.2). |
| 10 | WordPiece (BERT basic tokenizer + greedy longest match) 7.3 WordPiece | You can explain how WordPiece’s basic tokenizer splits text and how greedy longest match picks subwords (L1.3). |
| 11 | Unigram LM tokenizer (EM, Viterbi, subword sampling) 7.4 The Unigram language model tokenizer | You can explain how the Unigram LM trains with EM, segments with Viterbi, and samples subwords (L1.4). |
| 12 | Tokenizer metrics 7.6 Tokenizer metrics | You can explain what each tokenizer metric measures and how it is computed (L1.6). |
| 13 | Robin Hood hash map with backward-shift deletion 15.5 A Robin Hood hash map | You can explain how Robin Hood hashing bounds probe lengths and why backward-shift deletion needs no tombstones (ds.05). |
| 14 | Binary heap with lazy deletion 15.6 A binary heap with lazy deletion | You can explain how a binary heap with lazy deletion skips stale entries (ds.06). |
| 15 | Rust fast BPE (tl-tok), byte tokenizer, streaming decoder 7.5 Fast BPE in Rust | You can explain how the Rust fast BPE applies merges, how the byte tokenizer works, and how the streaming decoder holds back incomplete UTF-8 (L1.5). |
| 16 | Property-based tests (R4) with Hypothesis, proptest, rapid, and ol_prop.h 3.2 Property-based tests with Hypothesis, proptest, rapid, and ol_prop.h | You can explain which properties the tokenizers and data structures satisfy and how a property-based test shrinks a failing case (craft.04). |
| 17 | Data licensing and the ledger 33.1 Data licensing and the ledger | You can explain what the licensing ledger records for each source and why (ethics.01). |
| 18 | Privacy and PII policy 34.1 Privacy and PII policy | You can explain the privacy and PII policy and which data it lets the pipeline keep (ethics.02). |
| 19 | Bloom filter 15.8 A Bloom filter | You can derive a Bloom filter’s false-positive rate and explain how the number of hashes is chosen (ds.08). |
| 20 | Python asyncio: event loop, tasks, cancellation, bounded concurrency 1.8 Python asyncio: event loop, tasks, cancellation, bounded concurrency | You can explain how asyncio schedules tasks, how cancellation propagates, and how a semaphore bounds concurrency (lang.08). |
| 21 | Async fetch with resume, checksums, license capture 5.1 Async fetch with resume, checksums, and license capture | You can explain how the async fetcher resumes downloads, verifies checksums, and captures licenses (data.01). |
| 22 | Extract, normalize, quality filters (generator stages) 5.2 Extract, normalize, and quality filters | You can trace how the generator stages extract, normalize, and quality-filter documents (data.02). |
| 23 | Exact dedup: paragraph hashes, Bloom screen, sort-merge confirm 5.3 Exact dedup: paragraph hashes, a Bloom screen, and a sort-merge confirm | You can explain how paragraph hashes, a Bloom screen, and a sort-merge confirm find exact duplicates (data.03). |
| 24 | Near-duplicate dedup: MinHash, LSH, union-find, and decontamination 5.4 Near-duplicate dedup: MinHash, LSH, union-find, and decontamination | You can explain how MinHash, LSH, and union-find group near-duplicates and how decontamination removes eval overlap (data.04). |
| 25 | PII scrub with typed placeholders and audit spans 5.5 PII scrub with typed placeholders and audit spans | You can explain how the PII scrub substitutes typed placeholders and records audit spans (data.05). |
| 26 | Parquet shards, manifest, and the document-hash split 5.6 Parquet shards, the manifest, and the document-hash split | You can explain how Parquet shards and the manifest are written and why the split is decided by document hash (data.06). |
| 27 | Tokenize and pack to llm.c .bin 5.7 Tokenize and pack to llm.c .bin | You can explain how documents are tokenized and packed into llm.c .bin shards (data.07). |
| 28 | Licensing ledger verification and the datasheet 5.8 Licensing ledger verification and the datasheet | You can explain how the ledger is verified against the corpus and what the datasheet reports (data.08). |
| 29 | SVD, Eckart-Young low rank, and least squares 9.5 SVD, Eckart-Young low rank, and least squares | You can explain why truncated SVD is the best low-rank approximation (Eckart-Young) and how it solves least squares (M03.5). |
| 30 | Inner products, projections, cosine similarity, and top-k 9.6 Inner products, projections, cosine similarity, and top-k | You can explain how projections and cosine similarity relate to inner products and how top-k is selected (M03.6). |
| 31 | Linear algebra problem set, part b: inner products, SVD and low rank, matmul accounting and the roofline 9.8 Linear algebra problem set, part b: inner products, SVD and low rank, matmul accounting and the roofline | You can work every S-M03b problem and explain each answer. |
| 32 | Mutual information, PMI, and PPMI 17.3 Mutual information, PMI, and PPMI | You can explain how mutual information, PMI, and PPMI are computed from co-occurrence counts (M11.4). |
| 33 | Information theory problem set, part b: coding and Huffman, mutual information, maximum entropy, rate-distortion 17.5 Information theory problem set, part b: coding and Huffman, mutual information, maximum entropy, rate-distortion | You can work every S-M11b problem and explain each answer. |
| 34 | n-gram language model with interpolated modified Kneser-Ney 8.1 n-gram language model with interpolated modified Kneser-Ney | You can explain how interpolated modified Kneser-Ney uses continuation counts and three discounts (L2.1). |
| 35 | Bengio’s neural probabilistic language model 8.2 Bengio’s neural probabilistic language model | You can explain how Bengio’s NPLM turns a context window of embeddings into a next-token distribution (L2.2). |
| 36 | word2vec skip-gram with negative sampling, and the PPMI-SVD baseline 8.3 word2vec skip-gram with negative sampling, and the PPMI-SVD baseline | You can explain how skip-gram with negative sampling trains embeddings and how the PPMI-SVD baseline relates to it (L2.3). |
| 37 | What the system does after Pass 3 B.5.1 What the System Does After Pass 3 | You can explain how the corpus pipeline builds a licensed, deduplicated corpus and how the tokenizers and n-gram and neural language models run on it through the CLI. |