opacus icon indicating copy to clipboard operation
opacus copied to clipboard

The Sigma binary search is not conservative and can overshoot target_epsilon

Open tholop opened this issue 4 years ago • 3 comments

🐛 Small bug/unexpected behavior

There is a binary search to compute the value of sigma necessary to reach a given target_epsilon here: https://github.com/pytorch/opacus/blob/579546053050827fa9d71587c87ac477983db17b/opacus/accountants/utils.py#L55 The search stops when the search interval is smaller than SIGMA_PRECISION, which is 0.01 by default.

The problem is that the value returned for sigma can be anywhere in [sigma_opt - SIGMA_PRECISION/2, sigma_opt + SIGMA_PRECISION/2], where sigma_opt is the value that would give us exactly target_epsilon. As a result, the binary search sometimes overshoots sigma_opt and returns a value of sigma that yields epsilon > target_epsilon.

This is expected for a standard binary search, but for privacy budget I would expect a conservative binary search that returns a value in [sigma_opt, sigma_opt + SIGMA_PRECISION], to make sure that sigma doesn't over-consume privacy. This is not very impactful for small values of SIGMA_PRECISION but the error is not completely negligible (my example below shows an over-consumption of 0.5 for epsilon).

If this is the intended behavior, I think that the docstring should make it clear. Right now, it claims that it returns "The noise level sigma to ensure privacy budget of (target_epsilon, target_delta)".

To Reproduce

Copy the following lines at the end of utils.py:

def main():
    
    sample_rate = 0.01
    epochs = 100
    target_delta = 1e-5
    target_epsilon = 5.0
    
    sigma = get_noise_multiplier(target_epsilon=target_epsilon, target_delta=target_delta, sample_rate=sample_rate,epochs=epochs, accountant="rdp")
    accountant = create_accountant(mechanism="rdp")
    accountant.steps = [(sigma, sample_rate, int(epochs / sample_rate))]
    
    actual_epsilon = accountant.get_epsilon(delta=target_delta)

    print(f"Target epsilon: {target_epsilon}.\n Sigma returned by binary search: {sigma}.\n Actual epsilon using this sigma: {actual_epsilon}")

if __name__ == "__main__":
   main()

Then, execute python utils.py:

Target epsilon: 5.0.
 Sigma returned by binary search: 1.1715283203125.
 Actual epsilon using this sigma: 5.055592282662654

Expected behavior

I would expect a conservative estimate of sigma that would give epsilon below the target epsilon.

tholop avatar Feb 02 '22 03:02 tholop

A quick fix would be to change line 65 (https://github.com/pytorch/opacus/blob/579546053050827fa9d71587c87ac477983db17b/opacus/accountants/utils.py#L65) to return sigma_min

The logic is that sigma_min always corresponds to a sigma value that provides an epsilon that this less than target_epsilon, while sigma_max gives an epsilon that is greater than (or equal to) target_epsilon. So returning sigma_min after the binary search stops would produce the desired behavior.

gcormode avatar Feb 02 '22 13:02 gcormode

@gcormode's proposed solution is implemented in #348 (with the caveat that it's sigma_high that corresponds to the lower epsilon, since epsilon is a decreasing function of sigma).

alexandresablayrolles avatar Feb 04 '22 13:02 alexandresablayrolles

#348 landed, closing the issue

igorshh avatar Mar 25 '22 16:03 igorshh