Phase 1: Pretraining at Scale · ~90 minutes · Python
Large Corpus Downloader
Training a language model begins long before the first forward pass.
What you will learn
- Stream remote shards with `urllib` and decompress with `zstandard` without buffering the whole file in memory.
- Resume partial downloads by issuing HTTP `Range` requests against a verified byte offset.
- Build a MinHash signature per document and bucket it with LSH so near-duplicates collide.
- Emit a shard manifest with content hash, byte size, document count, and dedup verdict.
Introduction
The corpus has to land on disk, decompressed, deduplicated, and addressable, with the resume story already worked out before the network drops at 4 percent. This lesson builds a streaming downloader that pulls compressed shards, decompresses on the fly with Zstandard, fingerprints near-duplicates via MinHash plus locality-sensitive hashing, and writes a shard manifest the rest of the pipeline can trust.
- Type: Build
- Languages: Python
- Prerequisites: Earlier lessons in this course
- Time: ~90 minutes
Objective
Learning objectives
- Stream remote shards with
urllib and decompress with zstandard without buffering the whole file in memory. - Resume partial downloads by issuing HTTP
Range requests against a verified byte offset. - Build a MinHash signature per document and bucket it with LSH so near-duplicates collide.
- Emit a shard manifest with content hash, byte size, document count, and dedup verdict.
The Problem
The first time you train on a 200 GB corpus the network drops at percent 41 and the script exits with a urllib exception. The second time it drops at percent 78. By percent 99 you have rewritten the loop three times. The two failures you have to design for from minute one are partial-download resume and duplicate document removal. Both have well-known solutions; both are routinely skipped because the pipeline begins as a one-line requests.get call that grew teeth.
Resume is an HTTP problem. The server has to honour Range, the client has to track verified offset against an on-disk record, and the verified offset has to survive process death. If the offset and the file diverge by even one byte the resumed download writes garbage and the corpus is corrupted in a way that only shows up during tokenization.
Deduplication is a signature problem. Exact-hash dedup misses near-duplicates: the same Wikipedia article shows up with three different boilerplate footers, the same code file with a different license header, the same blog post with a tracking parameter on every link. MinHash plus LSH catches these at sub-linear cost. The cost is one signature per document and one bucket lookup per signature.
The Concept
Streaming with urllib
The standard-library urllib.request.urlopen returns a file-like object. Wrap it in a zstandard.ZstdDecompressor().stream_reader and the bytes flow from the network through the decompressor into the document iterator without ever materialising the compressed shard or the decompressed shard in memory. The only memory cost is the line buffer, the MinHash signature for the current document, and the LSH index.
Resume with Range
The downloader writes two files per shard: the shard itself and a .partial.json checkpoint. The checkpoint records verified_bytes, expected_size, sha256_prefix (computed over the first verified_bytes bytes), and the source URL. On startup the downloader reads the checkpoint, recomputes sha256_prefix over the on-disk bytes, and only resumes if the recomputed hash matches. If the hash is wrong the partial is discarded and the download restarts from byte zero. Silent corruption is impossible because the verified bytes are checked, not assumed.
MinHash plus LSH
MinHash estimates the Jaccard similarity of two sets in fixed space. For a document the set is the shingles (overlapping n-grams) of its text. The signature is k minimum hash values, one per independent hash function. Two documents with Jaccard similarity s have a probability s of agreeing on any single component of the signature.
LSH then groups the k components into b bands of r rows each, where k = b * r. Two documents collide in at least one band with probability 1 - (1 - s^r)^b, which is a sharp threshold around the value of s you tune (b, r) to. The threshold for typical corpus dedup is s = 0.8, which the LSH research literature reaches with k = 128, b = 32, r = 4.
Shard manifest as a contract
The downloader's only durable output is the manifest. The manifest holds, per shard, the URL, the decompressed byte count, the document count, the unique document count after dedup, and the sha256 of the final shard file. Downstream tokenization reads the manifest, not the directory listing. If a shard is missing or its sha256 is wrong, the manifest tells the next stage to refuse to start. The manifest is the deciding edge between "the data is downloaded" and "the data is downloaded and verifiable".
This lesson is free
You've read the first 4 sections. The rest covers Build, Check Yourself, Check Yourself, Key Terms & Next. A free account opens all of it, with its practice questions. No card.
Create a free account — the opening phases of 29 of 35 courses, and the first lesson of every phase, are free, no credit card.
Browse all courses · View pricing · DeVenture Academy