logger.py 6.85 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
25
26
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import datetime
import json
27
import multiprocessing
Scott Zhu's avatar
Scott Zhu committed
28
29
30
31
import numbers
import os

import tensorflow as tf
32
from tensorflow.python.client import device_lib
Scott Zhu's avatar
Scott Zhu committed
33

34
35
METRIC_LOG_FILE_NAME = "metric.log"
BENCHMARK_RUN_LOG_FILE_NAME = "benchmark_run.log"
Scott Zhu's avatar
Scott Zhu committed
36
37
38
39
40
41
42
43
44
45
46
_DATE_TIME_FORMAT_PATTERN = "%Y-%m-%dT%H:%M:%S.%fZ"


class BenchmarkLogger(object):
  """Class to log the benchmark information to local disk."""

  def __init__(self, logging_dir):
    self._logging_dir = logging_dir
    if not tf.gfile.IsDirectory(self._logging_dir):
      tf.gfile.MakeDirs(self._logging_dir)

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  def log_estimator_evaluation_result(self, eval_results):
    """Log the evaluation result for a estimator.

    The evaluate result is a directory that contains metrics defined in
    model_fn. It also contains a entry for global_step which contains the value
    of the global step when evaluation was performed.

    Args:
      eval_results: dict, the result of evaluate() from a estimator.
    """
    if not isinstance(eval_results, dict):
      tf.logging.warning("eval_results should be directory for logging. Got %s",
                         type(eval_results))
      return
    global_step = eval_results[tf.GraphKeys.GLOBAL_STEP]
62
    for key in sorted(eval_results):
63
64
65
      if key != tf.GraphKeys.GLOBAL_STEP:
        self.log_metric(key, eval_results[key], global_step=global_step)

Scott Zhu's avatar
Scott Zhu committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  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.
    """
    if not isinstance(value, numbers.Number):
      tf.logging.warning(
Karmel Allison's avatar
Karmel Allison committed
82
          "Metric value to log should be a number. Got %s", type(value))
Scott Zhu's avatar
Scott Zhu committed
83
      return
84
85
86
87
    if extras:
      extras = [{"name": k, "value": v} for k, v in sorted(extras.items())]
    else:
      extras = []
Scott Zhu's avatar
Scott Zhu committed
88
    with tf.gfile.GFile(
89
        os.path.join(self._logging_dir, METRIC_LOG_FILE_NAME), "a") as f:
Scott Zhu's avatar
Scott Zhu committed
90
91
      metric = {
          "name": name,
Scott Zhu's avatar
Scott Zhu committed
92
          "value": float(value),
Scott Zhu's avatar
Scott Zhu committed
93
94
95
96
97
          "unit": unit,
          "global_step": global_step,
          "timestamp": datetime.datetime.now().strftime(
              _DATE_TIME_FORMAT_PATTERN),
          "extras": extras}
Scott Zhu's avatar
Scott Zhu committed
98
99
100
101
      try:
        json.dump(metric, f)
        f.write("\n")
      except (TypeError, ValueError) as e:
Karmel Allison's avatar
Karmel Allison committed
102
103
        tf.logging.warning("Failed to dump metric to log file: "
                           "name %s, value %s, error %s", name, value, e)
104
105
106
107
108
109
110
111
112

  def log_run_info(self, model_name):
    """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.
    """
113
114
115
116
    run_info = {
        "model_name": model_name,
        "machine_config": {},
        "run_date": datetime.datetime.now().strftime(_DATE_TIME_FORMAT_PATTERN)}
117
118
119
120
121
122
123
    _collect_tensorflow_info(run_info)
    _collect_tensorflow_environment_variables(run_info)
    _collect_cpu_info(run_info)
    _collect_gpu_info(run_info)
    _collect_memory_info(run_info)

    with tf.gfile.GFile(os.path.join(
124
        self._logging_dir, BENCHMARK_RUN_LOG_FILE_NAME), "w") as f:
125
126
127
128
129
130
131
132
133
134
135
136
137
138
      try:
        json.dump(run_info, f)
        f.write("\n")
      except (TypeError, ValueError) as e:
        tf.logging.warning("Failed to dump benchmark run info to log file: %s",
                           e)


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


def _collect_tensorflow_environment_variables(run_info):
139
140
141
  run_info["tensorflow_environment_variables"] = [
      {"name": k, "value": v}
      for k, v in sorted(os.environ.items()) if k.startswith("TF_")]
142
143
144
145
146
147
148
149
150
151


# The following code is mirrored from tensorflow/tools/test/system_info_lib
# which is not exposed for import.
def _collect_cpu_info(run_info):
  """Collect the CPU information for the local environment."""
  cpu_info = {}

  cpu_info["num_cores"] = multiprocessing.cpu_count()

152
153
154
155
  # Note: cpuinfo is not installed in the TensorFlow OSS tree.
  # It is installable via pip.
  import cpuinfo    # pylint: disable=g-import-not-at-top

156
157
158
159
  info = cpuinfo.get_cpu_info()
  cpu_info["cpu_info"] = info["brand"]
  cpu_info["mhz_per_cpu"] = info["hz_advertised_raw"][0] / 1.0e6

160
  run_info["machine_config"]["cpu_info"] = cpu_info
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177


def _collect_gpu_info(run_info):
  """Collect local GPU information by TF device library."""
  gpu_info = {}
  local_device_protos = device_lib.list_local_devices()

  gpu_info["count"] = len([d for d in local_device_protos
                           if d.device_type == "GPU"])
  # The device description usually is a JSON string, which contains the GPU
  # model info, eg:
  # "device: 0, name: Tesla P100-PCIE-16GB, pci bus id: 0000:00:04.0"
  for d in local_device_protos:
    if d.device_type == "GPU":
      gpu_info["model"] = _parse_gpu_model(d.physical_device_desc)
      # Assume all the GPU connected are same model
      break
178
  run_info["machine_config"]["gpu_info"] = gpu_info
179
180
181


def _collect_memory_info(run_info):
182
183
184
  # Note: psutil is not installed in the TensorFlow OSS tree.
  # It is installable via pip.
  import psutil   # pylint: disable=g-import-not-at-top
185
  vmem = psutil.virtual_memory()
186
187
  run_info["machine_config"]["memory_total"] = vmem.total
  run_info["machine_config"]["memory_available"] = vmem.available
188
189
190
191
192
193
194
195
196


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