|
| 1 | +# Copyright 2023 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 | +from __future__ import absolute_import |
| 16 | +from __future__ import division |
| 17 | +from __future__ import print_function |
| 18 | + |
| 19 | +import os |
| 20 | + |
| 21 | +from tensorflow.compiler.plugin.poplar.driver.config_pb2 import IpuOptions |
| 22 | +from tensorflow.compiler.plugin.poplar.ops import gen_ipu_ops |
| 23 | +from tensorflow.python.client import session as session_lib |
| 24 | +from tensorflow.python.framework import ops |
| 25 | +from tensorflow.python.framework import test_util |
| 26 | +from tensorflow.python.ipu import config |
| 27 | +from tensorflow.python.ipu.utils import DeviceConnectionType |
| 28 | +from tensorflow.python.platform import googletest |
| 29 | +from tensorflow.python.platform import test |
| 30 | +import tensorflow.compat.v1 as tf |
| 31 | +tf.disable_v2_behavior() |
| 32 | + |
| 33 | + |
| 34 | +class UseOnDemandTest(test_util.TensorFlowTestCase): |
| 35 | + def _configure(self, configured_type, expected_type): |
| 36 | + cfg = config.IPUConfig() |
| 37 | + cfg.device_connection.type = configured_type |
| 38 | + cfg.configure_ipu_system() |
| 39 | + |
| 40 | + # Get the current config. |
| 41 | + g = ops.Graph() |
| 42 | + with g.as_default(): |
| 43 | + with ops.device("CPU"): |
| 44 | + with session_lib.Session(graph=g) as s: |
| 45 | + configurations = s.run(gen_ipu_ops.ipu_get_configuration()) |
| 46 | + |
| 47 | + self.assertEqual(len(configurations), 1) |
| 48 | + actual_cfg = IpuOptions() |
| 49 | + actual_cfg.ParseFromString(configurations[0]) |
| 50 | + |
| 51 | + self.assertEqual(actual_cfg.device_connection_type, expected_type) |
| 52 | + |
| 53 | + @test_util.deprecated_graph_mode_only |
| 54 | + def testNoFlag(self): |
| 55 | + self._configure(DeviceConnectionType.ALWAYS, |
| 56 | + DeviceConnectionType.ALWAYS.value) |
| 57 | + |
| 58 | + @test_util.deprecated_graph_mode_only |
| 59 | + def testFlag(self): |
| 60 | + flags = os.environ.get("TF_POPLAR_FLAGS", "") + ' --use_on_demand' |
| 61 | + with test.mock.patch.dict("os.environ", {"TF_POPLAR_FLAGS": flags}): |
| 62 | + self._configure(DeviceConnectionType.ALWAYS, |
| 63 | + DeviceConnectionType.ON_DEMAND.value) |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == "__main__": |
| 67 | + os.environ['TF_XLA_FLAGS'] = ('--tf_xla_min_cluster_size=1' + |
| 68 | + os.environ.get('TF_XLA_FLAGS', '')) |
| 69 | + googletest.main() |
0 commit comments