Comments (5)
At least for exponential and normal distribution there appears to be an alternative approach.
One can create a Tensor and fill it with numbers from the given distribution in-place, as described here.
The in-place sampling even allows a generator, so the secure_generator
(now called self.random_number_generator
) created in the privacy_engine
could be utilized and therefore urandom
could be used as a source.
The device can be given to a tensor on declaration as well, so this appears to solve both problems.
However this approach lacks Laplace, again.
It is really problematic that the distributions are not standardized so that they behave the same (regarding parameters and configurations) no matter where we call them in the torch
libs.
from opacus.
Please let me know if I missed something.
from opacus.
I updated our _generate_noise
method to use a tensor for scale
and rate
to pass on the device.
So the only thing missing is the generator support. Hoping that our proposal in pytorch/pytorch#45115 gets addressed and we will be able to add it later and therefore use urandom
as a source.
For our research purposes however this should be sufficient.
def _generate_noise(self, max_norm, parameter):
if self.noise_multiplier > 0:
mean = 0
scale_scalar = self.noise_multiplier * max_norm
scale = torch.full(size=parameter.grad.shape, fill_value=scale_scalar, dtype=torch.float32, device=self.device)
if self.noise_type == "gaussian":
dist = torch.distributions.normal.Normal(mean, scale)
elif self.noise_type == "laplacian":
dist = torch.distributions.laplace.Laplace(mean, scale)
elif self.noise_type == "exponential":
rate = 1 / scale
dist = torch.distributions.exponential.Exponential(rate)
else:
dist = torch.distributions.normal.Normal(mean, scale)
noise = dist.sample()
return noise
return 0.0
from opacus.
Hi @pepper-jk !
It looks like this is blocked on the PyTorch side, but there are a few comments I'd like to add: in general, while I agree that it would be very simple for us to extend support for more distributions, we are hesitant to do that because we don't see a use case where you would want to use an exponential or Laplace distribution for DP-SGD.
That being said, you should absolutely be able to plug in whatever RNG you want without you having to edit our lib or upstream a PR to us necessarily, just like you don't need to commit a new nn.Module
you wrote to PyTorch every time. We are sorry this is not yet very doable, but we are working on making this lib a lot more modular for research for this exact reason :)
For now, I would recommend either editing in-place or monkeypatching in your script:
privacy_engine = PrivacyEngine(...)
privacy_engine._generate_noise = my_generate_noise
from opacus.
Just for the record, We since published a small library we extended to create exponantial and laplace noise using opacus.
In case anyone else needs this.
We are using it in the code for our Label Leakage from Gradients paper.
from opacus.
Related Issues (20)
- IndexError: pop from empty list HOT 1
- Implementing augmentation multiplicity using Functorch [Error: AttributeError: 'Tensor' object has no attribute '_forward_counter] HOT 1
- UnsupportedModuleError: [IllegalModuleConfigurationError('Model needs to be in training mode')] HOT 1
- LLM finetuning with Opacus HOT 2
- Add context manager to toggle on/off privacy in training loop
- Some issue with loading model with 'weight' as opposed to 'pretrained=True' HOT 1
- Error: Trying to add hooks twice to the same model HOT 2
- OverflowError: cannot convert float infinity to integer HOT 2
- ModuleValidator.fix() causes layer gradients to be None. HOT 1
- Integrating Opacus for a custom pytorch model raises errors but works fine on its own HOT 2
- `BatchSplittingSampler` return wrong length HOT 5
- Error occurred when executing GroundingDinoSAMSegment (segment anything): Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument mat2 in method wrapper_CUDA_mm)
- Why GDP and RDP give different result for the same config HOT 1
- Making a custom transformer architecture work with opacus HOT 3
- ValueError: Per sample gradient is not initialized. Not updated in backward pass? HOT 4
- Spectral normalization in Opacus HOT 4
- BatchMemoryManager for training private GANs
- Wrapper references can be easily replaced, consider using properties instead HOT 3
- Error in DPOptimizer: Inconsistency between batch_first argument of PrivacyEngine and DPMultiheadAttention HOT 2
- Grad Sample Module: Use full backward hook to save activations and backprop values. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from opacus.