# Dataset curation for fine-tuning

> LIMA tuned LLaMA 65B on 1,000 curated examples and was rated equal to or better than GPT-4 in 43% of comparisons. Curation: exact and MinHash dedup, quality filters, decontamination, consistent formatting.

Source: https://www.mlfoundry.in/learn/dataset-curation-for-fine-tuning  
Section: Generative AI › Customization  
Level: core · 8 min read · updated 2026-10-21

**Dataset curation for fine-tuning:** is the process of turning a raw pool of candidate examples into a small, clean, diverse and correctly formatted training set. The steps are deduplication, quality filtering, decontamination against evaluation data, diversity balancing and formatting. The reference result is **LIMA** (Zhou et al., "LIMA: Less Is More for Alignment", NeurIPS 2023): LLaMA 65B fine-tuned on only 1,000 carefully chosen examples.

Fine-tuning data comes from logs, forums, annotators and generator models (see **Synthetic data generation**). Raw pools are full of repeats, wrong answers, boilerplate, refusals, leaked test questions and mixed formats. **Supervised fine-tuning (SFT)** copies all of it faithfully. A model trained on 50K noisy pairs can end up worse than one trained on the best 5K. Curation sits between collection and training, and it usually decides the result more than the hyperparameters do.

## Less is more

LIMA's 1,000 training examples were 750 questions and answers selected from Stack Exchange, wikiHow and Reddit's r/WritingPrompts. The rest were written by the authors or adapted from Super-NaturalInstructions. All shared a consistent helpful-assistant style. There was no RLHF. In a human preference study, LIMA's answers were equal to or preferred over GPT-4's in 43% of comparisons, over Bard's in 58% and over DaVinci003's in 65%. The authors' *superficial alignment hypothesis*: knowledge comes from pretraining, and fine-tuning mostly teaches format and style, so a small set of excellent examples is enough.

Their ablations on a 7B model point the same way. Filtering Stack Exchange for quality helped, and mixing in diverse sources helped. Raising the number of examples from 2K to 32K without adding diversity did not. AlpaGasus (Chen et al., 2023) found the same thing in Alpaca's 52K synthetic pairs. It had ChatGPT rate each pair from 0 to 5 and kept the 9K pairs scoring at least 4.5. The model trained on those beat the one trained on all 52K.

> Figure: A typical curation funnel with illustrative counts: exact and near-duplicate removal, quality filtering, decontamination, diversity sampling, then formatting into the chat template the model will be served with. Most of the pool is dropped. Right: one formatted record (loss on the assistant turn only) and LIMA's result with 1,000 examples.

## Deduplication with MinHash

Exact duplicates are removed by hashing normalised text (lower-cased, whitespace collapsed). Near-duplicates, such as the same question with one word changed or a template filled twice, need a similarity measure. Represent each example as a set of shingles (word $n$-grams) and use Jaccard similarity. Comparing all pairs costs $O(N^2)$, which is impossible for millions of examples. MinHash (Broder, 1997) compresses each set to $k$ numbers so that similar sets get similar signatures:

$$
J(A,B)=\frac{|A\cap B|}{|A\cup B|},\qquad \Pr\big[\min h(A)=\min h(B)\big]=J(A,B),\qquad \hat J=\frac{1}{k}\sum_{j=1}^{k}\mathbb 1\big[m_j(A)=m_j(B)\big]
$$

- $A,B$: shingle sets of two examples
- $h$: a random hash function; $\min h(A)$ is the smallest hash over the set
- $m_j$: the $j$-th of $k$ minima (one per hash function), the MinHash signature
- $\hat J$: unbiased estimate of $J$ with standard error $\sqrt{J(1-J)/k}$

Locality-sensitive hashing (LSH) avoids comparing every signature pair. Split the $k=br$ values into $b$ bands of $r$ rows. Two examples become a candidate pair if all $r$ values agree in at least one band. Only candidates are compared exactly.

$$
P_\text{cand}(s)=1-\big(1-s^{\,r}\big)^{b},\qquad s^\ast\approx(1/b)^{1/r}
$$

- $s$: true Jaccard similarity of the pair
- $b,\ r$: bands and rows per band; more bands raise recall, more rows raise precision
- $s^\ast$: approximate midpoint of the S-curve; choose $b,r$ to put it just below the duplicate threshold

> Figure: MinHash LSH on 36 instructions (12 originals plus 2 edited copies each), with word-bigram shingles; 59 of the 630 pairs share any shingle. Defaults b = 20, r = 5 (k = 100), duplicates when J ≥ 0.5: midpoint 0.549. 15 pairs become candidates; 7 of the 10 true duplicates are caught, 3 are missed, and 8 candidates fail the exact check. 29 docs are kept. b = 32, r = 2 catches all 10 but compares 36 pairs (26 rejected), leaving 26 docs. b = 8, r = 8 (midpoint 0.771) finds nothing.

Worked example: LSH with b = 20, r = 5

| Jaccard s | s⁵ | (1 − s⁵)²⁰ | P(candidate) |
| --- | --- | --- | --- |
| 0.3 | 0.00243 | 0.9525 | 0.047 |
| 0.5 | 0.03125 | 0.5299 | 0.470 |
| 0.8 | 0.32768 | 0.000356 | 0.9996 |

The midpoint is $(1/20)^{1/5}=0.549$. A pair at $s=0.8$ is almost surely caught, while an unrelated pair at $0.3$ rarely costs a comparison. For the viz's pair of doc 0 and its first copy ($J=0.444$), 100 hashes estimate $\hat J=0.46$, inside one standard error of $\sqrt{0.444\cdot0.556/100}=0.050$.

## Quality, decontamination, formatting

**After dedup**

1. **Heuristic filters.** Drop empty or truncated answers, refusals and boilerplate ("As an AI language model"), wrong language, broken markup, answers far longer or shorter than the task needs, and personal data (see **Privacy and data leakage**).
2. **Model-based quality scores.** Rate each pair with a judge model and a rubric (AlpaGasus kept scores ≥ 4.5 of 5; see **LLM-as-a-judge**), or score difficulty with the model's own losses (IFD, below). For code and maths, execute or check the answer.
3. **Decontamination.** Remove any training example that shares a long n-gram with an evaluation set. GPT-3 (Brown et al., 2020) measured overlap with 13-grams. Run the same MinHash pass between the training set and every benchmark you will report (see **LLM evaluation and benchmarking**).
4. **Diversity.** Embed examples, cluster them, and cap examples per cluster so one frequent request type does not dominate. Check coverage of the task taxonomy you care about.
5. **Formatting.** Render every example with the exact chat template and system prompt used at serving time, mask the loss to assistant tokens, and keep the end-of-turn token (see **Supervised fine-tuning (SFT)**). Split train and held-out sets after dedup, by cluster, so near-copies never straddle the split.

$$
\text{IFD}(Q,A)=\frac{s(A\mid Q)}{s(A)},\qquad s(A\mid Q)=-\frac{1}{|A|}\sum_{t}\log p_\theta(a_t\mid Q,a_{<t})
$$

- $s(A\mid Q)$: mean token loss of the answer given the instruction
- $s(A)$: mean token loss of the answer alone
- $\text{IFD}$: Instruction-Following Difficulty (Li et al., 2023): near or above 1 means the instruction does not help predict the answer, so the pair is hard or misaligned

Li et al. trained on the high-IFD examples, a small fraction of Alpaca, and matched or beat training on all of it. Pairs with IFD above 1 often have answers that do not follow from the instruction. Inspect them before keeping them.

**Reference points**

- **LIMA:** LLaMA 65B, 1,000 examples (750 mined from community Q&A, the rest written or adapted by the authors), up to 2,048 tokens each, 15 epochs; no RLHF.
- **AlpaGasus:** ChatGPT-rated filter of Alpaca's 52K → 9K pairs (score ≥ 4.5/5); the filtered model outperformed the original Alpaca.
- **MinHash:** Broder (1997). Collision probability of minima equals Jaccard; LSH banding gives the S-curve 1 − (1 − sʳ)ᵇ.
- **Contamination check:** GPT-3 used 13-gram overlap between training data and benchmarks to flag contaminated test items.

```python
import hashlib, itertools
from collections import defaultdict

def shingles(text, n=2):
    w = text.lower().split()
    return {" ".join(w[i:i + n]) for i in range(len(w) - n + 1)}

def minhash(sh, k=100):
    # k salted hashes; the signature keeps the minimum of each over the shingle set
    return [min(int.from_bytes(hashlib.blake2b(f"{j}:{s}".encode(), digest_size=8).digest(), "big")
                for s in sh) for j in range(k)]

def lsh_candidates(sigs, b=20, r=5):
    buckets = defaultdict(set)
    for doc, sig in sigs.items():
        for band in range(b):
            buckets[(band, tuple(sig[band * r:(band + 1) * r]))].add(doc)
    return {p for ids in buckets.values() for p in itertools.combinations(sorted(ids), 2)}

docs = {
    "a": "explain how a binary search works and give its time complexity in big o notation",
    "b": "explain how binary search works and give its time complexity in big o notation please",
    "c": "list five healthy breakfast ideas that take less than ten minutes to prepare",
}
sh = {d: shingles(t) for d, t in docs.items()}
sigs = {d: minhash(s) for d, s in sh.items()}
for x, y in sorted(lsh_candidates(sigs)):
    jac = len(sh[x] & sh[y]) / len(sh[x] | sh[y])          # verify with the exact Jaccard
    est = sum(u == v for u, v in zip(sigs[x], sigs[y])) / 100
    print(x, y, f"J={jac:.3f} MinHash={est:.2f}")
# a b J=0.750 MinHash=0.79
```

MinHash signatures, LSH banding and exact verification in pure Python (runs as is). For millions of examples, use a library such as datasketch with the same b and r.

> **Dedup before you split:** If near-duplicates are removed after the train/test split, copies of test items stay in training, and held-out scores measure memorisation. Deduplicate the whole pool first, split by cluster, then decontaminate the training set against every external benchmark.

> **Read the data:** Automated scores miss systematic problems: a scraper that cut every answer at 512 characters, a template that put the answer in the user turn, a generator that opens every reply with "Certainly!". Read 100 random examples, plus the 50 highest- and lowest-scored, after every pipeline change.

**Question:** You use LSH with b = 20 bands of r = 5 rows. Roughly where does a pair's chance of becoming a candidate cross 50%?

- A. Jaccard 0.20
- B. Jaccard 0.35
- C. Jaccard 0.55
- D. Jaccard 0.90

**Answer:** C. Jaccard 0.55 — The S-curve midpoint is about $(1/b)^{1/r}=(1/20)^{1/5}=0.549$. Exactly at $s=0.5$ the probability is $0.470$ and at $0.8$ it is $0.9996$.

**Question:** Which LIMA finding motivates small curated sets?

- A. More data always helps if it is filtered
- B. 1,000 diverse, high-quality examples produced answers preferred over or equal to GPT-4's in 43% of comparisons, and adding 16 times more homogeneous data did not help
- C. RLHF is required for instruction following
- D. Synthetic data cannot be used for fine-tuning

**Answer:** B. 1,000 diverse, high-quality examples produced answers preferred over or equal to GPT-4's in 43% of comparisons, and adding 16 times more homogeneous data did not help — LIMA used only SFT on 1,000 examples. Its ablation found that diversity and quality mattered while scaling homogeneous data from 2K to 32K did not, which supports the superficial alignment hypothesis.

**Key points**

- LIMA: 1,000 curated examples, no RLHF, LLaMA 65B equal to or preferred over GPT-4 in 43% of comparisons; quality and diversity beat quantity.
- MinHash estimates Jaccard with $k$ hashes; LSH bands give $P_\text{cand}=1-(1-s^r)^b$ with midpoint $(1/b)^{1/r}$; verify candidates exactly.
- Filter with heuristics, judge-model ratings (AlpaGasus: 9K of 52K) or IFD, and execution checks where answers are verifiable.
- Decontaminate against every evaluation set (n-gram or MinHash overlap) and dedup before splitting.
- Format exactly as served: same chat template, assistant-only loss, end-of-turn token kept.

---

From MLFoundry (https://www.mlfoundry.in). The web page adds interactive figures and diagrams.
