-
Notifications
You must be signed in to change notification settings - Fork 45.5k
Add benchmark logger that does stream upload to bigquery. #4210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1bb15a8
Move the benchmark_uploader to new location.
qlzh727 be4198c
Update benchmark logger to streaming upload.
qlzh727 5eb3ce7
Fix lint and unit test error.
qlzh727 ce01d0c
delint.
qlzh727 14b4ba2
Update the benchmark uploader test.
qlzh727 4f178ae
Merge the 2 classes of benchmark uploader into 1.
qlzh727 6f96a76
Address review comments.
qlzh727 6e4d7de
delint.
qlzh727 7ec4708
Execute bigquery upload in a separate thread.
qlzh727 2d89263
Change to use python six.moves for importing.
qlzh727 2f2cade
Address review comments and delint.
qlzh727 b5be15a
Address review comment.
qlzh727 d4f4038
Fix random failure on py3.
qlzh727 bc3fb26
Fix the order of flag saver to avoid the randomness.
qlzh727 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The naming of this is confusing-- is it JSON or a python dict? If the latter, why is it called json?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In python, there isn't a specific type of JSON. python uses dict to represent JSON data, and of course it has some restriction about what's the type of the key and value in the dict.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should absolutely not refer to dicts as JSON. Some dicts can be converted to JSON, but I tend to think that json in the variable names implies that the variable refers to the serialized string.