"test/algo/nas/test_oneshot_utils.py" did not exist on "b4559f609532cfde179220f10e6028f913d33501"
keras_imagenet_benchmark.py 39 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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.
# ==============================================================================
"""Executes Keras benchmarks and accuracy tests."""
from __future__ import print_function

import os
19
import time
20
21

from absl import flags
22
import tensorflow as tf  # pylint: disable=g-bad-import-order
23

24
from official.benchmark import keras_benchmark
25
from official.utils.testing import benchmark_wrappers
26
from official.vision.image_classification import resnet_imagenet_main
27

Toby Boyd's avatar
Toby Boyd committed
28
29
MIN_TOP_1_ACCURACY = 0.76
MAX_TOP_1_ACCURACY = 0.77
30

Toby Boyd's avatar
Toby Boyd committed
31
FLAGS = flags.FLAGS
32
33


Toby Boyd's avatar
Toby Boyd committed
34
35
class Resnet50KerasAccuracy(keras_benchmark.KerasBenchmark):
  """Benchmark accuracy tests for ResNet50 in Keras."""
36

37
  def __init__(self, output_dir=None, root_data_dir=None, **kwargs):
38
39
40
41
42
    """A benchmark class.

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

48
    flag_methods = [resnet_imagenet_main.define_imagenet_keras_flags]
Toby Boyd's avatar
Toby Boyd committed
49

50
    self.data_dir = os.path.join(root_data_dir, 'imagenet')
51
52
    super(Resnet50KerasAccuracy, self).__init__(
        output_dir=output_dir, flag_methods=flag_methods)
53

Toby Boyd's avatar
Toby Boyd committed
54
  def benchmark_graph_8_gpu(self):
55
56
    """Test Keras model with Keras fit/dist_strat and 8 GPUs."""
    self._setup()
Toby Boyd's avatar
Toby Boyd committed
57
    FLAGS.num_gpus = 8
58
    FLAGS.data_dir = self.data_dir
59
    FLAGS.batch_size = 128 * 8
Toby Boyd's avatar
Toby Boyd committed
60
    FLAGS.train_epochs = 90
61
    FLAGS.epochs_between_evals = 10
62
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_8_gpu')
Toby Boyd's avatar
Toby Boyd committed
63
    FLAGS.dtype = 'fp32'
64
    FLAGS.use_tensor_lr = True
65
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
66
67

  def benchmark_8_gpu(self):
68
69
    """Test Keras model with eager, dist_strat and 8 GPUs."""
    self._setup()
Toby Boyd's avatar
Toby Boyd committed
70
    FLAGS.num_gpus = 8
71
    FLAGS.data_dir = self.data_dir
72
    FLAGS.batch_size = 128 * 8
Toby Boyd's avatar
Toby Boyd committed
73
    FLAGS.train_epochs = 90
74
    FLAGS.epochs_between_evals = 10
75
    FLAGS.model_dir = self._get_model_dir('benchmark_8_gpu')
Toby Boyd's avatar
Toby Boyd committed
76
77
    FLAGS.dtype = 'fp32'
    FLAGS.enable_eager = True
78
79
    # Add some thread tunings to improve performance.
    FLAGS.datasets_num_private_threads = 14
80
    FLAGS.use_tensor_lr = True
81
    self._run_and_report_benchmark()
82

83
84
85
86
87
88
89
90
91
  def benchmark_8_gpu_amp(self):
    """Test Keras model with eager, dist_strat and 8 GPUs with automatic mixed precision."""
    self._setup()
    FLAGS.num_gpus = 8
    FLAGS.data_dir = self.data_dir
    FLAGS.batch_size = 128 * 8
    FLAGS.train_epochs = 90
    FLAGS.epochs_between_evals = 10
    FLAGS.model_dir = self._get_model_dir('benchmark_8_gpu_amp')
Vinh Nguyen's avatar
Vinh Nguyen committed
92
    FLAGS.dtype = 'fp16'
93
    FLAGS.enable_eager = True
94
    FLAGS.fp16_implementation = 'graph_rewrite'
95
96
97
98
    # Add some thread tunings to improve performance.
    FLAGS.datasets_num_private_threads = 14
    FLAGS.use_tensor_lr = True
    self._run_and_report_benchmark()
99

Reed's avatar
Reed committed
100
101
102
103
104
105
106
  def benchmark_8_gpu_fp16(self):
    """Test Keras model with eager, dist_strat, 8 GPUs, and fp16."""
    self._setup()
    FLAGS.num_gpus = 8
    FLAGS.data_dir = self.data_dir
    FLAGS.batch_size = 256 * 8
    FLAGS.train_epochs = 90
107
    FLAGS.epochs_between_evals = 10
Reed's avatar
Reed committed
108
109
110
    FLAGS.model_dir = self._get_model_dir('benchmark_8_gpu_fp16')
    FLAGS.dtype = 'fp16'
    FLAGS.enable_eager = True
111
112
    # Thread tuning to improve performance.
    FLAGS.tf_gpu_thread_mode = 'gpu_private'
113
    FLAGS.use_tensor_lr = True
Reed's avatar
Reed committed
114
115
116
117
118
119
120
121
122
    self._run_and_report_benchmark()

  def benchmark_xla_8_gpu_fp16(self):
    """Test Keras model with XLA, eager, dist_strat, 8 GPUs and fp16."""
    self._setup()
    FLAGS.num_gpus = 8
    FLAGS.data_dir = self.data_dir
    FLAGS.batch_size = 256 * 8
    FLAGS.train_epochs = 90
123
    FLAGS.epochs_between_evals = 10
Reed's avatar
Reed committed
124
125
126
127
    FLAGS.model_dir = self._get_model_dir('benchmark_xla_8_gpu_fp16')
    FLAGS.dtype = 'fp16'
    FLAGS.enable_eager = True
    FLAGS.enable_xla = True
128
129
    # Thread tuning to improve performance.
    FLAGS.tf_gpu_thread_mode = 'gpu_private'
130
    FLAGS.use_tensor_lr = True
Reed's avatar
Reed committed
131
132
    self._run_and_report_benchmark()

Toby Boyd's avatar
Toby Boyd committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
  def benchmark_8_gpu_mlperf_like(self):
    """Test similar to the rules for MLPerf 0.5.

    Listed below are reasons this comparison is not to the MLSpec, but this is
    still a decent directional measurement:
      - Eval is every 4 epochs and again at the end. ~2 extra times.
      - Learning rate is not tuned to hit 75%, but we know the model is correct.
      - We measure total time and MLPerf 0.5 excluded some startup time.
      - Eval is not on the total set, need to set eval batch_size where
        8*batch_size/50K is even. 250 is a good number.
      - Not sure if we are doing any extra or too few steps due to epoch bleed.
    """
    self._setup()
    FLAGS.num_gpus = 8
    FLAGS.data_dir = self.data_dir
    FLAGS.batch_size = 256 * 8
    FLAGS.train_epochs = 61
    FLAGS.epochs_between_evals = 4
    FLAGS.model_dir = self._get_model_dir('benchmark_8_gpu_mlperf_like')
    FLAGS.dtype = 'fp16'
    FLAGS.enable_eager = True
    FLAGS.enable_xla = True
155
    self._run_and_report_benchmark(top_1_min=0.736)
Toby Boyd's avatar
Toby Boyd committed
156

157
158
159
160
161
162
163
  def benchmark_xla_8_gpu_fp16_dynamic(self):
    """Test Keras model with XLA, eager, dist_strat, 8 GPUs, dynamic fp16."""
    self._setup()
    FLAGS.num_gpus = 8
    FLAGS.data_dir = self.data_dir
    FLAGS.batch_size = 256 * 8
    FLAGS.train_epochs = 90
164
    FLAGS.epochs_between_evals = 10
165
166
167
168
169
170
171
    FLAGS.model_dir = self._get_model_dir('benchmark_xla_8_gpu_fp16_dynamic')
    FLAGS.dtype = 'fp16'
    FLAGS.enable_eager = True
    FLAGS.enable_xla = True
    FLAGS.loss_scale = 'dynamic'
    # Thread tuning to improve performance.
    FLAGS.tf_gpu_thread_mode = 'gpu_private'
172
    FLAGS.use_tensor_lr = True
173
    self._run_and_report_benchmark(top_1_min=0.736)
174

175
  @benchmark_wrappers.enable_runtime_flags
176
177
178
  def _run_and_report_benchmark(self,
                                top_1_min=MIN_TOP_1_ACCURACY,
                                top_1_max=MAX_TOP_1_ACCURACY):
179
    start_time_sec = time.time()
180
    stats = resnet_imagenet_main.run(flags.FLAGS)
181
182
183
    wall_time_sec = time.time() - start_time_sec

    super(Resnet50KerasAccuracy, self)._report_benchmark(
Toby Boyd's avatar
Toby Boyd committed
184
        stats,
185
        wall_time_sec,
186
187
        top_1_min=top_1_min,
        top_1_max=top_1_max,
188
        total_batch_size=FLAGS.batch_size,
Toby Boyd's avatar
Toby Boyd committed
189
        log_steps=100)
190
191
192
193

  def _get_model_dir(self, folder_name):
    return os.path.join(self.output_dir, folder_name)

Toby Boyd's avatar
Toby Boyd committed
194
195
196
197
198

class Resnet50KerasBenchmarkBase(keras_benchmark.KerasBenchmark):
  """Resnet50 benchmarks."""

  def __init__(self, output_dir=None, default_flags=None):
199
    flag_methods = [resnet_imagenet_main.define_imagenet_keras_flags]
Toby Boyd's avatar
Toby Boyd committed
200
201
202
203
204
205

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

206
  @benchmark_wrappers.enable_runtime_flags
207
  def _run_and_report_benchmark(self, skip_steps=None):
208
    start_time_sec = time.time()
209
    stats = resnet_imagenet_main.run(FLAGS)
210
    wall_time_sec = time.time() - start_time_sec
211
    # Number of logged step time entries that are excluded in performance
212
213
214
    # report. We keep results from last 100 batches, or skip the steps based on
    # input skip_steps.
    warmup = (skip_steps or (FLAGS.train_steps - 100)) // FLAGS.log_steps
215
216
217
218
219

    super(Resnet50KerasBenchmarkBase, self)._report_benchmark(
        stats,
        wall_time_sec,
        total_batch_size=FLAGS.batch_size,
220
221
        log_steps=FLAGS.log_steps,
        warmup=warmup)
Toby Boyd's avatar
Toby Boyd committed
222
223

  def benchmark_1_gpu_no_dist_strat(self):
Haoyu Zhang's avatar
Haoyu Zhang committed
224
    """Test Keras model with 1 GPU, no distribution strategy."""
Toby Boyd's avatar
Toby Boyd committed
225
226
227
228
    self._setup()

    FLAGS.num_gpus = 1
    FLAGS.enable_eager = True
229
    FLAGS.distribution_strategy = 'off'
230
    FLAGS.model_dir = self._get_model_dir('benchmark_1_gpu_no_dist_strat')
Toby Boyd's avatar
Toby Boyd committed
231
    FLAGS.batch_size = 128
232
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
233

234
235
236
237
238
239
240
241
242
243
244
245
246
  def benchmark_1_gpu_no_dist_strat_run_eagerly(self):
    """Test Keras model with 1 GPU, no distribution strategy, run eagerly."""
    self._setup()

    FLAGS.num_gpus = 1
    FLAGS.enable_eager = True
    FLAGS.run_eagerly = True
    FLAGS.distribution_strategy = 'off'
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_1_gpu_no_dist_strat_run_eagerly')
    FLAGS.batch_size = 64
    self._run_and_report_benchmark()

247
248
249
250
251
252
253
254
255
256
257
258
259
260
  def benchmark_1_gpu_no_dist_strat_run_eagerly_tweaked(self):
    """Test Keras model with 1 GPU, no distribution strategy, run eagerly."""
    self._setup()

    FLAGS.num_gpus = 1
    FLAGS.enable_eager = True
    FLAGS.run_eagerly = True
    FLAGS.explicit_gpu_placement = True
    FLAGS.distribution_strategy = 'off'
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_1_gpu_no_dist_strat_run_eagerly_tweaked')
    FLAGS.batch_size = 64
    self._run_and_report_benchmark()

261
262
263
264
265
266
267
268
269
270
271
272
273
274
  def benchmark_1_gpu_no_dist_strat_run_eagerly_fp16(self):
    """Test with 1 GPU, no distribution strategy, fp16, run eagerly."""
    self._setup()

    FLAGS.num_gpus = 1
    FLAGS.enable_eager = True
    FLAGS.run_eagerly = True
    FLAGS.distribution_strategy = 'off'
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_1_gpu_no_dist_strat_run_eagerly_fp16')
    FLAGS.dtype = 'fp16'
    FLAGS.batch_size = 128
    self._run_and_report_benchmark()

275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
  def benchmark_1_gpu_no_dist_strat_run_eagerly_fp16_tweaked(self):
    """Test with 1 GPU, no distribution strategy, fp16, run eagerly."""
    self._setup()

    FLAGS.num_gpus = 1
    FLAGS.enable_eager = True
    FLAGS.run_eagerly = True
    FLAGS.explicit_gpu_placement = True
    FLAGS.distribution_strategy = 'off'
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_1_gpu_no_dist_strat_run_eagerly_fp16_tweaked')
    FLAGS.dtype = 'fp16'
    FLAGS.batch_size = 128
    self._run_and_report_benchmark()

Toby Boyd's avatar
Toby Boyd committed
290
  def benchmark_graph_1_gpu_no_dist_strat(self):
Haoyu Zhang's avatar
Haoyu Zhang committed
291
    """Test Keras model in legacy graph mode with 1 GPU, no dist strat."""
Toby Boyd's avatar
Toby Boyd committed
292
293
294
295
    self._setup()

    FLAGS.num_gpus = 1
    FLAGS.enable_eager = False
296
    FLAGS.distribution_strategy = 'off'
297
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_1_gpu_no_dist_strat')
298
    FLAGS.batch_size = 96  # BatchNorm is less efficient in legacy graph mode
299
    # due to its reliance on v1 cond.
300
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
301
302

  def benchmark_1_gpu(self):
Haoyu Zhang's avatar
Haoyu Zhang committed
303
    """Test Keras model with 1 GPU."""
Toby Boyd's avatar
Toby Boyd committed
304
305
306
307
    self._setup()

    FLAGS.num_gpus = 1
    FLAGS.enable_eager = True
308
    FLAGS.distribution_strategy = 'one_device'
309
    FLAGS.model_dir = self._get_model_dir('benchmark_1_gpu')
Toby Boyd's avatar
Toby Boyd committed
310
    FLAGS.batch_size = 128
311
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
312

313
314
315
316
317
318
  def benchmark_1_gpu_amp(self):
    """Test Keras model with 1 GPU with automatic mixed precision."""
    self._setup()

    FLAGS.num_gpus = 1
    FLAGS.enable_eager = True
Vinh Nguyen's avatar
Vinh Nguyen committed
319
    FLAGS.dtype = 'fp16'
320
    FLAGS.fp16_implementation = 'graph_rewrite'
321
    FLAGS.distribution_strategy = 'one_device'
322
323
324
    FLAGS.model_dir = self._get_model_dir('benchmark_1_gpu_amp')
    FLAGS.batch_size = 256
    self._run_and_report_benchmark()
325

Haoyu Zhang's avatar
Haoyu Zhang committed
326
327
328
329
330
331
332
  def benchmark_xla_1_gpu(self):
    """Test Keras model with XLA and 1 GPU."""
    self._setup()

    FLAGS.num_gpus = 1
    FLAGS.enable_eager = True
    FLAGS.enable_xla = True
333
    FLAGS.distribution_strategy = 'one_device'
Haoyu Zhang's avatar
Haoyu Zhang committed
334
335
336
337
    FLAGS.model_dir = self._get_model_dir('benchmark_xla_1_gpu')
    FLAGS.batch_size = 128
    self._run_and_report_benchmark()

338
339
340
341
342
343
  def benchmark_xla_1_gpu_amp(self):
    """Test Keras model with XLA and 1 GPU with automatic mixed precision."""
    self._setup()

    FLAGS.num_gpus = 1
    FLAGS.enable_eager = True
Vinh Nguyen's avatar
Vinh Nguyen committed
344
    FLAGS.dtype = 'fp16'
345
    FLAGS.fp16_implementation = 'graph_rewrite'
346
    FLAGS.enable_xla = True
347
    FLAGS.distribution_strategy = 'one_device'
348
349
350
351
    FLAGS.model_dir = self._get_model_dir('benchmark_xla_1_gpu_amp')
    FLAGS.batch_size = 256
    self._run_and_report_benchmark()

Reed's avatar
Reed committed
352
  def benchmark_1_gpu_fp16(self):
353
    """Test Keras model with 1 GPU and fp16."""
Reed's avatar
Reed committed
354
355
356
357
    self._setup()

    FLAGS.num_gpus = 1
    FLAGS.enable_eager = True
358
    FLAGS.distribution_strategy = 'one_device'
Reed's avatar
Reed committed
359
360
361
362
363
    FLAGS.model_dir = self._get_model_dir('benchmark_1_gpu_fp16')
    FLAGS.dtype = 'fp16'
    FLAGS.batch_size = 256
    self._run_and_report_benchmark()

364
365
366
367
368
369
  def benchmark_1_gpu_fp16_dynamic(self):
    """Test Keras model with 1 GPU, fp16, and dynamic loss scaling."""
    self._setup()

    FLAGS.num_gpus = 1
    FLAGS.enable_eager = True
370
    FLAGS.distribution_strategy = 'one_device'
371
372
373
374
375
376
    FLAGS.model_dir = self._get_model_dir('benchmark_1_gpu_fp16_dynamic')
    FLAGS.dtype = 'fp16'
    FLAGS.batch_size = 256
    FLAGS.loss_scale = 'dynamic'
    self._run_and_report_benchmark()

Reed's avatar
Reed committed
377
378
379
380
381
382
383
  def benchmark_xla_1_gpu_fp16(self):
    """Test Keras model with XLA, 1 GPU and fp16."""
    self._setup()

    FLAGS.num_gpus = 1
    FLAGS.enable_eager = True
    FLAGS.enable_xla = True
384
    FLAGS.distribution_strategy = 'one_device'
Reed's avatar
Reed committed
385
386
387
388
389
    FLAGS.model_dir = self._get_model_dir('benchmark_xla_1_gpu_fp16')
    FLAGS.dtype = 'fp16'
    FLAGS.batch_size = 256
    self._run_and_report_benchmark()

390
391
392
393
394
395
396
  def benchmark_xla_1_gpu_fp16_tweaked(self):
    """Test Keras model with XLA, 1 GPU, fp16, and manual config tuning."""
    self._setup()

    FLAGS.num_gpus = 1
    FLAGS.enable_eager = True
    FLAGS.enable_xla = True
397
    FLAGS.distribution_strategy = 'one_device'
398
399
400
    FLAGS.model_dir = self._get_model_dir('benchmark_xla_1_gpu_fp16_tweaked')
    FLAGS.dtype = 'fp16'
    FLAGS.batch_size = 256
401
    FLAGS.use_tensor_lr = True
402
    FLAGS.tf_gpu_thread_mode = 'gpu_private'
403
404
    self._run_and_report_benchmark()

405
406
407
408
409
410
411
  def benchmark_xla_1_gpu_fp16_dynamic(self):
    """Test Keras model with XLA, 1 GPU, fp16, and dynamic loss scaling."""
    self._setup()

    FLAGS.num_gpus = 1
    FLAGS.enable_eager = True
    FLAGS.enable_xla = True
412
    FLAGS.distribution_strategy = 'one_device'
413
414
415
416
417
418
    FLAGS.model_dir = self._get_model_dir('benchmark_xla_1_gpu_fp16_dynamic')
    FLAGS.dtype = 'fp16'
    FLAGS.batch_size = 256
    FLAGS.loss_scale = 'dynamic'
    self._run_and_report_benchmark()

Toby Boyd's avatar
Toby Boyd committed
419
  def benchmark_graph_1_gpu(self):
Haoyu Zhang's avatar
Haoyu Zhang committed
420
    """Test Keras model in legacy graph mode with 1 GPU."""
Toby Boyd's avatar
Toby Boyd committed
421
422
423
424
    self._setup()

    FLAGS.num_gpus = 1
    FLAGS.enable_eager = False
425
    FLAGS.distribution_strategy = 'one_device'
426
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_1_gpu')
Toby Boyd's avatar
Toby Boyd committed
427
    FLAGS.batch_size = 128
428
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
429

Haoyu Zhang's avatar
Haoyu Zhang committed
430
431
432
433
434
435
436
  def benchmark_graph_xla_1_gpu(self):
    """Test Keras model in legacy graph mode with XLA and 1 GPU."""
    self._setup()

    FLAGS.num_gpus = 1
    FLAGS.enable_eager = False
    FLAGS.enable_xla = True
437
    FLAGS.distribution_strategy = 'one_device'
Haoyu Zhang's avatar
Haoyu Zhang committed
438
439
440
441
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_xla_1_gpu')
    FLAGS.batch_size = 128
    self._run_and_report_benchmark()

442
443
444
445
446
  def benchmark_graph_1_gpu_fp16(self):
    """Test Keras model in legacy graph mode with 1 GPU and fp16."""
    self._setup()

    FLAGS.num_gpus = 1
447
    FLAGS.dtype = 'fp16'
448
    FLAGS.enable_eager = False
449
    FLAGS.distribution_strategy = 'one_device'
450
451
452
453
454
455
456
457
458
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_1_gpu_fp16')
    FLAGS.batch_size = 256
    self._run_and_report_benchmark()

  def benchmark_graph_xla_1_gpu_fp16(self):
    """Test Keras model in legacy graph mode with 1 GPU, fp16 and XLA."""
    self._setup()

    FLAGS.num_gpus = 1
459
    FLAGS.dtype = 'fp16'
460
461
    FLAGS.enable_eager = False
    FLAGS.enable_xla = True
462
    FLAGS.distribution_strategy = 'one_device'
463
464
465
466
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_xla_1_gpu_fp16')
    FLAGS.batch_size = 256
    self._run_and_report_benchmark()

467
  def benchmark_graph_xla_1_gpu_fp16_tweaked(self):
468
    """Test Keras model in legacy graph with 1 GPU, fp16, XLA, and tuning."""
469
470
471
472
473
    self._setup()

    FLAGS.num_gpus = 1
    FLAGS.enable_eager = False
    FLAGS.enable_xla = True
474
    FLAGS.distribution_strategy = 'one_device'
475
476
477
478
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_graph_xla_1_gpu_fp16_tweaked')
    FLAGS.dtype = 'fp16'
    FLAGS.batch_size = 256
479
    FLAGS.use_tensor_lr = True
480
481
482
    FLAGS.tf_gpu_thread_mode = 'gpu_private'
    self._run_and_report_benchmark()

Toby Boyd's avatar
Toby Boyd committed
483
  def benchmark_8_gpu(self):
Haoyu Zhang's avatar
Haoyu Zhang committed
484
    """Test Keras model with 8 GPUs."""
Toby Boyd's avatar
Toby Boyd committed
485
486
487
488
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.enable_eager = True
489
    FLAGS.distribution_strategy = 'mirrored'
490
    FLAGS.model_dir = self._get_model_dir('benchmark_8_gpu')
Toby Boyd's avatar
Toby Boyd committed
491
    FLAGS.batch_size = 128 * 8  # 8 GPUs
492
    self._run_and_report_benchmark()
493

494
495
496
497
498
499
  def benchmark_8_gpu_amp(self):
    """Test Keras model with 8 GPUs with automatic mixed precision."""
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.enable_eager = True
Vinh Nguyen's avatar
Vinh Nguyen committed
500
    FLAGS.dtype = 'fp16'
501
    FLAGS.fp16_implementation = 'graph_rewrite'
502
    FLAGS.distribution_strategy = 'mirrored'
503
504
505
    FLAGS.model_dir = self._get_model_dir('benchmark_8_gpu_amp')
    FLAGS.batch_size = 256 * 8  # 8 GPUs
    self._run_and_report_benchmark()
506

507
  def benchmark_8_gpu_tweaked(self):
Haoyu Zhang's avatar
Haoyu Zhang committed
508
    """Test Keras model with manual config tuning and 8 GPUs."""
509
510
511
512
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.enable_eager = True
513
    FLAGS.distribution_strategy = 'mirrored'
514
515
    FLAGS.model_dir = self._get_model_dir('benchmark_8_gpu_tweaked')
    FLAGS.batch_size = 128 * 8  # 8 GPUs
516
    FLAGS.use_tensor_lr = True
517
    FLAGS.datasets_num_private_threads = 14
518
519
    self._run_and_report_benchmark()

Haoyu Zhang's avatar
Haoyu Zhang committed
520
521
522
523
524
525
526
  def benchmark_xla_8_gpu(self):
    """Test Keras model with XLA and 8 GPUs."""
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.enable_eager = True
    FLAGS.enable_xla = True
527
    FLAGS.distribution_strategy = 'mirrored'
Haoyu Zhang's avatar
Haoyu Zhang committed
528
    FLAGS.model_dir = self._get_model_dir('benchmark_xla_8_gpu')
529
    FLAGS.batch_size = 128 * 8  # 8 GPUs
Haoyu Zhang's avatar
Haoyu Zhang committed
530
531
    self._run_and_report_benchmark()

532
533
534
535
536
537
  def benchmark_xla_8_gpu_amp(self):
    """Test Keras model with XLA and 8 GPUs with automatic mixed precision."""
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.enable_eager = True
Vinh Nguyen's avatar
Vinh Nguyen committed
538
    FLAGS.dtype = 'fp16'
539
    FLAGS.fp16_implementation = 'graph_rewrite'
540
    FLAGS.enable_xla = True
541
    FLAGS.distribution_strategy = 'mirrored'
542
543
544
    FLAGS.model_dir = self._get_model_dir('benchmark_xla_8_gpu_amp')
    FLAGS.batch_size = 256 * 8  # 8 GPUs
    self._run_and_report_benchmark()
545

546
547
548
549
550
551
552
  def benchmark_xla_8_gpu_tweaked(self):
    """Test Keras model with manual config tuning, 8 GPUs, and XLA."""
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.enable_eager = True
    FLAGS.enable_xla = True
553
    FLAGS.distribution_strategy = 'mirrored'
554
555
556
557
558
559
560
    FLAGS.model_dir = self._get_model_dir('benchmark_xla_8_gpu_tweaked')
    FLAGS.batch_size = 128 * 8
    FLAGS.use_tensor_lr = True
    FLAGS.tf_gpu_thread_mode = 'gpu_private'
    FLAGS.datasets_num_private_threads = 24
    self._run_and_report_benchmark()

Reed's avatar
Reed committed
561
  def benchmark_8_gpu_fp16(self):
562
    """Test Keras model with 8 GPUs and fp16."""
Reed's avatar
Reed committed
563
564
565
    self._setup()

    FLAGS.num_gpus = 8
566
    FLAGS.dtype = 'fp16'
Reed's avatar
Reed committed
567
    FLAGS.enable_eager = True
568
    FLAGS.distribution_strategy = 'mirrored'
Reed's avatar
Reed committed
569
570
571
572
    FLAGS.model_dir = self._get_model_dir('benchmark_8_gpu_fp16')
    FLAGS.batch_size = 256 * 8  # 8 GPUs
    self._run_and_report_benchmark()

573
  def benchmark_8_gpu_fp16_tweaked(self):
574
    """Test Keras model with 8 GPUs, fp16, and manual config tuning."""
575
576
577
578
579
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.dtype = 'fp16'
    FLAGS.enable_eager = True
580
    FLAGS.distribution_strategy = 'mirrored'
581
    FLAGS.model_dir = self._get_model_dir('benchmark_8_gpu_fp16_tweaked')
582
    FLAGS.batch_size = 256 * 8  # 8 GPUs
583
    FLAGS.use_tensor_lr = True
584
585
586
    FLAGS.tf_gpu_thread_mode = 'gpu_private'
    self._run_and_report_benchmark()

587
  def benchmark_8_gpu_fp16_dynamic_tweaked(self):
Toby Boyd's avatar
Toby Boyd committed
588
    """Test Keras model with 8 GPUs, fp16, dynamic loss scaling, and tuned."""
589
590
591
592
593
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.dtype = 'fp16'
    FLAGS.enable_eager = True
594
    FLAGS.distribution_strategy = 'mirrored'
595
596
597
598
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_8_gpu_fp16_dynamic_tweaked')
    FLAGS.batch_size = 256 * 8  # 8 GPUs
    FLAGS.loss_scale = 'dynamic'
599
    FLAGS.use_tensor_lr = True
600
601
602
    FLAGS.tf_gpu_thread_mode = 'gpu_private'
    self._run_and_report_benchmark()

Reed's avatar
Reed committed
603
  def benchmark_xla_8_gpu_fp16(self):
604
    """Test Keras model with XLA, 8 GPUs and fp16."""
Reed's avatar
Reed committed
605
606
607
    self._setup()

    FLAGS.num_gpus = 8
608
    FLAGS.dtype = 'fp16'
Reed's avatar
Reed committed
609
610
    FLAGS.enable_eager = True
    FLAGS.enable_xla = True
611
    FLAGS.distribution_strategy = 'mirrored'
Reed's avatar
Reed committed
612
613
    FLAGS.model_dir = self._get_model_dir('benchmark_xla_8_gpu_fp16')
    FLAGS.batch_size = 256 * 8  # 8 GPUs
614
615
    self._run_and_report_benchmark()

616
617
618
619
620
621
622
623
  def benchmark_xla_8_gpu_fp16_tweaked(self):
    """Test Keras model with manual config tuning, XLA, 8 GPUs and fp16."""
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.dtype = 'fp16'
    FLAGS.enable_eager = True
    FLAGS.enable_xla = True
624
    FLAGS.distribution_strategy = 'mirrored'
625
626
    FLAGS.model_dir = self._get_model_dir('benchmark_xla_8_gpu_fp16_tweaked')
    FLAGS.batch_size = 256 * 8  # 8 GPUs
627
    FLAGS.use_tensor_lr = True
628
629
    FLAGS.tf_gpu_thread_mode = 'gpu_private'
    FLAGS.datasets_num_private_threads = 48
630
631
    self._run_and_report_benchmark()

632
  def benchmark_xla_8_gpu_fp16_tweaked_delay_measure(self):
Haoyu Zhang's avatar
Haoyu Zhang committed
633
634
635
    """Test with manual config tuning, XLA, 8 GPUs and fp16.

    Delay performance measurement for stable performance on 96 vCPU platforms.
636
637
638
639
640
641
642
    """
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.dtype = 'fp16'
    FLAGS.enable_eager = True
    FLAGS.enable_xla = True
643
    FLAGS.distribution_strategy = 'mirrored'
644
645
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_xla_8_gpu_fp16_tweaked_delay_measure')
646
    FLAGS.batch_size = 256 * 8
647
648
649
650
651
    FLAGS.use_tensor_lr = True
    FLAGS.tf_gpu_thread_mode = 'gpu_private'
    FLAGS.train_steps = 310
    self._run_and_report_benchmark()

652
653
654
655
656
657
658
659
  def benchmark_xla_8_gpu_fp16_dynamic_tweaked(self):
    """Test Keras model with config tuning, XLA, 8 GPUs and dynamic fp16."""
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.dtype = 'fp16'
    FLAGS.enable_eager = True
    FLAGS.enable_xla = True
660
    FLAGS.distribution_strategy = 'mirrored'
661
662
663
664
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_xla_8_gpu_fp16_dynamic_tweaked')
    FLAGS.batch_size = 256 * 8  # 8 GPUs
    FLAGS.loss_scale = 'dynamic'
665
    FLAGS.use_tensor_lr = True
666
    FLAGS.tf_gpu_thread_mode = 'gpu_private'
667
    FLAGS.datasets_num_private_threads = 48
668
669
    self._run_and_report_benchmark()

Toby Boyd's avatar
Toby Boyd committed
670
  def benchmark_graph_8_gpu(self):
Haoyu Zhang's avatar
Haoyu Zhang committed
671
    """Test Keras model in legacy graph mode with 8 GPUs."""
Toby Boyd's avatar
Toby Boyd committed
672
673
674
675
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.enable_eager = False
676
    FLAGS.distribution_strategy = 'mirrored'
677
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_8_gpu')
Toby Boyd's avatar
Toby Boyd committed
678
    FLAGS.batch_size = 128 * 8  # 8 GPUs
679
    self._run_and_report_benchmark()
Toby Boyd's avatar
Toby Boyd committed
680

Haoyu Zhang's avatar
Haoyu Zhang committed
681
682
683
684
685
686
687
  def benchmark_graph_xla_8_gpu(self):
    """Test Keras model in legacy graph mode with XLA and 8 GPUs."""
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.enable_eager = False
    FLAGS.enable_xla = True
688
    FLAGS.distribution_strategy = 'mirrored'
Haoyu Zhang's avatar
Haoyu Zhang committed
689
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_xla_8_gpu')
690
    FLAGS.batch_size = 128 * 8  # 8 GPUs
Haoyu Zhang's avatar
Haoyu Zhang committed
691
692
    self._run_and_report_benchmark()

693
694
695
696
697
698
699
  def benchmark_graph_8_gpu_fp16(self):
    """Test Keras model in legacy graph mode with 8 GPUs and fp16."""
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.dtype = 'fp16'
    FLAGS.enable_eager = False
700
    FLAGS.distribution_strategy = 'mirrored'
701
702
703
704
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_8_gpu_fp16')
    FLAGS.batch_size = 256 * 8  # 8 GPUs
    self._run_and_report_benchmark()

705
706
707
708
709
710
711
712
  def benchmark_graph_xla_8_gpu_fp16(self):
    """Test Keras model in legacy graph mode with XLA, 8 GPUs and fp16."""
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.dtype = 'fp16'
    FLAGS.enable_eager = False
    FLAGS.enable_xla = True
713
    FLAGS.distribution_strategy = 'mirrored'
714
715
716
717
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_xla_8_gpu_fp16')
    FLAGS.batch_size = 256 * 8  # 8 GPUs
    self._run_and_report_benchmark()

718
  def benchmark_graph_8_gpu_fp16_tweaked(self):
719
    """Test Keras model in legacy graph mode, tuning, 8 GPUs, and FP16."""
720
721
722
723
724
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.dtype = 'fp16'
    FLAGS.enable_eager = False
725
    FLAGS.distribution_strategy = 'mirrored'
726
727
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_8_gpu_fp16_tweaked')
    FLAGS.batch_size = 256 * 8  # 8 GPUs
728
    FLAGS.use_tensor_lr = True
729
730
731
    FLAGS.tf_gpu_thread_mode = 'gpu_private'
    self._run_and_report_benchmark()

732
  def benchmark_graph_xla_8_gpu_fp16_tweaked(self):
733
    """Test Keras model in legacy graph tuning, XLA_FP16, 8 GPUs and fp16."""
734
735
736
737
738
739
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.dtype = 'fp16'
    FLAGS.enable_eager = False
    FLAGS.enable_xla = True
740
    FLAGS.distribution_strategy = 'mirrored'
741
742
743
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_graph_xla_8_gpu_fp16_tweaked')
    FLAGS.batch_size = 256 * 8  # 8 GPUs
744
    FLAGS.use_tensor_lr = True
745
746
747
    FLAGS.tf_gpu_thread_mode = 'gpu_private'
    self._run_and_report_benchmark()

748
  def benchmark_graph_xla_8_gpu_fp16_tweaked_delay_measure(self):
Haoyu Zhang's avatar
Haoyu Zhang committed
749
750
751
    """Test in legacy graph mode with manual config tuning, XLA, 8 GPUs, fp16.

    Delay performance measurement for stable performance on 96 vCPU platforms.
752
753
754
755
756
757
758
    """
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.dtype = 'fp16'
    FLAGS.enable_eager = False
    FLAGS.enable_xla = True
759
    FLAGS.distribution_strategy = 'mirrored'
760
761
762
763
764
765
766
767
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_graph_xla_8_gpu_fp16_tweaked_delay_measure')
    FLAGS.batch_size = 256 * 8
    FLAGS.use_tensor_lr = True
    FLAGS.tf_gpu_thread_mode = 'gpu_private'
    FLAGS.train_steps = 310
    self._run_and_report_benchmark()

768
769
770
771
772
773
774
  def benchmark_graph_8_gpu_fp16_dynamic_tweaked(self):
    """Test graph Keras with config tuning, 8 GPUs and dynamic fp16."""
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.dtype = 'fp16'
    FLAGS.enable_eager = False
775
    FLAGS.distribution_strategy = 'mirrored'
776
777
778
779
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_graph_8_gpu_fp16_dynamic_tweaked')
    FLAGS.batch_size = 256 * 8  # 8 GPUs
    FLAGS.loss_scale = 'dynamic'
780
    FLAGS.use_tensor_lr = True
781
782
783
    FLAGS.tf_gpu_thread_mode = 'gpu_private'
    self._run_and_report_benchmark()

784
785
786
787
788
789
790
791
  def benchmark_graph_xla_8_gpu_fp16_dynamic_tweaked(self):
    """Test graph Keras with config tuning, XLA, 8 GPUs and dynamic fp16."""
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.dtype = 'fp16'
    FLAGS.enable_eager = False
    FLAGS.enable_xla = True
792
    FLAGS.distribution_strategy = 'mirrored'
793
794
795
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_graph_xla_8_gpu_fp16_dynamic_tweaked')
    FLAGS.batch_size = 256 * 8  # 8 GPUs
796
    FLAGS.use_tensor_lr = True
797
798
799
800
    FLAGS.loss_scale = 'dynamic'
    FLAGS.tf_gpu_thread_mode = 'gpu_private'
    self._run_and_report_benchmark()

Toby Boyd's avatar
Toby Boyd committed
801
802
803
804
805
806
  def fill_report_object(self, stats):
    super(Resnet50KerasBenchmarkBase, self).fill_report_object(
        stats,
        total_batch_size=FLAGS.batch_size,
        log_steps=FLAGS.log_steps)

Toby Boyd's avatar
Toby Boyd committed
807
808
809
810

class Resnet50KerasBenchmarkSynth(Resnet50KerasBenchmarkBase):
  """Resnet50 synthetic benchmark tests."""

811
  def __init__(self, output_dir=None, root_data_dir=None, **kwargs):
Toby Boyd's avatar
Toby Boyd committed
812
813
    def_flags = {}
    def_flags['skip_eval'] = True
814
    def_flags['report_accuracy_metrics'] = False
Toby Boyd's avatar
Toby Boyd committed
815
816
817
818
    def_flags['use_synthetic_data'] = True
    def_flags['train_steps'] = 110
    def_flags['log_steps'] = 10

819
820
    super(Resnet50KerasBenchmarkSynth, self).__init__(
        output_dir=output_dir, default_flags=def_flags)
Toby Boyd's avatar
Toby Boyd committed
821
822
823
824
825


class Resnet50KerasBenchmarkReal(Resnet50KerasBenchmarkBase):
  """Resnet50 real data benchmark tests."""

826
  def __init__(self, output_dir=None, root_data_dir=None, **kwargs):
Toby Boyd's avatar
Toby Boyd committed
827
828
    def_flags = {}
    def_flags['skip_eval'] = True
829
    def_flags['report_accuracy_metrics'] = False
830
    def_flags['data_dir'] = os.path.join(root_data_dir, 'imagenet')
Toby Boyd's avatar
Toby Boyd committed
831
832
833
    def_flags['train_steps'] = 110
    def_flags['log_steps'] = 10

834
835
    super(Resnet50KerasBenchmarkReal, self).__init__(
        output_dir=output_dir, default_flags=def_flags)
836
837


838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
class Resnet50KerasBenchmarkRemoteData(Resnet50KerasBenchmarkBase):
  """Resnet50 real data (stored in remote storage) benchmark tests."""

  def __init__(self, output_dir=None, root_data_dir=None, **kwargs):
    def_flags = {}
    def_flags['skip_eval'] = True
    def_flags['report_accuracy_metrics'] = False
    def_flags['data_dir'] = os.path.join(root_data_dir, 'imagenet')
    # Defining multiple epochs overrides the train_steps setting in benchmarks.
    def_flags['train_epochs'] = 2
    # Cache dataset so performance is stable after the first epoch.
    def_flags['training_dataset_cache'] = True
    def_flags['log_steps'] = 100

    super(Resnet50KerasBenchmarkRemoteData, self).__init__(
        output_dir=output_dir, default_flags=def_flags)

855
  @benchmark_wrappers.enable_runtime_flags
856
857
858
859
860
861
  def _run_and_report_benchmark(self):
    # skip the first epoch for performance measurement.
    super(Resnet50KerasBenchmarkRemoteData,
          self)._run_and_report_benchmark(skip_steps=600)


862
class TrivialKerasBenchmarkReal(keras_benchmark.KerasBenchmark):
863
864
865
  """Trivial model with real data benchmark tests."""

  def __init__(self, output_dir=None, root_data_dir=None, **kwargs):
866
    flag_methods = [resnet_imagenet_main.define_imagenet_keras_flags]
Toby Boyd's avatar
Toby Boyd committed
867

868
    def_flags = {}
869
    def_flags['use_trivial_model'] = True
870
    def_flags['skip_eval'] = True
871
    def_flags['report_accuracy_metrics'] = False
872
    def_flags['use_tensor_lr'] = True
873
    def_flags['dtype'] = 'fp16'
874
    def_flags['data_dir'] = os.path.join(root_data_dir, 'imagenet')
875
876
    def_flags['train_steps'] = 600
    def_flags['log_steps'] = 100
877
    def_flags['distribution_strategy'] = 'mirrored'
878

879
    super(TrivialKerasBenchmarkReal, self).__init__(
880
881
882
883
        output_dir=output_dir,
        flag_methods=flag_methods,
        default_flags=def_flags)

884
  @benchmark_wrappers.enable_runtime_flags
885
886
  def _run_and_report_benchmark(self):
    start_time_sec = time.time()
887
    stats = resnet_imagenet_main.run(FLAGS)
888
889
    wall_time_sec = time.time() - start_time_sec

890
    super(TrivialKerasBenchmarkReal, self)._report_benchmark(
891
892
893
894
895
        stats,
        wall_time_sec,
        total_batch_size=FLAGS.batch_size,
        log_steps=FLAGS.log_steps)

896
897
898
899
900
901
902
  def benchmark_8_gpu_warmup(self):
    """Dummy test that runs over an epoch to warmup the machine."""
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.enable_eager = True
    FLAGS.model_dir = self._get_model_dir('benchmark_8_gpu_warmup')
903
    FLAGS.batch_size = 256 * 8
904
905
906
    FLAGS.train_steps = 700
    self._run_and_report_benchmark()

907
908
909
910
911
912
  def benchmark_1_gpu(self):
    """Test trivial Keras model (input pipeline) with 1 GPU."""
    self._setup()

    FLAGS.num_gpus = 1
    FLAGS.enable_eager = True
913
    FLAGS.enable_xla = True
914
915
916
917
918
919
920
921
    FLAGS.model_dir = self._get_model_dir('benchmark_1_gpu')
    FLAGS.batch_size = 256
    self._run_and_report_benchmark()

  def benchmark_graph_1_gpu(self):
    """Test trivial Keras model (input pipeline) with 1 GPU."""
    self._setup()

922
    FLAGS.num_gpus = 1
923
    FLAGS.enable_eager = False
924
    FLAGS.enable_xla = True
925
926
927
928
929
930
931
932
933
934
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_1_gpu')
    FLAGS.batch_size = 256
    self._run_and_report_benchmark()

  def benchmark_8_gpu(self):
    """Test trivial Keras model (input pipeline) with 8 GPUs."""
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.enable_eager = True
935
    FLAGS.enable_xla = True
936
937
938
939
940
    FLAGS.model_dir = self._get_model_dir('benchmark_8_gpu')
    FLAGS.batch_size = 256 * 8
    self._run_and_report_benchmark()

  def benchmark_8_gpu_tweaked(self):
941
    """Test trivial Keras model with tuning and 8 GPUs."""
942
943
944
945
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.enable_eager = True
946
    FLAGS.enable_xla = True
947
948
949
    FLAGS.model_dir = self._get_model_dir('benchmark_8_gpu_tweaked')
    FLAGS.batch_size = 256 * 8
    FLAGS.tf_gpu_thread_mode = 'gpu_private'
950
    FLAGS.datasets_num_private_threads = 48
951
952
953
    self._run_and_report_benchmark()

  def benchmark_graph_8_gpu(self):
954
    """Test trivial Keras model in legacy graph mode with 8 GPUs."""
955
956
957
958
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.enable_eager = False
959
    FLAGS.enable_xla = True
960
961
962
963
964
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_8_gpu')
    FLAGS.batch_size = 256 * 8
    self._run_and_report_benchmark()

  def benchmark_graph_8_gpu_tweaked(self):
965
    """Test trivial Keras model in legacy graph mode with tuning and 8 GPUs."""
966
967
968
969
    self._setup()

    FLAGS.num_gpus = 8
    FLAGS.enable_eager = False
970
    FLAGS.enable_xla = True
971
972
973
    FLAGS.model_dir = self._get_model_dir('benchmark_graph_8_gpu_tweaked')
    FLAGS.batch_size = 256 * 8
    FLAGS.tf_gpu_thread_mode = 'gpu_private'
974
    FLAGS.datasets_num_private_threads = 48
975
976
977
    self._run_and_report_benchmark()

  def fill_report_object(self, stats):
978
    super(TrivialKerasBenchmarkReal, self).fill_report_object(
979
980
981
        stats,
        total_batch_size=FLAGS.batch_size,
        log_steps=FLAGS.log_steps)
982
983


984
985
986
987
988
class Resnet50MultiWorkerKerasAccuracy(keras_benchmark.KerasBenchmark):
  """Resnet50 distributed accuracy tests with multiple workers."""

  def __init__(self, output_dir=None, root_data_dir=None, **kwargs):
    flag_methods = [resnet_imagenet_main.define_imagenet_keras_flags]
989
    self.data_dir = os.path.join(root_data_dir, 'imagenet')
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
    super(Resnet50MultiWorkerKerasAccuracy, self).__init__(
        output_dir=output_dir, flag_methods=flag_methods)

  def _benchmark_common(self, eager, num_workers, all_reduce_alg):
    """Common to all benchmarks in this class."""
    self._setup()

    num_gpus = 8
    FLAGS.num_gpus = num_gpus
    FLAGS.data_dir = self.data_dir
    FLAGS.train_epochs = 90
    FLAGS.epochs_between_evals = 10
    FLAGS.dtype = 'fp16'
    FLAGS.enable_eager = eager
    FLAGS.enable_xla = False
    FLAGS.distribution_strategy = 'multi_worker_mirrored'
    FLAGS.use_tensor_lr = True
    FLAGS.tf_gpu_thread_mode = 'gpu_private'
1008
    FLAGS.datasets_num_private_threads = 32
1009
1010
1011
1012
1013
1014
1015
1016
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_{}_8_gpu_{}_worker_fp16_{}_tweaked'.format(
            'eager' if eager else 'graph', num_workers, all_reduce_alg))
    FLAGS.batch_size = 256 * num_gpus * num_workers
    FLAGS.all_reduce_alg = all_reduce_alg

    self._run_and_report_benchmark()

1017
  @benchmark_wrappers.enable_runtime_flags
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
  def _run_and_report_benchmark(self,
                                top_1_min=MIN_TOP_1_ACCURACY,
                                top_1_max=MAX_TOP_1_ACCURACY):
    start_time_sec = time.time()
    stats = resnet_imagenet_main.run(flags.FLAGS)
    wall_time_sec = time.time() - start_time_sec

    super(Resnet50MultiWorkerKerasAccuracy, self)._report_benchmark(
        stats,
        wall_time_sec,
        top_1_min=top_1_min,
        top_1_max=top_1_max,
        total_batch_size=FLAGS.batch_size,
        log_steps=100)

  def _get_model_dir(self, folder_name):
    return os.path.join(self.output_dir, folder_name)

  def benchmark_eager_8_gpu_2_workers_fp16_ring_tweaked(self):
    """Eager, 8 GPUs per worker, 2 workers, fp16, ring all-reduce."""
    self._benchmark_common(eager=True, num_workers=2, all_reduce_alg='ring')

  def benchmark_eager_8_gpu_2_workers_fp16_nccl_tweaked(self):
    """Eager, 8 GPUs per worker, 2 workers, fp16, nccl all-reduce."""
    self._benchmark_common(eager=True, num_workers=2, all_reduce_alg='nccl')

  def benchmark_eager_8_gpu_8_workers_fp16_ring_tweaked(self):
    """Eager, 8 GPUs per worker, 8 workers, fp16, ring all-reduce."""
    self._benchmark_common(eager=True, num_workers=8, all_reduce_alg='ring')

  def benchmark_eager_8_gpu_8_workers_fp16_nccl_tweaked(self):
    """Eager, 8 GPUs per worker, 8 workers, fp16, nccl all-reduce."""
    self._benchmark_common(eager=True, num_workers=8, all_reduce_alg='nccl')


1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
class Resnet50MultiWorkerKerasBenchmark(Resnet50KerasBenchmarkBase):
  """Resnet50 distributed benchmark tests with multiple workers."""

  def __init__(self, output_dir=None, default_flags=None):
    super(Resnet50MultiWorkerKerasBenchmark, self).__init__(
        output_dir=output_dir, default_flags=default_flags)

  def _benchmark_common(self, eager, num_workers, all_reduce_alg):
    """Common to all benchmarks in this class."""
    self._setup()

    num_gpus = 8
    FLAGS.num_gpus = num_gpus
    FLAGS.dtype = 'fp16'
    FLAGS.enable_eager = eager
    FLAGS.enable_xla = False
    FLAGS.distribution_strategy = 'multi_worker_mirrored'
    FLAGS.use_tensor_lr = True
    FLAGS.tf_gpu_thread_mode = 'gpu_private'
1072
    FLAGS.datasets_num_private_threads = 32
1073
    FLAGS.model_dir = self._get_model_dir(
1074
1075
        'benchmark_{}_8_gpu_{}_worker_fp16_{}_tweaked'.format(
            'eager' if eager else 'graph', num_workers, all_reduce_alg))
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
    FLAGS.batch_size = 256 * num_gpus * num_workers
    FLAGS.all_reduce_alg = all_reduce_alg

    self._run_and_report_benchmark()

  def benchmark_eager_8_gpu_1_worker_fp16_ring_tweaked(self):
    """Eager, 8 GPUs per worker, 1 worker, fp16, ring all-reduce."""
    self._benchmark_common(eager=True, num_workers=1, all_reduce_alg='ring')

  def benchmark_eager_8_gpu_1_worker_fp16_nccl_tweaked(self):
    """Eager, 8 GPUs per worker, 1 worker, fp16, nccl all-reduce."""
    self._benchmark_common(eager=True, num_workers=1, all_reduce_alg='nccl')

  def benchmark_eager_8_gpu_2_workers_fp16_ring_tweaked(self):
    """Eager, 8 GPUs per worker, 2 workers, fp16, ring all-reduce."""
    self._benchmark_common(eager=True, num_workers=2, all_reduce_alg='ring')

  def benchmark_eager_8_gpu_2_workers_fp16_nccl_tweaked(self):
    """Eager, 8 GPUs per worker, 2 workers, fp16, nccl all-reduce."""
    self._benchmark_common(eager=True, num_workers=2, all_reduce_alg='nccl')

  def benchmark_eager_8_gpu_8_workers_fp16_ring_tweaked(self):
    """Eager, 8 GPUs per worker, 8 workers, fp16, ring all-reduce."""
    self._benchmark_common(eager=True, num_workers=8, all_reduce_alg='ring')

  def benchmark_eager_8_gpu_8_workers_fp16_nccl_tweaked(self):
    """Eager, 8 GPUs per worker, 8 workers, fp16, nccl all-reduce."""
    self._benchmark_common(eager=True, num_workers=8, all_reduce_alg='nccl')


Ayush Dubey's avatar
Ayush Dubey committed
1106
class Resnet50MultiWorkerKerasBenchmarkSynth(Resnet50MultiWorkerKerasBenchmark):
1107
  """Resnet50 multi-worker synthetic data benchmark tests."""
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120

  def __init__(self, output_dir=None, root_data_dir=None, **kwargs):
    def_flags = {}
    def_flags['skip_eval'] = True
    def_flags['report_accuracy_metrics'] = False
    def_flags['use_synthetic_data'] = True
    def_flags['train_steps'] = 110
    def_flags['log_steps'] = 10

    super(Resnet50MultiWorkerKerasBenchmarkSynth, self).__init__(
        output_dir=output_dir, default_flags=def_flags)


1121
1122
1123
1124
1125
1126
1127
class Resnet50MultiWorkerKerasBenchmarkReal(Resnet50MultiWorkerKerasBenchmark):
  """Resnet50 multi-worker real data benchmark tests."""

  def __init__(self, output_dir=None, root_data_dir=None, **kwargs):
    def_flags = {}
    def_flags['skip_eval'] = True
    def_flags['report_accuracy_metrics'] = False
1128
    def_flags['data_dir'] = os.path.join(root_data_dir, 'imagenet')
1129
1130
1131
1132
1133
1134
1135
    def_flags['train_steps'] = 110
    def_flags['log_steps'] = 10

    super(Resnet50MultiWorkerKerasBenchmarkReal, self).__init__(
        output_dir=output_dir, default_flags=def_flags)


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