Skip to main content

A not-so-efficient library for easy implementation of multi-gpu differential privacy

Project description

easy-dp

This library provides a simple way to implement differential privacy in PyTorch. The focus is only on supporting multi-gpu training with FSDP to allow for large models.

Note: the code is heavily based on private-transformers

Installation

pip install easy-dp

Usage

from easy_dp import PrivacyEngine

privacy_engine = PrivacyEngine(
    len_dataset=len(train_dataset),
    batch_size=batch_size,
    max_grad_norm=max_grad_norm,
    num_epochs=num_epochs,
    target_epsilon=target_epsilon,
    target_delta=target_delta,
)

Compute the gradient of a single sample, then clip it:

privacy_engine.clip_gradient(model.parameters())

You can accumulate the gradients into a variable, for example here we use the summed_clipped_gradients attribute. Then add noise to the accumulated gradients and divide by the batch size before saving the gradients to the model parameters:

for param in model.parameters():
    param.grad = privacy_engine.add_noise(param.summed_clipped_gradients)
    param.grad = param.grad / batch_size

Now you can call optimizer.step() to update the model parameters.

Upload to PyPI

python -m build

twine upload dist/*

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

easy_dp-0.1.0.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

easy_dp-0.1.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file easy_dp-0.1.0.tar.gz.

File metadata

  • Download URL: easy_dp-0.1.0.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.12

File hashes

Hashes for easy_dp-0.1.0.tar.gz
Algorithm Hash digest
SHA256 87c219280dca1338647ec54fed98a34408c8b9297d255fe6348ceadf6be9f42e
MD5 bd04cdbf3c346de1002eb0b1dd8d5786
BLAKE2b-256 763853411d881a48e46b4bf9e3a700d95381f4202db588485d111ade4a0fd8e3

See more details on using hashes here.

File details

Details for the file easy_dp-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: easy_dp-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.12

File hashes

Hashes for easy_dp-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ab97bc143a0c9cf17dd3fc68fc6342ae814b91f80574cdbde7482d94215ce4f9
MD5 2617aecca0e84ffa461d1d9dd57edab3
BLAKE2b-256 6be389ae8c578bb6dd945ec79fdbe887685874b84de74c58d2c6fb3131d1cc7e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page