-
Notifications
You must be signed in to change notification settings - Fork 359
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
Conservative search for target epsilon in get_noise_multiplier #348
Closed
alexandresablayrolles
wants to merge
1
commit into
pytorch:main
from
alexandresablayrolles:export-D34003401
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,26 +13,19 @@ | |
# limitations under the License. | ||
|
||
from opacus.accountants import create_accountant | ||
from typing import Optional | ||
|
||
|
||
# min range bound when searching for sigma given epsilon | ||
DEFAULT_SIGMA_MIN_BOUND = 0.01 | ||
# starting point for a max range bound when searching for sigma given epsilon | ||
DEFAULT_SIGMA_MAX_BOUND = 10 | ||
# condition to halt binary search for sigma given epsilon | ||
SIGMA_PRECISION = 0.01 | ||
# max possible value for returned sigma. | ||
# Noise higher than MAX_SIGMA considered unreasonable | ||
MAX_SIGMA = 2000 | ||
|
||
MAX_SIGMA = 1e6 | ||
|
||
def get_noise_multiplier( | ||
*, | ||
target_epsilon: float, | ||
target_delta: float, | ||
sample_rate: float, | ||
epochs: int, | ||
epochs: Optional[int] = None, | ||
steps: Optional[int] = None, | ||
accountant: str = "rdp", | ||
epsilon_tolerance: float = 0.01, | ||
**kwargs, | ||
) -> float: | ||
r""" | ||
|
@@ -44,7 +37,9 @@ def get_noise_multiplier( | |
target_delta: the privacy budget's delta | ||
sample_rate: the sampling rate (usually batch_size / n_data) | ||
epochs: the number of epochs to run | ||
steps: number of steps to run | ||
accountant: accounting mechanism used to estimate epsilon | ||
epsilon_tolerance: precision for the binary search | ||
Returns: | ||
The noise level sigma to ensure privacy budget of (target_epsilon, target_delta) | ||
""" | ||
|
@@ -53,27 +48,33 @@ def get_noise_multiplier( | |
raise NotImplementedError( | ||
"get_noise_multiplier is currently only supports RDP accountant" | ||
) | ||
if (steps is None) == (epochs is None): | ||
raise ValueError( | ||
"get_noise_multiplier takes as input EITHER a number of steps or a number of epochs" | ||
) | ||
if steps is None: | ||
steps = int(epochs / sample_rate) | ||
|
||
eps = float("inf") | ||
sigma_min = DEFAULT_SIGMA_MIN_BOUND | ||
sigma_max = DEFAULT_SIGMA_MAX_BOUND | ||
eps_high = float("inf") | ||
accountant = create_accountant(mechanism=accountant) | ||
|
||
while eps > target_epsilon: | ||
sigma_max = 2 * sigma_max | ||
accountant.steps = [(sigma_max, sample_rate, int(epochs / sample_rate))] | ||
eps = accountant.get_epsilon(delta=target_delta, **kwargs) | ||
if sigma_max > MAX_SIGMA: | ||
sigma_low, sigma_high = 0, 10 | ||
while eps_high > target_epsilon: | ||
sigma_high = 2 * sigma_high | ||
accountant.steps = [(sigma_high, sample_rate, int(epochs / sample_rate))] | ||
eps_high = accountant.get_epsilon(delta=target_delta, **kwargs) | ||
if sigma_high > MAX_SIGMA: | ||
raise ValueError("The privacy budget is too low.") | ||
|
||
while sigma_max - sigma_min > SIGMA_PRECISION: | ||
sigma = (sigma_min + sigma_max) / 2 | ||
accountant.steps = [(sigma, sample_rate, int(epochs / sample_rate))] | ||
while target_epsilon - eps_high > epsilon_tolerance: | ||
sigma = (sigma_low + sigma_high) / 2 | ||
accountant.steps = [(sigma, sample_rate, steps)] | ||
eps = accountant.get_epsilon(delta=target_delta, **kwargs) | ||
|
||
if eps < target_epsilon: | ||
sigma_max = sigma | ||
sigma_high = sigma | ||
eps_high = eps | ||
else: | ||
sigma_min = sigma | ||
sigma_low = sigma | ||
|
||
return sigma | ||
return sigma_high | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A possible corner case when |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a comment before which I recommend to leave because the constant looks a bit surprising.