All courses

Deduplication, memorization and contamination

Explain why duplicated text gets memorized verbatim and wastes compute. Explain how the questions of benchmarks (standard test sets used to score models) leaking into the training data inflate evaluation scores.

Builds on: Building a pretraining corpus

▶ Watch this lesson Start the whole course

What the animation says

Many continuations, or one

Training rewards one thing: predicting the token that actually came next. After 'the cat sat on the', the web offers mat, sofa, floor, roof, and many more. So the best guess is a spread over words, and spreads force the model to learn general patterns. Now take a license blurb that appears ten thousand times across the crawl. After 'free of', what will the model predict?

Copies multiply weight

Charge, at about ninety-nine percent. Every copy shows the same next word, so the model memorizes it verbatim. Here is the loss. Add up over distinct sequences, weight each by its number of copies, and divide by the total training tokens. So ten thousand copies pull ten thousand times as hard as one unique text. But once the blurb is memorized, the surprisal of each token is nearly zero. So, after memorization, which adds more to the loss: one more blurb copy, or one fresh fifty-token document?

Compute spent, nothing learned

The fresh document, by far. Here are the numbers. A memorized blurb token has probability point nine nine. Its surprisal is about one hundredth of a nat. Fifty tokens times that: half a nat for the whole copy. A fresh document, at about three nats per token, gives one hundred fifty nats. Yet both cost the same compute. Ten thousand copies are half a million tokens, mostly teaching nothing. Deduplication spends that compute on new text instead.

A leaked benchmark

Repetition has a second cost: test questions inside the training data. A benchmark has a thousand questions. The model's real skill gets sixty percent of them right. But two hundred questions, with their answers, were posted online and crawled. The model recalls those two hundred perfectly. So what score does it report?

Catching leaks by shared spans

Sixty-eight percent: eight points of recall, not skill. To catch leaks, compare long runs of words. This function collects every run of eight words in a row. Two texts are flagged if they share any run. The same check also finds duplicates in the corpus. Here is a crawled quiz page, and two versions of a test question. Tap the version this check would miss.

Trust clean items

Exact span matching catches copies, not rewordings. So add one more check. Split the benchmark into flagged items and clean items, and compare accuracy. If all two hundred leaks were flagged, that is one hundred percent against sixty. The gap is recall. So: copies add weight, repeated text gets memorized, and later copies burn compute. And trust a score only on clean, unseen items.

What does a model learn from repeated text?

Deduplication is one step in the corpus pipeline. Why does it matter so much? If the same license blurb appears 10,000 times in the crawl, what does the model take from it, and what does that cost? And if a benchmark's questions and answers were posted online and crawled, what does a high score on that benchmark still tell you?

One continuation, repeated, means recall

Cross-entropy rewards predicting exactly what came next. For most prefixes, the web offers many different continuations. The best prediction is then a spread over plausible tokens, which forces the model to learn general patterns. A duplicated passage offers the **same** continuation every time, so the best prediction is that exact token: **verbatim memorization**. Once the passage is memorized, further copies teach almost nothing but still cost full compute.

Copies multiply a sequence's weight

$$\mathcal{L}=\frac{1}{D}\sum_s c_s\sum_t -\log p_\theta(x_{t+1}\mid x_{\le t})$$ Here $s$ ranges over distinct sequences, $c_s$ is its number of copies, and $D$ is the total number of training tokens. Sequence $s$ pulls on the model with $c_s$ times the weight of a unique one. If the data holds only one continuation after a prefix, that prefix's term is minimized by $p_\theta=1$ on it. Once that is nearly reached, surprisal $\approx 0$, so extra copies add almost no learning signal.

Wasted tokens and an inflated score

**Waste:** a 50-token blurb appears 10,000 times. Once it is memorized, each token has $p=0.99$, so surprisal is $-\ln 0.99\approx0.01$ nats and one copy contributes $\approx0.5$ nats of loss. A fresh 50-token document at ~3 nats/token contributes ~150 nats, so it still has much more to teach. Nearly all of the blurb's 500,000 tokens of compute buy almost nothing. **Contamination:** a benchmark has 1000 questions and the true accuracy is 60%. 200 questions leaked with answers and are recalled perfectly. Score $=0.8(60\%)+0.2(100\%)=68\%$.

Catching leaks by shared long spans

Deduplication and contamination checks both look for long runs of words shared between two texts. Here `spans` collects every run of 8 consecutive words, and two texts are flagged if they share any run. This exact-span match catches copies. A reworded question slips past the check, so some leaks survive it. One more check helps: compare accuracy on flagged items against clean items. In the example above, that would be 100% vs 60%.
def spans(text, n=8):
    w = text.lower().split()
    return {tuple(w[i:i+n]) for i in range(len(w) - n + 1)}

train_doc = "quiz: if a train leaves at 3 pm going 60 mph how far by 5 pm? answer 120 miles"
test_q = "if a train leaves at 3 pm going 60 mph how far by 5 pm?"
reworded = "a train departs 3 pm at 60 mph; distance by 5 pm?"

print(len(spans(train_doc) & spans(test_q)) > 0)    # True: leak found
print(len(spans(train_doc) & spans(reworded)) > 0)  # False: leak missed

Remember this

Each copy of a text adds to its weight in the objective. Repeated text has only one continuation, so the model memorizes it verbatim, and later copies burn compute while teaching almost nothing. Benchmark items in the training data let a model score by recall, so the score overstates its ability. Trust results only on clean, unseen items.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.