logger_test.py 11.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
16
17
18
19
20
21
22
23
24
# 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.
# ==============================================================================

"""Tests for benchmark logger."""

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

import json
import os
import tempfile
25
import time
26
import unittest
Scott Zhu's avatar
Scott Zhu committed
27

28
29
import mock
from absl.testing import flagsaver
Karmel Allison's avatar
Karmel Allison committed
30
import tensorflow as tf  # pylint: disable=g-bad-import-order
Scott Zhu's avatar
Scott Zhu committed
31

32
33
34
35
36
37
try:
  from google.cloud import bigquery
except ImportError:
  bigquery = None

from official.utils.flags import core as flags_core
38
from official.utils.logs import logger
Scott Zhu's avatar
Scott Zhu committed
39
40
41
42


class BenchmarkLoggerTest(tf.test.TestCase):

43
44
45
46
47
  @classmethod
  def setUpClass(cls):  # pylint: disable=invalid-name
    super(BenchmarkLoggerTest, cls).setUpClass()
    flags_core.define_benchmark()

Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
48
  def test_get_default_benchmark_logger(self):
49
50
51
    with flagsaver.flagsaver(benchmark_logger_type='foo'):
      self.assertIsInstance(logger.get_benchmark_logger(),
                            logger.BaseBenchmarkLogger)
Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
52
53

  def test_config_base_benchmark_logger(self):
54
55
56
57
    with flagsaver.flagsaver(benchmark_logger_type='BaseBenchmarkLogger'):
      logger.config_benchmark_logger()
      self.assertIsInstance(logger.get_benchmark_logger(),
                            logger.BaseBenchmarkLogger)
Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
58
59

  def test_config_benchmark_file_logger(self):
60
61
62
63
64
65
66
67
68
69
70
71
72
73
    # Set the benchmark_log_dir first since the benchmark_logger_type will need
    # the value to be set when it does the validation.
    with flagsaver.flagsaver(benchmark_log_dir='/tmp'):
      with flagsaver.flagsaver(benchmark_logger_type='BenchmarkFileLogger'):
        logger.config_benchmark_logger()
        self.assertIsInstance(logger.get_benchmark_logger(),
                              logger.BenchmarkFileLogger)

  @unittest.skipIf(bigquery is None, 'Bigquery dependency is not installed.')
  def test_config_benchmark_bigquery_logger(self):
    with flagsaver.flagsaver(benchmark_logger_type='BenchmarkBigQueryLogger'):
      logger.config_benchmark_logger()
      self.assertIsInstance(logger.get_benchmark_logger(),
                            logger.BenchmarkBigQueryLogger)
Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102


class BaseBenchmarkLoggerTest(tf.test.TestCase):

  def setUp(self):
    super(BaseBenchmarkLoggerTest, self).setUp()
    self._actual_log = tf.logging.info
    self.logged_message = None

    def mock_log(*args, **kwargs):
      self.logged_message = args
      self._actual_log(*args, **kwargs)

    tf.logging.info = mock_log

  def tearDown(self):
    super(BaseBenchmarkLoggerTest, self).tearDown()
    tf.logging.info = self._actual_log

  def test_log_metric(self):
    log = logger.BaseBenchmarkLogger()
    log.log_metric("accuracy", 0.999, global_step=1e4, extras={"name": "value"})

    expected_log_prefix = "Benchmark metric:"
    self.assertRegexpMatches(str(self.logged_message), expected_log_prefix)


class BenchmarkFileLoggerTest(tf.test.TestCase):

103
  def setUp(self):
Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
104
    super(BenchmarkFileLoggerTest, self).setUp()
105
106
107
108
109
110
    # Avoid pulling extra env vars from test environment which affects the test
    # result, eg. Kokoro test has a TF_PKG env which affect the test case
    # test_collect_tensorflow_environment_variables()
    self.original_environ = dict(os.environ)
    os.environ.clear()

Scott Zhu's avatar
Scott Zhu committed
111
  def tearDown(self):
Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
112
    super(BenchmarkFileLoggerTest, self).tearDown()
Scott Zhu's avatar
Scott Zhu committed
113
    tf.gfile.DeleteRecursively(self.get_temp_dir())
114
115
    os.environ.clear()
    os.environ.update(self.original_environ)
Scott Zhu's avatar
Scott Zhu committed
116
117
118
119
120

  def test_create_logging_dir(self):
    non_exist_temp_dir = os.path.join(self.get_temp_dir(), "unknown_dir")
    self.assertFalse(tf.gfile.IsDirectory(non_exist_temp_dir))

Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
121
    logger.BenchmarkFileLogger(non_exist_temp_dir)
Scott Zhu's avatar
Scott Zhu committed
122
123
124
125
    self.assertTrue(tf.gfile.IsDirectory(non_exist_temp_dir))

  def test_log_metric(self):
    log_dir = tempfile.mkdtemp(dir=self.get_temp_dir())
Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
126
    log = logger.BenchmarkFileLogger(log_dir)
Scott Zhu's avatar
Scott Zhu committed
127
128
129
130
131
132
133
134
135
136
    log.log_metric("accuracy", 0.999, global_step=1e4, extras={"name": "value"})

    metric_log = os.path.join(log_dir, "metric.log")
    self.assertTrue(tf.gfile.Exists(metric_log))
    with tf.gfile.GFile(metric_log) as f:
      metric = json.loads(f.readline())
      self.assertEqual(metric["name"], "accuracy")
      self.assertEqual(metric["value"], 0.999)
      self.assertEqual(metric["unit"], None)
      self.assertEqual(metric["global_step"], 1e4)
137
      self.assertEqual(metric["extras"], [{"name": "name", "value": "value"}])
Scott Zhu's avatar
Scott Zhu committed
138
139
140

  def test_log_multiple_metrics(self):
    log_dir = tempfile.mkdtemp(dir=self.get_temp_dir())
Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
141
    log = logger.BenchmarkFileLogger(log_dir)
Scott Zhu's avatar
Scott Zhu committed
142
143
144
145
146
147
148
149
150
151
152
    log.log_metric("accuracy", 0.999, global_step=1e4, extras={"name": "value"})
    log.log_metric("loss", 0.02, global_step=1e4)

    metric_log = os.path.join(log_dir, "metric.log")
    self.assertTrue(tf.gfile.Exists(metric_log))
    with tf.gfile.GFile(metric_log) as f:
      accuracy = json.loads(f.readline())
      self.assertEqual(accuracy["name"], "accuracy")
      self.assertEqual(accuracy["value"], 0.999)
      self.assertEqual(accuracy["unit"], None)
      self.assertEqual(accuracy["global_step"], 1e4)
153
      self.assertEqual(accuracy["extras"], [{"name": "name", "value": "value"}])
Scott Zhu's avatar
Scott Zhu committed
154
155
156
157
158
159

      loss = json.loads(f.readline())
      self.assertEqual(loss["name"], "loss")
      self.assertEqual(loss["value"], 0.02)
      self.assertEqual(loss["unit"], None)
      self.assertEqual(loss["global_step"], 1e4)
160
      self.assertEqual(loss["extras"], [])
Scott Zhu's avatar
Scott Zhu committed
161

Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
162
  def test_log_non_number_value(self):
Scott Zhu's avatar
Scott Zhu committed
163
    log_dir = tempfile.mkdtemp(dir=self.get_temp_dir())
Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
164
    log = logger.BenchmarkFileLogger(log_dir)
Scott Zhu's avatar
Scott Zhu committed
165
166
167
168
169
170
    const = tf.constant(1)
    log.log_metric("accuracy", const)

    metric_log = os.path.join(log_dir, "metric.log")
    self.assertFalse(tf.gfile.Exists(metric_log))

171
  def test_log_evaluation_result(self):
172
173
174
    eval_result = {"loss": 0.46237424,
                   "global_step": 207082,
                   "accuracy": 0.9285}
175
    log_dir = tempfile.mkdtemp(dir=self.get_temp_dir())
Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
176
177
    log = logger.BenchmarkFileLogger(log_dir)
    log.log_evaluation_result(eval_result)
178
179
180
181
182
183
184
185
186
187

    metric_log = os.path.join(log_dir, "metric.log")
    self.assertTrue(tf.gfile.Exists(metric_log))
    with tf.gfile.GFile(metric_log) as f:
      accuracy = json.loads(f.readline())
      self.assertEqual(accuracy["name"], "accuracy")
      self.assertEqual(accuracy["value"], 0.9285)
      self.assertEqual(accuracy["unit"], None)
      self.assertEqual(accuracy["global_step"], 207082)

188
189
190
191
192
193
      loss = json.loads(f.readline())
      self.assertEqual(loss["name"], "loss")
      self.assertEqual(loss["value"], 0.46237424)
      self.assertEqual(loss["unit"], None)
      self.assertEqual(loss["global_step"], 207082)

194
195
196
  def test_log_evaluation_result_with_invalid_type(self):
    eval_result = "{'loss': 0.46237424, 'global_step': 207082}"
    log_dir = tempfile.mkdtemp(dir=self.get_temp_dir())
Qianli Scott Zhu's avatar
Qianli Scott Zhu committed
197
198
    log = logger.BenchmarkFileLogger(log_dir)
    log.log_evaluation_result(eval_result)
199
200
201
202

    metric_log = os.path.join(log_dir, "metric.log")
    self.assertFalse(tf.gfile.Exists(metric_log))

203
204
205
206
207
208
209
  def test_collect_tensorflow_info(self):
    run_info = {}
    logger._collect_tensorflow_info(run_info)
    self.assertNotEqual(run_info["tensorflow_version"], {})
    self.assertEqual(run_info["tensorflow_version"]["version"], tf.VERSION)
    self.assertEqual(run_info["tensorflow_version"]["git_hash"], tf.GIT_VERSION)

210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
  def test_collect_run_params(self):
    run_info = {}
    run_parameters = {
        "batch_size": 32,
        "synthetic_data": True,
        "train_epochs": 100.00,
        "dtype": "fp16",
        "resnet_size": 50,
        "random_tensor": tf.constant(2.0)
    }
    logger._collect_run_params(run_info, run_parameters)
    self.assertEqual(len(run_info["run_parameters"]), 6)
    self.assertEqual(run_info["run_parameters"][0],
                     {"name": "batch_size", "long_value": 32})
    self.assertEqual(run_info["run_parameters"][1],
                     {"name": "dtype", "string_value": "fp16"})
    self.assertEqual(run_info["run_parameters"][2],
                     {"name": "random_tensor", "string_value":
                          "Tensor(\"Const:0\", shape=(), dtype=float32)"})
    self.assertEqual(run_info["run_parameters"][3],
                     {"name": "resnet_size", "long_value": 50})
    self.assertEqual(run_info["run_parameters"][4],
                     {"name": "synthetic_data", "bool_value": "True"})
    self.assertEqual(run_info["run_parameters"][5],
                     {"name": "train_epochs", "float_value": 100.00})

236
237
  def test_collect_tensorflow_environment_variables(self):
    os.environ["TF_ENABLE_WINOGRAD_NONFUSED"] = "1"
238
239
    os.environ["TF_OTHER"] = "2"
    os.environ["OTHER"] = "3"
240
241
242
243

    run_info = {}
    logger._collect_tensorflow_environment_variables(run_info)
    self.assertIsNotNone(run_info["tensorflow_environment_variables"])
244
245
246
247
248
249
    expected_tf_envs = [
        {"name": "TF_ENABLE_WINOGRAD_NONFUSED", "value": "1"},
        {"name": "TF_OTHER", "value": "2"},
    ]
    self.assertEqual(run_info["tensorflow_environment_variables"],
                     expected_tf_envs)
250
251
252

  @unittest.skipUnless(tf.test.is_built_with_cuda(), "requires GPU")
  def test_collect_gpu_info(self):
253
    run_info = {"machine_config": {}}
254
    logger._collect_gpu_info(run_info)
255
    self.assertNotEqual(run_info["machine_config"]["gpu_info"], {})
256
257

  def test_collect_memory_info(self):
258
    run_info = {"machine_config": {}}
259
    logger._collect_memory_info(run_info)
260
261
    self.assertIsNotNone(run_info["machine_config"]["memory_total"])
    self.assertIsNotNone(run_info["machine_config"]["memory_available"])
262

263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303

@unittest.skipIf(bigquery is None, 'Bigquery dependency is not installed.')
class BenchmarkBigQueryLoggerTest(tf.test.TestCase):

  def setUp(self):
    super(BenchmarkBigQueryLoggerTest, self).setUp()
    # Avoid pulling extra env vars from test environment which affects the test
    # result, eg. Kokoro test has a TF_PKG env which affect the test case
    # test_collect_tensorflow_environment_variables()
    self.original_environ = dict(os.environ)
    os.environ.clear()

    self.mock_bq_uploader = mock.MagicMock()
    self.logger = logger.BenchmarkBigQueryLogger(
        self.mock_bq_uploader, "dataset", "run_table", "metric_table",
        "run_id")

  def tearDown(self):
    super(BenchmarkBigQueryLoggerTest, self).tearDown()
    tf.gfile.DeleteRecursively(self.get_temp_dir())
    os.environ.clear()
    os.environ.update(self.original_environ)

  def test_log_metric(self):
    self.logger.log_metric(
        "accuracy", 0.999, global_step=1e4, extras={"name": "value"})
    expected_metric_json = [{
        "name": "accuracy",
        "value": 0.999,
        "unit": None,
        "global_step": 1e4,
        "timestamp": mock.ANY,
        "extras": [{"name": "name", "value": "value"}]
    }]
    # log_metric will call upload_benchmark_metric_json in a separate thread.
    # Give it some grace period for the new thread before assert.
    time.sleep(1)
    self.mock_bq_uploader.upload_benchmark_metric_json.assert_called_once_with(
        "dataset", "metric_table", "run_id", expected_metric_json)


Scott Zhu's avatar
Scott Zhu committed
304
305
if __name__ == "__main__":
  tf.test.main()