|
7 | 7 | import abc
|
8 | 8 | import contextlib
|
9 | 9 | import copy
|
| 10 | +import csv |
10 | 11 | import json
|
11 | 12 | import logging
|
12 | 13 | import sys
|
@@ -125,28 +126,70 @@ def _debug_print(self, msg):
|
125 | 126 |
|
126 | 127 | def _export_runtime_metrics_to_json(self, metric_dict):
|
127 | 128 |
|
128 |
| - metric_dict = { |
129 |
| - # Creating a copy to avoid modifying the original |
130 |
| - "results": copy.deepcopy(metric_dict), |
131 |
| - "runtime_arguments": vars(self._args) |
132 |
| - } |
| 129 | + try: |
133 | 130 |
|
134 |
| - json_path = self._args.export_metrics_json_path |
135 |
| - if json_path is not None: |
136 |
| - try: |
137 |
| - with open(json_path, 'w') as json_f: |
138 |
| - json_string = json.dumps( |
139 |
| - metric_dict, |
140 |
| - default=lambda o: o.__dict__, |
141 |
| - sort_keys=True, |
142 |
| - indent=4 |
143 |
| - ) |
144 |
| - print(json_string, file=json_f) |
145 |
| - except Exception as e: |
146 |
| - print( |
147 |
| - "[ERROR] Impossible to save JSON File at path: " |
148 |
| - f"{json_path}.\nError: {str(e)}" |
| 131 | + file_path = self._args.export_metrics_json_path |
| 132 | + if file_path is None: |
| 133 | + return |
| 134 | + |
| 135 | + metric_dict = { |
| 136 | + # Creating a copy to avoid modifying the original |
| 137 | + "results": copy.deepcopy(metric_dict), |
| 138 | + "runtime_arguments": vars(self._args) |
| 139 | + } |
| 140 | + |
| 141 | + with open(file_path, 'w') as json_f: |
| 142 | + json_string = json.dumps( |
| 143 | + metric_dict, |
| 144 | + default=lambda o: o.__dict__, |
| 145 | + sort_keys=True, |
| 146 | + indent=4 |
149 | 147 | )
|
| 148 | + print(json_string, file=json_f) |
| 149 | + |
| 150 | + except Exception as e: |
| 151 | + print(f"An exception occured during export to JSON: {e}") |
| 152 | + |
| 153 | + def _export_runtime_metrics_to_csv(self, metric_dict): |
| 154 | + |
| 155 | + try: |
| 156 | + |
| 157 | + file_path = self._args.export_metrics_csv_path |
| 158 | + if file_path is None: |
| 159 | + return |
| 160 | + |
| 161 | + data = {f"metric_{k}": v for k, v in metric_dict.items()} |
| 162 | + |
| 163 | + args_to_save = [ |
| 164 | + "batch_size", |
| 165 | + "input_saved_model_dir", |
| 166 | + "minimum_segment_size", |
| 167 | + "no_tf32", |
| 168 | + "precision", |
| 169 | + "use_dynamic_shape", |
| 170 | + "use_synthetic_data", |
| 171 | + "use_tftrt", |
| 172 | + "use_xla", |
| 173 | + "use_xla_auto_jit" |
| 174 | + ] |
| 175 | +- |
| 176 | + runtime_arguments = vars(self._args) |
| 177 | + for key in args_to_save: |
| 178 | + data[f"arg_{key}"] = str(runtime_arguments[key]).split("/")[-1] |
| 179 | + |
| 180 | + fieldnames = sorted(data.keys()) |
| 181 | + |
| 182 | + if not os.path.isfile(file_path): |
| 183 | + with open(file_path, 'w') as outcsv: |
| 184 | + writer = csv.DictWriter(outcsv, fieldnames=fieldnames, delimiter=',') |
| 185 | + writer.writeheader() |
| 186 | + |
| 187 | + with open(file_path, 'a') as outcsv: |
| 188 | + writer = csv.DictWriter(outcsv, fieldnames=fieldnames, delimiter=',') |
| 189 | + writer.writerow(data) |
| 190 | + |
| 191 | + except Exception as e: |
| 192 | + print(f"An exception occured during export to CSV: {e}") |
150 | 193 |
|
151 | 194 | def _get_graph_func(self):
|
152 | 195 | """Retreives a frozen SavedModel and applies TF-TRT
|
@@ -524,6 +567,7 @@ def timing_metrics(time_arr, log_prefix):
|
524 | 567 | metrics.update(timing_metrics(memcopy_times, "Data MemCopyHtoD Time"))
|
525 | 568 |
|
526 | 569 | self._export_runtime_metrics_to_json(metrics)
|
| 570 | + self._export_runtime_metrics_to_csv(metrics) |
527 | 571 |
|
528 | 572 | def log_value(key, val):
|
529 | 573 | if isinstance(val, int):
|
|
0 commit comments