"docs/zh_CN/TrialExample/Trials.md" did not exist on "222611924431d9bfc29c36016a302463f73398bb"
bert_pretrain_benchmark.py 20 KB
Newer Older
Chen Chen's avatar
Chen Chen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Lint as: python3
# Copyright 2020 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 benchmark testing for bert pretraining."""
# pylint: disable=line-too-long
import json
import os
import time
from typing import Optional

from absl import flags
from absl import logging
25
import tensorflow as tf
Chen Chen's avatar
Chen Chen committed
26
27
28

from official.benchmark import benchmark_wrappers
from official.benchmark import bert_benchmark_utils
Jing Li's avatar
Jing Li committed
29
from official.benchmark import owner_utils
30
from official.common import distribute_utils
Chen Chen's avatar
Chen Chen committed
31
32
33
34
from official.nlp.bert import run_pretraining
from official.utils.flags import core as flags_core

# Pretrain masked lanauge modeling accuracy range:
Chen Chen's avatar
Chen Chen committed
35
36
MIN_MLM_ACCURACY = 0.635
MAX_MLM_ACCURACY = 0.645
Chen Chen's avatar
Chen Chen committed
37
38

# Pretrain next sentence prediction accuracy range:
Chen Chen's avatar
Chen Chen committed
39
40
MIN_NSP_ACCURACY = 0.94
MAX_NSP_ACCURACY = 0.96
Chen Chen's avatar
Chen Chen committed
41

Zongwei Zhou's avatar
Zongwei Zhou committed
42
43
44
45
46
47
48
49
50
51

# Pretrain masked lanauge modeling accuracy range:
MIN_MLM_ACCURACY_GPU = 0.378
MAX_MLM_ACCURACY_GPU = 0.388

# Pretrain next sentence prediction accuracy range:
MIN_NSP_ACCURACY_GPU = 0.82
MAX_NSP_ACCURACY_GPU = 0.84


Chen Chen's avatar
Chen Chen committed
52
53
54
55
56
57
58
59
60
61
62
BERT_PRETRAIN_FILES_SEQ128 = 'gs://mlcompass-data/bert/pretraining_data/seq_128/wikipedia.tfrecord*,gs://mlcompass-data/bert/pretraining_data/seq_128/books.tfrecord*'
BERT_BASE_CONFIG_FILE = 'gs://cloud-tpu-checkpoints/bert/keras_bert/uncased_L-12_H-768_A-12/bert_config.json'

FLAGS = flags.FLAGS


class BertPretrainAccuracyBenchmark(bert_benchmark_utils.BertBenchmarkBase):
  """Benchmark accuracy tests for BERT Pretraining."""

  def __init__(self,
               output_dir: Optional[str] = None,
Chen Chen's avatar
Chen Chen committed
63
64
               tpu: Optional[str] = None,
               **kwargs):
Chen Chen's avatar
Chen Chen committed
65
66
67
68
69
    """Inits BertPretrainAccuracyBenchmark class.

    Args:
      output_dir: Directory where to output e.g. log files
      tpu: TPU name to use in a TPU benchmark.
Chen Chen's avatar
Chen Chen committed
70
      **kwargs: Additional keyword arguments.
Chen Chen's avatar
Chen Chen committed
71
72
    """
    super(BertPretrainAccuracyBenchmark, self).__init__(
Chen Chen's avatar
Chen Chen committed
73
        output_dir=output_dir, tpu=tpu, **kwargs)
Chen Chen's avatar
Chen Chen committed
74

Zongwei Zhou's avatar
Zongwei Zhou committed
75
76
77
78
79
80
81
82
83
84
85
  def _get_distribution_strategy(self, ds_type='mirrored'):
    """Gets the distribution strategy.

    Args:
      ds_type: String, the distribution strategy type to be used. Can be
        'mirrored', 'multi_worker_mirrored', 'tpu' and 'off'.

    Returns:
      A `tf.distribute.DistibutionStrategy` object.
    """
    if self.tpu or ds_type == 'tpu':
86
      return distribute_utils.get_distribution_strategy(
Zongwei Zhou's avatar
Zongwei Zhou committed
87
88
89
          distribution_strategy='tpu', tpu_address=self.tpu)
    elif ds_type == 'multi_worker_mirrored':
      # Configures cluster spec for multi-worker distribution strategy.
90
91
92
      _ = distribute_utils.configure_cluster(FLAGS.worker_hosts,
                                             FLAGS.task_index)
    return distribute_utils.get_distribution_strategy(
Zongwei Zhou's avatar
Zongwei Zhou committed
93
94
95
96
        distribution_strategy=ds_type,
        num_gpus=FLAGS.num_gpus,
        all_reduce_alg=FLAGS.all_reduce_alg)

Chen Chen's avatar
Chen Chen committed
97
  @benchmark_wrappers.enable_runtime_flags
Zongwei Zhou's avatar
Zongwei Zhou committed
98
99
  def _run_and_report_benchmark(self, summary_path: str, report_accuracy: bool,
                                ds_type: str):
Chen Chen's avatar
Chen Chen committed
100
    """Runs and reports the benchmark given the provided configuration."""
Zongwei Zhou's avatar
Zongwei Zhou committed
101
    distribution = self._get_distribution_strategy(ds_type=ds_type)
Chen Chen's avatar
Chen Chen committed
102
103
104
105
106
107
    logging.info('Flags: %s', flags_core.get_nondefault_flags_as_str())
    start_time_sec = time.time()
    run_pretraining.run_bert_pretrain(
        strategy=distribution, custom_callbacks=self.timer_callback)
    wall_time_sec = time.time() - start_time_sec

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
108
109
110
111
112
113
114
    # For GPU multi-worker, the summary text file is only generated on chief
    # (metrics aggregated), so only chief has to report the result.
    if tf.io.gfile.exists(summary_path):
      with tf.io.gfile.GFile(summary_path, 'rb') as reader:
        summary = json.loads(reader.read().decode('utf-8'))
      self._report_benchmark(summary, start_time_sec, wall_time_sec,
                             report_accuracy, ds_type)
Chen Chen's avatar
Chen Chen committed
115

Jing Li's avatar
Jing Li committed
116
  def _report_benchmark(self, summary, start_time_sec, wall_time_sec,
Zongwei Zhou's avatar
Zongwei Zhou committed
117
                        report_accuracy, ds_type):
Chen Chen's avatar
Chen Chen committed
118
119
120
121
122
    metrics = [{
        'name': 'train_loss',
        'value': summary['train_loss'],
    }, {
        'name':
Jing Li's avatar
Jing Li committed
123
            'exp_per_second',
Chen Chen's avatar
Chen Chen committed
124
125
126
127
128
129
130
        'value':
            self.timer_callback.get_examples_per_sec(FLAGS.train_batch_size *
                                                     FLAGS.steps_per_loop)
    }, {
        'name': 'startup_time',
        'value': self.timer_callback.get_startup_time(start_time_sec)
    }]
Jing Li's avatar
Jing Li committed
131
    if report_accuracy:
Zongwei Zhou's avatar
Zongwei Zhou committed
132
133
134
135
136
137
138
139
140
141
      if ds_type == 'tpu':
        min_mlm_acc = MIN_MLM_ACCURACY
        max_mlm_acc = MAX_MLM_ACCURACY
        min_nsp_acc = MIN_NSP_ACCURACY
        max_nsp_acc = MAX_NSP_ACCURACY
      else:
        min_mlm_acc = MIN_MLM_ACCURACY_GPU
        max_mlm_acc = MAX_MLM_ACCURACY_GPU
        min_nsp_acc = MIN_NSP_ACCURACY_GPU
        max_nsp_acc = MAX_NSP_ACCURACY_GPU
Jing Li's avatar
Jing Li committed
142
143
144
      metrics.extend([{
          'name': 'masked_lm_accuracy',
          'value': summary['masked_lm_accuracy'],
Zongwei Zhou's avatar
Zongwei Zhou committed
145
146
          'min_value': min_mlm_acc,
          'max_value': max_mlm_acc,
Jing Li's avatar
Jing Li committed
147
148
149
      }, {
          'name': 'next_sentence_accuracy',
          'value': summary['next_sentence_accuracy'],
Zongwei Zhou's avatar
Zongwei Zhou committed
150
151
          'min_value': min_nsp_acc,
          'max_value': max_nsp_acc,
Jing Li's avatar
Jing Li committed
152
      }])
Chen Chen's avatar
Chen Chen committed
153
154
155
156
157
158
159
160
161
162
    self.report_benchmark(
        iters=summary['total_training_steps'],
        wall_time=wall_time_sec,
        metrics=metrics,
        extras={'flags': flags_core.get_nondefault_flags_as_str()})

  def _specify_common_flags(self):
    FLAGS.bert_config_file = BERT_BASE_CONFIG_FILE
    FLAGS.learning_rate = 1e-4
    FLAGS.warmup_steps = 10000
Chen Chen's avatar
Chen Chen committed
163
    FLAGS.steps_per_loop = 10000
Chen Chen's avatar
Chen Chen committed
164
165
166
    FLAGS.input_files = BERT_PRETRAIN_FILES_SEQ128
    FLAGS.max_seq_length = 128
    FLAGS.max_predictions_per_seq = 20
Zongwei Zhou's avatar
Zongwei Zhou committed
167
168
169

  def _specify_tpu_common_flags(self):
    FLAGS.distribution_strategy = 'tpu'
Chen Chen's avatar
Chen Chen committed
170
171
    FLAGS.dtype = 'bf16'

Zongwei Zhou's avatar
Zongwei Zhou committed
172
173
174
175
176
  def _specify_gpu_common_flags(self):
    FLAGS.distribution_strategy = 'mirrored'
    FLAGS.dtype = 'fp16'
    FLAGS.loss_scale = 'dynamic'

Jing Li's avatar
Jing Li committed
177
  @owner_utils.Owner('tf-model-garden')
Chen Chen's avatar
Chen Chen committed
178
179
  def benchmark_accuracy_8x8_tpu_bf16_seq128_500k_steps(self):
    """Test bert pretraining with 8x8 TPU for 500k steps."""
Chen Chen's avatar
Chen Chen committed
180
181
182
    # This is used for accuracy test.
    self._setup()
    self._specify_common_flags()
Zongwei Zhou's avatar
Zongwei Zhou committed
183
184
    self._specify_tpu_common_flags()
    FLAGS.train_batch_size = 512
Chen Chen's avatar
Chen Chen committed
185
    FLAGS.num_steps_per_epoch = 500000
Chen Chen's avatar
Chen Chen committed
186
    FLAGS.num_train_epochs = 1
Chen Chen's avatar
Chen Chen committed
187
    FLAGS.model_dir = self._get_model_dir(
Chen Chen's avatar
Chen Chen committed
188
        'benchmark_accuracy_8x8_tpu_bf16_seq128_500k_steps')
Chen Chen's avatar
Chen Chen committed
189
190
    summary_path = os.path.join(FLAGS.model_dir,
                                'summaries/training_summary.txt')
Chen Chen's avatar
Chen Chen committed
191
192
193
194
    # Set train_summary_interval to -1 to disable training summary, because
    # writing summary to gcs may fail and summaries are not needed for this
    # accuracy benchmark test.
    FLAGS.train_summary_interval = -1
Hongkun Yu's avatar
Hongkun Yu committed
195
    self._run_and_report_benchmark(
Zongwei Zhou's avatar
Zongwei Zhou committed
196
197
198
        summary_path=summary_path,
        report_accuracy=True,
        ds_type=FLAGS.distribution_strategy)
Chen Chen's avatar
Chen Chen committed
199

Allen Wang's avatar
Allen Wang committed
200
201
202
203
204
  @owner_utils.Owner('tf-model-garden')
  def benchmark_perf_2x2_tpu_bf16_seq128_10k_steps(self):
    """Test bert pretraining with 2x2 TPU for 10000 steps."""
    self._setup()
    self._specify_common_flags()
Zongwei Zhou's avatar
Zongwei Zhou committed
205
    self._specify_tpu_common_flags()
Allen Wang's avatar
Allen Wang committed
206
207
208
209
210
211
212
213
214
    FLAGS.num_steps_per_epoch = 5000
    FLAGS.num_train_epochs = 2
    FLAGS.train_batch_size = 128
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_perf_2x2_tpu_bf16_seq128_10k_steps')
    summary_path = os.path.join(FLAGS.model_dir,
                                'summaries/training_summary.txt')
    # Disable accuracy check.
    self._run_and_report_benchmark(
Zongwei Zhou's avatar
Zongwei Zhou committed
215
216
217
        summary_path=summary_path,
        report_accuracy=False,
        ds_type=FLAGS.distribution_strategy)
Allen Wang's avatar
Allen Wang committed
218
219
220
221
222
223

  @owner_utils.Owner('tf-model-garden')
  def benchmark_perf_2x2_tpu_bf16_seq128_10k_steps_mlir(self):
    """Test bert pretraining with 2x2 TPU with MLIR for 10000 steps."""
    self._setup()
    self._specify_common_flags()
Zongwei Zhou's avatar
Zongwei Zhou committed
224
    self._specify_tpu_common_flags()
Allen Wang's avatar
Allen Wang committed
225
226
227
228
229
230
231
232
233
234
    FLAGS.num_steps_per_epoch = 5000
    FLAGS.num_train_epochs = 2
    FLAGS.train_batch_size = 128
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_perf_2x2_tpu_bf16_seq128_10k_steps_mlir')
    summary_path = os.path.join(FLAGS.model_dir,
                                'summaries/training_summary.txt')
    tf.config.experimental.enable_mlir_bridge()
    # Disable accuracy check.
    self._run_and_report_benchmark(
Zongwei Zhou's avatar
Zongwei Zhou committed
235
236
237
        summary_path=summary_path,
        report_accuracy=False,
        ds_type=FLAGS.distribution_strategy)
Allen Wang's avatar
Allen Wang committed
238

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
239
240
241
242
243
  @owner_utils.Owner('tf-model-garden')
  def benchmark_perf_4x4_tpu_bf16_seq128_10k_steps(self):
    """Test bert pretraining with 4x4 TPU for 10000 steps."""
    self._setup()
    self._specify_common_flags()
Zongwei Zhou's avatar
Zongwei Zhou committed
244
245
    self._specify_tpu_common_flags()
    FLAGS.train_batch_size = 512
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
246
247
248
249
250
251
252
253
    FLAGS.num_steps_per_epoch = 5000
    FLAGS.num_train_epochs = 2
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_perf_4x4_tpu_bf16_seq128_10k_steps')
    summary_path = os.path.join(FLAGS.model_dir,
                                'summaries/training_summary.txt')
    # Disable accuracy check.
    self._run_and_report_benchmark(
Zongwei Zhou's avatar
Zongwei Zhou committed
254
255
256
        summary_path=summary_path,
        report_accuracy=False,
        ds_type=FLAGS.distribution_strategy)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
257

Allen Wang's avatar
Allen Wang committed
258
259
260
261
262
  @owner_utils.Owner('tf-model-garden')
  def benchmark_perf_4x4_tpu_bf16_seq128_10k_steps_mlir(self):
    """Test bert pretraining with 4x4 TPU with MLIR for 10000 steps."""
    self._setup()
    self._specify_common_flags()
Zongwei Zhou's avatar
Zongwei Zhou committed
263
264
    self._specify_tpu_common_flags()
    FLAGS.train_batch_size = 512
Allen Wang's avatar
Allen Wang committed
265
266
267
268
269
270
271
272
273
    FLAGS.num_steps_per_epoch = 5000
    FLAGS.num_train_epochs = 2
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_perf_4x4_tpu_bf16_seq128_10k_steps_mlir')
    summary_path = os.path.join(FLAGS.model_dir,
                                'summaries/training_summary.txt')
    tf.config.experimental.enable_mlir_bridge()
    # Disable accuracy check.
    self._run_and_report_benchmark(
Zongwei Zhou's avatar
Zongwei Zhou committed
274
275
276
        summary_path=summary_path,
        report_accuracy=False,
        ds_type=FLAGS.distribution_strategy)
Allen Wang's avatar
Allen Wang committed
277

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
  @owner_utils.Owner('tf-model-garden')
  def benchmark_perf_4x4_tpu_bf16_seq128_1k_steps(self):
    """Test bert pretraining with 4x4 TPU for 1000 steps."""
    self._setup()
    self._specify_common_flags()
    self._specify_tpu_common_flags()
    FLAGS.train_batch_size = 512
    FLAGS.warmup_steps = 0
    FLAGS.num_steps_per_epoch = 1000
    FLAGS.num_train_epochs = 1
    FLAGS.steps_per_loop = 500
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_perf_4x4_tpu_bf16_seq128_1k_steps')
    summary_path = os.path.join(FLAGS.model_dir,
                                'summaries/training_summary.txt')
    # Disable accuracy check.
    self._run_and_report_benchmark(
        summary_path=summary_path,
        report_accuracy=False,
        ds_type=FLAGS.distribution_strategy)

Jing Li's avatar
Jing Li committed
299
300
301
  @owner_utils.Owner('tf-model-garden')
  def benchmark_perf_8x8_tpu_bf16_seq128_10k_steps(self):
    """Test bert pretraining with 8x8 TPU for 10000 steps."""
Chen Chen's avatar
Chen Chen committed
302
303
    self._setup()
    self._specify_common_flags()
Zongwei Zhou's avatar
Zongwei Zhou committed
304
305
    self._specify_tpu_common_flags()
    FLAGS.train_batch_size = 512
Jing Li's avatar
Jing Li committed
306
307
    FLAGS.num_steps_per_epoch = 5000
    FLAGS.num_train_epochs = 2
Chen Chen's avatar
Chen Chen committed
308
    FLAGS.model_dir = self._get_model_dir(
Jing Li's avatar
Jing Li committed
309
        'benchmark_perf_8x8_tpu_bf16_seq128_10k_steps')
Chen Chen's avatar
Chen Chen committed
310
311
    summary_path = os.path.join(FLAGS.model_dir,
                                'summaries/training_summary.txt')
Jing Li's avatar
Jing Li committed
312
    # Disable accuracy check.
Hongkun Yu's avatar
Hongkun Yu committed
313
    self._run_and_report_benchmark(
Zongwei Zhou's avatar
Zongwei Zhou committed
314
315
316
317
        summary_path=summary_path,
        report_accuracy=False,
        ds_type=FLAGS.distribution_strategy)

A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
  @owner_utils.Owner('tf-model-garden')
  def benchmark_perf_8x16_tpu_bf16_seq128_1k_steps(self):
    """Test bert pretraining with 8x16 TPU for 1000 steps."""
    self._setup()
    self._specify_common_flags()
    self._specify_tpu_common_flags()
    FLAGS.train_batch_size = 4096
    FLAGS.warmup_steps = 0
    FLAGS.num_steps_per_epoch = 1000
    FLAGS.num_train_epochs = 1
    FLAGS.steps_per_loop = 500
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_perf_8x16_tpu_bf16_seq128_1k_steps')
    summary_path = os.path.join(FLAGS.model_dir,
                                'summaries/training_summary.txt')
    # Disable accuracy check.
    self._run_and_report_benchmark(
        summary_path=summary_path,
        report_accuracy=False,
        ds_type=FLAGS.distribution_strategy)

Zongwei Zhou's avatar
Zongwei Zhou committed
339
340
341
342
343
344
345
  @owner_utils.Owner('tf-dist-strat')
  def benchmark_accuracy_1x8_gpu_fp16_seq128_15k_steps(self):
    """Test bert pretraining with 8 GPU for 15k steps."""
    # This is used for accuracy test.
    self._setup()
    self._specify_common_flags()
    self._specify_gpu_common_flags()
Zongwei Zhou's avatar
Zongwei Zhou committed
346
    FLAGS.num_gpus = 8
Zongwei Zhou's avatar
Zongwei Zhou committed
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
    FLAGS.train_batch_size = 96
    FLAGS.num_steps_per_epoch = 5000
    FLAGS.num_train_epochs = 3
    FLAGS.steps_per_loop = 5000
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_accuracy_1x8_gpu_fp16_seq128_15k_steps')
    summary_path = os.path.join(FLAGS.model_dir,
                                'summaries/training_summary.txt')
    # Set train_summary_interval to -1 to disable training summary, because
    # writing summary to gcs may fail and summaries are not needed for this
    # accuracy benchmark test.
    FLAGS.train_summary_interval = -1
    self._run_and_report_benchmark(
        summary_path=summary_path,
        report_accuracy=True,
        ds_type=FLAGS.distribution_strategy)

  @owner_utils.Owner('tf-dist-strat')
  def benchmark_perf_1x1_gpu_fp16_seq128_200_steps(self):
    """Test bert pretraining with 1 GPU for 200 steps."""
    self._setup()
    self._specify_common_flags()
    self._specify_gpu_common_flags()
    FLAGS.num_steps_per_epoch = 200
    FLAGS.num_train_epochs = 1
    FLAGS.num_gpus = 1
    FLAGS.train_batch_size = 12
    FLAGS.steps_per_loop = 100
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_perf_1x1_gpu_fp16_seq128_200_steps')
    summary_path = os.path.join(FLAGS.model_dir,
                                'summaries/training_summary.txt')
    # Disable accuracy check.
    self._run_and_report_benchmark(
        summary_path=summary_path,
        report_accuracy=False,
        ds_type=FLAGS.distribution_strategy)

  @owner_utils.Owner('tf-dist-strat')
  def benchmark_perf_1x8_gpu_fp16_seq128_200_steps(self):
    """Test bert pretraining with 8 GPU for 200 steps."""
    self._setup()
    self._specify_common_flags()
    self._specify_gpu_common_flags()
    FLAGS.num_steps_per_epoch = 200
    FLAGS.num_train_epochs = 1
    FLAGS.num_gpus = 8
    FLAGS.train_batch_size = 96
    FLAGS.steps_per_loop = 100
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_perf_1x8_gpu_fp16_seq128_200_steps')
    summary_path = os.path.join(FLAGS.model_dir,
                                'summaries/training_summary.txt')
    # Disable accuracy check.
    self._run_and_report_benchmark(
        summary_path=summary_path,
        report_accuracy=False,
        ds_type=FLAGS.distribution_strategy)
Chen Chen's avatar
Chen Chen committed
405
406


Zongwei Zhou's avatar
Zongwei Zhou committed
407
class BertPretrainMultiWorkerBenchmark(BertPretrainAccuracyBenchmark):
Zongwei Zhou's avatar
Zongwei Zhou committed
408
  """Bert pretrain distributed benchmark tests with multiple workers."""
Zongwei Zhou's avatar
Zongwei Zhou committed
409

Zongwei Zhou's avatar
Zongwei Zhou committed
410
  def __init__(self, output_dir=None, tpu=None, **kwargs):
Zongwei Zhou's avatar
Zongwei Zhou committed
411
    super(BertPretrainMultiWorkerBenchmark, self).__init__(
Zongwei Zhou's avatar
Zongwei Zhou committed
412
        output_dir=output_dir, tpu=tpu, **kwargs)
Zongwei Zhou's avatar
Zongwei Zhou committed
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474

  def _specify_gpu_mwms_flags(self):
    FLAGS.distribution_strategy = 'multi_worker_mirrored'
    FLAGS.all_reduce_alg = 'nccl'
    FLAGS.dtype = 'fp16'
    FLAGS.loss_scale = 'dynamic'
    FLAGS.num_gpus = 8

  @owner_utils.Owner('tf-dist-strat')
  def benchmark_accuracy_mwms_1x8_gpu_fp16_seq128_15k_steps(self):
    """Test bert pretraining with 8 GPU for 15k steps."""
    # This is used for accuracy test.
    self._setup()
    self._specify_common_flags()
    self._specify_gpu_mwms_flags()
    FLAGS.train_batch_size = 96
    FLAGS.num_steps_per_epoch = 5000
    FLAGS.num_train_epochs = 3
    FLAGS.steps_per_loop = 5000
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_accuracy_mwms_1x8_gpu_fp16_seq128_15k_steps')
    summary_path = os.path.join(FLAGS.model_dir,
                                'summaries/training_summary.txt')
    # Set train_summary_interval to -1 to disable training summary, because
    # writing summary to gcs may fail and summaries are not needed for this
    # accuracy benchmark test.
    FLAGS.train_summary_interval = -1
    self._run_and_report_benchmark(
        summary_path=summary_path,
        report_accuracy=True,
        ds_type=FLAGS.distribution_strategy)

  @owner_utils.Owner('tf-dist-strat')
  def benchmark_accuracy_mwms_2x8_gpu_fp16_seq128_15k_steps(self):
    """Test bert pretraining with 2x8 GPU for 15k steps."""
    # This is used for accuracy test.
    self._setup()
    self._specify_common_flags()
    self._specify_gpu_mwms_flags()
    # ues the same global batch size as accuracy_mwms_1x8 benchmark.
    FLAGS.train_batch_size = 96
    FLAGS.num_steps_per_epoch = 5000
    FLAGS.num_train_epochs = 3
    FLAGS.steps_per_loop = 5000
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_accuracy_mwms_2x8_gpu_fp16_seq128_15k_steps')
    summary_path = os.path.join(FLAGS.model_dir,
                                'summaries/training_summary.txt')
    # Set train_summary_interval to -1 to disable training summary, because
    # writing summary to gcs may fail and summaries are not needed for this
    # accuracy benchmark test.
    FLAGS.train_summary_interval = -1
    self._run_and_report_benchmark(
        summary_path=summary_path,
        report_accuracy=True,
        ds_type=FLAGS.distribution_strategy)

  @owner_utils.Owner('tf-dist-strat')
  def benchmark_perf_mwms_1x8_gpu_fp16_seq128_200_steps(self):
    """Test bert pretraining with 1x8 GPU for 200 steps."""
    self._setup()
    self._specify_common_flags()
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
475
    self._specify_gpu_mwms_flags()
Zongwei Zhou's avatar
Zongwei Zhou committed
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
    FLAGS.num_steps_per_epoch = 200
    FLAGS.num_train_epochs = 1
    FLAGS.train_batch_size = 96 * 1
    FLAGS.steps_per_loop = 100
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_perf_mwms_1x8_gpu_fp16_seq128_200_steps')
    summary_path = os.path.join(FLAGS.model_dir,
                                'summaries/training_summary.txt')
    # Disable accuracy check.
    self._run_and_report_benchmark(
        summary_path=summary_path,
        report_accuracy=False,
        ds_type=FLAGS.distribution_strategy)

  @owner_utils.Owner('tf-dist-strat')
  def benchmark_perf_mwms_2x8_gpu_fp16_seq128_200_steps(self):
    """Test bert pretraining with 2x8 GPU for 200 steps."""
    self._setup()
    self._specify_common_flags()
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
495
    self._specify_gpu_mwms_flags()
Zongwei Zhou's avatar
Zongwei Zhou committed
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
    FLAGS.num_steps_per_epoch = 200
    FLAGS.num_train_epochs = 1
    FLAGS.train_batch_size = 96 * 2
    FLAGS.steps_per_loop = 100
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_perf_mwms_2x8_gpu_fp16_seq128_200_steps')
    summary_path = os.path.join(FLAGS.model_dir,
                                'summaries/training_summary.txt')
    # Disable accuracy check.
    self._run_and_report_benchmark(
        summary_path=summary_path,
        report_accuracy=False,
        ds_type=FLAGS.distribution_strategy)

  @owner_utils.Owner('tf-dist-strat')
  def benchmark_perf_mwms_8x8_gpu_fp16_seq128_200_steps(self):
    """Test bert pretraining with 8x8 GPU for 200 steps."""
    self._setup()
    self._specify_common_flags()
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
515
    self._specify_gpu_mwms_flags()
Zongwei Zhou's avatar
Zongwei Zhou committed
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
    FLAGS.num_steps_per_epoch = 200
    FLAGS.num_train_epochs = 1
    FLAGS.train_batch_size = 96*8
    FLAGS.steps_per_loop = 100
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_perf_mwms_8x8_gpu_fp16_seq128_200_steps')
    summary_path = os.path.join(FLAGS.model_dir,
                                'summaries/training_summary.txt')
    # Disable accuracy check.
    self._run_and_report_benchmark(
        summary_path=summary_path,
        report_accuracy=False,
        ds_type=FLAGS.distribution_strategy)


Chen Chen's avatar
Chen Chen committed
531
532
if __name__ == '__main__':
  tf.test.main()