<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/differential-privacy-for-hugging-face-trainers-without-rewriting-your-training-loop-dsrjwvsx4" -->

---
title: Differential Privacy for Hugging Face Trainers – Without...
description: JetBrains Research has open-sourced DPTrainer, a library that integrates Opacus's DP-SGD implementation with Hugging Face's Trainer and TRL alignment trainers...
canonical: https://daily.dev/posts/differential-privacy-for-hugging-face-trainers-without-rewriting-your-training-loop-dsrjwvsx4
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Differential Privacy for Hugging Face Trainers – Without Rewriting Your Training Loop | daily.dev
og:description: JetBrains Research has open-sourced DPTrainer, a library that integrates Opacus's DP-SGD implementation with Hugging Face's Trainer and TRL alignment trainers...
og:url: https://daily.dev/posts/differential-privacy-for-hugging-face-trainers-without-rewriting-your-training-loop-dsrjwvsx4
og:image: https://api.daily.dev/og/posts/dSRjWvsx4.png
og:image:alt: Differential Privacy for Hugging Face Trainers – Without Rewriting Your Training Loop
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

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

**[JetBrains](https://daily.dev/sources/jetbrains)** · 7 min read · 0 upvotes · 0 comments

## Summary

JetBrains Research has open-sourced DPTrainer, a library that integrates Opacus's DP-SGD implementation with Hugging Face's Trainer and TRL alignment trainers (SFTTrainer, DPOTrainer), enabling differentially private training without rewriting training loops. Using a privatize_trainer function, developers can patch any Trainer subclass at runtime to add differential privacy while keeping all existing behavior like checkpointing, callbacks, and evaluation. The library handles noise calibration, gradient clipping, per-sample gradient computation, Poisson sub-sampling, privacy accounting, and budget-aware early stopping automatically, targeting teams that need to train transformers on sensitive data while meeting compliance requirements around personal data.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://blog.jetbrains.com/research/2026/08/dp-trainer>

## Questions this post answers

### How can I add differential privacy to Hugging Face Trainer without rewriting my training loop?

DPTrainer, an open-source library from JetBrains Research, extends transformers.Trainer with a PrivacyArguments dataclass so you set a target_epsilon and per_sample_max_grad_norm while all standard training arguments, callbacks, checkpointing, and evaluation keep working unchanged. It wraps Opacus's DP-SGD (gradient clipping, noise injection, Poisson sampling, privacy accounting) behind the existing Trainer interface.

_daily.dev surfaces practical guides like this for engineers adding compliance-grade privacy to model training._

### Can I apply differential privacy to TRL trainers like DPOTrainer or SFTTrainer?

Yes, using the privatize_trainer function from the DPTrainer library, which patches any Trainer-based class at runtime by injecting DPTrainer into its inheritance chain without touching the class's own logic. A single call such as privatize_trainer(DPOTrainer) adds DP-SGD while preserving the trainer's original reward computation, DPO loss, and generation behavior.

_Developers wiring privacy into RLHF or instruction-tuning pipelines can track tools like this on daily.dev._

### What does Opacus's DP-SGD actually do to gradients during training?

Differentially private stochastic gradient descent computes one gradient per individual sample rather than a single batch gradient, clips each per-sample gradient to bound outliers, aggregates the clipped gradients, and then injects calibrated noise so that no single training example is distinguishable in the result. This makes the trained model behave nearly identically whether or not any specific example was included.

_daily.dev helps ML practitioners keep up with privacy-preserving training techniques like DP-SGD._

## Similar posts on daily.dev

- [We Got Claude to Fine-Tune an Open Source LLM](https://daily.dev/posts/we-got-claude-to-fine-tune-an-open-source-llm-syttcqt2y) · Hugging Face · 4 upvotes · 0 comments
- [Beyond Anonymization: Understanding Differential Privacy](https://daily.dev/posts/beyond-anonymization-understanding-differential-privacy-b3mhmmeo5) · Medium · 0 upvotes · 0 comments
- [TRL v1.0: Post-Training Library Built to Move with the Field](https://daily.dev/posts/trl-v1-0-post-training-library-built-to-move-with-the-field-jllpohxch) · Hugging Face · 0 upvotes · 0 comments
- [The Python Ecosystem That Changed AI Development](https://daily.dev/posts/the-python-ecosystem-that-changed-ai-development-n4oc6yaom) · Towards Data Science · 2 upvotes · 0 comments

---

Tags: [#machine-learning](https://daily.dev/tags/machine-learning), [#pytorch](https://daily.dev/tags/pytorch)

[View this post on daily.dev](https://daily.dev/posts/differential-privacy-for-hugging-face-trainers-without-rewriting-your-training-loop-dsrjwvsx4)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Differential Privacy for Hugging Face Trainers – Without Rewriting Your Training Loop","url":"https://daily.dev/posts/differential-privacy-for-hugging-face-trainers-without-rewriting-your-training-loop-dsrjwvsx4","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/differential-privacy-for-hugging-face-trainers-without-rewriting-your-training-loop-dsrjwvsx4"},"datePublished":"2026-08-27T16:36:03.100Z","dateModified":"2026-10-15T02:22:06.074Z","description":"JetBrains Research has open-sourced DPTrainer, a library that integrates Opacus's DP-SGD implementation with Hugging Face's Trainer and TRL alignment trainers...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/ce0c7132c36be13f1c24d35a615138d0?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/ce0c7132c36be13f1c24d35a615138d0?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"JetBrains","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"JetBrains","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/53ecf0c676f34b3896ee109609d91efa","url":"https://daily.dev/sources/jetbrains"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/differential-privacy-for-hugging-face-trainers-without-rewriting-your-training-loop-dsrjwvsx4","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"machine-learning,pytorch","timeRequired":"PT7M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"JetBrains","item":"https://daily.dev/sources/jetbrains"},{"@type":"ListItem","position":3,"name":"Differential Privacy for Hugging Face Trainers – Without Rewriting Your Training Loop"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/differential-privacy-for-hugging-face-trainers-without-rewriting-your-training-loop-dsrjwvsx4#faq","mainEntity":[{"@type":"Question","name":"How can I add differential privacy to Hugging Face Trainer without rewriting my training loop?","acceptedAnswer":{"@type":"Answer","text":"DPTrainer, an open-source library from JetBrains Research, extends transformers.Trainer with a PrivacyArguments dataclass so you set a target_epsilon and per_sample_max_grad_norm while all standard training arguments, callbacks, checkpointing, and evaluation keep working unchanged. It wraps Opacus's DP-SGD (gradient clipping, noise injection, Poisson sampling, privacy accounting) behind the existing Trainer interface. daily.dev surfaces practical guides like this for engineers adding compliance-grade privacy to model training."}},{"@type":"Question","name":"Can I apply differential privacy to TRL trainers like DPOTrainer or SFTTrainer?","acceptedAnswer":{"@type":"Answer","text":"Yes, using the privatize_trainer function from the DPTrainer library, which patches any Trainer-based class at runtime by injecting DPTrainer into its inheritance chain without touching the class's own logic. A single call such as privatize_trainer(DPOTrainer) adds DP-SGD while preserving the trainer's original reward computation, DPO loss, and generation behavior. Developers wiring privacy into RLHF or instruction-tuning pipelines can track tools like this on daily.dev."}},{"@type":"Question","name":"What does Opacus's DP-SGD actually do to gradients during training?","acceptedAnswer":{"@type":"Answer","text":"Differentially private stochastic gradient descent computes one gradient per individual sample rather than a single batch gradient, clips each per-sample gradient to bound outliers, aggregates the clipped gradients, and then injects calibrated noise so that no single training example is distinguishable in the result. This makes the trained model behave nearly identically whether or not any specific example was included. daily.dev helps ML practitioners keep up with privacy-preserving training techniques like DP-SGD."}}]}
```

