Registry / ai-ml / opacus

opacus

JSON →
library1.6.0pypypi✓ verified 83d ago

Opacus is a library for training PyTorch models with differential privacy. It supports per-sample gradient clipping, privacy accounting (including PRVAccountant), and fast gradient clipping. Current version is 1.5.4, compatible with Python >=3.7.5 and PyTorch 1.13+. It is actively maintained by Meta.

pip install opacus
INSTALL
IMPORT
SIG · OPACUS
O
opacus
ai-mlpythonv1.6.0
Install
71.1s avg
Import
11725ms
Disk
4992MB
Pass rate
4/ 10
Env Coverage4 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.6.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✕ build_error
✓ 83.6s
py 3.11
✕ build_error
✓ 75.6s
py 3.12
✕ build_error
✓ 64.6s
py 3.13
✕ build_error
✓ 60.7s
py 3.9
✕ build_error
✕ timeout
4992MB installed
● package 4992MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

PrivacyEngine
from opacus import PrivacyEngine
from opacus.privacy_engine import PrivacyEngine
PrivacyEngine is exported from top-level opacus package
DPOptimizer
from opacus.optimizers import DPOptimizer
from opacus import DPOptimizer
DPOptimizer is in opacus.optimizers, not top-level
GradSampleModule
from opacus import GradSampleModule
from opacus.grad_sample import GradSampleModule
GradSampleModule is exported from top-level opacus
PRVAccountant
from opacus.accountants import PRVAccountant
from opacus import PRVAccountant
PRVAccountant is in opacus.accountants, not top-level
RDPAccountant
from opacus.accountants import RDPAccountant
from opacus import RDPAccountant
RDPAccountant is in opacus.accountants

Minimal DP training loop with Opacus using PrivacyEngine.make_private.

import torch from torch import nn from torch.utils.data import DataLoader, TensorDataset from opacus import PrivacyEngine # Create a simple model torch.manual_seed(0) model = nn.Linear(10, 2) data = torch.randn(64, 10) labels = torch.randint(0, 2, (64,)) train_dataset = TensorDataset(data, labels) train_loader = DataLoader(train_dataset, batch_size=32) # Define optimizer and loss optimizer = torch.optim.SGD(model.parameters(), lr=0.01) criterion = nn.CrossEntropyLoss() # Attach privacy engine privacy_engine = PrivacyEngine() model, optimizer, train_loader = privacy_engine.make_private( module=model, optimizer=optimizer, data_loader=train_loader, noise_multiplier=0.5, max_grad_norm=1.0, ) # Training loop for epoch in range(2): for x, y in train_loader: outputs = model(x) loss = criterion(outputs, y) loss.backward() optimizer.step() optimizer.zero_grad() # Get privacy spent epsilon = privacy_engine.get_epsilon(delta=1e-5) print(f"Privacy spent: epsilon = {epsilon:.2f}")
Debug
Known issues
gotchaDataLoader must be passed to make_private() and the returned loader used for training. Using the original loader breaks privacy.
fix
Always use the data_loader returned by privacy_engine.make_private() in training loops.
affects: >=1.0.0
breakingIn v1.5, the make_private() method signature changed to accept keyword arguments and returns three objects (model, optimizer, data_loader). Old code unpacking two items (e.g., model, optimizer = privacy_engine.make_private(...)) will break.
fix
Update to: model, optimizer, data_loader = privacy_engine.make_private(...). Read the v1.5 migration guide.
affects: >=1.5.0
deprecatedThe old accountant interface via privacy_engine.accountant is deprecated. Use privacy_engine.get_epsilon(delta) directly.
fix
Replace privacy_engine.accountant.get_epsilon(delta) with privacy_engine.get_epsilon(delta).
affects: >=1.4.0
gotchaGhost clipping (for language models) requires a separate setup: use make_private_with_epsilon or specify ghost_clipping=True. Vanilla make_private does not apply ghost clipping.
fix
For LLM fine-tuning, use GhostClippingEngine or set ghost_clipping=True in make_private.
affects: >=1.5.0
Errors
Common errors & fixes
AttributeError: 'PrivacyEngine' object has no attribute 'make_private'
Using an older version of Opacus (pre-1.0) where the method was named differently.
fix
Upgrade to latest Opacus: pip install --upgrade opacus
TypeError: cannot unpack non-iterable PrivacyEngine object
Unpacking make_private() result into two variables but it returns three (model, optimizer, data_loader) since v1.5.
fix
Change to: model, optimizer, data_loader = privacy_engine.make_private(...)
RuntimeError: DataLoader worker process exited before finishing
Using the original data_loader instead of the one returned by make_private. Opacus replaces the sampler.
fix
Always use the data_loader returned by privacy_engine.make_private(). Never use the original DataLoader directly.
ModuleNotFoundError: No module named 'opt_einsum'
Opacus 1.5.0 and earlier required opt_einsum for linear layers; removed in 1.5.1+ but old installs may still need it.
fix
Upgrade to opacus>=1.5.1 or install opt_einsum: pip install opt_einsum
Upgrade
Version history
1.6.0latest on PyPI · released May 5, 2026
Audit
Dependencies
torchrequiredOpacus requires PyTorch >=1.13
Agent activity
20 hits · last 30 days
node
18
OpenAI (training)
1
Resources
opacus — pip install opacus · libregistry