opacus
Train PyTorch models with Differential Privacy
What it is and what it does
Opacus is a PyTorch library that adds differential privacy to model training through DP-SGD (Differentially Private Stochastic Gradient Descent). It wraps your existing model, optimizer, and data loader with privacy-aware counterparts via a PrivacyEngine, allowing you to train with formal privacy guarantees while monitoring privacy budget expenditure in real time.
The library targets both ML practitioners seeking an accessible introduction to privacy-preserving training and differential privacy researchers needing flexibility for experimentation. It depends on numpy, torch, scipy, and opt-einsum, and recent updates have introduced Fast Gradient Clipping and Ghost Clipping to reduce memory overhead during training.
Use it for:
- Train models on sensitive data (medical records, financial data) with formal privacy guarantees for regulatory compliance
- Federated learning scenarios where model updates must not leak information about individual training samples
- Research into differential privacy algorithms and DP-SGD variants with minimal boilerplate
- Building text classifiers on BERT with privacy (as shown in updated tutorials with LoRA/peft integration)
- Image classification tasks where privacy budget must be tracked and controlled throughout training
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Opacus enables training PyTorch models with differential privacy using DP-SGD, requiring minimal code changes to add privacy guarantees while tracking privacy budget consumption.
Yes, if you need to train PyTorch models with formal differential privacy guarantees. The library is actively maintained, has low installation friction, carries a permissive license, and integrates cleanly into existing PyTorch workflows. Install if privacy-preserving training is a requirement; skip if you have no privacy constraints.
Install
opacus on PyPI
pip
pip install opacusuv
uv add opacuspoetry
poetry add opacusInstalling opacus
Before you install
Low friction installation with a pure Python wheel. Active maintenance with recent updates (2024-12-18 and 2024-08-20) including Fast Gradient Clipping and Ghost Clipping features. Repository shows 1949 stars and active development.
License in practice
Released under Apache-2.0 (permissive), allowing commercial and private use with minimal restrictions. You may use, modify, and distribute the code freely provided you include the license notice.
Quickstart
pip install opacus
from opacus import PrivacyEngine
import torch
model = Net()
optimizer = torch.optim.SGD(model.parameters(), lr=0.05)
data_loader = torch.utils.data.DataLoader(dataset, batch_size=1024)
privacy_engine = PrivacyEngine()
model, optimizer, data_loader = privacy_engine.make_private(
module=model,
optimizer=optimizer,
data_loader=data_loader,
noise_multiplier=1.1,
max_grad_norm=1.0,
)
Requires torch and numpy. Model architecture must be compatible with DP-SGD (some layer types may need validation via Opacus's module validator).
Verify before relying
- Performance overhead compared to standard PyTorch training under typical workloads
- Compatibility with specific PyTorch versions beyond the stated Python 3.7.5+ requirement
- Memory savings quantified for the Ghost Clipping feature mentioned in recent updates
Package facts
| License | Apache-2.0 (permissive) |
| Python support | supports the current Python release (>=3.7.5) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 4 — numpy, torch, scipy, opt-einsum |
| Maintenance | actively maintained — 101 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 170,543/month — #10,391 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: opacus-1.6.0-py3-none-any.whl
Keywords: PyTorch, Differential Privacy, DP-SGD, DP SGD, Privacy Preserving Machine Learning, PPML, PPAI
Tags
More Scientific/Engineering packages
NumPy provides an N-dimensional array object…
permissive · top 100 on PyPI
pandaspandas provides fast, flexible data structures…
permissive · top 100 on PyPI
scipyscipy provides numerical algorithms for…
permissive · top 100 on PyPI
scikit-learnscikit-learn provides a comprehensive Python…
permissive · top 1,000 on PyPI
dilldill extends Python's pickle module to…
permissive · top 1,000 on PyPI
multiprocessMultiprocess is an enhanced fork of Python's…
permissive · top 1,000 on PyPI
torchsdeSolves stochastic differential equations (SDEs)…
permissive · top 5,000 on PyPI
pytorch-rangerRanger is a PyTorch optimizer that combines…
permissive · top 15,000 on PyPI
torchPyTorch provides GPU-accelerated tensor…
permissive · top 1,000 on PyPI
zukoZuko implements normalizing flows in PyTorch as…
permissive · top 15,000 on PyPI
accelerateAccelerate abstracts away distributed training…
permissive · top 1,000 on PyPI
adam-atan2-pytorchProvides an Adam optimizer variant using atan2…
permissive · top 15,000 on PyPI
k-diffusionk-diffusion is a PyTorch library implementing…
permissive · top 15,000 on PyPI
pytorch-forecastingPyTorch Forecasting provides neural network…
permissive · top 15,000 on PyPI
flashoptimFlashOptim provides drop-in replacements for…
permissive · top 15,000 on PyPI
pytorch_optimizerProvides a collection of modern optimizers,…
permissive · top 15,000 on PyPI