Topic 5 of 5 · 5 min read
Testing a model for training-data leakage
Goal: Design a pre-release test that inserts canaries into training data and measures whether the model reproduces them.
Before a model ships, test whether it leaks: plant fake secrets in the training data, try to get them back, and compare behavior on training and unseen text. A model that passes your scrubbing step can still repeat things you did not expect.
Check yourself · 1 of 3
What is a canary in leak testing?
Why it matters
Scrubbing and DP reduce risk but do not prove anything about the finished model. Leak tests give evidence. They also give you a number to track from one version to the next, and a clear gate for release.
Three tests
- Canaries. Insert unique, fake secrets into the training data, for example
canary-7f3a-pilot-9921inside a realistic sentence. They must be random enough that the model cannot guess them. After training, prompt with the prefix and see if the model completes the secret. The Secret Sharer paper describes this and an exposure score that ranks how much more likely the model finds the canary than random guesses. Insert the same canary a different number of times (1, 10, 100) to see how repetition changes recall. - Extraction prompts. Write prefixes that look like your data: "Dear customer, your account number is". Sample many completions at a few temperatures and search them for real training strings, for patterns such as emails and phone numbers, and for any of your canaries.
- Membership inference. Compare the model's loss on a sample of training records with its loss on similar held-out records. A big gap means the model treats training text differently and an attacker can exploit it.
A release gate
Decide the pass rule before you run: for example, no canary inserted once is reproduced, no verbatim span longer than a set length from training appears in extraction output, and the loss gap on a held-out set is below a stated limit. Run the test on every candidate model, and store results with the model version.
def canary_hit(model, prefix, secret, n=50):
outs = [model.generate(prefix, temperature=0.8) for _ in range(n)]
return sum(secret in o for o in outs) / n
What goes wrong
- Canaries that are too guessable, such as "12345".
- One greedy sample. Leaks often show up only across many samples.
- Testing only the final model. Test the checkpoints you might ship.
Examples
A canary run
A team adds three canaries to a 5,000-row set: one inserted once, one 10 times and one 100 times. After fine-tuning, 50 samples per prefix show hits of 0, 3 and 41 out of 50. The result says what repetition does, and the team removes duplicates and retrains until the 10x canary also reads 0 hits.
Extraction sweep
Prefixes such as "Order confirmation for" and "Call me on" are sampled 200 times each. A script scans every output for emails, phone patterns and any 40-character span found in the training file. Any match fails the release.
Key points
- Plant unique fake secrets in the training data and test whether the model completes them.
- Insert canaries at different repeat counts to see how repetition changes recall.
- Sample many completions from data-like prefixes and search them for training text and PII patterns.
- Compare loss on training and held-out records to estimate membership-inference risk.
- Fix the pass rule before testing and keep results with each model version.
Common mistakes
- Using guessable canaries such as sequential numbers.
- Testing with a single greedy output.
- Deciding what counts as a pass after seeing the results.
- Skipping the test because the data was scrubbed.
Check yourself · 2 of 3
Why sample many completions in an extraction test?
Check yourself · 3 of 3
When should you set the pass rule for a leak test?
Practice items, not counted towards your mastery.
Start here
- Extracting Training Data from Large Language Models (Carlini et al.) (opens in a new tab)PaperFree
Shows that a language model can emit verbatim training sequences, including personal details, to a plain attacker.
- The Secret Sharer: measuring unintended memorization (Carlini et al.) (opens in a new tab)PaperFree
Introduces canary insertion and the exposure metric used to test whether a model memorized a planted secret.
- Quantifying Memorization Across Neural Language Models (opens in a new tab)PaperFree
Measures how memorization grows with model size, repeated data and prompt length.
More for this topic
- Membership Inference Attacks against Machine Learning Models (Shokri et al.) (opens in a new tab)PaperFree
Defines the attack that tests whether a given record was in the training set.
Where to go in your country
- EDPB Opinion 28/2024 on data protection aspects of AI models (opens in a new tab)DocsFreeEU
The EU regulators' view on when a trained model counts as anonymous and what that means for erasure.