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
This commit is contained in:
Ilya Mironov 2019-04-11 17:06:33 -07:00 committed by A. Unique TensorFlower
parent e8113a0365
commit 3c4409d6d6
2 changed files with 30 additions and 19 deletions

View file

@ -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

View file

@ -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