Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add benchmark logger that does stream upload to bigquery. (#4210)
* Move the benchmark_uploader to new location. * Update benchmark logger to streaming upload. * Fix lint and unit test error. * delint. * Update the benchmark uploader test. Skip the import of benchmark_uploader when bigquery is not installed. * Merge the 2 classes of benchmark uploader into 1. * Address review comments. * delint. * Execute bigquery upload in a separate thread. * Change to use python six.moves for importing. * Address review comments and delint. * Address review comment. Adding comment for potential performance impact for model on CPU. * Fix random failure on py3. * Fix the order of flag saver to avoid the randomness. The test is broken when the benchmark_logger_type is set first, and validated when the benchmark_log_dir is not set yet.
- Loading branch information
Showing
10 changed files
with
450 additions
and
116 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# 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. | ||
# ============================================================================== | ||
|
||
"""Binary to upload benchmark generated by BenchmarkLogger to remote repo. | ||
This library require google cloud bigquery lib as dependency, which can be | ||
installed with: | ||
> pip install --upgrade google-cloud-bigquery | ||
""" | ||
|
||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
import os | ||
import sys | ||
import uuid | ||
|
||
from absl import app as absl_app | ||
from absl import flags | ||
|
||
from official.benchmark import benchmark_uploader | ||
from official.utils.flags import core as flags_core | ||
from official.utils.logs import logger | ||
|
||
def main(_): | ||
if not flags.FLAGS.benchmark_log_dir: | ||
print("Usage: benchmark_uploader.py --benchmark_log_dir=/some/dir") | ||
sys.exit(1) | ||
|
||
uploader = benchmark_uploader.BigQueryUploader( | ||
gcp_project=flags.FLAGS.gcp_project) | ||
run_id = str(uuid.uuid4()) | ||
run_json_file = os.path.join( | ||
flags.FLAGS.benchmark_log_dir, logger.BENCHMARK_RUN_LOG_FILE_NAME) | ||
metric_json_file = os.path.join( | ||
flags.FLAGS.benchmark_log_dir, logger.METRIC_LOG_FILE_NAME) | ||
|
||
uploader.upload_benchmark_run_file( | ||
flags.FLAGS.bigquery_data_set, flags.FLAGS.bigquery_run_table, run_id, | ||
run_json_file) | ||
uploader.upload_metric_file( | ||
flags.FLAGS.bigquery_data_set, flags.FLAGS.bigquery_metric_table, run_id, | ||
metric_json_file) | ||
|
||
|
||
if __name__ == "__main__": | ||
flags_core.define_benchmark() | ||
flags.adopt_module_key_flags(flags_core) | ||
absl_app.run(main=main) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# Copyright 2017 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_uploader.""" | ||
|
||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
import json | ||
import os | ||
import tempfile | ||
import unittest | ||
from mock import MagicMock | ||
from mock import patch | ||
|
||
import tensorflow as tf # pylint: disable=g-bad-import-order | ||
|
||
try: | ||
from google.cloud import bigquery | ||
from official.benchmark import benchmark_uploader | ||
except ImportError: | ||
bigquery = None | ||
benchmark_uploader = None | ||
|
||
|
||
@unittest.skipIf(bigquery is None, 'Bigquery dependency is not installed.') | ||
class BigQueryUploaderTest(tf.test.TestCase): | ||
|
||
@patch.object(bigquery, 'Client') | ||
def setUp(self, mock_bigquery): | ||
self.mock_client = mock_bigquery.return_value | ||
self.mock_dataset = MagicMock(name="dataset") | ||
self.mock_table = MagicMock(name="table") | ||
self.mock_client.dataset.return_value = self.mock_dataset | ||
self.mock_dataset.table.return_value = self.mock_table | ||
self.mock_client.insert_rows_json.return_value = [] | ||
|
||
self.benchmark_uploader = benchmark_uploader.BigQueryUploader() | ||
self.benchmark_uploader._bq_client = self.mock_client | ||
|
||
self.log_dir = tempfile.mkdtemp(dir=self.get_temp_dir()) | ||
with open(os.path.join(self.log_dir, 'metric.log'), 'a') as f: | ||
json.dump({'name': 'accuracy', 'value': 1.0}, f) | ||
f.write("\n") | ||
json.dump({'name': 'loss', 'value': 0.5}, f) | ||
f.write("\n") | ||
with open(os.path.join(self.log_dir, 'run.log'), 'w') as f: | ||
json.dump({'model_name': 'value'}, f) | ||
|
||
def tearDown(self): | ||
tf.gfile.DeleteRecursively(self.get_temp_dir()) | ||
|
||
def test_upload_benchmark_run_json(self): | ||
self.benchmark_uploader.upload_benchmark_run_json( | ||
'dataset', 'table', 'run_id', {'model_name': 'value'}) | ||
|
||
self.mock_client.insert_rows_json.assert_called_once_with( | ||
self.mock_table, [{'model_name': 'value', 'model_id': 'run_id'}]) | ||
|
||
def test_upload_benchmark_metric_json(self): | ||
metric_json_list = [ | ||
{'name': 'accuracy', 'value': 1.0}, | ||
{'name': 'loss', 'value': 0.5} | ||
] | ||
expected_params = [ | ||
{'run_id': 'run_id', 'name': 'accuracy', 'value': 1.0}, | ||
{'run_id': 'run_id', 'name': 'loss', 'value': 0.5} | ||
] | ||
self.benchmark_uploader.upload_benchmark_metric_json( | ||
'dataset', 'table', 'run_id', metric_json_list) | ||
self.mock_client.insert_rows_json.assert_called_once_with( | ||
self.mock_table, expected_params) | ||
|
||
def test_upload_benchmark_run_file(self): | ||
self.benchmark_uploader.upload_benchmark_run_file( | ||
'dataset', 'table', 'run_id', os.path.join(self.log_dir, 'run.log')) | ||
|
||
self.mock_client.insert_rows_json.assert_called_once_with( | ||
self.mock_table, [{'model_name': 'value', 'model_id': 'run_id'}]) | ||
|
||
def test_upload_metric_file(self): | ||
self.benchmark_uploader.upload_metric_file( | ||
'dataset', 'table', 'run_id', | ||
os.path.join(self.log_dir, 'metric.log')) | ||
expected_params = [ | ||
{'run_id': 'run_id', 'name': 'accuracy', 'value': 1.0}, | ||
{'run_id': 'run_id', 'name': 'loss', 'value': 0.5} | ||
] | ||
self.mock_client.insert_rows_json.assert_called_once_with( | ||
self.mock_table, expected_params) | ||
|
||
|
||
if __name__ == '__main__': | ||
tf.test.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.