distribute_utils_test.py 2.17 KB
Newer Older
1
# Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2
3
4
5
6
7
8
9
10
11
12
13
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Hongkun Yu's avatar
Hongkun Yu committed
14

15
16
""" Tests for distribution util functions."""

17
import tensorflow as tf
18

19
from official.common import distribute_utils
20
21
22
23


class GetDistributionStrategyTest(tf.test.TestCase):
  """Tests for get_distribution_strategy."""
Hongkun Yu's avatar
Hongkun Yu committed
24

25
  def test_one_device_strategy_cpu(self):
26
    ds = distribute_utils.get_distribution_strategy(num_gpus=0)
27
    self.assertEquals(ds.num_replicas_in_sync, 1)
28
29
    self.assertEquals(len(ds.extended.worker_devices), 1)
    self.assertIn('CPU', ds.extended.worker_devices[0])
30
31

  def test_one_device_strategy_gpu(self):
32
    ds = distribute_utils.get_distribution_strategy(num_gpus=1)
33
    self.assertEquals(ds.num_replicas_in_sync, 1)
34
35
    self.assertEquals(len(ds.extended.worker_devices), 1)
    self.assertIn('GPU', ds.extended.worker_devices[0])
36
37

  def test_mirrored_strategy(self):
38
    ds = distribute_utils.get_distribution_strategy(num_gpus=5)
39
    self.assertEquals(ds.num_replicas_in_sync, 5)
40
41
    self.assertEquals(len(ds.extended.worker_devices), 5)
    for device in ds.extended.worker_devices:
42
43
      self.assertIn('GPU', device)

44
45
  def test_no_strategy(self):
    ds = distribute_utils.get_distribution_strategy('off')
46
    self.assertIs(ds, tf.distribute.get_strategy())
47
48
49
50
51
52
53
54
55
56

  def test_invalid_strategy(self):
    with self.assertRaisesRegexp(
        ValueError,
        'distribution_strategy must be a string but got: False. If'):
      distribute_utils.get_distribution_strategy(False)
    with self.assertRaisesRegexp(
        ValueError, 'distribution_strategy must be a string but got: 1'):
      distribute_utils.get_distribution_strategy(1)

57

Hongkun Yu's avatar
Hongkun Yu committed
58
if __name__ == '__main__':
59
  tf.test.main()