distribute_utils_test.py 4.79 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

Hongkun Yu's avatar
Hongkun Yu committed
15
"""Tests for distribution util functions."""
16

Hongkun Yu's avatar
Hongkun Yu committed
17
import sys
18
import tensorflow as tf
19

20
from official.common import distribute_utils
21

Hongkun Yu's avatar
Hongkun Yu committed
22
23
TPU_TEST = 'test_tpu' in sys.argv[0]

24

Hongkun Yu's avatar
Hongkun Yu committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class DistributeUtilsTest(tf.test.TestCase):
  """Tests for distribute util functions."""

  def test_invalid_args(self):
    with self.assertRaisesRegex(ValueError, '`num_gpus` can not be negative.'):
      _ = distribute_utils.get_distribution_strategy(num_gpus=-1)

    with self.assertRaisesRegex(ValueError,
                                '.*If you meant to pass the string .*'):
      _ = distribute_utils.get_distribution_strategy(
          distribution_strategy=False, num_gpus=0)
    with self.assertRaisesRegex(ValueError, 'When 2 GPUs are specified.*'):
      _ = distribute_utils.get_distribution_strategy(
          distribution_strategy='off', num_gpus=2)
    with self.assertRaisesRegex(ValueError,
                                '`OneDeviceStrategy` can not be used.*'):
      _ = distribute_utils.get_distribution_strategy(
          distribution_strategy='one_device', num_gpus=2)
Hongkun Yu's avatar
Hongkun Yu committed
43

44
  def test_one_device_strategy_cpu(self):
Hongkun Yu's avatar
Hongkun Yu committed
45
    ds = distribute_utils.get_distribution_strategy('one_device', num_gpus=0)
46
    self.assertEquals(ds.num_replicas_in_sync, 1)
47
48
    self.assertEquals(len(ds.extended.worker_devices), 1)
    self.assertIn('CPU', ds.extended.worker_devices[0])
49
50

  def test_one_device_strategy_gpu(self):
Hongkun Yu's avatar
Hongkun Yu committed
51
    ds = distribute_utils.get_distribution_strategy('one_device', num_gpus=1)
52
    self.assertEquals(ds.num_replicas_in_sync, 1)
53
54
    self.assertEquals(len(ds.extended.worker_devices), 1)
    self.assertIn('GPU', ds.extended.worker_devices[0])
55
56

  def test_mirrored_strategy(self):
Hongkun Yu's avatar
Hongkun Yu committed
57
58
59
    # CPU only.
    _ = distribute_utils.get_distribution_strategy(num_gpus=0)
    # 5 GPUs.
60
    ds = distribute_utils.get_distribution_strategy(num_gpus=5)
61
    self.assertEquals(ds.num_replicas_in_sync, 5)
62
63
    self.assertEquals(len(ds.extended.worker_devices), 5)
    for device in ds.extended.worker_devices:
64
65
      self.assertIn('GPU', device)

Hongkun Yu's avatar
Hongkun Yu committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    _ = distribute_utils.get_distribution_strategy(
        distribution_strategy='mirrored',
        num_gpus=2,
        all_reduce_alg='nccl',
        num_packs=2)
    with self.assertRaisesRegex(
        ValueError,
        'When used with `mirrored`, valid values for all_reduce_alg are.*'):
      _ = distribute_utils.get_distribution_strategy(
          distribution_strategy='mirrored',
          num_gpus=2,
          all_reduce_alg='dummy',
          num_packs=2)

  def test_mwms(self):
    distribute_utils.configure_cluster(worker_hosts=None, task_index=-1)
    ds = distribute_utils.get_distribution_strategy(
        'multi_worker_mirrored', all_reduce_alg='nccl')
    self.assertIsInstance(
        ds, tf.distribute.experimental.MultiWorkerMirroredStrategy)

Hongkun Yu's avatar
Hongkun Yu committed
87
88
89
90
91
92
    with self.assertRaisesRegex(
        ValueError,
        'When used with `multi_worker_mirrored`, valid values.*'):
      _ = distribute_utils.get_distribution_strategy(
          'multi_worker_mirrored', all_reduce_alg='dummy')

93
94
  def test_no_strategy(self):
    ds = distribute_utils.get_distribution_strategy('off')
95
    self.assertIs(ds, tf.distribute.get_strategy())
96

Hongkun Yu's avatar
Hongkun Yu committed
97
98
99
100
101
102
103
104
105
106
  def test_tpu_strategy(self):
    if not TPU_TEST:
      self.skipTest('Only Cloud TPU VM instances can have local TPUs.')
    with self.assertRaises(ValueError):
      _ = distribute_utils.get_distribution_strategy('tpu')

    ds = distribute_utils.get_distribution_strategy('tpu', tpu_address='local')
    self.assertIsInstance(
        ds, tf.distribute.TPUStrategy)

107
108
109
110
111
112
113
114
115
  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)

Hongkun Yu's avatar
Hongkun Yu committed
116
117
118
119
120
121
  def test_get_strategy_scope(self):
    ds = distribute_utils.get_distribution_strategy('one_device', num_gpus=0)
    with distribute_utils.get_strategy_scope(ds):
      self.assertIs(tf.distribute.get_strategy(), ds)
    with distribute_utils.get_strategy_scope(None):
      self.assertIsNot(tf.distribute.get_strategy(), ds)
122

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