Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit f8c43a2

Browse files
author
DEKHTIARJonathan
committed
[TF-TRT] Remote Upload Implemented
1 parent 0de3370 commit f8c43a2

File tree

3 files changed

+64
-16
lines changed

3 files changed

+64
-16
lines changed

tftrt/benchmarking-python/benchmark_args.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,16 @@ def _validate_args(self, args):
424424
"doesn't exist or is not a directory"
425425
)
426426

427-
if args.upload_metrics_endpoint is not None:
428-
raise NotImplementedError("This feature is not yet implemented.")
427+
# yapf: disable
428+
if (
429+
args.upload_metrics_endpoint is not None and
430+
args.experiment_name is None
431+
):
432+
raise NotImplementedError(
433+
"--experiment_name must be specified if "
434+
"--upload_metrics_endpoint is set."
435+
)
436+
# yapf: enable
429437

430438
def _post_process_args(self, args):
431439
if args.use_synthetic_data:

tftrt/benchmarking-python/benchmark_runner.py

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import json
1010
import logging as _logging
1111
import os
12+
import requests
1213
import sys
1314
import time
1415

@@ -32,6 +33,7 @@
3233
from benchmark_logger import logging
3334

3435
from benchmark_utils import DataAggregator
36+
from benchmark_utils import generate_json_metrics
3537
from benchmark_utils import print_dict
3638
from benchmark_utils import timed_section
3739

@@ -140,19 +142,12 @@ def _export_runtime_metrics_to_json(self, metric_dict):
140142
if file_path is None:
141143
return
142144

143-
metric_dict = {
144-
# Creating a copy to avoid modifying the original
145-
"results": copy.deepcopy(metric_dict),
146-
"runtime_arguments": vars(self._args)
147-
}
145+
json_string = generate_json_metrics(
146+
metrics=metric_dict,
147+
args=vars(self._args),
148+
)
148149

149150
with open(file_path, 'w') as json_f:
150-
json_string = json.dumps(
151-
metric_dict,
152-
default=lambda o: o.__dict__,
153-
sort_keys=True,
154-
indent=4
155-
)
156151
print(json_string, file=json_f)
157152

158153
except Exception as e:
@@ -205,6 +200,36 @@ def _export_runtime_metrics_to_csv(self, metric_dict):
205200
except Exception as e:
206201
logging.error(f"An exception occured during export to CSV: {e}")
207202

203+
def _upload_metrics_to_endpoint(self, metric_dict):
204+
205+
try:
206+
207+
if self._args.upload_metrics_endpoint is None:
208+
return
209+
210+
json_string = generate_json_metrics(
211+
metrics=metric_dict,
212+
args=vars(self._args),
213+
)
214+
215+
headers = {"Content-Type": "application/json"}
216+
217+
response = requests.put(
218+
self._args.upload_metrics_endpoint,
219+
data=json.dumps(data),
220+
headers=headers
221+
)
222+
response.raise_for_status()
223+
224+
logging.info(
225+
"Metrics Uploaded to endpoint: "
226+
f"`{self._args.upload_metrics_endpoint}` with experiment name: "
227+
f"`{self._args.experiment_name}`."
228+
)
229+
230+
except Exception as e:
231+
logging.error(f"An exception occured during export to JSON: {e}")
232+
208233
def _get_graph_func(self):
209234
"""Retreives a frozen SavedModel and applies TF-TRT
210235
use_tftrt: bool, if true use TensorRT
@@ -587,9 +612,12 @@ def start_profiling():
587612
if not self._args.use_synthetic_data:
588613
data_aggregator.aggregate_data(y_pred, y)
589614

590-
if (not self._args.debug_performance and
591-
step_idx % self._args.display_every !=
592-
0): # avoids double printing
615+
# yapf: disable
616+
if (
617+
not self._args.debug_performance and
618+
# avoids double printing
619+
step_idx % self._args.display_every != 0
620+
):
593621
log_step(
594622
step_idx,
595623
display_every=1, # force print
@@ -602,6 +630,7 @@ def start_profiling():
602630
dequeue_times[-self._args.display_every:]
603631
) * 1000
604632
)
633+
# yapf: enable
605634

606635
if step_idx >= 100:
607636
stop_profiling()
@@ -668,6 +697,7 @@ def timing_metrics(time_arr, log_prefix):
668697

669698
self._export_runtime_metrics_to_json(metrics)
670699
self._export_runtime_metrics_to_csv(metrics)
700+
self._upload_metrics_to_endpoint(metrics)
671701

672702
def log_value(key, val):
673703
if isinstance(val, (int, str)):

tftrt/benchmarking-python/benchmark_utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
33
# -*- coding: utf-8 -*-
44

5+
import json
56
import time
67

78
import numpy as np
@@ -114,6 +115,15 @@ def _format(tensor):
114115
return predictions, expected
115116

116117

118+
def generate_json_metrics(metrics, args):
119+
metric_dict = {"results": metrics, "runtime_arguments": args}
120+
121+
json_string = json.dumps(
122+
metric_dict, default=lambda o: o.__dict__, sort_keys=True, indent=4
123+
)
124+
return json_string
125+
126+
117127
class DataAggregator(object):
118128

119129
def __init__(self, postprocess_model_outputs_fn, args):

0 commit comments

Comments
 (0)