|
| 1 | +# Copyright 2018 The TensorFlow Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | + |
| 16 | +"""Library to upload benchmark generated by BenchmarkLogger to remote repo. |
| 17 | +
|
| 18 | +This library require google cloud bigquery lib as dependency, which can be |
| 19 | +installed with: |
| 20 | + > pip install --upgrade google-cloud-bigquery |
| 21 | +""" |
| 22 | + |
| 23 | +from __future__ import absolute_import |
| 24 | +from __future__ import division |
| 25 | +from __future__ import print_function |
| 26 | + |
| 27 | +import json |
| 28 | +import os |
| 29 | +import sys |
| 30 | +import uuid |
| 31 | + |
| 32 | +from google.cloud import bigquery |
| 33 | + |
| 34 | +import tensorflow as tf # pylint: disable=g-bad-import-order |
| 35 | + |
| 36 | +from official.utils.arg_parsers import parsers |
| 37 | +from official.utils.logging import logger |
| 38 | + |
| 39 | + |
| 40 | +class BigQueryUploader(object): |
| 41 | + """Upload the benchmark and metric info to BigQuery.""" |
| 42 | + |
| 43 | + def __init__(self, logging_dir, gcp_project=None, credentials=None): |
| 44 | + """Initialized BigQueryUploader with proper setting. |
| 45 | +
|
| 46 | + Args: |
| 47 | + logging_dir: string, logging directory that contains the benchmark log. |
| 48 | + gcp_project: string, the name of the GCP project that the log will be |
| 49 | + uploaded to. The default project name will be detected from local |
| 50 | + environment if no value is provided. |
| 51 | + credentials: google.auth.credentials. The credential to access the |
| 52 | + BigQuery service. The default service account credential will be |
| 53 | + detected from local environment if no value is provided. Please use |
| 54 | + google.oauth2.service_account.Credentials to load credential from local |
| 55 | + file for the case that the test is run out side of GCP. |
| 56 | + """ |
| 57 | + self._logging_dir = logging_dir |
| 58 | + self._bq_client = bigquery.Client( |
| 59 | + project=gcp_project, credentials=credentials) |
| 60 | + |
| 61 | + def upload_benchmark_run(self, dataset_name, table_name, run_id): |
| 62 | + """Upload benchmark run information to Bigquery. |
| 63 | +
|
| 64 | + Args: |
| 65 | + dataset_name: string, the name of bigquery dataset where the data will be |
| 66 | + uploaded. |
| 67 | + table_name: string, the name of bigquery table under the dataset where |
| 68 | + the data will be uploaded. |
| 69 | + run_id: string, a unique ID that will be attached to the data, usually |
| 70 | + this is a UUID4 format. |
| 71 | + """ |
| 72 | + expected_file = os.path.join( |
| 73 | + self._logging_dir, logger.BENCHMARK_RUN_LOG_FILE_NAME) |
| 74 | + with tf.gfile.GFile(expected_file) as f: |
| 75 | + benchmark_json = json.load(f) |
| 76 | + benchmark_json["model_id"] = run_id |
| 77 | + table_ref = self._bq_client.dataset(dataset_name).table(table_name) |
| 78 | + errors = self._bq_client.insert_rows_json(table_ref, [benchmark_json]) |
| 79 | + if errors: |
| 80 | + tf.logging.error( |
| 81 | + "Failed to upload benchmark info to bigquery: {}".format(errors)) |
| 82 | + |
| 83 | + def upload_metric(self, dataset_name, table_name, run_id): |
| 84 | + """Upload metric information to Bigquery. |
| 85 | +
|
| 86 | + Args: |
| 87 | + dataset_name: string, the name of bigquery dataset where the data will be |
| 88 | + uploaded. |
| 89 | + table_name: string, the name of bigquery table under the dataset where |
| 90 | + the metric data will be uploaded. This is different from the |
| 91 | + benchmark_run table. |
| 92 | + run_id: string, a unique ID that will be attached to the data, usually |
| 93 | + this is a UUID4 format. This should be the same as the benchmark run_id. |
| 94 | + """ |
| 95 | + expected_file = os.path.join( |
| 96 | + self._logging_dir, logger.METRIC_LOG_FILE_NAME) |
| 97 | + with tf.gfile.GFile(expected_file) as f: |
| 98 | + lines = f.readlines() |
| 99 | + metrics = [] |
| 100 | + for line in filter(lambda l: l.strip(), lines): |
| 101 | + metric = json.loads(line) |
| 102 | + metric["run_id"] = run_id |
| 103 | + metrics.append(metric) |
| 104 | + table_ref = self._bq_client.dataset(dataset_name).table(table_name) |
| 105 | + errors = self._bq_client.insert_rows_json(table_ref, metrics) |
| 106 | + if errors: |
| 107 | + tf.logging.error( |
| 108 | + "Failed to upload benchmark info to bigquery: {}".format(errors)) |
| 109 | + |
| 110 | + |
| 111 | +def main(argv): |
| 112 | + parser = parsers.BenchmarkParser() |
| 113 | + flags = parser.parse_args(args=argv[1:]) |
| 114 | + if not flags.benchmark_log_dir: |
| 115 | + print("Usage: benchmark_uploader.py --benchmark_log_dir=/some/dir") |
| 116 | + sys.exit(1) |
| 117 | + |
| 118 | + uploader = BigQueryUploader( |
| 119 | + flags.benchmark_log_dir, |
| 120 | + gcp_project=flags.gcp_project) |
| 121 | + run_id = str(uuid.uuid4()) |
| 122 | + uploader.upload_benchmark_run( |
| 123 | + flags.bigquery_data_set, flags.bigquery_run_table, run_id) |
| 124 | + uploader.upload_metric( |
| 125 | + flags.bigquery_data_set, flags.bigquery_metric_table, run_id) |
| 126 | + |
| 127 | + |
| 128 | +if __name__ == "__main__": |
| 129 | + main(argv=sys.argv) |
0 commit comments