TAI BUI
Back to curriculum
BuildPython

Differential Privacy for LLMs

DP-SGD is still the standard. It injects noise into gradient updates and gets a formal (epsilon, delta) guarantee in return, at a real cost in compute, memory, and utility. Parameter-efficient DP fine-tuning (LoRA + DP-SGD) is the common 2025 configuration (ACM 2025). The evidence on leakage pulls in two directions: canary-based membership inference (Duan et al., 2024) reports limited success against language models, while training-data extraction (Carlini et al., 2021; Nasr et al., 2025) recovers a lot of verbatim memorization. A March 2025 paper (arXiv:2503.06808) argues the gap comes from what each one measures, inserted canaries versus the most extractable data. Its new canary designs allow loss-based MIA without shadow models and give the first nontrivial DP audit of an LLM trained on real data with realistic DP guarantees. Two alternatives to DP training are PMixED (arXiv:2403.15638), which does private prediction at inference time with a mixture of experts over next-token distributions, and DP synthetic data generation (Google Research 2024). One emerging attack to know about is Differential Privacy Reversal via LLM Feedback, which exploits confidence-score leakage.

TIME: ~60 minutesPREREQUISITES: Phase 01 · 09 (information theory), Phase 10 · 01 (large-model training)

Learning Objectives

  • Define (epsilon, delta)-differential privacy and state the DP-SGD recipe.
  • Explain the 2024-2025 tension: canary MIA vs training-data extraction give different pictures.
  • Describe PMixED and why inference-time private prediction is an alternative to DP training.
  • Describe the Differential Privacy Reversal via LLM Feedback attack.

The Problem

LLMs memorize. Carlini et al. (2021) showed that production language models will reproduce verbatim training text when prompted for it. Differential privacy is the formal defense: train so the output is provably insensitive to any single training example. The 2024-2025 evidence says DP-SGD is necessary, but the ε values people deploy may not match the threat model they care about.

Warm-up0/3
1. Which quantity measures how much observing one random variable reveals about another?Warm-up
Two overlapping circles labeled rain today and umbrella carried, with a blank question mark in the overlap and a caption asking how much learning one tells you about the other.
2. A measurement is perturbed by adding random Gaussian noise. What happens as the noise standard deviation grows?Warm-up
A scatter of repeated readings of one fixed true value, generated with real Gaussian noise draws: a tight cluster of 14 points at a small noise standard deviation next to a widely spread cluster of 14 points at a larger one.
3. In gradient descent, what does the learning rate control?Warm-up
A number line showing one weight's position before an update, with two illustrative gradient steps of different sizes landing at different points depending on the learning rate used.

The Concept

(ε, δ)-differential privacy

A randomized algorithm M is (ε, δ)-DP if for any two datasets differing in one example and any event S: P(M(D) in S) <= e^ε * P(M(D') in S) + δ.

Interpretation: the output distribution is close enough (parametrized by ε) that the contribution of any single individual cannot be reliably inferred, except with probability δ.

DP-SGD

Abadi et al. 2016. The standard recipe:

  1. Sample a mini-batch.
  2. Compute per-example gradients.
  3. Clip each per-example gradient to a threshold C.
  4. Sum the clipped gradients and add Gaussian noise with std σ * C.
  5. Use the noisy sum to update parameters.

Privacy cost is tracked by an accountant (Moments Accountant, Rényi DP accountant). Reported ε values in the LLM literature vary widely by threat model, data sensitivity, and utility target; there is no universally "safe" default ε. Published examples span roughly ε ≈ 1–10 in some LLM training settings, but treat those as illustrations, not recommended defaults. Lower ε generally needs more noise, and more noise tends to cost utility.

LoRA + DP-SGD

Full DP-SGD on a frontier model is prohibitive. LoRA (Hu et al. 2022) confines the updates to a small adapter, which shrinks per-example gradient storage. LoRA + DP-SGD is the common 2025 configuration. The DP guarantee covers the adapter, and the base model stays fixed.

The 2024-2025 tension

Two lines of evidence:

  • Canary MIA (Duan et al. 2024). Insert unique canaries into training data, measure whether a membership-inference attacker can identify them. It reports limited success on language models, which suggests MIA is hard.
  • Training-data extraction (Carlini 2021, Nasr et al. 2025). Prompt the model with a prefix; measure whether it recovers verbatim text from training. It reports substantial memorization, which suggests that in the sense that matters, MIA is easy.

The March 2025 paper (arXiv:2503.06808) resolves it by pointing out that the two measure different things. MIA asks "is example e in D?" on inserted canaries. Extraction asks "what can I recover of D?" For privacy, the "most extractable" example is the one that counts, and canaries under-report it because nobody optimized them to be extractable.

The paper also introduces new canary designs. They support loss-based MIA without shadow models, and they yield the first nontrivial DP audit of an LLM trained on real data with realistic DP guarantees.

Alternatives to DP training

  • PMixED (arXiv:2403.15638). Private prediction at inference time. Mixture of experts on next-token distributions; each expert sees a shard of training data; aggregation adds noise for DP. Avoids DP training entirely.
  • DP synthetic data generation (Google Research 2024). LoRA-fine-tune with DP-SGD, sample synthetic data, train a downstream classifier on the synthetic data.

Both sidestep the utility cost of full DP training, and the price is a different threat model.

Differential Privacy Reversal via LLM Feedback

This is an emerging 2025 attack. It uses a DP-trained model's confidence scores as an oracle to re-identify individuals. Outputs can look clean while the confidence distributions still leak.

The defense is to withhold confidences, or to truncate or quantize them before exposure. That is a separate requirement on top of (ε, δ)-DP training.

Where this fits in Phase 18

Lessons 20-21 cover bias and fairness, this lesson covers privacy, Lesson 23 covers provenance via watermarking, and Lesson 27 covers the regulatory data-provenance layer.

Check: The Concept0/21
1. What does (epsilon, delta)-differential privacy bound for a randomized algorithm M?Define
Two datasets differing in one record feeding into a randomized algorithm M, with the lesson's own inequality written below: P(M(D) in S) is at most e^epsilon times P(M(D') in S) plus delta.
2. Which of these is NOT a step of DP-SGD?Define
DP-SGD's five real stages in order: sample a mini-batch, compute per-example gradients, clip to C, sum the clipped gradients and add Gaussian noise with std sigma times C, then update the weights.
3. What is LoRA + DP-SGD?Define
A frozen base model with a no-entry mark, next to a small separate adapter box with an arrow showing the DP-SGD update landing only on the adapter, not on the base model.
4. What does a membership inference attack (MIA) try to determine?Define
An attacker box asking a model the question 'was example e in D?', with the yes-or-no outcome left blank.
5. In privacy evaluation of a language model, what is a canary?Define
A grid of plain training-data dots with one dot drawn distinctly and labeled 'the canary,' next to an attacker box that studies the model's behavior on the whole dataset.
6. What does PMixED (arXiv:2403.15638) offer as an alternative to DP training?Define
A box labeled DP-SGD showing a training run with clipping, noise and an update, next to a separate box labeled PMixED with its mechanism left as an open question of what it does instead of that training run.
7. What is the DP Reversal via LLM Feedback attack?Define
A DP-trained model with two separate outgoing arrows, one to a box labeled response text that reads as ordinary clean text, and one to a box labeled confidence scores, a list of numbers, with neither box marked as risky.
8. What does the DP synthetic data approach (Google Research 2024) do?Define
Sensitive data feeding into a box named the DP synthetic data approach (Google Research 2024), with its output left as a blank question mark.
9. Why is LoRA + DP-SGD the common 2025 configuration for LLMs?Explain
Two bars of illustrative width contrasting per-example gradient storage: a wide bar for all of a frontier model's weights under full DP-SGD, and a much narrower bar for just an adapter's weights, with the lesson's own line that full DP-SGD on a frontier model is prohibitive.
10. In the (epsilon, delta) inequality, what does delta allow?Explain
The same (epsilon, delta) inequality from before, with the delta term circled, and a blank question asking what that term allows.
11. How does lowering epsilon usually affect training with DP-SGD?Explain
A line chart of approx-epsilon against sigma, computed by calling main.py's own analytical_epsilon function at sigma 0.5, 1.0, 2.0 and 4.0 with steps and delta held fixed, showing epsilon falling as sigma rises.
12. How did the March 2025 paper (arXiv:2503.06808) resolve the canary-MIA versus extraction tension?Explain
Two boxes side by side: canary MIA, which inserts canaries and tests whether an attacker can identify them, reporting limited success, and extraction, which prompts with a prefix and checks for verbatim recovered text, reporting substantial memorization, with the paper's resolution of the tension left blank.
13. Which question does a training-data extraction attack ask, in contrast to canary MIA?Explain
A three-step flow showing the extraction attack's own procedure: prompt the model with a training prefix, let it continue, then check the continuation for verbatim text, with a note that none of the quiz's four phrasings is shown here.
14. Why can PMixED give privacy without any DP training run?Explain
Three expert boxes each with a blank 'data?' chip, all arrows converging on an aggregation box that has a blank question asking where noise gets added.
15. Why can a DP-trained model's clean-looking outputs still enable re-identification?Explain
Two chips side by side, one saying response text reads clean, the other saying confidence scores are a separate channel, with a blank question asking whether a clean channel protects the other one.
16. A team fine-tuned with DP-SGD and its API returns full confidence scores. How should they address the DP Reversal via LLM Feedback attack?Apply
A DP-SGD fine-tuned model whose API response includes both text and a confidence field returned in full, with a blank question asking how to address the DP Reversal attack.
17. A vendor says its LLM is private because a canary-based MIA scored at chance and it trained with epsilon = 8. What should an auditor ask next?Analyze
A vendor's stated claim, that a canary-based MIA scored at chance and the model trained with epsilon equals 8, with the auditor's next question left blank.
18. A teammate says PMixED and DP-SGD training give the same kind of guarantee because both are 'DP'. What is the flaw?Analyze
Two boxes, DP-SGD with a guarantee covering trained weights and PMixED with a guarantee covering served predictions, under a teammate's claim that both are 'DP,' with the question of whether that makes the guarantees the same left blank.
19. Predictions over sensitive records must be private per query at inference time, with no DP training run. Which design fits?Design
A stated requirement, private predictions per query at inference time with no DP training run allowed, with which design fits left blank.
20. You must privately fine-tune a very large model on sensitive text with limited GPU memory and serve it through an API. Which design fits?Design
A stated requirement, fine-tuning a very large model on sensitive text with limited GPU memory and serving it through an API, with which design fits left blank.
21. A team must give outside vendors training data for a downstream classifier without exposing raw records. Which approach follows the lesson's alternatives?Design
A stated requirement, giving outside vendors what a downstream classifier needs without exposing raw records, with which approach fits left blank.

Use It

main.py runs a DP-SGD toy on a 2-feature binary logistic regression with 500 training and 200 test points. It sweeps the noise multiplier σ over 0.0, 0.5, 1.0, 2.0, and 4.0 with the clipping norm C fixed at 1.0, and prints an approximate ε and the test accuracy for each. The ε is an analytical Gaussian-mechanism proxy, not output from a real accountant, so read it as a rough trend. There is no canary attack in the file. Exercise 2 asks you to add one.

Check: Use It0/8
1. Why is the main.py toy only a rough stand-in for real DP-SGD?Explain
Real source lines from main.py's dp_sgd function showing its per-example for-loop, clip call, and noise draw inside that same loop, next to the Concept section's own stage names for the real recipe, with a question asking how many examples one noise draw covers in the code above.
2. main.py sweeps sigma over 0.0, 0.5, 1.0, 2.0 and 4.0. What does the sigma = 0.0 run amount to?Apply
Real source lines showing the sigma sweep and the noise-draw line that uses sigma times C as its standard deviation, with a blank question asking what each noise draw becomes when sigma is 0.0.
3. main.py trains 10 epochs over 500 training points with one update per example. How many steps does it pass to analytical_epsilon?Apply
Real source lines from main.py showing train_data has 500 points, epochs is 10, and steps is passed to analytical_epsilon as epochs times the length of train_data, with the product left blank.
4. In main.py, clip is called with the gradient [3.0, 4.0] and C = 1.0. What does it return?Apply
The real clip function's source code, which scales a gradient down only when its norm exceeds C, applied to the call clip([3.0, 4.0], C=1.0) with the returned list left blank.
5. analytical_epsilon in main.py is proportional to 1 / sigma with steps and delta fixed. Going from sigma = 2.0 to sigma = 4.0, what happens to the printed proxy epsilon?Apply
A boxed statement that approx-epsilon is proportional to 1 over sigma with steps and delta fixed, next to sigma doubling from 2.0 to 4.0, with the resulting change to approx-epsilon left blank.
6. With sigma = 2.0 and C = 1.0, what standard deviation does each Gaussian noise draw in dp_sgd use?Apply
The real noise-draw line from dp_sgd using sigma times C as its standard deviation, with this run's sigma of 2.0 and C of 1.0 given and the resulting standard deviation left blank.
7. On one seeded run of main.py, sigma = 4.0 scores higher test accuracy than sigma = 2.0. What is the sound reading?Analyze
A bar chart of main.py's own real test-accuracy for sigma 2.0 and sigma 4.0 from one seeded run, with sigma 4.0 scoring higher.
8. A report cites the approx-epsilon column from main.py as proof that a setting is private enough. What is wrong with that?Analyze
A small table of real sigma and approx-epsilon values from main.py next to a report's quoted claim that this proves the setting is private enough, with what's wrong with that claim left blank.

Ship It

This lesson produces outputs/skill-dp-audit.md. Hand it a DP claim about a language model deployment and it audits four things: the (ε, δ) values, the accountant used, the MIA evaluation protocol, and whether anyone has assessed confidence-score exposure.

Check: Ship It0/1
1. An audit request lists the epsilon and delta values, names the accountant, and describes a canary MIA protocol, but never mentions what the API returns alongside text. Which of the skill's four checks is missing?Analyze
Three named checks from an audit request, epsilon and delta values, the accountant named, and the canary MIA protocol, with a fourth check left unnamed.
Author's Note

I'd run main.py first and read the ε column with suspicion. It is a proxy that composes a Gaussian-mechanism bound over every step, and with 10 epochs on 500 points that means 5000 steps. On my run the proxy stays far above the 1-10 range the closing printout mentions, even at σ = 4.0. So the table shows the direction of the tradeoff and nothing about whether a setting is private enough.

Two other things about the toy are worth knowing. It updates on one example at a time and adds noise at every update, where real DP-SGD sums clipped gradients over a mini-batch and noises the sum once. And accuracy is not monotone in σ on a single seeded run: at the seed in the file, σ = 4.0 scores higher than σ = 2.0. With 200 test points and one seed I wouldn't read anything into that, and it is a good reason to average over seeds before drawing a utility curve.

My bet is that the part to carry out of this lesson is the extraction result, not the accountant. A low ε on a canary test doesn't tell you the most extractable training example is safe. When I audit a DP claim, I'd ask what was measured before I ask what the ε was.

Exercises

  1. Run code/main.py. Sweep σ in {0.5, 1.0, 2.0} and report the (ε, δ)-accuracy trade-off. Identify the point at which utility collapses.

  2. Implement a canary insertion and a log-loss test. Measure detection rate before and after DP-SGD at σ = 1.0.

  3. Read Nasr et al. 2025 on training-data extraction. Why does extraction success not collapse under moderate ε? What does this imply about MIA-as-evaluation?

  4. Design a deployment using PMixED (arXiv:2403.15638) that operates entirely at inference time. What is the threat model that PMixED addresses that DP-SGD does not?

  5. Sketch the DP Reversal via LLM Feedback attack. Design a countermeasure that limits confidence-score leakage and estimate its deployment cost.

Key Terms

TermWhat people sayWhat it actually means
DP"(ε, δ)-differential privacy"Formal privacy: output distribution close under neighbouring-dataset change
DP-SGD"noise-injected SGD"Gradient clipping + Gaussian noise addition; standard DP training
LoRA + DP-SGD"efficient private fine-tune"DP-SGD on low-rank adapters; standard 2025 configuration
MIA"membership inference"Attack that determines whether an example was in training data
Canary"inserted watermark example"Unique training example used to measure DP leakage
PMixED"private inference mixture"Inference-time DP via mixture-of-experts on next-token distributions
DP Reversal"confidence leakage attack"Attack that uses a model's confidence as an oracle for re-identification

Further Reading

Code

Ships With