Skip to content

Commit 88314e6

Browse files
committed
Add --output-iter-metrics flag to cpu userbenchmark scripts
Adds a new `--output-iter-metrics` flag which adds per-iteration metrics to benchmark result JSON files.
1 parent 50e2f74 commit 88314e6

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

userbenchmark/cpu/cpu_utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,22 +119,34 @@ def add_test_results(runs, result_metrics):
119119
ins_number = len(run["results"])
120120
assert ins_number
121121
latency_metric = "latency" in run["results"][0]["metrics"]
122+
iter_latencies_metric = "iter_latencies" in run["results"][0]["metrics"]
122123
throughput_metric = "throughput" in run["results"][0]["metrics"]
124+
iter_throughputs_metric = "iter_throughputs" in run["results"][0]["metrics"]
123125
cmem_metric = "cpu_peak_mem" in run["results"][0]["metrics"]
124126
latency_sum = 0
127+
iter_latencies = []
125128
throughput_sum = 0
129+
iter_throughputs = []
126130
cmem_sum = 0
127131
for ins_res in run["results"]:
128132
if latency_metric:
129133
latency_sum += ins_res["metrics"]["latency"]
134+
if iter_latencies_metric:
135+
iter_latencies += ins_res["metrics"]["iter_latencies"]
130136
if throughput_metric:
131137
throughput_sum += ins_res["metrics"]["throughput"]
138+
if iter_throughputs_metric:
139+
iter_throughputs += ins_res["metrics"]["iter_throughputs"]
132140
if cmem_metric:
133141
cmem_sum += ins_res["metrics"]["cpu_peak_mem"]
134142
if latency_metric:
135143
result_metrics[f"{run_base_name}_latency"] = latency_sum / ins_number
144+
if iter_latencies_metric:
145+
result_metrics[f"{run_base_name}_iter_latencies"] = iter_latencies
136146
if throughput_metric:
137147
result_metrics[f"{run_base_name}_throughput"] = throughput_sum
148+
if iter_throughputs_metric:
149+
result_metrics[f"{run_base_name}_iter_throughputs"] = iter_throughputs
138150
if cmem_metric:
139151
result_metrics[f"{run_base_name}_cmem"] = cmem_sum / ins_number
140152
return result_metrics

userbenchmark/cpu/run_config.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
4040

4141
def result_to_output_metrics(
42-
metrics: List[str], metrics_res: TorchBenchModelMetrics
42+
metrics: List[str], metrics_res: TorchBenchModelMetrics, output_iter_metrics: bool
4343
) -> Dict[str, float]:
4444
result_metrics = {}
4545
if metrics_res:
@@ -48,11 +48,17 @@ def result_to_output_metrics(
4848
median_latency = numpy.median(metrics_res.latencies)
4949
assert median_latency, f"Run failed for metric {latency_metric}"
5050
result_metrics[latency_metric] = median_latency
51+
if output_iter_metrics:
52+
iter_latencies_metric = "iter_latencies"
53+
result_metrics[iter_latencies_metric] = list(metrics_res.latencies)
5154
if "throughputs" in metrics and metrics_res.throughputs:
5255
throughput_metric = "throughput"
5356
median_throughput = numpy.median(metrics_res.throughputs)
5457
assert median_throughput, f"Run failed for metric {throughput_metric}"
5558
result_metrics[throughput_metric] = median_throughput
59+
if output_iter_metrics:
60+
iter_throughputs_metric = "iter_throughputs"
61+
result_metrics[iter_throughputs_metric] = list(metrics_res.throughputs)
5662
if "cpu_peak_mem" in metrics and metrics_res.cpu_peak_mem:
5763
cpu_peak_mem = "cpu_peak_mem"
5864
result_metrics[cpu_peak_mem] = metrics_res.cpu_peak_mem
@@ -118,7 +124,7 @@ def run(args: List[str], extra_args: List[str]):
118124
args.output = args.output if args.output else get_output_dir(BM_NAME)
119125
target_dir = Path(args.output).joinpath(f"{config.name}-{config.test}")
120126
target_dir.mkdir(exist_ok=True, parents=True)
121-
metrics_dict = result_to_output_metrics(metrics, metrics_res)
127+
metrics_dict = result_to_output_metrics(metrics, metrics_res, args.output_iter_metrics)
122128
dump_result_to_json(metrics_dict, target_dir)
123129

124130
if __name__ == "__main__":
@@ -143,6 +149,9 @@ def run(args: List[str], extra_args: List[str]):
143149
parser.add_argument(
144150
"--metrics", default="latencies", help="Benchmark metrics, split by comma."
145151
)
152+
parser.add_argument(
153+
"--output-iter-metrics", action=argparse.BooleanOptionalAction, help="Enable per-iteration benchmark metrics"
154+
)
146155
parser.add_argument(
147156
"--nwarmup", default=20, help="Benchmark warmup iteration number."
148157
)

0 commit comments

Comments
 (0)