|
| 1 | +# Copyright 2021 The TensorFlow Authors. All Rights Reserved. |
| 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 | +"""Tests for tf_utils.""" |
| 16 | +from absl.testing import parameterized |
| 17 | +import numpy as np |
| 18 | +import tensorflow as tf |
| 19 | + |
| 20 | +from tensorflow.python.distribute import combinations |
| 21 | +from tensorflow.python.distribute import strategy_combinations |
| 22 | +from official.modeling import tf_utils |
| 23 | + |
| 24 | + |
| 25 | +def all_strategy_combinations(): |
| 26 | + return combinations.combine( |
| 27 | + strategy=[ |
| 28 | + strategy_combinations.cloud_tpu_strategy, |
| 29 | + strategy_combinations.mirrored_strategy_with_two_gpus, |
| 30 | + ], |
| 31 | + mode='eager', |
| 32 | + ) |
| 33 | + |
| 34 | + |
| 35 | +class TFUtilsTest(tf.test.TestCase, parameterized.TestCase): |
| 36 | + |
| 37 | + @combinations.generate(all_strategy_combinations()) |
| 38 | + def test_cross_replica_concat(self, strategy): |
| 39 | + num_cores = strategy.num_replicas_in_sync |
| 40 | + |
| 41 | + shape = (2, 3, 4) |
| 42 | + |
| 43 | + def concat(axis): |
| 44 | + |
| 45 | + @tf.function |
| 46 | + def function(): |
| 47 | + replica_value = tf.fill(shape, tf_utils.get_replica_id()) |
| 48 | + return tf_utils.cross_replica_concat(replica_value, axis=axis) |
| 49 | + |
| 50 | + return function |
| 51 | + |
| 52 | + def expected(axis): |
| 53 | + values = [np.full(shape, i) for i in range(num_cores)] |
| 54 | + return np.concatenate(values, axis=axis) |
| 55 | + |
| 56 | + per_replica_results = strategy.run(concat(axis=0)) |
| 57 | + replica_0_result = per_replica_results.values[0].numpy() |
| 58 | + for value in per_replica_results.values[1:]: |
| 59 | + self.assertAllClose(value.numpy(), replica_0_result) |
| 60 | + self.assertAllClose(replica_0_result, expected(axis=0)) |
| 61 | + |
| 62 | + replica_0_result = strategy.run(concat(axis=1)).values[0].numpy() |
| 63 | + self.assertAllClose(replica_0_result, expected(axis=1)) |
| 64 | + |
| 65 | + replica_0_result = strategy.run(concat(axis=2)).values[0].numpy() |
| 66 | + self.assertAllClose(replica_0_result, expected(axis=2)) |
| 67 | + |
| 68 | + @combinations.generate(all_strategy_combinations()) |
| 69 | + def test_cross_replica_concat_gradient(self, strategy): |
| 70 | + num_cores = strategy.num_replicas_in_sync |
| 71 | + |
| 72 | + shape = (10, 5) |
| 73 | + |
| 74 | + @tf.function |
| 75 | + def function(): |
| 76 | + replica_value = tf.random.normal(shape) |
| 77 | + with tf.GradientTape() as tape: |
| 78 | + tape.watch(replica_value) |
| 79 | + concat_value = tf_utils.cross_replica_concat(replica_value, axis=0) |
| 80 | + output = tf.reduce_sum(concat_value) |
| 81 | + return tape.gradient(output, replica_value) |
| 82 | + |
| 83 | + per_replica_gradients = strategy.run(function) |
| 84 | + for gradient in per_replica_gradients.values: |
| 85 | + self.assertAllClose(gradient, num_cores * tf.ones(shape)) |
| 86 | + |
| 87 | + |
| 88 | +if __name__ == '__main__': |
| 89 | + tf.test.main() |
0 commit comments