MCPcopy
hub / github.com/meta-pytorch/opacus

github.com/meta-pytorch/opacus @v1.6.0 sqlite

repository ↗ · DeepWiki ↗ · release v1.6.0 ↗
1,318 symbols 4,901 edges 179 files 458 documented · 35%
README

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.

Core symbols most depended-on inside this repo

to
called by 132
benchmarks/layers.py
state_dict
called by 74
opacus/optimizers/optimizer.py
backward
called by 59
opacus/utils/fast_gradient_clipping_utils.py
zero_grad
called by 54
opacus/optimizers/optimizer.py
mean
called by 45
opacus/accountants/analysis/prv/prvs.py
make_private
called by 44
opacus/privacy_engine.py
item
called by 41
opacus/utils/fast_gradient_clipping_utils.py
load_state_dict
called by 29
opacus/optimizers/optimizer.py

Shape

Method 824
Function 250
Class 237
Route 7

Languages

Python98%
TypeScript2%

Modules by API surface

opacus/tests/privacy_engine_test.py89 symbols
opacus/tests/randomness_test.py46 symbols
benchmarks/layers.py37 symbols
opacus/tests/grad_sample_module_fast_gradient_clipping_test.py34 symbols
opacus/tests/mixed_precision_utils.py31 symbols
opacus/layers/dp_rnn.py28 symbols
opacus/tests/grad_sample_module_test.py25 symbols
opacus/tests/dpdataloader_test.py25 symbols
opacus/optimizers/optimizer.py25 symbols
opacus/tests/grad_sample_hooks_test.py24 symbols
opacus/tests/accountants_test.py24 symbols
opacus/grad_sample/grad_sample_module.py21 symbols

Dependencies from manifests, versioned

@babel/helper-compilation-targets8.0.0-alpha.14 · 1×
bl5.0.0 · 1×
browserslist4.21.4 · 1×
docusaurus1.14.7 · 1×
prismjs1.29.0 · 1×
numpy1.15 · 1×
opt-einsum3.3.0 · 1×
scipy1.2 · 1×
torch2.6.0 · 1×

For agents

$ claude mcp add opacus \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact