"tests/testconfigs/arc_test.yaml" did not exist on "e2529a6fa881a0b63a03921bdb68ca599bbeb7d4"
keras_cifar_benchmark.py 8.68 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 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.
# ==============================================================================
Toby Boyd's avatar
Toby Boyd committed
15
"""Executes Keras benchmarks and accuracy tests."""
Shining Sun's avatar
Shining Sun committed
16
17
18

from __future__ import absolute_import
from __future__ import division
Toby Boyd's avatar
Toby Boyd committed
19
20
from __future__ import print_function

21
import os
22
import time
Toby Boyd's avatar
Toby Boyd committed
23
from absl import flags
24
import tensorflow as tf # pylint: disable=g-bad-import-order
Toby Boyd's avatar
Toby Boyd committed
25
26

from official.resnet import cifar10_main as cifar_main
Toby Boyd's avatar
Toby Boyd committed
27
from official.resnet.keras import keras_benchmark
28
29
30
from official.resnet.keras import keras_cifar_main
from official.resnet.keras import keras_common

31
32
MIN_TOP_1_ACCURACY = 0.925
MAX_TOP_1_ACCURACY = 0.938
Toby Boyd's avatar
Toby Boyd committed
33

Toby Boyd's avatar
Toby Boyd committed
34
FLAGS = flags.FLAGS
35
CIFAR_DATA_DIR_NAME = 'cifar-10-batches-bin'
Toby Boyd's avatar
Toby Boyd committed
36

37

Toby Boyd's avatar
Toby Boyd committed
38
39
class Resnet56KerasAccuracy(keras_benchmark.KerasBenchmark):
  """Accuracy tests for ResNet56 Keras CIFAR-10."""
40

41
  def __init__(self, output_dir=None, root_data_dir=None, **kwargs):
42
43
44
45
46
    """A benchmark class.

    Args:
      output_dir: directory where to output e.g. log files
      root_data_dir: directory under which to look for dataset
47
48
49
      **kwargs: arbitrary named arguments. This is needed to make the
                constructor forward compatible in case PerfZero provides more
                named arguments before updating the constructor.
50
51
    """

52
    self.data_dir = os.path.join(root_data_dir, CIFAR_DATA_DIR_NAME)
53
54
55
    flag_methods = [
        keras_common.define_keras_flags, cifar_main.define_cifar_flags
    ]
Toby Boyd's avatar
Toby Boyd committed
56

57
58
    super(Resnet56KerasAccuracy, self).__init__(
        output_dir=output_dir, flag_methods=flag_methods)
Toby Boyd's avatar
Toby Boyd committed
59

Toby Boyd's avatar
Toby Boyd committed
60
  def benchmark_graph_1_gpu(self):
61
    """Test keras based model with Keras fit and distribution strategies."""
Toby Boyd's avatar
Toby Boyd committed
62
    self._setup()
Toby Boyd's avatar
Toby Boyd committed
63
    FLAGS.num_gpus = 1
64
    FLAGS.data_dir = self.data_dir
Toby Boyd's avatar
Toby Boyd committed
65
66
    FLAGS.batch_size = 128
    FLAGS.train_epochs = 182
67
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_1_gpu')
Toby Boyd's avatar
Toby Boyd committed
68
    FLAGS.dtype = 'fp32'
69
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
70
71

  def benchmark_1_gpu(self):
72
73
    """Test keras based model with eager and distribution strategies."""
    self._setup()
Toby Boyd's avatar
Toby Boyd committed
74
    FLAGS.num_gpus = 1
75
    FLAGS.data_dir = self.data_dir
Toby Boyd's avatar
Toby Boyd committed
76
77
    FLAGS.batch_size = 128
    FLAGS.train_epochs = 182
78
    FLAGS.model_dir = self._get_model_dir('benchmark_1_gpu')
Toby Boyd's avatar
Toby Boyd committed
79
80
    FLAGS.dtype = 'fp32'
    FLAGS.enable_eager = True
81
    self._run_and_report_benchmark()
82

Toby Boyd's avatar
Toby Boyd committed
83
  def benchmark_2_gpu(self):
84
85
    """Test keras based model with eager and distribution strategies."""
    self._setup()
Toby Boyd's avatar
Toby Boyd committed
86
    FLAGS.num_gpus = 2
87
    FLAGS.data_dir = self.data_dir
Toby Boyd's avatar
Toby Boyd committed
88
89
    FLAGS.batch_size = 128
    FLAGS.train_epochs = 182
90
    FLAGS.model_dir = self._get_model_dir('benchmark_2_gpu')
Toby Boyd's avatar
Toby Boyd committed
91
92
    FLAGS.dtype = 'fp32'
    FLAGS.enable_eager = True
93
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
94

95
96
97
98
99
100
101
102
103
104
105
106
107
  def benchmark_2_gpu_no_cloning(self):
    """Test keras based model with eager, distributed no-cloning."""
    self._setup()
    FLAGS.num_gpus = 2
    FLAGS.data_dir = self.data_dir
    FLAGS.batch_size = 128
    FLAGS.train_epochs = 182
    FLAGS.model_dir = self._get_model_dir('benchmark_2_gpu_no_cloning')
    FLAGS.dtype = 'fp32'
    FLAGS.clone_model_in_keras_dist_strat = False
    FLAGS.enable_eager = True
    self._run_and_report_benchmark()

Toby Boyd's avatar
Toby Boyd committed
108
  def benchmark_graph_2_gpu(self):
109
110
    """Test keras based model with Keras fit and distribution strategies."""
    self._setup()
Toby Boyd's avatar
Toby Boyd committed
111
    FLAGS.num_gpus = 2
112
    FLAGS.data_dir = self.data_dir
Toby Boyd's avatar
Toby Boyd committed
113
114
    FLAGS.batch_size = 128
    FLAGS.train_epochs = 182
115
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_2_gpu')
Toby Boyd's avatar
Toby Boyd committed
116
    FLAGS.dtype = 'fp32'
117
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
118
119

  def benchmark_graph_1_gpu_no_dist_strat(self):
120
    """Test keras based model with Keras fit but not distribution strategies."""
Toby Boyd's avatar
Toby Boyd committed
121
    self._setup()
122
    FLAGS.distribution_strategy = 'off'
Toby Boyd's avatar
Toby Boyd committed
123
    FLAGS.num_gpus = 1
124
    FLAGS.data_dir = self.data_dir
Toby Boyd's avatar
Toby Boyd committed
125
126
    FLAGS.batch_size = 128
    FLAGS.train_epochs = 182
127
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_1_gpu_no_dist_strat')
Toby Boyd's avatar
Toby Boyd committed
128
    FLAGS.dtype = 'fp32'
129
130
131
132
    self._run_and_report_benchmark()

  def _run_and_report_benchmark(self):
    start_time_sec = time.time()
Toby Boyd's avatar
Toby Boyd committed
133
    stats = keras_cifar_main.run(FLAGS)
134
    wall_time_sec = time.time() - start_time_sec
Toby Boyd's avatar
Toby Boyd committed
135

136
    super(Resnet56KerasAccuracy, self)._report_benchmark(
Toby Boyd's avatar
Toby Boyd committed
137
        stats,
138
        wall_time_sec,
Toby Boyd's avatar
Toby Boyd committed
139
140
        top_1_min=MIN_TOP_1_ACCURACY,
        top_1_max=MAX_TOP_1_ACCURACY,
141
        total_batch_size=FLAGS.batch_size,
Toby Boyd's avatar
Toby Boyd committed
142
143
144
145
146
147
148
        log_steps=100)


class Resnet56KerasBenchmarkBase(keras_benchmark.KerasBenchmark):
  """Short performance tests for ResNet56 via Keras and CIFAR-10."""

  def __init__(self, output_dir=None, default_flags=None):
149
150
151
    flag_methods = [
        keras_common.define_keras_flags, cifar_main.define_cifar_flags
    ]
Toby Boyd's avatar
Toby Boyd committed
152
153
154
155
156
157

    super(Resnet56KerasBenchmarkBase, self).__init__(
        output_dir=output_dir,
        flag_methods=flag_methods,
        default_flags=default_flags)

158
159
  def _run_and_report_benchmark(self):
    start_time_sec = time.time()
Toby Boyd's avatar
Toby Boyd committed
160
    stats = keras_cifar_main.run(FLAGS)
161
162
163
164
165
166
167
    wall_time_sec = time.time() - start_time_sec

    super(Resnet56KerasBenchmarkBase, self)._report_benchmark(
        stats,
        wall_time_sec,
        total_batch_size=FLAGS.batch_size,
        log_steps=FLAGS.log_steps)
Toby Boyd's avatar
Toby Boyd committed
168
169
170
171
172

  def benchmark_1_gpu_no_dist_strat(self):
    self._setup()
    FLAGS.num_gpus = 1
    FLAGS.enable_eager = True
173
    FLAGS.distribution_strategy = 'off'
174
    FLAGS.model_dir = self._get_model_dir('benchmark_1_gpu_no_dist_strat')
Toby Boyd's avatar
Toby Boyd committed
175
    FLAGS.batch_size = 128
176
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
177
178
179
180
181

  def benchmark_graph_1_gpu_no_dist_strat(self):
    self._setup()
    FLAGS.num_gpus = 1
    FLAGS.enable_eager = False
182
    FLAGS.distribution_strategy = 'off'
183
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_1_gpu_no_dist_strat')
Toby Boyd's avatar
Toby Boyd committed
184
    FLAGS.batch_size = 128
185
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
186
187
188
189
190

  def benchmark_1_gpu(self):
    self._setup()
    FLAGS.num_gpus = 1
    FLAGS.enable_eager = True
191
    FLAGS.distribution_strategy = 'default'
192
    FLAGS.model_dir = self._get_model_dir('benchmark_1_gpu')
Toby Boyd's avatar
Toby Boyd committed
193
    FLAGS.batch_size = 128
194
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
195
196
197
198
199

  def benchmark_graph_1_gpu(self):
    self._setup()
    FLAGS.num_gpus = 1
    FLAGS.enable_eager = False
200
    FLAGS.distribution_strategy = 'default'
201
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_1_gpu')
Toby Boyd's avatar
Toby Boyd committed
202
    FLAGS.batch_size = 128
203
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
204
205
206
207
208

  def benchmark_2_gpu(self):
    self._setup()
    FLAGS.num_gpus = 2
    FLAGS.enable_eager = True
209
    FLAGS.distribution_strategy = 'default'
210
    FLAGS.model_dir = self._get_model_dir('benchmark_2_gpu')
Toby Boyd's avatar
Toby Boyd committed
211
    FLAGS.batch_size = 128 * 2  # 2 GPUs
212
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
213

214
215
216
217
218
219
220
221
222
223
  def benchmark_2_gpu_no_cloning(self):
    self._setup()
    FLAGS.num_gpus = 2
    FLAGS.enable_eager = True
    FLAGS.distribution_strategy = 'default'
    FLAGS.model_dir = self._get_model_dir('benchmark_2_gpu_no_cloning')
    FLAGS.batch_size = 128 * 2  # 2 GPUs
    FLAGS.clone_model_in_keras_dist_strat = False
    self._run_and_report_benchmark()

Toby Boyd's avatar
Toby Boyd committed
224
225
226
227
  def benchmark_graph_2_gpu(self):
    self._setup()
    FLAGS.num_gpus = 2
    FLAGS.enable_eager = False
228
    FLAGS.distribution_strategy = 'default'
229
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_2_gpu')
Toby Boyd's avatar
Toby Boyd committed
230
    FLAGS.batch_size = 128 * 2  # 2 GPUs
231
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
232
233
234
235
236


class Resnet56KerasBenchmarkSynth(Resnet56KerasBenchmarkBase):
  """Synthetic benchmarks for ResNet56 and Keras."""

237
  def __init__(self, output_dir=None, root_data_dir=None, **kwargs):
238
239
240
241
242
    default_flags = {}
    default_flags['skip_eval'] = True
    default_flags['use_synthetic_data'] = True
    default_flags['train_steps'] = 110
    default_flags['log_steps'] = 10
Toby Boyd's avatar
Toby Boyd committed
243

244
    super(Resnet56KerasBenchmarkSynth, self).__init__(
245
        output_dir=output_dir, default_flags=default_flags)
Toby Boyd's avatar
Toby Boyd committed
246
247
248
249
250


class Resnet56KerasBenchmarkReal(Resnet56KerasBenchmarkBase):
  """Real data benchmarks for ResNet56 and Keras."""

251
  def __init__(self, output_dir=None, root_data_dir=None, **kwargs):
252
253
254
255
256
    default_flags = {}
    default_flags['skip_eval'] = True
    default_flags['data_dir'] = os.path.join(root_data_dir, CIFAR_DATA_DIR_NAME)
    default_flags['train_steps'] = 110
    default_flags['log_steps'] = 10
Toby Boyd's avatar
Toby Boyd committed
257

258
    super(Resnet56KerasBenchmarkReal, self).__init__(
259
        output_dir=output_dir, default_flags=default_flags)
260
261
262
263


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