logger.py 10.5 KB
Newer Older
Scott Zhu's avatar
Scott Zhu committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 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.
# ==============================================================================

16
17
18
"""Logging utilities for benchmark.

For collecting local environment metrics like CPU and memory, certain python
19
packages need be installed. See README for details.
20
"""
Scott Zhu's avatar
Scott Zhu committed
21
22
23
24
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

25
import contextlib
Scott Zhu's avatar
Scott Zhu committed
26
27
28
29
import datetime
import json
import numbers
import os
Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
30
import threading
31
import uuid
Scott Zhu's avatar
Scott Zhu committed
32

33
from absl import flags
34
35
from absl import logging
from six.moves import _thread as thread
Scott Zhu's avatar
Scott Zhu committed
36
import tensorflow as tf
37
from tensorflow.python.client import device_lib
38
from official.r1.utils.logs import cloud_lib
39

40
41
METRIC_LOG_FILE_NAME = "metric.log"
BENCHMARK_RUN_LOG_FILE_NAME = "benchmark_run.log"
Scott Zhu's avatar
Scott Zhu committed
42
_DATE_TIME_FORMAT_PATTERN = "%Y-%m-%dT%H:%M:%S.%fZ"
43
GCP_TEST_ENV = "GCP"
44
45
46
RUN_STATUS_SUCCESS = "success"
RUN_STATUS_FAILURE = "failure"
RUN_STATUS_RUNNING = "running"
Scott Zhu's avatar
Scott Zhu committed
47

48

49
FLAGS = flags.FLAGS
Scott Zhu's avatar
Scott Zhu committed
50

Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
51
52
53
# Don't use it directly. Use get_benchmark_logger to access a logger.
_benchmark_logger = None
_logger_lock = threading.Lock()
Scott Zhu's avatar
Scott Zhu committed
54
55


56
def config_benchmark_logger(flag_obj=None):
Karmel Allison's avatar
Karmel Allison committed
57
  """Config the global benchmark logger."""
Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
58
59
60
  _logger_lock.acquire()
  try:
    global _benchmark_logger
61
62
63
    if not flag_obj:
      flag_obj = FLAGS

Karmel Allison's avatar
Karmel Allison committed
64
65
    if (not hasattr(flag_obj, "benchmark_logger_type") or
        flag_obj.benchmark_logger_type == "BaseBenchmarkLogger"):
Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
66
      _benchmark_logger = BaseBenchmarkLogger()
Karmel Allison's avatar
Karmel Allison committed
67
    elif flag_obj.benchmark_logger_type == "BenchmarkFileLogger":
68
69
      _benchmark_logger = BenchmarkFileLogger(flag_obj.benchmark_log_dir)
    else:
Karmel Allison's avatar
Karmel Allison committed
70
71
      raise ValueError("Unrecognized benchmark_logger_type: %s"
                       % flag_obj.benchmark_logger_type)
72

Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
73
74
75
76
77
78
79
  finally:
    _logger_lock.release()
  return _benchmark_logger


def get_benchmark_logger():
  if not _benchmark_logger:
80
    config_benchmark_logger()
Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
81
82
83
  return _benchmark_logger


84
85
86
87
88
89
90
91
92
93
94
95
96
@contextlib.contextmanager
def benchmark_context(flag_obj):
  """Context of benchmark, which will update status of the run accordingly."""
  benchmark_logger = config_benchmark_logger(flag_obj)
  try:
    yield
    benchmark_logger.on_finish(RUN_STATUS_SUCCESS)
  except Exception:  # pylint: disable=broad-except
    # Catch all the exception, update the run status to be failure, and re-raise
    benchmark_logger.on_finish(RUN_STATUS_FAILURE)
    raise


Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
97
98
99
100
101
class BaseBenchmarkLogger(object):
  """Class to log the benchmark information to STDOUT."""

  def log_evaluation_result(self, eval_results):
    """Log the evaluation result.
102

Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
103
    The evaluate result is a dictionary that contains metrics defined in
104
105
106
107
    model_fn. It also contains a entry for global_step which contains the value
    of the global step when evaluation was performed.

    Args:
Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
108
      eval_results: dict, the result of evaluate.
109
110
    """
    if not isinstance(eval_results, dict):
111
112
      logging.warning("eval_results should be dictionary for logging. Got %s",
                      type(eval_results))
113
      return
114
    global_step = eval_results[tf.compat.v1.GraphKeys.GLOBAL_STEP]
115
    for key in sorted(eval_results):
116
      if key != tf.compat.v1.GraphKeys.GLOBAL_STEP:
117
118
        self.log_metric(key, eval_results[key], global_step=global_step)

Scott Zhu's avatar
Scott Zhu committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
  def log_metric(self, name, value, unit=None, global_step=None, extras=None):
    """Log the benchmark metric information to local file.

    Currently the logging is done in a synchronized way. This should be updated
    to log asynchronously.

    Args:
      name: string, the name of the metric to log.
      value: number, the value of the metric. The value will not be logged if it
        is not a number type.
      unit: string, the unit of the metric, E.g "image per second".
      global_step: int, the global_step when the metric is logged.
      extras: map of string:string, the extra information about the metric.
    """
133
134
    metric = _process_metric_to_json(name, value, unit, global_step, extras)
    if metric:
135
      logging.info("Benchmark metric: %s", metric)
Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
136

137
  def log_run_info(self, model_name, dataset_name, run_params, test_id=None):
138
    logging.info(
139
140
        "Benchmark run: %s",
        _gather_run_info(model_name, dataset_name, run_params, test_id))
Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
141

142
143
144
  def on_finish(self, status):
    pass

Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
145
146
147
148
149
150
151

class BenchmarkFileLogger(BaseBenchmarkLogger):
  """Class to log the benchmark information to local disk."""

  def __init__(self, logging_dir):
    super(BenchmarkFileLogger, self).__init__()
    self._logging_dir = logging_dir
152
153
154
    if not tf.io.gfile.isdir(self._logging_dir):
      tf.io.gfile.makedirs(self._logging_dir)
    self._metric_file_handler = tf.io.gfile.GFile(
155
        os.path.join(self._logging_dir, METRIC_LOG_FILE_NAME), "a")
Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170

  def log_metric(self, name, value, unit=None, global_step=None, extras=None):
    """Log the benchmark metric information to local file.

    Currently the logging is done in a synchronized way. This should be updated
    to log asynchronously.

    Args:
      name: string, the name of the metric to log.
      value: number, the value of the metric. The value will not be logged if it
        is not a number type.
      unit: string, the unit of the metric, E.g "image per second".
      global_step: int, the global_step when the metric is logged.
      extras: map of string:string, the extra information about the metric.
    """
171
172
    metric = _process_metric_to_json(name, value, unit, global_step, extras)
    if metric:
173
174
175
176
177
      try:
        json.dump(metric, self._metric_file_handler)
        self._metric_file_handler.write("\n")
        self._metric_file_handler.flush()
      except (TypeError, ValueError) as e:
178
        logging.warning(
179
180
            "Failed to dump metric to log file: name %s, value %s, error %s",
            name, value, e)
181

182
  def log_run_info(self, model_name, dataset_name, run_params, test_id=None):
183
184
185
186
187
188
    """Collect most of the TF runtime information for the local env.

    The schema of the run info follows official/benchmark/datastore/schema.

    Args:
      model_name: string, the name of the model.
189
190
191
      dataset_name: string, the name of dataset for training and evaluation.
      run_params: dict, the dictionary of parameters for the run, it could
        include hyperparameters or other params that are important for the run.
192
193
      test_id: string, the unique name of the test run by the combination of key
        parameters, eg batch size, num of GPU. It is hardware independent.
194
    """
195
    run_info = _gather_run_info(model_name, dataset_name, run_params, test_id)
196

197
    with tf.io.gfile.GFile(os.path.join(
198
        self._logging_dir, BENCHMARK_RUN_LOG_FILE_NAME), "w") as f:
199
200
201
202
      try:
        json.dump(run_info, f)
        f.write("\n")
      except (TypeError, ValueError) as e:
203
        logging.warning("Failed to dump benchmark run info to log file: %s", e)
204

205
  def on_finish(self, status):
206
207
    self._metric_file_handler.flush()
    self._metric_file_handler.close()
208

209

210
def _gather_run_info(model_name, dataset_name, run_params, test_id):
Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
211
212
213
  """Collect the benchmark run information for the local environment."""
  run_info = {
      "model_name": model_name,
214
      "dataset": {"name": dataset_name},
Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
215
      "machine_config": {},
216
      "test_id": test_id,
217
218
      "run_date": datetime.datetime.utcnow().strftime(
          _DATE_TIME_FORMAT_PATTERN)}
Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
219
220
  _collect_tensorflow_info(run_info)
  _collect_tensorflow_environment_variables(run_info)
221
  _collect_run_params(run_info, run_params)
Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
222
  _collect_memory_info(run_info)
223
  _collect_test_environment(run_info)
Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
224
225
226
  return run_info


227
228
229
230
def _process_metric_to_json(
    name, value, unit=None, global_step=None, extras=None):
  """Validate the metric data and generate JSON for insert."""
  if not isinstance(value, numbers.Number):
231
232
    logging.warning("Metric value to log should be a number. Got %s",
                    type(value))
233
234
235
236
237
238
239
240
241
242
243
244
245
    return None

  extras = _convert_to_json_dict(extras)
  return {
      "name": name,
      "value": float(value),
      "unit": unit,
      "global_step": global_step,
      "timestamp": datetime.datetime.utcnow().strftime(
          _DATE_TIME_FORMAT_PATTERN),
      "extras": extras}


246
247
def _collect_tensorflow_info(run_info):
  run_info["tensorflow_version"] = {
248
      "version": tf.version.VERSION, "git_hash": tf.version.GIT_VERSION}
249
250


251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
def _collect_run_params(run_info, run_params):
  """Log the parameter information for the benchmark run."""
  def process_param(name, value):
    type_check = {
        str: {"name": name, "string_value": value},
        int: {"name": name, "long_value": value},
        bool: {"name": name, "bool_value": str(value)},
        float: {"name": name, "float_value": value},
    }
    return type_check.get(type(value),
                          {"name": name, "string_value": str(value)})
  if run_params:
    run_info["run_parameters"] = [
        process_param(k, v) for k, v in sorted(run_params.items())]

Karmel Allison's avatar
Karmel Allison committed
266

267
def _collect_tensorflow_environment_variables(run_info):
268
269
270
  run_info["tensorflow_environment_variables"] = [
      {"name": k, "value": v}
      for k, v in sorted(os.environ.items()) if k.startswith("TF_")]
271
272
273


def _collect_memory_info(run_info):
274
275
276
277
278
279
280
281
  try:
    # Note: psutil is not installed in the TensorFlow OSS tree.
    # It is installable via pip.
    import psutil   # pylint: disable=g-import-not-at-top
    vmem = psutil.virtual_memory()
    run_info["machine_config"]["memory_total"] = vmem.total
    run_info["machine_config"]["memory_available"] = vmem.available
  except ImportError:
282
    logging.warn("'psutil' not imported. Memory info will not be logged.")
283
284


285
286
287
288
289
290
291
def _collect_test_environment(run_info):
  """Detect the local environment, eg GCE, AWS or DGX, etc."""
  if cloud_lib.on_gcp():
    run_info["test_environment"] = GCP_TEST_ENV
  # TODO(scottzhu): Add more testing env detection for other platform


292
293
294
295
296
297
298
def _parse_gpu_model(physical_device_desc):
  # Assume all the GPU connected are same model
  for kv in physical_device_desc.split(","):
    k, _, v = kv.partition(":")
    if k.strip() == "name":
      return v.strip()
  return None
Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
299
300
301
302
303
304
305


def _convert_to_json_dict(input_dict):
  if input_dict:
    return [{"name": k, "value": v} for k, v in sorted(input_dict.items())]
  else:
    return []