Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing users to use large target_epsilon for debugging and research #604

Closed
jeandut opened this issue Sep 14, 2023 · 8 comments
Closed
Labels
enhancement New feature or request

Comments

@jeandut
Copy link

jeandut commented Sep 14, 2023

🚀 Feature

Allowing users to use large target_epsilon when making their pipeline DP (as input to get_noise_multiplier before make_private)

Motivation

Currently using a target budget of epsilon > 10. is generally not supported by Opacus for most datasets / batch sizes / number of gradient steps because of discretization issues stemming from Microsoft's prv_accountant (microsoft/prv_accountant#36) from which opacus's PRVAccountant implementation originates).

I argue that this should be changed for multiple reasons:

  • allow researchers to study every epsilon regimes they like
  • this cut-off of epsilon=10 is completely arbitrary. It is well known that epsilon is highly application-dependent: for some applications it might makes sense perfectly to use epsilon even as high as 50 if other mitigations are in place or if participants agree to release their data under this privacy setting; for some other applications epsilon=10 could be considered way too high if the gradients are deemed extremely sensitive for instance (remember that exp(10.)=22,026).
  • this would allow debugging complex implementations based on opacus: What happens if epsilon is very large ?: Do we retrieve initial performance without DP or is there still some batching-effects at play even with very low noise, which impacts accuracy ?

Warning the user that the privacy budget they set could be considered as high by some industries should be sufficient.
What do you think ?
In the meantime is there a relatively simple hack one could use in research experiments to use large target epsilons ?

Pitch

User provides a large target_epsilon say 50. in get_noise_multiplier and it runs wo throwing:

RuntimeError: Discrete mean differs from continuous mean significantly.

By instead displaying a warning.

Alternatives

In the meantime is there a relatively simple hack one could use in research experiments to use large target epsilons ? Such as exposing another custom get_noise_multiplier function wo this limitation ?

Additional context

I was hesitating between posting it in prv_accountant or here

@Solosneros
Copy link
Contributor

Solosneros commented Sep 14, 2023

Hi @jeandut,

if I understood correctly, the $\epsilon=10$ cut off isn't chosen because of it is being not according to industry standards but rather the numerics of the implementation are breaking down at that point. The prv_accountant from Microsoft just hasn't been tested for these high epsilons (I agree with the developers there that (in most applications) higher epsilons make little sense, but I don't want to argue about that without knowing your usecase).

Could you provide privacy parameters (num_steps, noise_multiplier and subsampling_ratio) for the case where it breaks down? We were able to fork the opacus get_noise_multiplier() and make a bit more robust but we are still waiting on internal review before proposing it here.

Have you considered using the rdp accountant instead? It should work as a drop-in replacement for the prv accountant when creating the privacy_engine. The privacy guarantees of prv are tighter than rdp but in the end you will just get a larger noise_multiplier for the same target_epsilon and target_delta. The rdp implementation is more robust in my experience.

@jeandut
Copy link
Author

jeandut commented Sep 15, 2023

Thank you so much @Solosneros for your quick answer !
Indeed the cutoff is due to the numerical implementation not being robust enough with large epsilon an not because of some ad-hoc logic. It is very cool that you guys have something in store to robustify it.

In my use case I am just tracing a DP curve for a research article and want to make sure that my implementation is asymptotically equivalent to the one wo DP by taking epsilon very large aka for debugging purposes.
I have run into this issue for multiple parameters values but typical values include i.e.:

sample_rate
0.19120458891013384
num_total_steps
5000
dp_target_epsilon
20.0
dp_target_delta
0.001

Or

sample_rate
0.19120458891013384
num_total_steps
5000
dp_target_epsilon
50.0
dp_target_delta
0.001

As for the reason I am using prv it is because it was the default value in opacus. But you are right a quick fix would be probably to switch to rdp. I will try that in the mean time !

@jeandut
Copy link
Author

jeandut commented Sep 18, 2023

Note that indeed using RDP instead of PRV allows to use high epsilons. Feel free to either close or relabel my issue.

@Solosneros
Copy link
Contributor

Hi @jeandut,

sorry for the delay. We tried out our fix further and while it seems a bit more robust it still fails occasionally.

We'll get back to this but maybe somebody else has a fix. The RDP workaround definitely works.

@Solosneros
Copy link
Contributor

I posted a potential fix to this to the prv github but I am not sure if it is valid. Let's see what the folks from Microsoft say. microsoft/prv_accountant#36 (comment)

@Solosneros
Copy link
Contributor

PR is open #606.

@HuanyuZhang HuanyuZhang added the enhancement New feature or request label Nov 13, 2023
@HuanyuZhang
Copy link
Contributor

Thanks! Will take a look.

facebook-github-bot pushed a commit that referenced this issue Nov 28, 2023
Summary:
## Types of changes

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
- [ ] Docs change / refactoring / dependency upgrade

## Motivation and Context / Related issue

Hi,

this PR fixes #601 and #604.

It will introduce the same fix as in microsoft/prv_accountant#38. Lukas (author of prv accountant, wulu473) said that  `In general, adding any additional points is safe and won't affect the robustness negatively.`

The cause of these errors seems to be the grid for computing the  `mean()` function of the `PrivacyRandomVariableTruncated` class. The grid (`points` variable) used to compute the mean is constant apart from the lowest (`self.t_min`) and highest point (`self.t_max`).

This PR determines the grid (`points` variable)  based on the lowest and highest point. More information is below.

Best

**Observation**

I debugged the code and arrived at some point at the `mean()` function of the `PrivacyRandomVariableTruncated` class. The grid (`points` variable) used to compute the mean is constant apart from the lowest (`self.t_min`) and highest point (`self.t_max`). See the line of code [here](https://github.com/microsoft/prv_accountant/blob/a95c4e2d41ff4886c3e4a84925edf878a6540e0a/prv_accountant/privacy_random_variables/abstract_privacy_random_variable.py#L52). It looks like this `[self.tmin, -0.1, -0.01, -0.001, -0.0001, -1e-05, 1e-05, 0.0001, 0.001, 0.01, 0.1, self.tmax]`.

It seems that the `tmin` and `tmax` are of the order of `[-12,12]` for the examples that I posted above and even up to `[-48,48]` for the example that jeandut posted in the #604 issue whereas they are more like `[-7,7]` for the [readme example for DP-SGD](https://github.com/microsoft/prv_accountant#dp-sgd).

We suspect that the integration breaks down when the gridspacing between between `tmin` / `tmax` get's too large.

**Proposed solution**

Determine the points grid based on `tmin` and `tmax`  but determines the start and end of the logspace based on `tmin` and `tmax`.

Before: (https://github.com/pytorch/opacus/blob/95df0904ae5d2b3aaa26b708e5067e9271624036/opacus/accountants/analysis/prv/prvs.py#L99-L106)

After:
```
# determine points based on t_min and t_max
lower_exponent = int(np.log10(np.abs(self.t_min)))
upper_exponent = int(np.log10(self.t_max))
points = np.concatenate(
    [
        [self.t_min],
        -np.logspace(start=lower_exponent, stop=-5, num=10),
        [0],
        np.logspace(start=-5, stop=upper_exponent, num=10),
        [self.t_max],
    ]
)
```

## How Has This Been Tested (if it applies)

I ran the examples from the issues #601 and #604 and they don't break anymore.

```
import opacus
target_delta = 0.001
target_epsilon = 20
steps = 5000
sample_rate=0.19120458891013384

for target_epsilon in [20, 50]:
    noise_multiplier = opacus.privacy_engine.get_noise_multiplier(target_delta=target_delta, target_epsilon=target_epsilon, steps=steps, sample_rate=sample_rate, accountant="prv")
    prv_accountant = opacus.accountants.utils.create_accountant("prv")
    prv_accountant.history = [(noise_multiplier, sample_rate, steps)]
    obtained_epsilon = prv_accountant.get_epsilon(delta=target_delta)
    print(f"target epsilon {target_epsilon}, obtained epsilon {obtained_epsilon}")

```
> target epsilon 20, obtained epsilon 19.999332284974717
target epsilon 50, obtained epsilon 49.99460075990896

```
target_epsilon = 4
batch_size = 50
epochs = 5
delta = 1e-05
expected_len_dataloader = 500 // batch_size
sample_rate = 1/expected_len_dataloader

noise_multiplier = opacus.privacy_engine.get_noise_multiplier(target_delta=target_delta, target_epsilon=target_epsilon, epochs=epochs, sample_rate=sample_rate, accountant="prv")
prv_accountant = opacus.accountants.utils.create_accountant("prv")
prv_accountant.history = [(noise_multiplier, sample_rate, int(epochs / sample_rate))]
obtained_epsilon = prv_accountant.get_epsilon(delta=target_delta)
print(f"target epsilon {target_epsilon}, obtained epsilon {obtained_epsilon}")

```
> target epsilon 4, obtained epsilon 3.9968389923130356

## Checklist

- [x] The documentation is up-to-date with the changes I made.
- [x] I have read the **CONTRIBUTING** document and completed the CLA (see **CONTRIBUTING**).
- [ ] All tests passed, and additional code has been covered with new tests.

Not able to run all tests locally and unsure if new tests should be added.

Pull Request resolved: #606

Reviewed By: HuanyuZhang

Differential Revision: D50111887

fbshipit-source-id: 2f77f8bc0e59837f765b87f2e107bc01015b9481
@HuanyuZhang
Copy link
Contributor

Committed PR #606 so I close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants