|
| 1 | +# Copyright 2023, The TensorFlow Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from absl.testing import parameterized |
| 16 | +import tensorflow as tf |
| 17 | +import tensorflow_models as tfm |
| 18 | +from tensorflow_privacy.privacy.fast_gradient_clipping import common_test_utils |
| 19 | +from tensorflow_privacy.privacy.fast_gradient_clipping import layer_registry |
| 20 | +from tensorflow_privacy.privacy.fast_gradient_clipping.registry_functions import einsum_dense |
| 21 | + |
| 22 | + |
| 23 | +def get_einsum_layer_generators(): |
| 24 | + def pure_einsum_layer(equation, output_dims, bias_axes): |
| 25 | + return tf.keras.layers.EinsumDense( |
| 26 | + equation, output_dims, bias_axes=bias_axes |
| 27 | + ) |
| 28 | + |
| 29 | + def sigmoid_einsum_layer(equation, output_dims, bias_axes): |
| 30 | + return tf.keras.layers.EinsumDense( |
| 31 | + equation, output_dims, bias_axes=bias_axes, activation='sigmoid' |
| 32 | + ) |
| 33 | + |
| 34 | + return { |
| 35 | + 'pure_einsum': pure_einsum_layer, |
| 36 | + 'sigmoid_einsum': sigmoid_einsum_layer, |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | +def get_einsum_parameter_tuples(): |
| 41 | + # (equation, input_dims, output_dims, bias_axes) |
| 42 | + return [ |
| 43 | + # Case C1. |
| 44 | + ('ab,bc->ac', [2], [3], None), |
| 45 | + ('ab,bc->ac', [2], [3], 'c'), |
| 46 | + ('abc,cd->abd', [2, 3], [2, 4], None), |
| 47 | + ('abc,cd->abd', [2, 3], [2, 4], 'b'), |
| 48 | + ('abc,cd->abd', [2, 3], [2, 4], 'd'), |
| 49 | + ('abc,cd->abd', [2, 3], [2, 4], 'bd'), |
| 50 | + ('abc,cef->abef', [2, 3], [2, 4, 5], None), |
| 51 | + ('abc,cef->abef', [2, 3], [2, 4, 5], 'bf'), |
| 52 | + # Case C2. |
| 53 | + ('...b,bc->...c', [2, 3], [4], None), |
| 54 | + ('...b,bc->...c', [2, 3], [4], 'c'), |
| 55 | + ('...ab,bc->...ac', [2, 3], [2, 4], None), |
| 56 | + ('...ab,bc->...ac', [2, 4], [2, 4], 'c'), |
| 57 | + ('...abc,cd->...abd', [2, 3, 4], [2, 3, 5], None), |
| 58 | + ('...abc,cd->...abd', [2, 3, 4], [2, 3, 5], 'b'), |
| 59 | + ('...abc,cd->...abd', [2, 3, 4], [2, 3, 5], 'd'), |
| 60 | + ('...abc,cd->...abd', [2, 3, 4], [2, 3, 5], 'bd'), |
| 61 | + ('...abc,cef->...abef', [2, 3, 4], [2, 3, 5, 6], None), |
| 62 | + ('...abc,cef->...abef', [2, 3, 4], [2, 3, 5, 6], 'bf'), |
| 63 | + # Case C3. |
| 64 | + ('ab...,bc->ac...', [2, 3], [4, 3], None), |
| 65 | + ('ab...,bc->ac...', [2, 3], [4, 3], 'c'), |
| 66 | + ('abc...,cd->abd...', [2, 3, 4], [2, 5, 4], None), |
| 67 | + ('abc...,cd->abd...', [2, 3, 4], [2, 5, 4], 'b'), |
| 68 | + ('abc...,cd->abd...', [2, 3, 4], [2, 5, 4], 'd'), |
| 69 | + ('abc...,cd->abd...', [2, 3, 4], [2, 5, 4], 'bd'), |
| 70 | + ('abc...,cef->abef...', [2, 3, 4], [2, 5, 6, 4], None), |
| 71 | + ('abc...,cef->abef...', [2, 3, 4], [2, 5, 6, 4], 'bf'), |
| 72 | + ] |
| 73 | + |
| 74 | + |
| 75 | +def get_einsum_layer_registry(): |
| 76 | + einsum_registry = layer_registry.LayerRegistry() |
| 77 | + einsum_registry.insert( |
| 78 | + tfm.nlp.layers.EinsumDense, |
| 79 | + einsum_dense.einsum_layer_computation, |
| 80 | + ) |
| 81 | + return einsum_registry |
| 82 | + |
| 83 | + |
| 84 | +class GradNormTest(tf.test.TestCase, parameterized.TestCase): |
| 85 | + |
| 86 | + def setUp(self): |
| 87 | + super().setUp() |
| 88 | + self.strategy = tf.distribute.get_strategy() |
| 89 | + self.using_tpu = False |
| 90 | + |
| 91 | + @parameterized.product( |
| 92 | + layer_name=list(get_einsum_layer_generators()), |
| 93 | + param_tuple=get_einsum_parameter_tuples(), |
| 94 | + num_microbatches=[None, 2], |
| 95 | + is_eager=[True, False], |
| 96 | + ) |
| 97 | + def test_gradient_norms_on_various_models( |
| 98 | + self, |
| 99 | + layer_name, |
| 100 | + param_tuple, |
| 101 | + num_microbatches, |
| 102 | + is_eager, |
| 103 | + ): |
| 104 | + # Parse inputs to generate test data. Note that each batched input is a |
| 105 | + # reshape of a `tf.range()` call. |
| 106 | + equation, input_dims, output_dims, bias_axes = param_tuple |
| 107 | + batch_size = 4 |
| 108 | + example_size = tf.reduce_prod(input_dims) |
| 109 | + example_values = tf.range(batch_size * example_size, dtype=tf.float32) |
| 110 | + x_batch = tf.reshape(example_values, [batch_size] + input_dims) |
| 111 | + |
| 112 | + # Make the layer generator via currying. |
| 113 | + einsum_generator = get_einsum_layer_generators()[layer_name] |
| 114 | + |
| 115 | + def curried_generator(a, b): |
| 116 | + del a, b |
| 117 | + return einsum_generator(equation, output_dims, bias_axes) |
| 118 | + |
| 119 | + # Load shared assets to all devices. |
| 120 | + with self.strategy.scope(): |
| 121 | + model = common_test_utils.get_model_from_generator( |
| 122 | + model_generator=common_test_utils.make_one_layer_functional_model, |
| 123 | + layer_generator=curried_generator, |
| 124 | + input_dims=input_dims, |
| 125 | + output_dims=output_dims, |
| 126 | + is_eager=is_eager, |
| 127 | + ) |
| 128 | + |
| 129 | + # Define the main testing ops. These may be later compiled to a Graph op. |
| 130 | + def test_op(x): |
| 131 | + return common_test_utils.get_computed_and_true_norms_from_model( |
| 132 | + model=model, |
| 133 | + per_example_loss_fn=None, |
| 134 | + num_microbatches=num_microbatches, |
| 135 | + x_batch=x, |
| 136 | + registry=get_einsum_layer_registry(), |
| 137 | + ) |
| 138 | + |
| 139 | + # TPUs can only run `tf.function`-decorated functions. |
| 140 | + if self.using_tpu: |
| 141 | + test_op = tf.function(test_op, autograph=False) |
| 142 | + |
| 143 | + # TPUs use lower precision than CPUs, so we relax our criterion. |
| 144 | + # E.g., one of the TPU runs generated the following results: |
| 145 | + # |
| 146 | + # computed_norm = 93.48296 |
| 147 | + # true_norm = 93.31176 |
| 148 | + # abs_diff = 0.17120361 |
| 149 | + # rel_diff = 0.00183475 |
| 150 | + # |
| 151 | + # which is a reasonable level of error for computing gradient norms. |
| 152 | + # Other trials also give an absolute (resp. relative) error of around |
| 153 | + # 0.05 (resp. 0.0015). |
| 154 | + rtol = 1e-2 if self.using_tpu else 1e-3 |
| 155 | + atol = 5e-1 if self.using_tpu else 1e-2 |
| 156 | + |
| 157 | + # Set up the device ops and run the test. |
| 158 | + computed_norms, true_norms = self.strategy.run(test_op, args=(x_batch,)) |
| 159 | + # TPUs return replica contexts, which must be unwrapped. |
| 160 | + if self.using_tpu: |
| 161 | + common_test_utils.assert_replica_values_are_close(self, computed_norms) |
| 162 | + common_test_utils.assert_replica_values_are_close(self, true_norms) |
| 163 | + computed_norms = computed_norms.values[0] |
| 164 | + true_norms = true_norms.values[0] |
| 165 | + expected_size = num_microbatches or batch_size |
| 166 | + self.assertEqual(tf.shape(computed_norms)[0], expected_size) |
| 167 | + self.assertAllClose(computed_norms, true_norms, rtol=rtol, atol=atol) |
| 168 | + |
| 169 | + |
| 170 | +if __name__ == '__main__': |
| 171 | + tf.test.main() |
0 commit comments