Skip to content

Commit 49c3f18

Browse files
murste01facebook-github-bot
authored andcommitted
Add --output-iter-metrics flag to cpu userbenchmark scripts (#2600)
Summary: Adds a new `--output-iter-metrics` flag which adds per-iteration metrics to benchmark result JSON files. This allows us to do our own statistical analysis and comparison of latency/throughput. Pull Request resolved: #2600 Reviewed By: xuzhao9 Differential Revision: D71902373 Pulled By: FindHao fbshipit-source-id: 8216ff91f03e220ff6b7631c038d369206736935
1 parent a2b6092 commit 49c3f18

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

userbenchmark/cpu/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ All parameters of `cpu` userbenchmark as below,
3232
- `--niter` benchmark iteration number. Default value is 30.
3333
- `--output, -o` output dir. By default will create folder under
3434
`.userbenchmark/cpu`.
35+
- `--output-iter-metrics` include per-iteration metrics in output.
3536
- `--timeout` limit single model test run time. Default `None` means no
3637
limitation.
3738
- `--launcher` whether to use `torch.backends.xeon.run_cpu` to get the peak

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: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,12 @@
3838
BM_NAME = "cpu"
3939
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
4040

41+
# output_iter_metrics is True only when '--output-iter-metrics' is given,
42+
# otherwise it is False by default.
4143
def result_to_output_metrics(
42-
metrics: List[str], metrics_res: TorchBenchModelMetrics
44+
metrics: List[str],
45+
metrics_res: TorchBenchModelMetrics,
46+
output_iter_metrics: bool,
4347
) -> Dict[str, float]:
4448
result_metrics = {}
4549
if metrics_res:
@@ -48,11 +52,19 @@ def result_to_output_metrics(
4852
median_latency = numpy.median(metrics_res.latencies)
4953
assert median_latency, f"Run failed for metric {latency_metric}"
5054
result_metrics[latency_metric] = median_latency
55+
if output_iter_metrics:
56+
iter_latencies_metric = "iter_latencies"
57+
result_metrics[iter_latencies_metric] = list(metrics_res.latencies)
5158
if "throughputs" in metrics and metrics_res.throughputs:
5259
throughput_metric = "throughput"
5360
median_throughput = numpy.median(metrics_res.throughputs)
5461
assert median_throughput, f"Run failed for metric {throughput_metric}"
5562
result_metrics[throughput_metric] = median_throughput
63+
if output_iter_metrics:
64+
iter_throughputs_metric = "iter_throughputs"
65+
result_metrics[iter_throughputs_metric] = list(
66+
metrics_res.throughputs
67+
)
5668
if "cpu_peak_mem" in metrics and metrics_res.cpu_peak_mem:
5769
cpu_peak_mem = "cpu_peak_mem"
5870
result_metrics[cpu_peak_mem] = metrics_res.cpu_peak_mem
@@ -118,7 +130,9 @@ def run(args: List[str], extra_args: List[str]):
118130
args.output = args.output if args.output else get_output_dir(BM_NAME)
119131
target_dir = Path(args.output).joinpath(f"{config.name}-{config.test}")
120132
target_dir.mkdir(exist_ok=True, parents=True)
121-
metrics_dict = result_to_output_metrics(metrics, metrics_res)
133+
metrics_dict = result_to_output_metrics(
134+
metrics, metrics_res, args.output_iter_metrics
135+
)
122136
dump_result_to_json(metrics_dict, target_dir)
123137

124138
if __name__ == "__main__":
@@ -143,6 +157,12 @@ def run(args: List[str], extra_args: List[str]):
143157
parser.add_argument(
144158
"--metrics", default="latencies", help="Benchmark metrics, split by comma."
145159
)
160+
parser.add_argument(
161+
"--output-iter-metrics",
162+
action=argparse.BooleanOptionalAction,
163+
default=False,
164+
help="Enable per-iteration benchmark metrics",
165+
)
146166
parser.add_argument(
147167
"--nwarmup", default=20, help="Benchmark warmup iteration number."
148168
)

0 commit comments

Comments
 (0)