skillfed

opacus

Train PyTorch models with Differential Privacy

opacus v1.6.0 170.5K downloads/30d#10,391 on PyPI1,949
Permissive license Apache-2.0 Active released

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 opacus

uv

uv add opacus

poetry

poetry add opacus

Installing 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

Development Status :: 4 - BetaIntended Audience :: DevelopersIntended Audience :: EducationIntended Audience :: Science/ResearchLicense :: OSI Approved :: Apache Software LicenseProgramming Language :: Python :: 3 :: OnlyTopic :: Scientific/Engineering

Tags

differential privacy pytorchdp-sgd trainingprivacy preserving machine learningprivacy budget trackingdifferentially private neural networkspytorch privacy libraryfederated learning privacy
differential-privacypytorch-extensionprivacy-preserving-ml

More Scientific/Engineering packages