Skip to content

Commit

Permalink
Update TFPs dp_accounting dependency.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 456293476
  • Loading branch information
michaelreneer authored and tensorflower-gardener committed Jun 21, 2022
1 parent adce0fd commit ff9935b
Show file tree
Hide file tree
Showing 24 changed files with 106 additions and 150 deletions.
20 changes: 4 additions & 16 deletions tensorflow_privacy/privacy/analysis/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ py_library(
name = "compute_dp_sgd_privacy_lib",
srcs = ["compute_dp_sgd_privacy_lib.py"],
srcs_version = "PY3",
deps = [
"@com_google_differential_py//python/dp_accounting:dp_event",
"@com_google_differential_py//python/dp_accounting/rdp:rdp_privacy_accountant",
],
deps = ["@com_google_differential_py//python/dp_accounting"],
)

py_binary(
Expand Down Expand Up @@ -45,10 +42,7 @@ py_binary(
py_library(
name = "compute_noise_from_budget_lib",
srcs = ["compute_noise_from_budget_lib.py"],
deps = [
"@com_google_differential_py//python/dp_accounting:dp_event",
"@com_google_differential_py//python/dp_accounting/rdp:rdp_privacy_accountant",
],
deps = ["@com_google_differential_py//python/dp_accounting"],
)

py_test(
Expand All @@ -67,11 +61,7 @@ py_library(
srcs = ["rdp_accountant.py"],
srcs_version = "PY3",
visibility = ["//visibility:public"],
deps = [
"@com_google_differential_py//python/dp_accounting:dp_event",
"@com_google_differential_py//python/dp_accounting:privacy_accountant",
"@com_google_differential_py//python/dp_accounting/rdp:rdp_privacy_accountant",
],
deps = ["@com_google_differential_py//python/dp_accounting"],
)

py_test(
Expand Down Expand Up @@ -119,9 +109,7 @@ py_test(
python_version = "PY3",
srcs_version = "PY3",
deps = [
":rdp_accountant",
":tree_aggregation_accountant",
"@com_google_differential_py//python/dp_accounting:dp_event",
"@com_google_differential_py//python/dp_accounting/rdp:rdp_privacy_accountant",
"@com_google_differential_py//python/dp_accounting",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
import math

from absl import app

from com_google_differential_py.python.dp_accounting import dp_event
from com_google_differential_py.python.dp_accounting.rdp import rdp_privacy_accountant
from com_google_differential_py.python.dp_accounting


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

accountant = rdp_privacy_accountant.RdpAccountant(orders)
accountant = dp_accounting.RdpAccountant(orders)

event = dp_event.SelfComposedDpEvent(
dp_event.PoissonSampledDpEvent(q, dp_event.GaussianDpEvent(sigma)), steps)
event = dp_accounting.SelfComposedDpEvent(
dp_accounting.PoissonSampledDpEvent(q,
dp_accounting.GaussianDpEvent(sigma)),
steps)

accountant.compose(event)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
import math

from absl import app
from com_google_differential_py.python.dp_accounting
from scipy import optimize

from com_google_differential_py.python.dp_accounting import dp_event
from com_google_differential_py.python.dp_accounting.rdp import rdp_privacy_accountant


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

accountant = rdp_privacy_accountant.RdpAccountant(orders)
event = dp_event.SelfComposedDpEvent(
dp_event.PoissonSampledDpEvent(q, dp_event.GaussianDpEvent(sigma)), steps)
accountant = dp_accounting.RdpAccountant(orders)
event = dp_accounting.SelfComposedDpEvent(
dp_accounting.PoissonSampledDpEvent(q,
dp_accounting.GaussianDpEvent(sigma)),
steps)
accountant.compose(event)
return accountant.get_epsilon_and_optimal_order(delta)

Expand Down
28 changes: 12 additions & 16 deletions tensorflow_privacy/privacy/analysis/rdp_accountant.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,9 @@
eps, _, opt_order = rdp_accountant.get_privacy_spent(rdp, target_delta=delta)
"""

from com_google_differential_py.python.dp_accounting
import numpy as np

from com_google_differential_py.python.dp_accounting import dp_event
from com_google_differential_py.python.dp_accounting import privacy_accountant
from com_google_differential_py.python.dp_accounting.rdp import rdp_privacy_accountant


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

if isinstance(event, dp_event.SampledWithoutReplacementDpEvent):
neighboring_relation = privacy_accountant.NeighboringRelation.REPLACE_ONE
elif isinstance(event, dp_event.SingleEpochTreeAggregationDpEvent):
neighboring_relation = privacy_accountant.NeighboringRelation.REPLACE_SPECIAL
if isinstance(event, dp_accounting.SampledWithoutReplacementDpEvent):
neighboring_relation = dp_accounting.NeighboringRelation.REPLACE_ONE
elif isinstance(event, dp_accounting.SingleEpochTreeAggregationDpEvent):
neighboring_relation = dp_accounting.NeighboringRelation.REPLACE_SPECIAL
else:
neighboring_relation = privacy_accountant.NeighboringRelation.ADD_OR_REMOVE_ONE
neighboring_relation = dp_accounting.NeighboringRelation.ADD_OR_REMOVE_ONE

accountant = rdp_privacy_accountant.RdpAccountant(orders_vec,
neighboring_relation)
accountant = dp_accounting.RdpAccountant(orders_vec, neighboring_relation)
accountant.compose(event, count)
rdp = accountant._rdp # pylint: disable=protected-access

Expand All @@ -96,8 +92,8 @@ def compute_rdp(q, noise_multiplier, steps, orders):
Returns:
The RDPs at all orders. Can be `np.inf`.
"""
event = dp_event.PoissonSampledDpEvent(
q, dp_event.GaussianDpEvent(noise_multiplier))
event = dp_accounting.PoissonSampledDpEvent(
q, dp_accounting.GaussianDpEvent(noise_multiplier))

return _compute_rdp_from_event(orders, event, steps)

Expand Down Expand Up @@ -129,8 +125,8 @@ def compute_rdp_sample_without_replacement(q, noise_multiplier, steps, orders):
Returns:
The RDPs at all orders, can be np.inf.
"""
event = dp_event.SampledWithoutReplacementDpEvent(
1, q, dp_event.GaussianDpEvent(noise_multiplier))
event = dp_accounting.SampledWithoutReplacementDpEvent(
1, q, dp_accounting.GaussianDpEvent(noise_multiplier))

return _compute_rdp_from_event(orders, event, steps)

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

accountant = rdp_privacy_accountant.RdpAccountant(orders)
accountant = dp_accounting.RdpAccountant(orders)
accountant._rdp = rdp # pylint: disable=protected-access

if target_eps is not None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@
# ==============================================================================

from absl.testing import parameterized
from com_google_differential_py.python.dp_accounting
import tensorflow as tf

from tensorflow_privacy.privacy.analysis import tree_aggregation_accountant

from com_google_differential_py.python.dp_accounting import dp_event
from com_google_differential_py.python.dp_accounting.rdp import rdp_privacy_accountant


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

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

@parameterized.named_parameters(
Expand Down Expand Up @@ -67,7 +63,7 @@ def test_compute_eps_tree_decreasing(self, steps_list):
for noise_multiplier in [0.1 * x for x in range(1, 100, 5)]:
rdp = tree_aggregation_accountant.compute_rdp_tree_restart(
noise_multiplier, steps_list, orders)
eps = rdp_privacy_accountant.compute_epsilon(orders, rdp, target_delta)[0]
eps = dp_accounting.compute_epsilon(orders, rdp, target_delta)[0]
self.assertLess(eps, prev_eps)
prev_eps = eps

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

Expand Down
18 changes: 9 additions & 9 deletions tensorflow_privacy/privacy/dp_query/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ py_library(
deps = [
":discrete_gaussian_utils",
":dp_query",
"@com_google_differential_py//python/dp_accounting:dp_event",
"@com_google_differential_py//python/dp_accounting",
],
)

Expand All @@ -59,7 +59,7 @@ py_library(
deps = [
":discrete_gaussian_utils",
":dp_query",
"@com_google_differential_py//python/dp_accounting:dp_event",
"@com_google_differential_py//python/dp_accounting",
],
)

Expand All @@ -82,7 +82,7 @@ py_library(
deps = [
":dp_query",
":normalized_query",
"@com_google_differential_py//python/dp_accounting:dp_event",
"@com_google_differential_py//python/dp_accounting",
],
)

Expand All @@ -103,7 +103,7 @@ py_library(
srcs_version = "PY3",
deps = [
":dp_query",
"@com_google_differential_py//python/dp_accounting:dp_event",
"@com_google_differential_py//python/dp_accounting",
],
)

Expand All @@ -125,7 +125,7 @@ py_library(
srcs_version = "PY3",
deps = [
":dp_query",
"@com_google_differential_py//python/dp_accounting:dp_event",
"@com_google_differential_py//python/dp_accounting",
],
)

Expand Down Expand Up @@ -167,7 +167,7 @@ py_library(
srcs_version = "PY3",
deps = [
":dp_query",
"@com_google_differential_py//python/dp_accounting:dp_event",
"@com_google_differential_py//python/dp_accounting",
],
)

Expand All @@ -194,7 +194,7 @@ py_library(
":dp_query",
":gaussian_query",
":quantile_estimator_query",
"@com_google_differential_py//python/dp_accounting:dp_event",
"@com_google_differential_py//python/dp_accounting",
],
)

Expand Down Expand Up @@ -274,7 +274,7 @@ py_library(
deps = [
":dp_query",
":tree_aggregation",
"@com_google_differential_py//python/dp_accounting:dp_event",
"@com_google_differential_py//python/dp_accounting",
],
)

Expand All @@ -286,7 +286,7 @@ py_library(
":distributed_discrete_gaussian_query",
":dp_query",
":gaussian_query",
"@com_google_differential_py//python/dp_accounting:dp_event",
"@com_google_differential_py//python/dp_accounting",
],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@

import collections

from com_google_differential_py.python.dp_accounting
import tensorflow as tf
from tensorflow_privacy.privacy.dp_query import discrete_gaussian_utils
from tensorflow_privacy.privacy.dp_query import dp_query

from com_google_differential_py.python.dp_accounting import dp_event


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

result = tf.nest.map_structure(add_noise, sample_state)
event = dp_event.UnsupportedDpEvent()
event = dp_accounting.UnsupportedDpEvent()
return result, global_state, event
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@

import collections

from com_google_differential_py.python.dp_accounting
import tensorflow as tf
from tensorflow_privacy.privacy.dp_query import discrete_gaussian_utils
from tensorflow_privacy.privacy.dp_query import dp_query

from com_google_differential_py.python.dp_accounting import dp_event


class DistributedDiscreteGaussianSumQuery(dp_query.SumAggregationDPQuery):
"""Implements DPQuery for discrete distributed Gaussian sum queries.
Expand Down Expand Up @@ -108,5 +107,5 @@ def preprocess_record(self, params, record):
def get_noised_result(self, sample_state, global_state):
# Note that by directly returning the aggregate, this assumes that there
# will not be missing local noise shares during execution.
event = dp_event.UnsupportedDpEvent()
event = dp_accounting.UnsupportedDpEvent()
return sample_state, global_state, event
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@

import collections

from com_google_differential_py.python.dp_accounting
import tensorflow as tf
from tensorflow_privacy.privacy.dp_query import dp_query
from tensorflow_privacy.privacy.dp_query import normalized_query

from com_google_differential_py.python.dp_accounting import dp_event


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

def get_noised_result(self, sample_state, global_state):
"""The noise was already added locally, therefore just continue."""
event = dp_event.UnsupportedDpEvent()
event = dp_accounting.UnsupportedDpEvent()
return sample_state, global_state, event


Expand Down
5 changes: 2 additions & 3 deletions tensorflow_privacy/privacy/dp_query/gaussian_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
import collections
import distutils

from com_google_differential_py.python.dp_accounting
import tensorflow as tf
from tensorflow_privacy.privacy.dp_query import dp_query

from com_google_differential_py.python.dp_accounting import dp_event


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

result = tf.nest.map_structure(add_noise, sample_state)
noise_multiplier = global_state.stddev / global_state.l2_norm_clip
event = dp_event.GaussianDpEvent(noise_multiplier)
event = dp_accounting.GaussianDpEvent(noise_multiplier)

return result, global_state, event
Loading

0 comments on commit ff9935b

Please sign in to comment.