Skip to main content

Opacus


PyPI Downloads GitHub Actions Coverage Status PRs Welcome License

Opacus is a library that enables training PyTorch models with differential privacy. It supports training with minimal code changes required on the client, has little impact on training performance, and allows the client to online track the privacy budget expended at any given moment.

Target audience

This code release is aimed at two target audiences:

  1. ML practitioners will find this to be a gentle introduction to training a model with differential privacy as it requires minimal code changes.
  2. Differential Privacy researchers will find this easy to experiment and tinker with, allowing them to focus on what matters.

Latest updates

2024-12-18: We updated this tutorial to show how LoRA and peft library could be used in conjuncture with DP-SGD.

2024-08-20: We introduced Fast Gradient Clipping and Ghost Clipping(https://arxiv.org/abs/2110.05679) to Opacus, significantly reducing the memory requirements of DP-SGD. Please refer to our blogpost for more information.

Installation

The latest release of Opacus can be installed via pip:

pip install opacus

OR, alternatively, via conda:

conda install -c conda-forge opacus

You can also install directly from the source for the latest features (along with its quirks and potentially occasional bugs):

git clone https://github.com/pytorch/opacus.git
cd opacus
pip install -e .

Getting started

To train your model with differential privacy, all you need to do is to instantiate a PrivacyEngine and pass your model, data_loader, and optimizer to the engine's make_private() method to obtain their private counterparts.

# define your components as usual
model = Net()
optimizer = SGD(model.parameters(), lr=0.05)
data_loader = torch.utils.data.DataLoader(dataset, batch_size=1024)

# enter PrivacyEngine
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,
)
# Now it's business as usual

The MNIST example shows an end-to-end run using Opacus. The examples folder contains more such examples.

Learn more

Interactive tutorials

We've built a series of IPython-based tutorials as a gentle introduction to training models with privacy and using various Opacus features.

Technical report and citation

The technical report introducing Opacus, presenting its design principles, mathematical foundations, and benchmarks can be found here.

Consider citing the report if you use Opacus in your papers, as follows:

@article{opacus,
  title={Opacus: {U}ser-Friendly Differential Privacy Library in {PyTorch}},
  author={Ashkan Yousefpour and Igor Shilov and Alexandre Sablayrolles and Davide Testuggine and Karthik Prasad and Mani Malek and John Nguyen and Sayan Ghosh and Akash Bharadwaj and Jessica Zhao and Graham Cormode and Ilya Mironov},
  journal={arXiv preprint arXiv:2109.12298},
  year={2021}
}

Blogposts and talks

If you want to learn more about DP-SGD and related topics, check out our series of blogposts and talks:

FAQ

Check out the FAQ page for answers to some of the most frequently asked questions about differential privacy and Opacus.

Contributing

See the CONTRIBUTING file for how to help out. Do also check out the README files inside the repo to learn how the code is organized.

License

This code is released under Apache 2.0, as found in the LICENSE file.

Metadata

Release files for opacus 1.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for opacus 1.6.0
File Size Uploaded
opacus-1.6.0.tar.gz 184.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for opacus 1.6.0
File Interpreter ABI Platform
opacus-1.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 493.5 kB

Release files / opacus-1.6.0.tar.gz

Download URL opacus-1.6.0.tar.gz
Size 184.6 kB
Tags Source
SHA-256 checksum
How to use checksums
20d77252c1e8528cd5826fbaba345cd39ed5c3edb841707eafb6403fe276140a
BLAKE2b-256 checksum
How to use checksums
3139a92b485f5a9c65bdd18c574d24c3b17800e39f02d66e665ab9bfbde77c52
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / opacus-1.6.0-py3-none-any.whl

Download URL opacus-1.6.0-py3-none-any.whl
Size 308.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
342123624c56c09d47eaba44010ed216d51c63132b543d67cba53a49ed6dc41e
BLAKE2b-256 checksum
How to use checksums
452dcb1b04cc674f3e06dbe1afc02ed977f066f39c7a4a313372353f82c26117
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release history Release notifications | RSS feed

This release

1.6.0 This release

2 release files

1.5.4

2 release files

1.5.3

2 release files

1.5.2

2 release files

1.5.1

2 release files

1.5

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.15.0

2 release files

0.14.0

2 release files

0.13.0

2 release files

0.11.0

2 release files

0.10.1

2 release files

0.9.1

2 release files

0.9.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page