4.3.4Pretraining & Fine-Tuning LLMs

Pretraining data curation and cleaning

2,597 words12 min readdifficulty · medium

WHY curate at all?


The pipeline (WHAT the stages are)

Figure — Pretraining data curation and cleaning

HOW each stage works (derived from first principles)

1. Language identification

2. Quality filtering — deriving a heuristic score

3. Deduplication — deriving why and how

Exact dedup (WHAT): hash each document; drop repeats. O(N)O(N), but only catches byte-identical copies.

Near-dedup via MinHash + LSH (HOW, from scratch):

4. Content & PII filtering

5. Decontamination

6. Domain mixing / upsampling


Worked examples


Common mistakes (Steel-manned)


Active recall

Recall Feynman: explain to a 12-year-old

Imagine you're making flashcards to study, but your pile of paper has tons of duplicate cards, some cards are just ads, and a few are the actual test answers someone slipped in. If you memorize this pile you'll waste time on duplicates, learn ad-slogans, and cheat by memorizing test answers (so you can't really do it on the real test). Data cleaning is throwing out the duplicate cards, the ad cards, and the sneaked-in test answers, so what's left actually teaches you to think. To spot "same card written slightly differently," we use a clever trick (MinHash): we give every card a random lottery number and check if two cards keep drawing the same winning number — the more they do, the more alike they are.


Flashcards

Why is curation really "token-budget allocation"?
Fixed compute = fixed tokens; removing a junk token lets you add a good one, so cleaning directly reallocates the budget toward useful data.
Define the symbol-to-word ratio and its filter rule.
rsym=S/Wr_{sym}=S/W; drop docs with rsym>0.1r_{sym}>0.1 since junk/SEO pages spam symbols.
State the MinHash identity.
Pr[minhash(A)=minhash(B)]=J(A,B)\Pr[\text{minhash}(A)=\text{minhash}(B)]=J(A,B), the Jaccard similarity.
Why does the MinHash identity hold?
The global minimum hash over ABA\cup B is uniformly likely to be any element; it lands in ABA\cap B (making mins agree) with probability AB/AB=J|A\cap B|/|A\cup B|=J.
What is the LSH candidate probability formula?
P(s)=1(1sr)bP(s)=1-(1-s^{r})^{b} for bb bands of rr rows; an S-curve.
What is the exact LSH 50% crossover similarity?
s0=(121/b)1/rs_0=(1-2^{-1/b})^{1/r}, from solving 1(1s0r)b=121-(1-s_0^{r})^b=\tfrac12. The rough rule (1/b)1/r(1/b)^{1/r} is only an approximation.
Why use near-dedup, not just exact dedup?
Most web duplication differs by ads/one word; exact hashing misses near-duplicates that MinHash/LSH catch.
What is decontamination and a typical rule?
Removing pretraining docs overlapping eval benchmarks; drop docs sharing a ≥13-gram with a benchmark example.
Formula for effective epochs on a domain.
ed=wdT/Nde_d = w_d T / N_d; keep it below ~4 to avoid memorization when upsampling.
Steel-man: why "filter harder is always better" is wrong.
It biases toward narrow style, kills diversity, and shrinks the token budget below scaling-law needs; use soft filters validated downstream.
Why does dedup help privacy?
Fewer repeated strings means the model is far less likely to memorize and regurgitate specific documents/PII.
Variance of the MinHash Jaccard estimate with mm hashes?
Approximately J(1J)/mJ(1-J)/m; decreases as mm grows, so use hundreds of hashes.
Why threshold instead of argmax in language ID?
A threshold trades recall vs precision for the target language and discards low-confidence, code-switched, or short docs.

Connections

  • Scaling Laws for LLMs — curation decides how many useful tokens fill the budget.
  • Tokenization — cleaning happens on text; tokenizer stats depend on the cleaned corpus.
  • Deduplication and Memorization — dedup reduces verbatim memorization/privacy leakage.
  • Benchmark Contamination — decontamination protects eval validity.
  • Data Mixture and Domain Weighting — upsampling weights wdw_d.
  • MinHash and LSH — the near-dedup machinery.
  • Fine-Tuning and Instruction Data — a later, smaller, higher-quality curation stage.

Concept Map

is ~90% junk

goal

remove junk token = add good token

stage 1

p lang gt threshold

heuristic + model scores

remove copies

drop toxic PII

drop eval overlap

reweight domains

uses

uses

prevents

prevents

Raw web dump C0

Curation pipeline

Token-budget allocation

Better model per compute

Language ID

Quality filtering

Deduplication

Content / safety filter

Decontamination

Mixing / upsampling

Clean corpus Cn

Symbol-to-word ratio r_sym

Repetition penalty

Wasted capacity on duplicates

Benchmark contamination

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, ek LLM basically apne training data ka ek smart compression hai. Agar tum raw web data (jaise Common Crawl) seedha model me daal doge, to usme 90% kachra hota hai — duplicate pages, ads, "Add to cart" jaisa boilerplate, SEO spam. Model us kachre ko yaad karne me apni capacity waste kar deta hai, actual reasoning seekhne ke bajaye. Isliye curation matlab apne token-budget ko sahi jagah lagana — har junk token hatao, ek accha token add karo.

Pipeline simple hai: pehle language ID (target language rakho), phir quality filter (symbol-to-word ratio r=S/Wr=S/W; agar r>0.1r>0.1 to drop, kyunki junk me symbols bahut hote hain), phir dedup, phir content/PII filter, phir decontamination, aur last me domain mixing. Sabse zyada faayda quality filter aur dedup se aata hai — yahi tumhara 80/20 hai.

Dedup ka jaadu MinHash hai. Har document ko shingles (k-word tukde) ka set maano. Do docs ki similarity Jaccard J=AB/ABJ=|A\cap B|/|A\cup B| se aati hai, par saare pairs compare karna N2N^2 — impossible at billions. Trick: har shingle ko ek random lottery number do, aur dono docs ka minimum number match karta hai exactly probability JJ ke saath. Bahut saare hashes lo, matches ka fraction hi JJ ka estimate ban jaata hai. Phir LSH se sirf similar-lagne wale pairs hi check karte hain — S-curve P(s)=1(1sr)bP(s)=1-(1-s^r)^b. Yaad rakho, iska exact 50% crossover $s_0=(1-2^{-1/b})^{1/r

Go deeper — visual, from zero

Test yourself — Pretraining & Fine-Tuning LLMs

Connections