Tokenization, Corpus Identity & Packed Data
The model sees token identities and loss masks rather than raw text. This section makes every transformation from bytes to training examples deterministic, reversible where promised and auditable across splits.
Section 1 of 5 · 7 units · about 31 hours
By the end you can Implement and serialize deterministic byte-level BPE, pass adversarial round-trip, merge-tie, special-token and chunk-boundary tests, then publish a small packed split with provenance and an automated contamination check.
Units
- Text as bytes: Unicode, encodings and normalization3h
- Byte-level BPE: training, merges and tie-breaking6h
- Tokenizer design choices: vocabulary size, Unigram and special tokens3h
- A tokenizer you can trust: round trips, identity and streaming6h
- From documents to training examples: packing and loss masks4h
- Provenance, deduplication and contamination4h
- Filtering, mixtures and deterministic shards5h
Build it
Mini project · about 12 to 16 hours
Tokenizer and corpus audit pack
Turn a small public-domain corpus into a packed, auditable training split with your own byte-level BPE. Every packed token traces back to a source document, and every claim about the split, such as no duplicates across splits and no planted evaluation text in training, is backed by a test or a report you can rerun.
- 1Start one repository for the whole stage: a uv project with a
srcpackage and atestsfolder. Pick a small public-domain corpus under 20 MB, such as a few Project Gutenberg books or the TinyStories validation file. - 2Implement BPE training, encoding, decoding and streaming encoding. Connect them to
tests/adapters.pyin your clone of the CS336 assignment and passtests/test_train_bpe.pyandtests/test_tokenizer.py. - 3Serialize the tokenizer canonically with a content hash, and make loading fail on a changed merge list, special-token set or normalization policy.
- 4Assign raw documents to train and evaluation splits by a stable hash of their raw ID, before fitting the tokenizer or packing. Record source, raw hash, normalized hash and transform version for each document.
- 5Remove exact and near duplicates while keeping cluster lineage, and simulate a hash collision to show that your byte comparison catches it. Plant three evaluation passages in training documents, make your contamination probe find them, and review its false matches.
- 6Apply a reason-coded filter table with quality rules and synthetic PII and toxicity examples, and report document, byte and token yields per source.
- 7Pack the training split with EOS boundaries, a loss mask and per-token document IDs into ordered checksummed shards with a manifest, showing an interrupted run produces byte-identical shards. Check both masks against small hand-worked examples.
What you hand in
- The tokenizer package with its serialized artifact, identity hash and passing CS336 tokenizer tests.
- The packed training split: shards, manifest, per-token document IDs and loss masks.
- An audit report: split assignment, duplicate clusters, contamination hits with reviewed false matches, filter yields and per-source token shares.
- A resume test that interrupts packing mid-shard and compares checksums with an uninterrupted run.
How to check it
- CS336 `tests/test_train_bpe.py` and `tests/test_tokenizer.py`
uv run pytest tests/test_train_bpe.py tests/test_tokenizer.pyRun in your clone of the assignment with
tests/adapters.pycalling your package.test_encode_memory_usageis expected to fail. - Self-grade with the rubric below
How it's graded
Tokenizer correctness
Pass: Both CS336 tokenizer test files pass with no unexpected failures.
Great: Your own property tests also cover every two-chunk split of multilingual strings and partial special-token prefixes, and a reloaded artifact gives identical IDs.
Tokenizer identity
Pass: Loading rejects a changed merge list or special-token set.
Great: The hash covers a canonical serialization of vocabulary bytes, merges, special tokens, regex and normalization policy, and the error names what changed.
Splits and lineage
Pass: Splits are assigned before tokenizer fitting and packing, and every packed token maps to a document ID.
Great: Duplicate clusters that span splits are quarantined or reassigned, and the report counts them.
Packing and masks
Pass: Your mask tests pass on a hand-worked sequence that packs at least two documents.
Great: You explain which predictions the attention mask alone would not remove, and a deliberately wrong mask makes your tests fail.
Deduplication and contamination
Pass: The probe finds every planted passage and the report lists its hits.
Great: The report reviews false matches, states what the probe cannot detect, such as paraphrase or translation, and simulates a hash collision.
Reproducibility
Pass: Rerunning the pipeline gives the same manifest checksums.
Great: An interrupted run resumed from its saved cursor, RNG and partial pack gives byte-identical shards, and a changed config is rejected on resume.
Resources
- LLMs from Scratch companion - tokenization and data loadingSebastian Raschka
Work through the ch02 main notebook, 05_bpe-from-scratch and 04_bonus_dataloader-intuition. Rebuild byte-level BPE, serialization and packed windows in blank files, then add multilingual, control, whitespace and chunk-boundary properties.
- Stanford CS336 Assignment 1 - tokenizer contractStanford University
Read the Assignment 1 tokenizer handout and use the named unicode1, unicode2, train_bpe, tokenizer and tokenizer_experiments specifications plus public tests as an acceptance contract. Implement independently in your own repository and do not copy or modify course solutions.
- Modern LLM Notebook - tokenization and data experimentsWalking Labs
Run English notebooks 01-tokenizer-basics and 02-bpe-tokenizer. Use only the quality, deduplication and mixture examples from 15-data-engineering. Change the corpus and tie cases, then verify every quantitative claim independently.
- Tokenizer and Corpus Pipeline - reduced foundation sliceRoadmap-Final
Use the byte, identity, split, deduplication, contamination, packing, manifest and restart correctness requirements. Keep the corpus small and stop before distributed processing or internet-scale filtering.
- Hugging Face LLM Course - tokenizer translationHugging Face
After the blank-file tokenizer passes, use the tokenizer chapters to map vocabulary, special tokens, fast tokenizers and batching into library APIs. Do not replace the deterministic implementation gate with a pretrained tokenizer call.
- Python unicodedata - normalization formsPython Software Foundation
Read
unicodedata.normalizefor canonical and compatibility equivalence, andunidata_versionfor recording the normalization database version. - Hugging Face Datasets - streaming, sharding and resumeHugging Face
Read Shard, Interleave, Save a dataset checkpoint and resume iteration, and Export. Use tiny local data and inspect the warning that shuffle buffers are lost on resume; do not download web-scale corpora or upload to the Hub.