From 3c4409d6d6936f8f699687ffd85231515df311b8 Mon Sep 17 00:00:00 2001 From: Ilya Mironov Date: Thu, 11 Apr 2019 17:06:33 -0700 Subject: [PATCH] Restoring ability to run compute_dp_sgd_privacy.py as a standalone script. At present, the script has no heavy dependencies except for the rdp_accountant, which is by itself pretty light-weight. However, importing rdp_accountant triggers __init__.py in third_party/py/tensorflow_privacy/privacy, which loads TF and all of tf.privacy. The CL adds a check to the __init__.py, which controls this behavior. PiperOrigin-RevId: 243172355 --- privacy/__init__.py | 43 +++++++++++++--------- privacy/analysis/compute_dp_sgd_privacy.py | 6 ++- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/privacy/__init__.py b/privacy/__init__.py index b8f4b8f..d691fe8 100644 --- a/privacy/__init__.py +++ b/privacy/__init__.py @@ -13,23 +13,30 @@ # limitations under the License. """TensorFlow Privacy library.""" -from privacy.analysis.privacy_ledger import DummyLedger -from privacy.analysis.privacy_ledger import GaussianSumQueryEntry -from privacy.analysis.privacy_ledger import PrivacyLedger -from privacy.analysis.privacy_ledger import QueryWithLedger -from privacy.analysis.privacy_ledger import SampleEntry +import sys -from privacy.dp_query.dp_query import DPQuery -from privacy.dp_query.gaussian_query import GaussianAverageQuery -from privacy.dp_query.gaussian_query import GaussianSumQuery -from privacy.dp_query.nested_query import NestedQuery -from privacy.dp_query.no_privacy_query import NoPrivacyAverageQuery -from privacy.dp_query.no_privacy_query import NoPrivacySumQuery -from privacy.dp_query.normalized_query import NormalizedQuery +# pylint: disable=g-import-not-at-top -from privacy.optimizers.dp_optimizer import DPAdagradGaussianOptimizer -from privacy.optimizers.dp_optimizer import DPAdagradOptimizer -from privacy.optimizers.dp_optimizer import DPAdamGaussianOptimizer -from privacy.optimizers.dp_optimizer import DPAdamOptimizer -from privacy.optimizers.dp_optimizer import DPGradientDescentGaussianOptimizer -from privacy.optimizers.dp_optimizer import DPGradientDescentOptimizer +if hasattr(sys, 'skip_tf_privacy_import'): # Useful for standalone scripts. + pass +else: + from tensorflow_privacy.privacy.analysis.privacy_ledger import DummyLedger + from tensorflow_privacy.privacy.analysis.privacy_ledger import GaussianSumQueryEntry + from tensorflow_privacy.privacy.analysis.privacy_ledger import PrivacyLedger + from tensorflow_privacy.privacy.analysis.privacy_ledger import QueryWithLedger + from tensorflow_privacy.privacy.analysis.privacy_ledger import SampleEntry + + from tensorflow_privacy.privacy.dp_query.dp_query import DPQuery + from tensorflow_privacy.privacy.dp_query.gaussian_query import GaussianAverageQuery + from tensorflow_privacy.privacy.dp_query.gaussian_query import GaussianSumQuery + from tensorflow_privacy.privacy.dp_query.nested_query import NestedQuery + from tensorflow_privacy.privacy.dp_query.no_privacy_query import NoPrivacyAverageQuery + from tensorflow_privacy.privacy.dp_query.no_privacy_query import NoPrivacySumQuery + from tensorflow_privacy.privacy.dp_query.normalized_query import NormalizedQuery + + from tensorflow_privacy.privacy.optimizers.dp_optimizer import DPAdagradGaussianOptimizer + from tensorflow_privacy.privacy.optimizers.dp_optimizer import DPAdagradOptimizer + from tensorflow_privacy.privacy.optimizers.dp_optimizer import DPAdamGaussianOptimizer + from tensorflow_privacy.privacy.optimizers.dp_optimizer import DPAdamOptimizer + from tensorflow_privacy.privacy.optimizers.dp_optimizer import DPGradientDescentGaussianOptimizer + from tensorflow_privacy.privacy.optimizers.dp_optimizer import DPGradientDescentOptimizer diff --git a/privacy/analysis/compute_dp_sgd_privacy.py b/privacy/analysis/compute_dp_sgd_privacy.py index 083b85a..4ca2ab1 100644 --- a/privacy/analysis/compute_dp_sgd_privacy.py +++ b/privacy/analysis/compute_dp_sgd_privacy.py @@ -33,11 +33,15 @@ from __future__ import division from __future__ import print_function import math +import sys from absl import app from absl import flags -from privacy.analysis.rdp_accountant import compute_rdp +# Opting out of loading all sibling packages and their dependencies. +sys.skip_tf_privacy_import = True + +from privacy.analysis.rdp_accountant import compute_rdp # pylint: disable=g-import-not-at-top from privacy.analysis.rdp_accountant import get_privacy_spent FLAGS = flags.FLAGS