|
| 1 | +# Copyright 2020 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 TPUStrategy in regards to compiling programs.""" |
| 16 | + |
| 17 | +from __future__ import absolute_import |
| 18 | +from __future__ import division |
| 19 | +from __future__ import print_function |
| 20 | + |
| 21 | +from tensorflow.python.distribute import tpu_strategy as tpu_lib |
| 22 | +from tensorflow.python.distribute.cluster_resolver import tpu_cluster_resolver |
| 23 | +from tensorflow.python.eager import def_function |
| 24 | +from tensorflow.python.eager import remote |
| 25 | +from tensorflow.python.eager import test |
| 26 | +from tensorflow.python.framework import constant_op |
| 27 | +from tensorflow.python.platform import flags |
| 28 | +from tensorflow.python.tpu import tpu_strategy_util |
| 29 | + |
| 30 | +FLAGS = flags.FLAGS |
| 31 | +flags.DEFINE_string("tpu", "", "Name of TPU to connect to.") |
| 32 | +flags.DEFINE_string("project", None, "Name of GCP project with TPU.") |
| 33 | +flags.DEFINE_string("zone", None, "Name of GCP zone with TPU.") |
| 34 | + |
| 35 | + |
| 36 | +def get_tpu_cluster_resolver(): |
| 37 | + resolver = tpu_cluster_resolver.TPUClusterResolver( |
| 38 | + tpu=FLAGS.tpu, |
| 39 | + zone=FLAGS.zone, |
| 40 | + project=FLAGS.project, |
| 41 | + ) |
| 42 | + return resolver |
| 43 | + |
| 44 | + |
| 45 | +def get_tpu_strategy(): |
| 46 | + resolver = get_tpu_cluster_resolver() |
| 47 | + remote.connect_to_cluster(resolver) |
| 48 | + tpu_strategy_util.initialize_tpu_system(resolver) |
| 49 | + strategy = tpu_lib.TPUStrategyV2(resolver) |
| 50 | + return strategy |
| 51 | + |
| 52 | + |
| 53 | +# TODO(b/158494076): Merge this test back into TPUStrategy tests |
| 54 | +# (tpu_strategy_test) once MLIR bridge is enabled by default. |
| 55 | +class TPUStrategyCompilationTest(test.TestCase): |
| 56 | + |
| 57 | + def test_functions_compile_same_signature(self): |
| 58 | + """Tests compiling different functions with the same signature.""" |
| 59 | + strategy = get_tpu_strategy() |
| 60 | + |
| 61 | + @def_function.function |
| 62 | + def return_one(): |
| 63 | + |
| 64 | + def computation(): |
| 65 | + return constant_op.constant(1) |
| 66 | + |
| 67 | + return strategy.run(computation) |
| 68 | + |
| 69 | + @def_function.function |
| 70 | + def return_two(): |
| 71 | + |
| 72 | + def computation(): |
| 73 | + return constant_op.constant(2) |
| 74 | + |
| 75 | + return strategy.run(computation) |
| 76 | + |
| 77 | + expected_result_ones = [1 for _ in range(0, strategy.num_replicas_in_sync)] |
| 78 | + self.assertAllEqual(expected_result_ones, |
| 79 | + strategy.experimental_local_results(return_one())) |
| 80 | + |
| 81 | + expected_result_twos = [2 for _ in range(0, strategy.num_replicas_in_sync)] |
| 82 | + self.assertAllEqual(expected_result_twos, |
| 83 | + strategy.experimental_local_results(return_two())) |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + test.main() |
0 commit comments