bert_benchmark.py 14.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Copyright 2019 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 BERT benchmarks and accuracy tests."""

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

Rajagopal Ananthanarayanan's avatar
Rajagopal Ananthanarayanan committed
21
import functools
22
import json
23
import math
24
25
26
import os
import time

27
# pylint: disable=g-bad-import-order
28
29
from absl import flags
from absl.testing import flagsaver
30
import tensorflow as tf
31
# pylint: enable=g-bad-import-order
32

33
from official.benchmark import bert_benchmark_utils as benchmark_utils
34
35
from official.nlp import bert_modeling as modeling
from official.nlp.bert import run_classifier
36
from official.utils.misc import distribution_utils
37
from official.utils.testing import benchmark_wrappers
38
39

# pylint: disable=line-too-long
40
PRETRAINED_CHECKPOINT_PATH = 'gs://cloud-tpu-checkpoints/bert/keras_bert/uncased_L-24_H-1024_A-16/bert_model.ckpt'
41
42
43
CLASSIFIER_TRAIN_DATA_PATH = 'gs://tf-perfzero-data/bert/classification/mrpc_train.tf_record'
CLASSIFIER_EVAL_DATA_PATH = 'gs://tf-perfzero-data/bert/classification/mrpc_eval.tf_record'
CLASSIFIER_INPUT_META_DATA_PATH = 'gs://tf-perfzero-data/bert/classification/mrpc_meta_data'
David Chen's avatar
David Chen committed
44
MODEL_CONFIG_FILE_PATH = 'gs://cloud-tpu-checkpoints/bert/keras_bert/uncased_L-24_H-1024_A-16/bert_config.json'
45
46
# pylint: enable=line-too-long

David Chen's avatar
David Chen committed
47
TMP_DIR = os.getenv('TMPDIR')
48
49
50
FLAGS = flags.FLAGS


davidmochen's avatar
davidmochen committed
51
class BertClassifyBenchmarkBase(benchmark_utils.BertBenchmarkBase):
52
53
  """Base class to hold methods common to test classes in the module."""

David Chen's avatar
David Chen committed
54
  def __init__(self, output_dir=None, tpu=None):
55
    super(BertClassifyBenchmarkBase, self).__init__(output_dir)
56
57
    self.num_epochs = None
    self.num_steps_per_epoch = None
David Chen's avatar
David Chen committed
58
    self.tpu = tpu
59

60
  @flagsaver.flagsaver
61
  def _run_bert_classifier(self, callbacks=None, use_ds=True):
62
    """Starts BERT classification task."""
63
64
65
    with tf.io.gfile.GFile(FLAGS.input_meta_data_path, 'rb') as reader:
      input_meta_data = json.loads(reader.read().decode('utf-8'))

66
    bert_config = modeling.BertConfig.from_json_file(FLAGS.bert_config_file)
67
68
69
70
71
72
73
    epochs = self.num_epochs if self.num_epochs else FLAGS.num_train_epochs
    if self.num_steps_per_epoch:
      steps_per_epoch = self.num_steps_per_epoch
    else:
      train_data_size = input_meta_data['train_data_size']
      steps_per_epoch = int(train_data_size / FLAGS.train_batch_size)
    warmup_steps = int(epochs * steps_per_epoch * 0.1)
74
75
    eval_steps = int(
        math.ceil(input_meta_data['eval_data_size'] / FLAGS.eval_batch_size))
David Chen's avatar
David Chen committed
76
77
78
79
80
81
82
    if self.tpu:
      strategy = distribution_utils.get_distribution_strategy(
          distribution_strategy='tpu', tpu_address=self.tpu)
    else:
      strategy = distribution_utils.get_distribution_strategy(
          distribution_strategy='mirrored' if use_ds else 'off',
          num_gpus=self.num_gpus)
83

84
    steps_per_loop = 1
85

Rajagopal Ananthanarayanan's avatar
Rajagopal Ananthanarayanan committed
86
    max_seq_length = input_meta_data['max_seq_length']
Hongkun Yu's avatar
Hongkun Yu committed
87
    train_input_fn = run_classifier.get_dataset_fn(
Rajagopal Ananthanarayanan's avatar
Rajagopal Ananthanarayanan committed
88
        FLAGS.train_data_path,
Hongkun Yu's avatar
Hongkun Yu committed
89
90
91
92
        max_seq_length,
        FLAGS.train_batch_size,
        is_training=True)
    eval_input_fn = run_classifier.get_dataset_fn(
Rajagopal Ananthanarayanan's avatar
Rajagopal Ananthanarayanan committed
93
        FLAGS.eval_data_path,
Hongkun Yu's avatar
Hongkun Yu committed
94
95
96
        max_seq_length,
        FLAGS.eval_batch_size,
        is_training=False)
97
    run_classifier.run_bert_classifier(
98
99
100
101
102
103
        strategy,
        bert_config,
        input_meta_data,
        FLAGS.model_dir,
        epochs,
        steps_per_epoch,
104
        steps_per_loop,
105
106
107
108
        eval_steps,
        warmup_steps,
        FLAGS.learning_rate,
        FLAGS.init_checkpoint,
Rajagopal Ananthanarayanan's avatar
Rajagopal Ananthanarayanan committed
109
110
        train_input_fn,
        eval_input_fn,
111
112
113
        custom_callbacks=callbacks)


davidmochen's avatar
davidmochen committed
114
class BertClassifyBenchmarkReal(BertClassifyBenchmarkBase):
115
116
  """Short benchmark performance tests for BERT model.

David Chen's avatar
David Chen committed
117
  Tests BERT classification performance in different GPU, TPU configurations.
118
  The naming convention of below test cases follow
David Chen's avatar
David Chen committed
119
120
  `benchmark_(number of gpus)_gpu_(dataset type)` for GPUs and
  `benchmark_(topology)_tpu_(dataset type)` for TPUs.
121
  """
122

David Chen's avatar
David Chen committed
123
124
125
  def __init__(self, output_dir=TMP_DIR, tpu=None, **kwargs):
    super(BertClassifyBenchmarkReal, self).__init__(
        output_dir=output_dir, tpu=tpu)
126

127
128
129
130
    self.train_data_path = CLASSIFIER_TRAIN_DATA_PATH
    self.eval_data_path = CLASSIFIER_EVAL_DATA_PATH
    self.bert_config_file = MODEL_CONFIG_FILE_PATH
    self.input_meta_data_path = CLASSIFIER_INPUT_META_DATA_PATH
131

132
133
134
135
136
    # Since we only care about performance metrics, we limit
    # the number of training steps and epochs to prevent unnecessarily
    # long tests.
    self.num_steps_per_epoch = 110
    self.num_epochs = 1
137

138
  @benchmark_wrappers.enable_runtime_flags
139
140
141
  def _run_and_report_benchmark(self,
                                training_summary_path,
                                min_accuracy=0,
142
                                max_accuracy=1,
143
                                use_ds=True):
144
145
    """Starts BERT performance benchmark test."""
    start_time_sec = time.time()
146
    self._run_bert_classifier(callbacks=[self.timer_callback], use_ds=use_ds)
147
148
149
150
151
152
153
154
    wall_time_sec = time.time() - start_time_sec

    with tf.io.gfile.GFile(training_summary_path, 'rb') as reader:
      summary = json.loads(reader.read().decode('utf-8'))

    # Since we do not load from any pretrained checkpoints, we ignore all
    # accuracy metrics.
    summary.pop('eval_metrics', None)
155
    super(BertClassifyBenchmarkReal, self)._report_benchmark(
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
        stats=summary,
        wall_time_sec=wall_time_sec,
        min_accuracy=min_accuracy,
        max_accuracy=max_accuracy)

  def benchmark_1_gpu_mrpc(self):
    """Test BERT model performance with 1 GPU."""

    self._setup()
    self.num_gpus = 1
    FLAGS.model_dir = self._get_model_dir('benchmark_1_gpu_mrpc')
    FLAGS.train_data_path = self.train_data_path
    FLAGS.eval_data_path = self.eval_data_path
    FLAGS.input_meta_data_path = self.input_meta_data_path
    FLAGS.bert_config_file = self.bert_config_file
    FLAGS.train_batch_size = 4
    FLAGS.eval_batch_size = 4

174
175
    summary_path = os.path.join(FLAGS.model_dir,
                                'summaries/training_summary.txt')
176
177
    self._run_and_report_benchmark(summary_path)

178
179
180
181
182
183
184
185
186
187
188
189
  def benchmark_1_gpu_mrpc_xla(self):
    """Test BERT model performance with 1 GPU."""

    self._setup()
    self.num_gpus = 1
    FLAGS.model_dir = self._get_model_dir('benchmark_1_gpu_mrpc_xla')
    FLAGS.train_data_path = self.train_data_path
    FLAGS.eval_data_path = self.eval_data_path
    FLAGS.input_meta_data_path = self.input_meta_data_path
    FLAGS.bert_config_file = self.bert_config_file
    FLAGS.train_batch_size = 4
    FLAGS.eval_batch_size = 4
190
    FLAGS.enable_xla = True
191

192
193
    summary_path = os.path.join(FLAGS.model_dir,
                                'summaries/training_summary.txt')
194
    self._run_and_report_benchmark(summary_path)
195
196
197
198
199
200
201
202
203
204
205
206
207
208

  def benchmark_1_gpu_mrpc_no_dist_strat(self):
    """Test BERT model performance with 1 GPU, no distribution strategy."""

    self._setup()
    self.num_gpus = 1
    FLAGS.model_dir = self._get_model_dir('benchmark_1_gpu_mrpc_no_dist_strat')
    FLAGS.train_data_path = self.train_data_path
    FLAGS.eval_data_path = self.eval_data_path
    FLAGS.input_meta_data_path = self.input_meta_data_path
    FLAGS.bert_config_file = self.bert_config_file
    FLAGS.train_batch_size = 4
    FLAGS.eval_batch_size = 4

209
210
    summary_path = os.path.join(FLAGS.model_dir,
                                'summaries/training_summary.txt')
211
212
    self._run_and_report_benchmark(summary_path, use_ds=False)

213
  def benchmark_2_gpu_mrpc(self):
214
215
216
217
    """Test BERT model performance with 2 GPUs."""

    self._setup()
    self.num_gpus = 2
218
    FLAGS.model_dir = self._get_model_dir('benchmark_2_gpu_mrpc')
219
220
221
222
223
224
    FLAGS.train_data_path = self.train_data_path
    FLAGS.eval_data_path = self.eval_data_path
    FLAGS.input_meta_data_path = self.input_meta_data_path
    FLAGS.bert_config_file = self.bert_config_file
    FLAGS.train_batch_size = 8
    FLAGS.eval_batch_size = 8
225

226
227
    summary_path = os.path.join(FLAGS.model_dir,
                                'summaries/training_summary.txt')
228
229
230
231
232
233
234
235
236
237
238
239
240
241
    self._run_and_report_benchmark(summary_path)

  def benchmark_4_gpu_mrpc(self):
    """Test BERT model performance with 4 GPUs."""

    self._setup()
    self.num_gpus = 4
    FLAGS.model_dir = self._get_model_dir('benchmark_4_gpu_mrpc')
    FLAGS.train_data_path = self.train_data_path
    FLAGS.eval_data_path = self.eval_data_path
    FLAGS.input_meta_data_path = self.input_meta_data_path
    FLAGS.bert_config_file = self.bert_config_file
    FLAGS.train_batch_size = 16

242
243
    summary_path = os.path.join(FLAGS.model_dir,
                                'summaries/training_summary.txt')
244
245
246
    self._run_and_report_benchmark(summary_path)

  def benchmark_8_gpu_mrpc(self):
247
248
249
    """Test BERT model performance with 8 GPUs."""

    self._setup()
250
    FLAGS.model_dir = self._get_model_dir('benchmark_8_gpu_mrpc')
251
252
253
254
255
    FLAGS.train_data_path = self.train_data_path
    FLAGS.eval_data_path = self.eval_data_path
    FLAGS.input_meta_data_path = self.input_meta_data_path
    FLAGS.bert_config_file = self.bert_config_file

256
257
    summary_path = os.path.join(FLAGS.model_dir,
                                'summaries/training_summary.txt')
258
259
    self._run_and_report_benchmark(summary_path)

260
  def benchmark_1_gpu_amp_mrpc_no_dist_strat(self):
261
    """Performance for 1 GPU no DS with automatic mixed precision."""
262
263
    self._setup()
    self.num_gpus = 1
264
265
    FLAGS.model_dir = self._get_model_dir(
        'benchmark_1_gpu_amp_mrpc_no_dist_strat')
266
267
268
269
270
271
272
273
274
    FLAGS.train_data_path = self.train_data_path
    FLAGS.eval_data_path = self.eval_data_path
    FLAGS.input_meta_data_path = self.input_meta_data_path
    FLAGS.bert_config_file = self.bert_config_file
    FLAGS.train_batch_size = 4
    FLAGS.eval_batch_size = 4
    FLAGS.dtype = 'fp16'
    FLAGS.fp16_implementation = 'graph_rewrite'

275
276
    summary_path = os.path.join(FLAGS.model_dir,
                                'summaries/training_summary.txt')
277
278
279
    self._run_and_report_benchmark(summary_path, use_ds=False)

  def benchmark_8_gpu_amp_mrpc(self):
280
281
    """Test BERT model performance with 8 GPUs with automatic mixed precision.
    """
282
283
284
285
286
287
288
289
290
291
292
293
294

    self._setup()
    self.num_gpus = 8
    FLAGS.model_dir = self._get_model_dir('benchmark_8_gpu_amp_mrpc')
    FLAGS.train_data_path = self.train_data_path
    FLAGS.eval_data_path = self.eval_data_path
    FLAGS.input_meta_data_path = self.input_meta_data_path
    FLAGS.bert_config_file = self.bert_config_file
    FLAGS.train_batch_size = 32
    FLAGS.eval_batch_size = 32
    FLAGS.dtype = 'fp16'
    FLAGS.fp16_implementation = 'graph_rewrite'

295
296
    summary_path = os.path.join(FLAGS.model_dir,
                                'summaries/training_summary.txt')
297
    self._run_and_report_benchmark(summary_path, use_ds=False)
298

David Chen's avatar
David Chen committed
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
  def benchmark_2x2_tpu_mrpc(self):
    """Test BERT model performance with 2x2 TPU."""

    self._setup()
    FLAGS.model_dir = self._get_model_dir('benchmark_2x2_tpu_mrpc')
    FLAGS.train_data_path = self.train_data_path
    FLAGS.eval_data_path = self.eval_data_path
    FLAGS.input_meta_data_path = self.input_meta_data_path
    FLAGS.bert_config_file = self.bert_config_file
    FLAGS.train_batch_size = 32
    FLAGS.eval_batch_size = 32

    summary_path = os.path.join(FLAGS.model_dir,
                                'summaries/training_summary.txt')
    self._run_and_report_benchmark(summary_path, use_ds=False)

315

davidmochen's avatar
davidmochen committed
316
class BertClassifyAccuracy(BertClassifyBenchmarkBase):
317
318
319
320
321
322
  """Short accuracy test for BERT model.

  Tests BERT classification task model accuracy. The naming
  convention of below test cases follow
  `benchmark_(number of gpus)_gpu_(dataset type)` format.
  """
323

David Chen's avatar
David Chen committed
324
  def __init__(self, output_dir=TMP_DIR, **kwargs):
325
326
327
328
    self.train_data_path = CLASSIFIER_TRAIN_DATA_PATH
    self.eval_data_path = CLASSIFIER_EVAL_DATA_PATH
    self.bert_config_file = MODEL_CONFIG_FILE_PATH
    self.input_meta_data_path = CLASSIFIER_INPUT_META_DATA_PATH
329
    self.pretrained_checkpoint_path = PRETRAINED_CHECKPOINT_PATH
330

331
    super(BertClassifyAccuracy, self).__init__(output_dir=output_dir)
332

333
  @benchmark_wrappers.enable_runtime_flags
334
335
336
  def _run_and_report_benchmark(self,
                                training_summary_path,
                                min_accuracy=0.84,
337
                                max_accuracy=0.88):
338
339
    """Starts BERT accuracy benchmark test."""

340
    start_time_sec = time.time()
341
    self._run_bert_classifier(callbacks=[self.timer_callback])
342
343
    wall_time_sec = time.time() - start_time_sec

344
345
346
    with tf.io.gfile.GFile(training_summary_path, 'rb') as reader:
      summary = json.loads(reader.read().decode('utf-8'))

347
348
349
350
351
    super(BertClassifyAccuracy, self)._report_benchmark(
        stats=summary,
        wall_time_sec=wall_time_sec,
        min_accuracy=min_accuracy,
        max_accuracy=max_accuracy)
352

353
354
355
356
357
358
359
360
  def _setup(self):
    super(BertClassifyAccuracy, self)._setup()
    FLAGS.train_data_path = self.train_data_path
    FLAGS.eval_data_path = self.eval_data_path
    FLAGS.input_meta_data_path = self.input_meta_data_path
    FLAGS.bert_config_file = self.bert_config_file
    FLAGS.init_checkpoint = self.pretrained_checkpoint_path

361
362
363
364
365
366
367
  def benchmark_8_gpu_mrpc(self):
    """Run BERT model accuracy test with 8 GPUs.

    Due to comparatively small cardinality of  MRPC dataset, training
    accuracy metric has high variance between trainings. As so, we
    set the wide range of allowed accuracy (84% to 88%).
    """
368
    self._setup()
369
    FLAGS.model_dir = self._get_model_dir('benchmark_8_gpu_mrpc')
370

371
372
    summary_path = os.path.join(FLAGS.model_dir,
                                'summaries/training_summary.txt')
373
    self._run_and_report_benchmark(summary_path)
374

375
376
377
378
  def benchmark_8_gpu_mrpc_xla(self):
    """Run BERT model accuracy test with 8 GPUs with XLA."""
    self._setup()
    FLAGS.model_dir = self._get_model_dir('benchmark_8_gpu_mrpc_xla')
379
    FLAGS.enable_xla = True
380
381
    summary_path = os.path.join(FLAGS.model_dir,
                                'summaries/training_summary.txt')
382
    self._run_and_report_benchmark(summary_path)
383

384
385
386

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