Dive into Deep Learning · §20.3
Deduplication and Decontamination
exact hashes · MinHash · banding and the S-curve · substrings · embeddings · test-set leakage
The problem
Two posts on one music blog in FineWeb’s output: the second is the first plus one paragraph.
| measure | value |
|---|---|
| SHA-256 equal | no |
| Jaccard of 5-word shingles, J | 0.70 |
| share of the shorter found in the longer, C | 1.00 |
Exact comparison of all pairs, about 8 µs per pair:
| documents | time |
|---|---|
| 20,000 | half an hour |
| 10^9 | about 10^5 years |
The method has to avoid comparing pairs, not compare them faster.
Goals
Running example: 26,000 FineWeb documents (the audit stream plus 6,000 same-host pages) and c1, our 1,032 crawl documents after the rules.
Where this fits
| step | section |
|---|---|
| get text | Document Extraction from Web Crawls |
| choose text | Quality Filtering |
| Deduplication and Decontamination | |
| mix text | Data Mixtures |
| Synthetic and Rephrased Data | |
| cut text | Tokenizers at Scale |
Exact duplicates
| corpus | documents | exact copies | documents with a shared line |
|---|---|---|---|
| FineWeb slice | 26,000 | 0 | 1,742 |
| c1, our crawl | 1,032 | 12 | 73 |
Deduplicate before the train/validation split, or validation loss measures memory.
Similarity as set overlap
Shingles: runs of 5 words (NFC, lower case, \w+).
J(A,B) = \frac{|A\cap B|}{|A\cup B|}, \qquad C(A,B) = \frac{|A\cap B|}{|A|}
Hash shingles with blake2b. Python’s hash is salted per process.
Check
Every shingle of a short page occurs in a long one. J? C? Does MinHash at \tau = 0.8 flag the pair?
Answer. J = 100/1{,}000 = 0.1, C = 1. A pair at J = 0.1 is almost never a candidate. Containment needs its own test; under J, a short copy is a different document.
MinHash
h_\pi(A): the shingle of A that comes first in a random order \pi.
Let x^* be the first element of A \cup B, uniform over the union.
\Pr[h_\pi(A) = h_\pi(B)] = \frac{|A\cap B|}{|A \cup B|} = J
k orders: \hat J = share of agreeing positions, a binomial count, \operatorname{Var}\hat J = J(1-J)/k.
The signature
| step | choice |
|---|---|
| shingle hash | blake2b, 8 bytes, little-endian, mod p = 2^{61}-1 |
| position i | \min_x (a_i x + c_i) \bmod p |
| (a_i, c_i) | default_rng(0xD2), integers in [1, p) |
| product a_i x | exact: 122 bits; NumPy uint64 overflows silently |
Checks on 200 pairs per J and on the fixture:
95% half-width at J = 0.8: 1.96\sqrt{0.16/128} = 0.069.
Banding
k = br positions as b bands of r; a shared band bucket makes a candidate.
P(s) = 1 - (1 - s^r)^b, \qquad s^* = \Bigl(\frac{r-1}{br-1}\Bigr)^{1/r} \approx \Bigl(\frac{1}{b}\Bigr)^{1/r}
| (b, r) | P(0.5) | P(0.8) | P(0.9) | steepest |
|---|---|---|---|---|
| (2, 2) | 0.44 | 0.96 | 0.58 | |
| (14, 8), FineWeb | 0.053 | 0.92 | 0.71 | |
| (9, 13) | 0.40 | 0.84 |
FineWeb’s paper: 0.56, 0.77, 0.92, 0.988 at 0.7 to 0.85. Reproduced.
The S-curve, measured
12,000 labelled pairs: 6,000 edited copies, 6,000 negatives; label J \geq 0.8.
13 settings × 3 hash seeds × 0.05-wide bins of J; expected count \sum_i P(J_i), exact Poisson-binomial 95% interval.
| value | |
|---|---|
| cells with n \geq 30 | 780 |
| outside their interval | 19 (2.4%) |
| bound at the 0.999 level | 59 |
| (9,13), bin 0.80 to 0.85 | expected 445 [418, 473]; seeds 431, 438, 429 |
A flawed hash would leave many cells outside.
Tuning
E(b,r) = w_{\textrm{fp}}\!\int_0^\tau\! P\,ds + w_{\textrm{fn}}\!\int_\tau^1\!(1-P)\,ds \;\Rightarrow\; (9, 13) \textrm{ at } k = 128,\ \tau = 0.8
| test pairs | precision | recall | F1 |
|---|---|---|---|
| (11, 11), chosen on train, w_{\textrm{fn}} = 1 | 0.86 | 0.89 | 0.87 |
| (9, 13), minimum of E, datasketch | 0.90 | 0.77 | 0.83 |
| (14, 8), FineWeb, 112 positions | 0.72 | 0.99 | 0.83 |
(11, 11) - (9, 13): F1 +0.04 [+0.03, +0.05]. w_{\textrm{fn}} = 5 chooses (14, 9): fewer rows, as predicted.
Check
The hashes follow the S-curve. So why does the measured optimum differ from the minimum of E?
Answer. E weighs every s \in [0,1] equally; the copies crowd near \tau and most negatives sit at 0. With the pairs’ own J in place of the uniform weight, the expected error is least at (11, 11) and, for w_{\textrm{fn}} = 5, at (14, 9). Right about the curve, wrong about the data.
At scale
\mathbb{E}[\#\textrm{candidates}] = \sum_{i<j} P(s_{ij})
| (11, 11) | (14, 8) | |
|---|---|---|
| P(0.3) | 2 \times 10^{-5} | 9 \times 10^{-4} |
| P(0.5) | 0.005 | 0.053 |
| 10^6 documents, 0.1% of pairs at 0.5 | 3 \times 10^6 | 3 \times 10^7 |
max_bucket caps and reports them.From pairs to a corpus
| c1 at (11, 11) | |
|---|---|
| kept | 1,014 of 1,032 |
| clusters of two or more | 13 (31 documents) |
| largest | one page, 4 copies on 3 hosts |
near_dedup default (9, 13) |
1,015 kept |
Substring duplicates
A 60-word footer on thousands of different pages: J \approx 0.1, never a candidate, read thousands of times.
2 MiB of FineWeb, about 450,000 tokens: 3 repeats of 50+ tokens.
| repeat | tokens |
|---|---|
| a blog’s “read more recent posts” footer | 74 |
| the Fourth Amendment | 66 |
| a function signature from a manual | 50 |
Keeping one copy of every span also deletes quotations from the pages that need them.
Semantic duplicates
SemDeDup: k-means (64 clusters), remove a page whose cosine to an earlier member exceeds 1 - \varepsilon. 20,000 stream pages.
| \varepsilon | removed | what goes |
|---|---|---|
| 0.05 | 0 | |
| 0.1 | 1 | a template page, cosine 0.94, J = 0.17 |
| 0.15 to 0.2 | 6 to 27 | gambling, essay mills, frozen pipes (J \approx 0) |
| 0.3 | 181 (0.9%) | FAQs, estate planning |
Cosine to the nearest earlier member: median 0.46, largest 0.94.
\varepsilon belongs to one embedder; at large \varepsilon this is topic balancing, a mixture decision.
Decontamination
16,533 test items, 633,848 hashed 13-grams. 100 items per benchmark planted in three forms into 900 FineWeb pages; 1,000 controls.
| found by 13-grams | GSM8K | ARC | MMLU |
|---|---|---|---|
| verbatim | 100 | 100 | 100 |
| reformatted (case, punctuation, options reversed) | 100 | 100 | 100 |
| paraphrased by Qwen3.5-4B | 2 | 0 | 6 |
A second detector
128-word windows every 64 words; threshold at the controls’ 99th percentile; held-out controls above it: 7 of 1,000.
| recall | all plants | long plants (about 20) | short plants (about 280) |
|---|---|---|---|
| verbatim | 0.49 | 0.95 | 0.46 |
| reformatted | 0.54 | 0.95 | 0.51 |
| paraphrased | 0.42 [0.37, 0.48] | 1.00 | 0.38 |
Short plants have a median under 50 words.
Check
The detector finds long plants almost always and short plants about half the time, whatever their form.
Answer. Dilution, not paraphrase. A 50-word plant shares its 128-word window with about 80 words of the host page, and the window’s vector is about the host as much as about the item. Smaller windows help and add false positives.
Limits
| leak | 13-gram index | embeddings |
|---|---|---|
| paraphrase | misses | about 2 in 5 |
| numbers written as words (“two dozen”) | breaks nearby 13-grams | |
| item under 13 words (ARC 102, MMLU 1,589) | cannot match | |
| translation | misses | |
| famous quotation | false positive |
In the pipeline
| stage | out | tokens |
|---|---|---|
| extract, of 9,731 records | 9,286 | |
| lang_id, nfc, pii (c0) | 3,288 | 2,850,469 |
| heuristics, edu_score (c1) | 1,032 | 923,725 |
| exact_dedup | 1,020 | 920,541 |
| minhash_dedup at (11, 11) | 1,014 | 918,627 |
| decontam (c2) | 1,014 | 918,627 |
lm-c2-docs-v1: keep, stage, reason and kept copy per c0 id, no text.Recap
Next: how much of each kind of text a model should see.