"vscode:/vscode.git/clone" did not exist on "b5ed6b76f44dd4dc8f4ae6491df3655c409c38df"
distribution_utils_test.py 2.44 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# 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.
# ==============================================================================
""" Tests for distribution util functions."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf  # pylint: disable=g-bad-import-order

from official.utils.misc import distribution_utils


class GetDistributionStrategyTest(tf.test.TestCase):
  """Tests for get_distribution_strategy."""
  def test_one_device_strategy_cpu(self):
29
    ds = distribution_utils.get_distribution_strategy(num_gpus=0)
30
    self.assertEquals(ds.num_replicas_in_sync, 1)
31
32
    self.assertEquals(len(ds.extended.worker_devices), 1)
    self.assertIn('CPU', ds.extended.worker_devices[0])
33
34

  def test_one_device_strategy_gpu(self):
35
    ds = distribution_utils.get_distribution_strategy(num_gpus=1)
36
    self.assertEquals(ds.num_replicas_in_sync, 1)
37
38
    self.assertEquals(len(ds.extended.worker_devices), 1)
    self.assertIn('GPU', ds.extended.worker_devices[0])
39
40

  def test_mirrored_strategy(self):
41
    ds = distribution_utils.get_distribution_strategy(num_gpus=5)
42
    self.assertEquals(ds.num_replicas_in_sync, 5)
43
44
    self.assertEquals(len(ds.extended.worker_devices), 5)
    for device in ds.extended.worker_devices:
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
      self.assertIn('GPU', device)


class PerDeviceBatchSizeTest(tf.test.TestCase):
  """Tests for per_device_batch_size."""

  def test_batch_size(self):
    self.assertEquals(
        distribution_utils.per_device_batch_size(147, num_gpus=0), 147)
    self.assertEquals(
        distribution_utils.per_device_batch_size(147, num_gpus=1), 147)
    self.assertEquals(
        distribution_utils.per_device_batch_size(147, num_gpus=7), 21)

  def test_batch_size_with_remainder(self):
    with self.assertRaises(ValueError):
        distribution_utils.per_device_batch_size(147, num_gpus=5)


if __name__ == "__main__":
  tf.test.main()