---
title: "Differential Privacy for Hugging Face Trainers – Without Rewriting Your Training Loop"
description: "Training large language models on sensitive data risks memorization and membership inference attacks, yet standard high-level transformer tools lack native differential privacy capabilities. JetBrains Research created and open-sourced DPTrainer, a library that integrates PyTorch's Opacus framework with the Hugging Face Trainer ecosystem. The library provides differential privacy through differentially private stochastic gradient descent without requiring manual training loops or source code modifications. DPTrainer extends base training arguments with a PrivacyArguments dataclass, managing per-sample gradient calculation, gradient clipping, noise injection, and Poisson-sampled data loading. A dynamic patching mechanism injects differential privacy into specialized subclasses like DPOTrainer and SFTTrainer, while built-in privacy accountants track the cumulative privacy budget across training steps and checkpoints."
---

# Differential Privacy for Hugging Face Trainers – Without Rewriting Your Training Loop

[Jetbrains](https://yomu.fyi/company/jetbrains) · Katie Fraser · Aug 27, 2026

**Type:** Announcement

## Summary

Training large language models on sensitive data risks memorization and membership inference attacks, yet standard high-level transformer tools lack native differential privacy capabilities. JetBrains Research created and open-sourced DPTrainer, a library that integrates PyTorch's Opacus framework with the Hugging Face Trainer ecosystem. The library provides differential privacy through differentially private stochastic gradient descent without requiring manual training loops or source code modifications. DPTrainer extends base training arguments with a PrivacyArguments dataclass, managing per-sample gradient calculation, gradient clipping, noise injection, and Poisson-sampled data loading. A dynamic patching mechanism injects differential privacy into specialized subclasses like DPOTrainer and SFTTrainer, while built-in privacy accountants track the cumulative privacy budget across training steps and checkpoints.

## Context

Training LLMs on sensitive user data risks training data memorization, leakage through adversarial prompting, and membership inference attacks. While Opacus enables differentially private stochastic gradient descent (DP-SGD) in PyTorch, it requires manual training loops that do not integrate cleanly with Hugging Face Trainer or TRL workflows.

## Approach / What changed

JetBrains Research built and open-sourced DPTrainer to integrate Opacus with Hugging Face Trainer. It wraps models with Opacus's GradSampleModule, wraps optimizers with DPOptimizer, provides DPDataLoader with Poisson sub-sampling, tracks privacy budgets with DPCallback, and offers privatize\_trainer to patch classes like DPOTrainer and SFTTrainer dynamically.

## Takeaways

- DPTrainer automates DP-SGD mechanics including per-sample gradient clipping, Gaussian noise addition, and automated noise calibration from a target epsilon.
- The privatize\_trainer function patches specialized Trainer subclasses at runtime, enabling DP-SGD without modifying task-specific loss or generation logic.
- Privacy accounting state is saved during checkpointing, and training automatically halts via early stopping once the defined privacy budget is exhausted.

**Tags:** [LLMs](https://yomu.fyi/topic/llm), [Machine Learning](https://yomu.fyi/topic/machine-learning), [Open Source](https://yomu.fyi/topic/open-source), [Privacy](https://yomu.fyi/topic/privacy), [Python](https://yomu.fyi/topic/python)

- Source: [Jetbrains](https://blog.jetbrains.com/research/2026/08/dp-trainer)
- Source URL: https://blog.jetbrains.com/research/2026/08/dp-trainer
- Ingested by Yomu: 2026-08-27T20:00:43.401Z

[Read original post](https://blog.jetbrains.com/research/2026/08/dp-trainer)
