5.3 Exact dedup: paragraph hashes, a Bloom screen, and a sort-merge confirm
After the quality filters every document is clean, English, and plausibly prose, and many of them say the same thing. Web pages share navigation and license footers; dataset mirrors repeat whole files; story collections reuse an opening paragraph. A model trained on that sees the repeated text again and again. Lee et al. found that deduplicating C4 removed text that models otherwise reproduced verbatim, and that the deduplicated models trained faster to the same loss. Repeated rare strings are also the ones a model memorizes, which matters for privacy (see Privacy and PII policy in AI Engineering).
Concepts from other sections (6)
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.
- FNV-1a: a byte hash that xors each byte in and then multiplies by a fixed prime mod . Read more: 12.3 Modular arithmetic, hashing, and PCG32 in Python in Mathematics
- SplitMix64: the mixer used to derive child seeds that depend only on the seed and the purpose. Read more: 12.3 Modular arithmetic, hashing, and PCG32 in Python in Mathematics
- Suffix array: the starting positions of a string’s suffixes in sorted order, built in ; with its LCP (longest common prefix) array it answers substring queries. Read more: Chapter 16: Competitive Programming in Programming and Algorithms
- Double hashing: generating probes from two hashes. Read more: 15.8 A Bloom filter in Programming and Algorithms
- direct path: the optional linear map from the embeddings straight to the logits, in parallel with the hidden layer. Read more: 8.2 Bengio’s neural probabilistic language model in AI 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
The obvious fix, a Python set of every paragraph seen, costs about 90 bytes per paragraph (a 32-byte digest as a bytes object is 65 bytes, plus its slot in the set), so a billion paragraphs need 90 GB. A compact Bloom screen does the screening in about 10 bytes each. This section builds exact dedup from that screen, and shows what the Bloom filter’s false positives mean for correctness: with a sort-merge confirm behind it, nothing.
You will use the Doc type and compose from the previous section, the FNV-1a and SplitMix64 hashes from Modular arithmetic, hashing, and PCG32 in Mathematics, and the false-positive rate and optimal worked in the discrete math problem set on Bloom filters and LSH. The data structure itself is covered in A Bloom filter in Programming and Algorithms; the Python screen here follows the shared bit-sizing rules in formats/bloom.md, and the standalone Rust Bloom module from that chapter does not cross into this stage. The solution is python/corpus/dedup.py (paragraphs, paragraph_hash, bloom_rate, exact_dedup, STATS_KEYS), with its contract in dedup.pyi.
Learning objectives
- Split normalized text into paragraphs and explain why paragraphs are the unit of exact dedup.
- Size a Bloom filter from a bytes-per-item budget and compute its false-positive rate.
- Explain why a Bloom hit may only nominate a candidate, and confirm candidates exactly with a sort-merge.
- Implement dedup as a four-pass stage over a disk spool that cleans up after itself.
Paragraphs and their hashes
Section titled “Paragraphs and their hashes”Start with the unit of comparison. A paragraph is a piece of the text between blank lines, where a blank line is empty or holds only spaces and tabs. Each piece is stripped of white space at both ends, and empty pieces are skipped. A single line break stays inside a paragraph. After normalization, paragraphs are separated by exactly \n\n.
Comparing paragraphs by their text would mean storing the text. Instead compare their SHA-256 digests: paragraph_hash(p) = sha256(p.encode("utf-8")).digest(), 32 bytes. Equal paragraphs have equal digests; unequal ones collide with probability about , which for our purposes is never. A paragraph’s repeat is any occurrence whose digest occurred earlier in the input, in an earlier document or earlier in the same one. Exact dedup keeps the first occurrence of every paragraph, in input order, and removes the later ones; a document that loses every paragraph is dropped.
Why paragraphs and not whole documents? Duplicates are rarely whole documents: a copied story with a new title, or a page with a shared footer, differs as a document and repeats as paragraphs. Why not sentences or substrings? Shorter units remove ordinary phrases (“Once upon a time, there was a little girl.”) from unrelated stories, and finding repeated substrings of any length needs a suffix array (Lee et al.), a different tool. Paragraphs are the CCNet compromise.
The Bloom filter as a screen
Section titled “The Bloom filter as a screen”Digests are smaller than text but a set of them is still 90 bytes each. A Bloom filter is a bit array of bits and hash functions. Inserting an item sets its bits; asking about an item answers “present” when all bits are set. The notation:
| Symbol | Meaning | Type |
|---|---|---|
| paragraphs in the input (the items the filter will hold) | integer | |
| bits in the filter | integer | |
| hash functions (bits per item) | integer | |
bloom_bytes_per_item: filter bytes per paragraph, so | float, | |
| the false-positive rate: the chance that a never-inserted item is reported present | float in |
Two facts drive the design. First, there are no false negatives: an inserted item’s bits stay set, so the filter never says “new” about something it has seen. Second, there are false positives: with the optimal , the rate is . Solving for the memory per item gives . formats/bloom.md sizes a filter that way, so for a budget of bytes per item ( bits) you ask for
bloom_rate(B) computes that . gives ; gives ; gives .
The screen pass hashes every paragraph in input order and asks the filter. If it answers “present”, the digest becomes a candidate; otherwise it is inserted. By the first fact, every digest that occurs twice or more becomes a candidate at its second occurrence. By the second, some digests that occur once become candidates too: the false positives. So the candidates are a superset of the repeated digests, and a small one: the repeated digests plus about false positives. A Bloom filter answers “definitely new” or “maybe seen” in about 10 bytes per item, a tenth of what a set of hashes costs, but its “maybe” is only a candidate.
Confirming by sort-merge
Section titled “Confirming by sort-merge”The candidates still have to be sorted into real repeats and false alarms. The confirm pass reads the input again and records an occurrence for every paragraph whose digest is a candidate, where is the document’s index and the paragraph’s index within it. Sorting the occurrences makes equal digests adjacent, and within one digest the order is input order (by , then ). Then the pass merges, walking runs of equal . A run of length 1 is a false positive: that paragraph is unique, so keep it. A run of length is a confirmed repeat: keep its first occurrence and mark the other for removal.
Sorting is what makes this scale: an external sort handles candidate lists larger than memory, which a hash map of counts does not. Here the candidates fit in a Python list. Because the confirm is exact, no unique paragraph is ever dropped, whatever the filter size.
Four passes over a spool
Section titled “Four passes over a spool”There is one practical obstacle. The stage consumes an iterator, which can be read once, but the algorithm needs the input three more times. So it spools: the first pass writes every document as one JSON line to a temporary file and counts , and the screen, confirm, and emit passes reread that file. Memory holds the filter, the candidate set, and the candidate occurrences, never the documents.
The cost is that nothing is emitted until the input is exhausted: dedup is a barrier in the otherwise streaming pipeline. The spool lives in a tempfile.TemporaryDirectory (under spool_dir when given), which is removed when the generator finishes or is closed early, because the spool is a full copy of the corpus.
The emit pass yields each document with its marked paragraphs removed: unchanged (the same Doc, exact text and meta) when it lost nothing, with the remaining paragraphs joined by \n\n when it lost some, and not at all when it lost every paragraph. Output order is input order. With stats={} passed in, the stage fills docs_in, paragraphs, bloom_bits (), candidates, false_positives, duplicate_paragraphs, docs_dropped, and docs_out once the output is exhausted; the corpus manifest’s dedup.exact_dropped and the datasheet report them.
Working it by hand
Section titled “Working it by hand”Take three documents and :
| Doc | Text |
|---|---|
A (s:0) | The cat sat. (P1), blank line, The dog ran. (P2) |
B (s:1) | The dog ran. (P2), blank line, A bird sang. (P3) |
C (s:2) | The cat sat. (P1) |
Spool. There are paragraphs. The filter has bits and hashes. The digests begin h(P1) = 84549cfa..., h(P2) = 0ea960d6..., h(P3) = c2c47b92....
Screen. In input order, the filter sees:
| Occurrence | Paragraph | Filter says | Action |
|---|---|---|---|
| (0, 0) | P1 | new | insert |
| (0, 1) | P2 | new | insert |
| (1, 0) | P2 | present | candidate h(P2) |
| (1, 1) | P3 | new | insert |
| (2, 0) | P1 | present | candidate h(P1) |
Confirm. The occurrences of the two candidates are , , , . Sorted (0e... before 84...) they are , , , . There are two runs of length 2, so remove and . No run has length 1, so there are no false positives.
Emit. A loses nothing and comes out unchanged. B loses P2 and comes out as A bird sang.. C loses its only paragraph and is dropped. The stats: 3 documents in, 5 paragraphs, 400 bits, 2 candidates, 0 false positives, 2 duplicate paragraphs, 1 document dropped, 2 out.
With the filter would have bits and might report P3 as present on first sight. P3 would become a candidate, its run in the confirm pass would have length 1, and it would be kept: the output is identical, and only false_positives grows.
Designing the interface
Section titled “Designing the interface”STATS_KEYS: tuple[str, ...]def paragraphs(text: str) -> list[str]: ...def paragraph_hash(p: str) -> bytes: ...def bloom_rate(bloom_bytes_per_item: float) -> float: ...def exact_dedup(docs: Iterable[Doc], bloom_bytes_per_item: float = 10, *, spool_dir: str | Path | None = None, stats: dict[str, Any] | None = None) -> Iterator[Doc]: ...In a pipeline, bind the options: compose(build_filters(cfg["filters"]), functools.partial(exact_dedup, bloom_bytes_per_item=cfg["dedup"]["bloom_bytes_per_item"])). The private Python filter is sized for max(N, 1) at bloom_rate(B); use its insert and contains, and its m is the bit count reported in stats.
Building it step by step
Section titled “Building it step by step”The reasoning chain runs from the definition of “first” to the spool.
- “Keep the first occurrence” is a property of input order, so the algorithm must see occurrences in order and identify each by position : document index, paragraph index. Keeping the first makes the result stable when sources are appended later.
- Comparing text means storing text; comparing digests does not. SHA-256 turns each paragraph into 32 bytes with collisions at , so “equal digest” can stand for “equal paragraph”.
- A set of digests is exact but costs about 90 bytes each in Python. A Bloom filter costs bytes each and never forgets an insert (no false negatives), so every true repeat is caught at its second occurrence. Its only error is the other direction: it may call a new paragraph “seen”.
- So a Bloom hit can only nominate, never decide. The candidates are a superset of the repeated digests. A second, exact pass over just the candidates removes the false positives; because candidates are few (repeats plus about ), that pass is cheap.
- Exact confirmation needs equal digests together. Sorting the occurrences makes runs of equal adjacent and, inside a run, puts them in input order. A run of length 1 is a false positive; in a longer run, everything after the first element is a repeat. Sorting also scales to an external sort when the list outgrows memory.
- The algorithm reads the input four times but the stage gets a one-shot iterator. So the first pass spools it to disk, and memory holds only the filter, the candidates, and their occurrences. The spool is a corpus-sized copy, so it lives in a
TemporaryDirectorythat the generator’swithblock removes on exhaustion orclose(). - Size the filter by what it holds: paragraphs, not documents. The first pass counts , and
bloom_rate(B)turns the config’s byte budget into the target .
Paragraphs, digests, and sizing
Section titled “Paragraphs, digests, and sizing”The helpers in dedup.py are short:
_BLANK = re.compile(r"\n[ \t]*\n") # a blank line may hold spaces or tabs
def paragraphs(text: str) -> list[str]: return [p.strip() for p in _BLANK.split(text) if p.strip()] # empty pieces skipped
def paragraph_hash(p: str) -> bytes: return hashlib.sha256(p.encode("utf-8")).digest()
def bloom_rate(bloom_bytes_per_item: float) -> float: if not bloom_bytes_per_item > 0: raise ValueError("bloom_bytes_per_item must be positive") # m/n = 8B bits per item at the optimal k gives p = exp(-(m/n)(ln 2)^2) return math.exp(-8.0 * bloom_bytes_per_item * math.log(2) ** 2)The private filter
Section titled “The private filter”The filter follows formats/bloom.md: from and , rounded from , and double hashing with FNV-1a and SplitMix64:
class _Bloom: def __init__(self, n: int, rate: float) -> None: if n < 1 or not 0 < rate < 1: raise ValueError("Bloom needs n >= 1 and 0 < rate < 1") self.m = math.ceil(-n * math.log(rate) / math.log(2) ** 2) self.k = max(1, math.floor(self.m / n * math.log(2) + 0.5)) # round half up self.bits = bytearray((self.m + 7) // 8)
def _positions(self, item: bytes): h1 = fnv1a64(item) h2 = splitmix64(h1) | 1 # odd step: the k positions do not collapse onto a short cycle for i in range(self.k): yield ((h1 + i * h2) & ((1 << 64) - 1)) % self.m # Kirsch-Mitzenmacher, mod 2^64 then mod mThe stage: spool, screen, confirm, emit
Section titled “The stage: spool, screen, confirm, emit”The whole body sits inside the TemporaryDirectory block, so the spool goes away however the generator ends.
def exact_dedup(docs, bloom_bytes_per_item=10, *, spool_dir=None, stats=None): rate = bloom_rate(bloom_bytes_per_item) # fails fast on B <= 0, before reading input with tempfile.TemporaryDirectory(prefix="dedup-", dir=spool_dir) as td: spool = Path(td) / "spool.jsonl" n_docs = n_paras = 0 with spool.open("w", encoding="utf-8") as fh: for doc in docs: # pass 1: the only read of the real input fh.write(_doc_line(doc) + "\n") n_docs += 1 n_paras += len(paragraphs(doc.text)) # N counts paragraphs, not documents
# Screen: one Bloom filter sized for every paragraph of the input. bloom = _Bloom(max(n_paras, 1), rate) # max(.., 1): an empty input still gets a filter candidates: set[bytes] = set() for doc in _read_spool(spool): for p in paragraphs(doc.text): h = paragraph_hash(p) if bloom.contains(h): candidates.add(h) # maybe seen: nominate, do not drop else: bloom.insert(h)
# Confirm: sort-merge the occurrences of candidate hashes. occ: list[tuple[bytes, int, int]] = [] for i, doc in enumerate(_read_spool(spool)): for j, p in enumerate(paragraphs(doc.text)): h = paragraph_hash(p) if h in candidates: occ.append((h, i, j)) occ.sort() # equal h adjacent; within h, input order drop: set[tuple[int, int]] = set() false_pos = 0 k = 0 while k < len(occ): end = k while end + 1 < len(occ) and occ[end + 1][0] == occ[k][0]: end += 1 # [k, end] is one run of equal digests if end == k: false_pos += 1 # the filter said "seen", but this hash occurs once for _, i, j in occ[k + 1 : end + 1]: drop.add((i, j)) # every occurrence after the first k = end + 1
# Emit, in input order. dropped_docs = out_docs = 0 for i, doc in enumerate(_read_spool(spool)): paras = paragraphs(doc.text) keep = [p for j, p in enumerate(paras) if (i, j) not in drop] if not keep: dropped_docs += 1 continue out_docs += 1 if len(keep) == len(paras): yield doc # untouched: exact original text, not a rebuild else: yield dataclasses.replace(doc, text="\n\n".join(keep)) if stats is not None: stats.update(docs_in=n_docs, paragraphs=n_paras, bloom_bits=bloom.m, candidates=len(candidates), false_positives=false_pos, duplicate_paragraphs=len(drop), docs_dropped=dropped_docs, docs_out=out_docs) # filled only once the output is exhaustedIn the solution file the bloom = _Bloom(...) line is indented one level deeper, so it runs just before the spool file is closed; the behaviour is the same. The full solution is dedup.py.
Why this design
Section titled “Why this design”The cost is four linear passes over the spool, each hashing every paragraph: for bytes of text. Memory is bits for the filter plus for the candidates and their occurrences, where is the number of repeated digests; the sort is in the number of candidate occurrences . Disk holds one copy of the corpus.
A plain set is simpler and exact, but at 90 bytes per paragraph it is nine times the default filter; at a billion paragraphs that is the difference between 10 GB and 90 GB. Trusting the Bloom filter (dropping every hit) saves the confirm pass but silently deletes unique paragraphs at rate , and at a small that is a third of what the filter has seen. The confirm pass makes correctness independent of , so is purely a memory and time knob. A dict of counts for the confirm would also work while candidates fit in memory, but sort-merge gives the same answer and has a direct path to an external sort when they do not.
The barrier is the accepted cost. “First occurrence” cannot be decided until every earlier paragraph has been seen, and dropping a later one needs to know there was an earlier one, so some pass must see everything before emitting. Spooling to disk keeps that barrier out of memory.
What a correct dedup must handle
Section titled “What a correct dedup must handle”The tests pin the definition of “first”, the independence from filter size, and the cleanup:
| Case | What must hold | Why it matters |
|---|---|---|
| The hand example | A unchanged, B as P3, C dropped, the counts | “first occurrence” means exactly this |
| A corpus with planted copies | 200 documents: paragraph, repeat, and dropped counts and the sha256 of every kept text, all fixed by the generator | the counts in the manifest and the datasheet |
| A tiny filter | at (over 50 false positives) the output equals a plain set-based dedup, and candidates = repeated digests + false positives | a Bloom positive never drops a unique paragraph |
| Random corpora at several sizes | 40 seeded corpora at = 0.1, 1, 10 equal the set-based dedup | correctness does not depend on the filter size |
| Paragraph splitting | blank lines with spaces or tabs, single line breaks, stripping, empty pieces | what counts as a repeat |
| A repeat inside one document | a repeated footer inside one document goes, the document stays | repeats are positional, not per document |
| Untouched documents | a document with no repeats comes back equal, line breaks and meta included | dedup changes only what it dedups |
| Filter sizing | bloom_rate values; within 1 of | the config’s memory knob means what it says |
| Stats | every STATS_KEYS entry, also for an empty input | the manifest and datasheet fields |
| Spool cleanup | no spool left after a full read or an early close() | the spool is a full copy of the corpus |
| Use as a stage | composes with functools.partial; partial documents joined by \n\n | the pipeline wiring of CorpusBuild |
What can go wrong
Section titled “What can go wrong”The worst pitfall is invisible.
Warning: Dropping every Bloom positive without confirming makes unique paragraphs vanish with a small filter, and nothing reports it. In the solution a Bloom hit only adds to
candidates; the sort-merge decides.
Two pitfalls break the merge itself. Merging occurrences without sorting them leaves equal digests non-adjacent, so repeats look unique and survive; the solution calls occ.sort() before the merge. Keeping the last occurrence instead of the first makes the output depend on what came later, and reruns with appended sources rewrite earlier documents; the solution drops occ[k + 1 : end + 1], keeping the first element of each sorted run.
Two pitfalls are about the unit. Hashing whole documents lets copies with a new title or footer survive, and repeat counts come out far too low; the solution hashes each paragraph from paragraphs(doc.text). Splitting paragraphs only on \n\n lets a blank line holding a space hide a paragraph boundary, so repeats stick to their neighbours; the solution splits on \n[ \t]*\n.
The rest are about side effects. Rebuilding every document from its paragraphs changes untouched documents (line breaks collapse), so later hashes no longer match the filter stage’s output; the solution yields the original doc when len(keep) == len(paras). Sizing the filter by documents rather than paragraphs gives a filter several times too small, with a false-positive rate far above the budget; the solution uses _Bloom(max(n_paras, 1), rate). And a spool that outlives the stage leaves a full copy of the corpus in the temp directory on every run; the whole stage body runs inside tempfile.TemporaryDirectory.
Where this leads
Section titled “Where this leads”This stage relies on the Doc, compose, and the normalized text, whose paragraphs are separated by exactly one blank line, from the filter stages, and on FNV-1a and SplitMix64 for the shared Bloom hash rules. Going forward, near dedup with MinHash and LSH runs on what this stage keeps, so exact copies never reach the more expensive pass; without this stage, near dedup would have to remove exact copies itself, at MinHash cost. The manifest’s dedup.exact_dropped, written by the shard writer, comes from these stats.
Further reading
Section titled “Further reading”- Lee et al., Deduplicating Training Data Makes Language Models Better (2022), and its suffix-array dedup in
google-research/deduplicate-text-datasets; datatrove’s exact-substring stages (datatrove/pipeline/dedup/exact_substrings.py): repeated substrings of any length (50 tokens and up), across the whole corpus. - Wenzek et al., CCNet (2019), section 3.1: paragraph-level dedup with sharded hash sets over Common Crawl snapshots.
- Dolma’s
bff(allenai/bff): a Rust Bloom filter deduplicator over billions of paragraphs. - Broder and Mitzenmacher, “Network Applications of Bloom Filters: A Survey” (2004).
- Kirsch and Mitzenmacher, “Less Hashing, Same Performance” (2006): the double-hashing scheme used here.
- External merge sort and Spark
dropDuplicates(see Batch and Streaming Processing): candidate lists larger than memory, spilled and merged from disk. - Fan et al., “Cuckoo Filter” (2014), and counting Bloom filters: deletions, and better space at low false-positive rates.
Key terms
Section titled “Key terms”- Paragraph: a stripped, non-empty piece of text between blank lines.
- Repeat: an occurrence of a paragraph whose digest occurred earlier in the input.
- Bloom filter: a bit array with hash functions that has no false negatives and a tunable false-positive rate.
- Candidate: a digest the Bloom filter reported as maybe seen; a superset of the true repeats.
- Sort-merge confirm: sorting candidate occurrences so equal digests form runs, keeping the first of each run.
- Spool: an on-disk copy of the input that lets a one-shot iterator be read several times.
Summary
Section titled “Summary”- Exact dedup keeps the first occurrence of every paragraph, in input order, and removes the later ones; a document that loses every paragraph is dropped.
- A Bloom filter answers “definitely new” or “maybe seen” in about 10 bytes per item, a tenth of a set of hashes; its “maybe” is only a candidate.
- A sort-merge over the candidates’ exact hashes confirms which are real repeats, so no unique paragraph is ever dropped, whatever the filter size.
- The filter is sized from
bloom_bytes_per_item: and bits for paragraphs. - Dedup must read its whole input before emitting anything; it spools to disk, not memory, and cleans up after itself.
Review questions
Section titled “Review questions”- Why can a Bloom filter be used to find repeats without ever dropping a unique paragraph?
Answer
Because the filter is used only to nominate candidates. It has no false negatives, so every real repeat is nominated; its false positives are filtered out by the exact sort-merge, where a unique paragraph forms a run of length 1 and is kept. - Why is the filter sized from the paragraph count of the first pass and not from the document count?
Answer
The filter holds paragraphs. Sizing it for documents gives an $m$ several times too small for the $N$ actually inserted, and the false-positive rate grows exponentially as $m/N$ falls ($p = e^{-(m/N)(\ln 2)^2}$). Correctness survives, but candidates and confirm cost balloon. - Why does
occ.sort()order occurrences correctly within a run, and why does that matter?Answer
Tuples sort by digest, then by document index, then by paragraph index, so within one digest the occurrences are in input order. The first element of each run is therefore the first occurrence, which is the one to keep. - Why does the emit pass yield the original
docwhen nothing was removed, rather than"\n\n".join(paras)?Answer
Rebuilding from paragraphs would strip each paragraph and rewrite every blank line (a run of several, or one holding spaces or tabs) as exactly `\n\n`, changing documents dedup did not touch. Later stages (and the hashes in the manifest) would then disagree with the filter stage's output for no reason. - What happens to the spool if a consumer stops reading after the first document?
Answer
Closing the generator raises `GeneratorExit` at the `yield`, which unwinds the `with tempfile.TemporaryDirectory(...)` block and deletes the directory. A corpus-sized copy is never left behind.