Skip to content

Commit bc2f389

Browse files
Update TFPs dp_accounting dependency.
PiperOrigin-RevId: 456293476
1 parent adce0fd commit bc2f389

24 files changed

+106
-150
lines changed

Diff for: tensorflow_privacy/privacy/analysis/BUILD

+4-16
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ py_library(
1313
name = "compute_dp_sgd_privacy_lib",
1414
srcs = ["compute_dp_sgd_privacy_lib.py"],
1515
srcs_version = "PY3",
16-
deps = [
17-
"@com_google_differential_py//python/dp_accounting:dp_event",
18-
"@com_google_differential_py//python/dp_accounting/rdp:rdp_privacy_accountant",
19-
],
16+
deps = ["@com_google_differential_py//python/dp_accounting"],
2017
)
2118

2219
py_binary(
@@ -45,10 +42,7 @@ py_binary(
4542
py_library(
4643
name = "compute_noise_from_budget_lib",
4744
srcs = ["compute_noise_from_budget_lib.py"],
48-
deps = [
49-
"@com_google_differential_py//python/dp_accounting:dp_event",
50-
"@com_google_differential_py//python/dp_accounting/rdp:rdp_privacy_accountant",
51-
],
45+
deps = ["@com_google_differential_py//python/dp_accounting"],
5246
)
5347

5448
py_test(
@@ -67,11 +61,7 @@ py_library(
6761
srcs = ["rdp_accountant.py"],
6862
srcs_version = "PY3",
6963
visibility = ["//visibility:public"],
70-
deps = [
71-
"@com_google_differential_py//python/dp_accounting:dp_event",
72-
"@com_google_differential_py//python/dp_accounting:privacy_accountant",
73-
"@com_google_differential_py//python/dp_accounting/rdp:rdp_privacy_accountant",
74-
],
64+
deps = ["@com_google_differential_py//python/dp_accounting"],
7565
)
7666

7767
py_test(
@@ -119,9 +109,7 @@ py_test(
119109
python_version = "PY3",
120110
srcs_version = "PY3",
121111
deps = [
122-
":rdp_accountant",
123112
":tree_aggregation_accountant",
124-
"@com_google_differential_py//python/dp_accounting:dp_event",
125-
"@com_google_differential_py//python/dp_accounting/rdp:rdp_privacy_accountant",
113+
"@com_google_differential_py//python/dp_accounting",
126114
],
127115
)

Diff for: tensorflow_privacy/privacy/analysis/compute_dp_sgd_privacy_lib.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@
1717
import math
1818

1919
from absl import app
20-
21-
from com_google_differential_py.python.dp_accounting import dp_event
22-
from com_google_differential_py.python.dp_accounting.rdp import rdp_privacy_accountant
20+
from com_google_differential_py.python.dp_accounting
2321

2422

2523
def apply_dp_sgd_analysis(q, sigma, steps, orders, delta):
2624
"""Compute and print results of DP-SGD analysis."""
2725

28-
accountant = rdp_privacy_accountant.RdpAccountant(orders)
26+
accountant = dp_accounting.RdpAccountant(orders)
2927

30-
event = dp_event.SelfComposedDpEvent(
31-
dp_event.PoissonSampledDpEvent(q, dp_event.GaussianDpEvent(sigma)), steps)
28+
event = dp_accounting.SelfComposedDpEvent(
29+
dp_accounting.PoissonSampledDpEvent(q,
30+
dp_accounting.GaussianDpEvent(sigma)),
31+
steps)
3232

3333
accountant.compose(event)
3434

Diff for: tensorflow_privacy/privacy/analysis/compute_noise_from_budget_lib.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@
1717
import math
1818

1919
from absl import app
20+
from com_google_differential_py.python.dp_accounting
2021
from scipy import optimize
2122

22-
from com_google_differential_py.python.dp_accounting import dp_event
23-
from com_google_differential_py.python.dp_accounting.rdp import rdp_privacy_accountant
24-
2523

2624
def apply_dp_sgd_analysis(q, sigma, steps, orders, delta):
2725
"""Compute and print results of DP-SGD analysis."""
2826

29-
accountant = rdp_privacy_accountant.RdpAccountant(orders)
30-
event = dp_event.SelfComposedDpEvent(
31-
dp_event.PoissonSampledDpEvent(q, dp_event.GaussianDpEvent(sigma)), steps)
27+
accountant = dp_accounting.RdpAccountant(orders)
28+
event = dp_accounting.SelfComposedDpEvent(
29+
dp_accounting.PoissonSampledDpEvent(q,
30+
dp_accounting.GaussianDpEvent(sigma)),
31+
steps)
3232
accountant.compose(event)
3333
return accountant.get_epsilon_and_optimal_order(delta)
3434

Diff for: tensorflow_privacy/privacy/analysis/rdp_accountant.py

+12-16
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,9 @@
4141
eps, _, opt_order = rdp_accountant.get_privacy_spent(rdp, target_delta=delta)
4242
"""
4343

44+
from com_google_differential_py.python.dp_accounting
4445
import numpy as np
4546

46-
from com_google_differential_py.python.dp_accounting import dp_event
47-
from com_google_differential_py.python.dp_accounting import privacy_accountant
48-
from com_google_differential_py.python.dp_accounting.rdp import rdp_privacy_accountant
49-
5047

5148
def _compute_rdp_from_event(orders, event, count):
5249
"""Computes RDP from a DpEvent using RdpAccountant.
@@ -61,15 +58,14 @@ def _compute_rdp_from_event(orders, event, count):
6158
"""
6259
orders_vec = np.atleast_1d(orders)
6360

64-
if isinstance(event, dp_event.SampledWithoutReplacementDpEvent):
65-
neighboring_relation = privacy_accountant.NeighboringRelation.REPLACE_ONE
66-
elif isinstance(event, dp_event.SingleEpochTreeAggregationDpEvent):
67-
neighboring_relation = privacy_accountant.NeighboringRelation.REPLACE_SPECIAL
61+
if isinstance(event, dp_accounting.SampledWithoutReplacementDpEvent):
62+
neighboring_relation = dp_accounting.NeighboringRelation.REPLACE_ONE
63+
elif isinstance(event, dp_accounting.SingleEpochTreeAggregationDpEvent):
64+
neighboring_relation = dp_accounting.NeighboringRelation.REPLACE_SPECIAL
6865
else:
69-
neighboring_relation = privacy_accountant.NeighboringRelation.ADD_OR_REMOVE_ONE
66+
neighboring_relation = dp_accounting.NeighboringRelation.ADD_OR_REMOVE_ONE
7067

71-
accountant = rdp_privacy_accountant.RdpAccountant(orders_vec,
72-
neighboring_relation)
68+
accountant = dp_accounting.RdpAccountant(orders_vec, neighboring_relation)
7369
accountant.compose(event, count)
7470
rdp = accountant._rdp # pylint: disable=protected-access
7571

@@ -96,8 +92,8 @@ def compute_rdp(q, noise_multiplier, steps, orders):
9692
Returns:
9793
The RDPs at all orders. Can be `np.inf`.
9894
"""
99-
event = dp_event.PoissonSampledDpEvent(
100-
q, dp_event.GaussianDpEvent(noise_multiplier))
95+
event = dp_accounting.PoissonSampledDpEvent(
96+
q, dp_accounting.GaussianDpEvent(noise_multiplier))
10197

10298
return _compute_rdp_from_event(orders, event, steps)
10399

@@ -129,8 +125,8 @@ def compute_rdp_sample_without_replacement(q, noise_multiplier, steps, orders):
129125
Returns:
130126
The RDPs at all orders, can be np.inf.
131127
"""
132-
event = dp_event.SampledWithoutReplacementDpEvent(
133-
1, q, dp_event.GaussianDpEvent(noise_multiplier))
128+
event = dp_accounting.SampledWithoutReplacementDpEvent(
129+
1, q, dp_accounting.GaussianDpEvent(noise_multiplier))
134130

135131
return _compute_rdp_from_event(orders, event, steps)
136132

@@ -195,7 +191,7 @@ def get_privacy_spent(orders, rdp, target_eps=None, target_delta=None):
195191
raise ValueError(
196192
"Exactly one out of eps and delta must be None. (None is).")
197193

198-
accountant = rdp_privacy_accountant.RdpAccountant(orders)
194+
accountant = dp_accounting.RdpAccountant(orders)
199195
accountant._rdp = rdp # pylint: disable=protected-access
200196

201197
if target_eps is not None:

Diff for: tensorflow_privacy/privacy/analysis/tree_aggregation_accountant_test.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,10 @@
1414
# ==============================================================================
1515

1616
from absl.testing import parameterized
17+
from com_google_differential_py.python.dp_accounting
1718
import tensorflow as tf
18-
1919
from tensorflow_privacy.privacy.analysis import tree_aggregation_accountant
2020

21-
from com_google_differential_py.python.dp_accounting import dp_event
22-
from com_google_differential_py.python.dp_accounting.rdp import rdp_privacy_accountant
23-
2421

2522
class TreeAggregationTest(tf.test.TestCase, parameterized.TestCase):
2623

@@ -33,8 +30,7 @@ def test_compute_eps_tree(self, noise_multiplier, eps):
3330
steps_list, target_delta = 1600, 1e-6
3431
rdp = tree_aggregation_accountant.compute_rdp_tree_restart(
3532
noise_multiplier, steps_list, orders)
36-
new_eps = rdp_privacy_accountant.compute_epsilon(orders, rdp,
37-
target_delta)[0]
33+
new_eps = dp_accounting.compute_epsilon(orders, rdp, target_delta)[0]
3834
self.assertLess(new_eps, eps)
3935

4036
@parameterized.named_parameters(
@@ -67,7 +63,7 @@ def test_compute_eps_tree_decreasing(self, steps_list):
6763
for noise_multiplier in [0.1 * x for x in range(1, 100, 5)]:
6864
rdp = tree_aggregation_accountant.compute_rdp_tree_restart(
6965
noise_multiplier, steps_list, orders)
70-
eps = rdp_privacy_accountant.compute_epsilon(orders, rdp, target_delta)[0]
66+
eps = dp_accounting.compute_epsilon(orders, rdp, target_delta)[0]
7167
self.assertLess(eps, prev_eps)
7268
prev_eps = eps
7369

@@ -90,8 +86,9 @@ def test_no_tree_no_sampling(self, total_steps, noise_multiplier):
9086
orders = [1 + x / 10. for x in range(1, 100)] + list(range(12, 64))
9187
tree_rdp = tree_aggregation_accountant.compute_rdp_tree_restart(
9288
noise_multiplier, [1] * total_steps, orders)
93-
accountant = rdp_privacy_accountant.RdpAccountant(orders)
94-
accountant.compose(dp_event.GaussianDpEvent(noise_multiplier), total_steps)
89+
accountant = dp_accounting.RdpAccountant(orders)
90+
accountant.compose(
91+
dp_accounting.GaussianDpEvent(noise_multiplier), total_steps)
9592
rdp = accountant._rdp # pylint: disable=protected-access
9693
self.assertAllClose(tree_rdp, rdp, rtol=1e-12)
9794

Diff for: tensorflow_privacy/privacy/dp_query/BUILD

+9-9
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ py_library(
3636
deps = [
3737
":discrete_gaussian_utils",
3838
":dp_query",
39-
"@com_google_differential_py//python/dp_accounting:dp_event",
39+
"@com_google_differential_py//python/dp_accounting",
4040
],
4141
)
4242

@@ -59,7 +59,7 @@ py_library(
5959
deps = [
6060
":discrete_gaussian_utils",
6161
":dp_query",
62-
"@com_google_differential_py//python/dp_accounting:dp_event",
62+
"@com_google_differential_py//python/dp_accounting",
6363
],
6464
)
6565

@@ -82,7 +82,7 @@ py_library(
8282
deps = [
8383
":dp_query",
8484
":normalized_query",
85-
"@com_google_differential_py//python/dp_accounting:dp_event",
85+
"@com_google_differential_py//python/dp_accounting",
8686
],
8787
)
8888

@@ -103,7 +103,7 @@ py_library(
103103
srcs_version = "PY3",
104104
deps = [
105105
":dp_query",
106-
"@com_google_differential_py//python/dp_accounting:dp_event",
106+
"@com_google_differential_py//python/dp_accounting",
107107
],
108108
)
109109

@@ -125,7 +125,7 @@ py_library(
125125
srcs_version = "PY3",
126126
deps = [
127127
":dp_query",
128-
"@com_google_differential_py//python/dp_accounting:dp_event",
128+
"@com_google_differential_py//python/dp_accounting",
129129
],
130130
)
131131

@@ -167,7 +167,7 @@ py_library(
167167
srcs_version = "PY3",
168168
deps = [
169169
":dp_query",
170-
"@com_google_differential_py//python/dp_accounting:dp_event",
170+
"@com_google_differential_py//python/dp_accounting",
171171
],
172172
)
173173

@@ -194,7 +194,7 @@ py_library(
194194
":dp_query",
195195
":gaussian_query",
196196
":quantile_estimator_query",
197-
"@com_google_differential_py//python/dp_accounting:dp_event",
197+
"@com_google_differential_py//python/dp_accounting",
198198
],
199199
)
200200

@@ -274,7 +274,7 @@ py_library(
274274
deps = [
275275
":dp_query",
276276
":tree_aggregation",
277-
"@com_google_differential_py//python/dp_accounting:dp_event",
277+
"@com_google_differential_py//python/dp_accounting",
278278
],
279279
)
280280

@@ -286,7 +286,7 @@ py_library(
286286
":distributed_discrete_gaussian_query",
287287
":dp_query",
288288
":gaussian_query",
289-
"@com_google_differential_py//python/dp_accounting:dp_event",
289+
"@com_google_differential_py//python/dp_accounting",
290290
],
291291
)
292292

Diff for: tensorflow_privacy/privacy/dp_query/discrete_gaussian_query.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@
1515

1616
import collections
1717

18+
from com_google_differential_py.python.dp_accounting
1819
import tensorflow as tf
1920
from tensorflow_privacy.privacy.dp_query import discrete_gaussian_utils
2021
from tensorflow_privacy.privacy.dp_query import dp_query
2122

22-
from com_google_differential_py.python.dp_accounting import dp_event
23-
2423

2524
class DiscreteGaussianSumQuery(dp_query.SumAggregationDPQuery):
2625
"""Implements DPQuery for discrete Gaussian sum queries.
@@ -84,5 +83,5 @@ def add_noise(v):
8483
return tf.ensure_shape(noised_v, v.shape)
8584

8685
result = tf.nest.map_structure(add_noise, sample_state)
87-
event = dp_event.UnsupportedDpEvent()
86+
event = dp_accounting.UnsupportedDpEvent()
8887
return result, global_state, event

Diff for: tensorflow_privacy/privacy/dp_query/distributed_discrete_gaussian_query.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@
1515

1616
import collections
1717

18+
from com_google_differential_py.python.dp_accounting
1819
import tensorflow as tf
1920
from tensorflow_privacy.privacy.dp_query import discrete_gaussian_utils
2021
from tensorflow_privacy.privacy.dp_query import dp_query
2122

22-
from com_google_differential_py.python.dp_accounting import dp_event
23-
2423

2524
class DistributedDiscreteGaussianSumQuery(dp_query.SumAggregationDPQuery):
2625
"""Implements DPQuery for discrete distributed Gaussian sum queries.
@@ -108,5 +107,5 @@ def preprocess_record(self, params, record):
108107
def get_noised_result(self, sample_state, global_state):
109108
# Note that by directly returning the aggregate, this assumes that there
110109
# will not be missing local noise shares during execution.
111-
event = dp_event.UnsupportedDpEvent()
110+
event = dp_accounting.UnsupportedDpEvent()
112111
return sample_state, global_state, event

Diff for: tensorflow_privacy/privacy/dp_query/distributed_skellam_query.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@
1515

1616
import collections
1717

18+
from com_google_differential_py.python.dp_accounting
1819
import tensorflow as tf
1920
from tensorflow_privacy.privacy.dp_query import dp_query
2021
from tensorflow_privacy.privacy.dp_query import normalized_query
2122

22-
from com_google_differential_py.python.dp_accounting import dp_event
23-
2423

2524
class DistributedSkellamSumQuery(dp_query.SumAggregationDPQuery):
2625
"""Implements DPQuery interface for discrete distributed sum queries.
@@ -127,7 +126,7 @@ def preprocess_record(self, params, record):
127126

128127
def get_noised_result(self, sample_state, global_state):
129128
"""The noise was already added locally, therefore just continue."""
130-
event = dp_event.UnsupportedDpEvent()
129+
event = dp_accounting.UnsupportedDpEvent()
131130
return sample_state, global_state, event
132131

133132

Diff for: tensorflow_privacy/privacy/dp_query/gaussian_query.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
import collections
1717
import distutils
1818

19+
from com_google_differential_py.python.dp_accounting
1920
import tensorflow as tf
2021
from tensorflow_privacy.privacy.dp_query import dp_query
2122

22-
from com_google_differential_py.python.dp_accounting import dp_event
23-
2423

2524
class GaussianSumQuery(dp_query.SumAggregationDPQuery):
2625
"""Implements DPQuery interface for Gaussian sum queries.
@@ -94,6 +93,6 @@ def add_noise(v):
9493

9594
result = tf.nest.map_structure(add_noise, sample_state)
9695
noise_multiplier = global_state.stddev / global_state.l2_norm_clip
97-
event = dp_event.GaussianDpEvent(noise_multiplier)
96+
event = dp_accounting.GaussianDpEvent(noise_multiplier)
9897

9998
return result, global_state, event

0 commit comments

Comments
 (0)