bert_benchmark_utils.py 4.41 KB
Newer Older
davidmochen's avatar
davidmochen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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.
# ==============================================================================
"""Utility functions or classes shared between BERT benchmarks."""

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

import time

# pylint: disable=g-bad-import-order
Hongkun Yu's avatar
Hongkun Yu committed
24

davidmochen's avatar
davidmochen committed
25
26
import numpy as np
from absl import flags
Hongkun Yu's avatar
Hongkun Yu committed
27
import tensorflow as tf
davidmochen's avatar
davidmochen committed
28
29
# pylint: enable=g-bad-import-order

Toby Boyd's avatar
Toby Boyd committed
30
from official.utils.flags import core as flags_core
31
from official.benchmark.perfzero_benchmark import PerfZeroBenchmark
Toby Boyd's avatar
Toby Boyd committed
32

davidmochen's avatar
davidmochen committed
33
34
35
36
37
38
39
40
FLAGS = flags.FLAGS


class BenchmarkTimerCallback(tf.keras.callbacks.Callback):
  """Callback that records time it takes to run each batch."""

  def __init__(self, num_batches_to_skip=10):
    super(BenchmarkTimerCallback, self).__init__()
David Chen's avatar
David Chen committed
41
42
    self.batch_start_times = {}
    self.batch_stop_times = {}
davidmochen's avatar
davidmochen committed
43

44
  def on_batch_begin(self, batch, logs=None):
David Chen's avatar
David Chen committed
45
    self.batch_start_times[batch] = time.time()
davidmochen's avatar
davidmochen committed
46
47

  def on_batch_end(self, batch, logs=None):
48
49
50
51
52
    # If there are multiple steps_per_loop, the end batch index will not be the
    # same as the starting index. Use the last starting index instead.
    if batch not in self.batch_start_times:
      batch = max(self.batch_start_times.keys())

David Chen's avatar
David Chen committed
53
    self.batch_stop_times[batch] = time.time()
davidmochen's avatar
davidmochen committed
54

55
  def get_examples_per_sec(self, batch_size, num_batches_to_skip=1):
David Chen's avatar
David Chen committed
56
57
58
59
60
61
    batch_durations = []
    for batch in self.batch_start_times:
      if batch in self.batch_stop_times and batch >= num_batches_to_skip:
        batch_durations.append(self.batch_stop_times[batch] -
                               self.batch_start_times[batch])
    return batch_size / np.mean(batch_durations)
davidmochen's avatar
davidmochen committed
62

David Chen's avatar
David Chen committed
63
64
  def get_startup_time(self, program_start_time):
    return self.batch_start_times[0] - program_start_time
davidmochen's avatar
davidmochen committed
65
66


David Chen's avatar
David Chen committed
67
class BertBenchmarkBase(PerfZeroBenchmark):
davidmochen's avatar
davidmochen committed
68
69
70
  """Base class to hold methods common to test classes."""
  local_flags = None

Chen Chen's avatar
Chen Chen committed
71
72
73
  def __init__(self, output_dir=None, tpu=None, **kwargs):
    super(BertBenchmarkBase, self).__init__(
        output_dir=output_dir, tpu=tpu, **kwargs)
davidmochen's avatar
davidmochen committed
74
75
76
77
78
    self.num_gpus = 8
    self.timer_callback = None

  def _setup(self):
    """Sets up and resets flags before each test."""
David Chen's avatar
David Chen committed
79
    super(BertBenchmarkBase, self)._setup()
davidmochen's avatar
davidmochen committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    self.timer_callback = BenchmarkTimerCallback()

  def _report_benchmark(self, stats, wall_time_sec, min_accuracy, max_accuracy):
    """Report benchmark results by writing to local protobuf file.

    Args:
      stats: dict returned from BERT models with known entries.
      wall_time_sec: the during of the benchmark execution in seconds
      min_accuracy: Minimum classification accuracy constraint to verify
        correctness of the model.
      max_accuracy: Maximum classification accuracy constraint to verify
        correctness of the model.
    """
    metrics = [{
        'name': 'training_loss',
        'value': stats['train_loss'],
    }]
97
98
99
100
101
    if self.timer_callback:
      metrics.append({
          'name':
              'exp_per_second',
          'value':
102
103
              self.timer_callback.get_examples_per_sec(FLAGS.train_batch_size *
                                                       FLAGS.steps_per_loop)
104
105
106
107
108
109
      })
    else:
      metrics.append({
          'name': 'exp_per_second',
          'value': 0.0,
      })
David Chen's avatar
David Chen committed
110
111
112
113
114
    if self.timer_callback and 'start_time_sec' in stats:
      metrics.append({
          'name': 'startup_time',
          'value': self.timer_callback.get_startup_time(stats['start_time_sec'])
      })
davidmochen's avatar
davidmochen committed
115
116
117
118
119
120
121
122

    if 'eval_metrics' in stats:
      metrics.append({
          'name': 'eval_accuracy',
          'value': stats['eval_metrics'],
          'min_value': min_accuracy,
          'max_value': max_accuracy,
      })
Toby Boyd's avatar
Toby Boyd committed
123
    flags_str = flags_core.get_nondefault_flags_as_str()
davidmochen's avatar
davidmochen committed
124
125
126
    self.report_benchmark(
        iters=stats['total_training_steps'],
        wall_time=wall_time_sec,
Toby Boyd's avatar
Toby Boyd committed
127
128
        metrics=metrics,
        extras={'flags': flags_str})