"tools/run_text_generation_server.py" did not exist on "463d125732de0cfdea8765d6e71986fac7f5f5e6"
standard_runner_test.py 4.94 KB
Newer Older
1
# Copyright 2021 The Orbit Authors. All Rights Reserved.
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
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

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
15
16
"""Tests for orbit.standard_runner."""

17
18
from absl.testing import parameterized

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
19
from orbit import standard_runner
Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
20
from orbit import utils
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

import tensorflow as tf


def dataset_fn(input_context=None):
  del input_context

  def dummy_data(_):
    return tf.zeros((1, 1), dtype=tf.float32)

  dataset = tf.data.Dataset.range(1)
  dataset = dataset.repeat()
  dataset = dataset.map(
      dummy_data, num_parallel_calls=tf.data.experimental.AUTOTUNE)
  return dataset


Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
38
39
class TestTrainer(standard_runner.StandardTrainer):
  """A StandardTrainer subclass for tests."""
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
40

Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
41
  def __init__(self, options=None):
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
42
    self.strategy = tf.distribute.get_strategy()
Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
43
    self.global_step = utils.create_global_step()
Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
44
    dataset = self.strategy.distribute_datasets_from_function(dataset_fn)
Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
45
    super().__init__(train_dataset=dataset, options=options)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
46
47

  def train_loop_begin(self):
Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
48
    self.global_step.assign(0)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
49
50
51

  def train_step(self, iterator):

Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
52
    def replica_step(_):
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
53
54
      self.global_step.assign_add(1)

Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
55
    self.strategy.run(replica_step, args=(next(iterator),))
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
56
57
58
59

  def train_loop_end(self):
    return self.global_step.numpy()

Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
60
61
62
63
64
65
66

class TestEvaluator(standard_runner.StandardEvaluator):
  """A StandardEvaluator subclass for tests."""

  def __init__(self, options=None):
    self.strategy = tf.distribute.get_strategy()
    self.global_step = utils.create_global_step()
Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
67
    dataset = self.strategy.distribute_datasets_from_function(dataset_fn)
Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
68
69
    super().__init__(eval_dataset=dataset, options=options)

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
70
  def eval_begin(self):
Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
71
    self.global_step.assign(0)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
72
73
74

  def eval_step(self, iterator):

Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
75
    def replica_step(_):
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
76
77
      self.global_step.assign_add(1)

Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
78
    self.strategy.run(replica_step, args=(next(iterator),))
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
79
80
81
82
83

  def eval_end(self):
    return self.global_step.numpy()


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
class TestEvaluatorWithOutputsAggregation(standard_runner.StandardEvaluator):
  """A StandardEvaluator subclass for tests."""

  def __init__(self, options=None):
    self.strategy = tf.distribute.get_strategy()
    dataset = self.strategy.distribute_datasets_from_function(
        lambda _: tf.data.Dataset.range(10))
    super().__init__(eval_dataset=dataset, options=options)

  def eval_begin(self):
    return tf.constant((0.0,))

  def eval_reduce(self, state, step_outputs):
    state = tf.concat([state, step_outputs], 0)
    return state

  def eval_step(self, iterator):

    def replica_step(x):
      x = tf.cast(x, tf.float32)
      return tf.reduce_sum(x)

    return self.strategy.experimental_local_results(
        self.strategy.run(replica_step, args=(next(iterator),)))

  def eval_end(self, outputs):
    return tf.reduce_sum(outputs)


class StandardRunnerTest(parameterized.TestCase):
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
114

Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
115
116
117
118
119
120
121
122
123
  def test_default_trainer(self):
    trainer = TestTrainer()
    self.assertEqual(trainer.train(tf.constant(10)), 10)

  def test_trainer_with_tpu_summary_optimization(self):
    options = standard_runner.StandardTrainerOptions(
        use_tpu_summary_optimization=True)
    trainer = TestTrainer(options)
    self.assertEqual(trainer.train(tf.constant(10)), 10)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
124

125
126
127
128
129
  @parameterized.named_parameters(("use_tf_while_loop", True), ("", False))
  def test_default_evaluator(self, use_tf_while_loop):
    options = standard_runner.StandardEvaluatorOptions(
        use_tf_while_loop=use_tf_while_loop)
    evaluator = TestEvaluator(options)
Dan Holtmann-Rice's avatar
Dan Holtmann-Rice committed
130
    self.assertEqual(evaluator.evaluate(tf.constant(10)), 10)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
131

132
133
134
135
136
137
138
  @parameterized.named_parameters(("use_tf_while_loop", True), ("", False))
  def test_evaluator_with_outputs_aggregation(self, use_tf_while_loop):
    options = standard_runner.StandardEvaluatorOptions(
        use_tf_while_loop=use_tf_while_loop)
    evaluator = TestEvaluatorWithOutputsAggregation(options)
    self.assertEqual(evaluator.evaluate(tf.constant(10)), 45)

139
140
141
142
143
144
145
146
147
148
149
  @parameterized.named_parameters(
      ("recreate_iterator_for_each_eval", True, 10, 10),
      ("not_recreate_iterator_for_each_eval", False, 10, 35))
  def test_evaluator_with_repeat_dataset(self, recreate_iterator_for_each_eval,
                                         sum_for_1st_time, sum_for_2nd_time):
    options = standard_runner.StandardEvaluatorOptions(
        recreate_iterator_for_each_eval=recreate_iterator_for_each_eval)
    evaluator = TestEvaluatorWithOutputsAggregation(options)
    self.assertEqual(evaluator.evaluate(tf.constant(5)), sum_for_1st_time)
    self.assertEqual(evaluator.evaluate(tf.constant(5)), sum_for_2nd_time)

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
150

151
if __name__ == "__main__":
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
152
  tf.test.main()