|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import json |
| 4 | +import re |
| 5 | + |
| 6 | + |
| 7 | +class BenchmarkResult: |
| 8 | + def __init__(self, args, framework, hardware, compile_framework_version): |
| 9 | + self.configuration = { |
| 10 | + "model_name": self.get_model_name(args), |
| 11 | + "subgraph_tag": self.get_subgraph_tag(args), |
| 12 | + "model_path": args.model_path, |
| 13 | + "device": args.device, |
| 14 | + "hardware": hardware, |
| 15 | + "framework": framework, |
| 16 | + "compiler": args.compiler, |
| 17 | + "compile_framework_version": compile_framework_version, |
| 18 | + "warmup": args.warmup, |
| 19 | + "trials": args.trials, |
| 20 | + } |
| 21 | + self.model_info = { |
| 22 | + "num_ops": -1, |
| 23 | + "input_dtypes": None, |
| 24 | + "param_dtypes": None, |
| 25 | + } |
| 26 | + self.correctness = {} |
| 27 | + self.performance = { |
| 28 | + "eager": None, |
| 29 | + "compiled": None, |
| 30 | + "speedup": {}, |
| 31 | + } |
| 32 | + |
| 33 | + self.device = args.device |
| 34 | + |
| 35 | + self.eager_e2e_time_ms = -1 |
| 36 | + self.compiled_e2e_time_ms = -1 |
| 37 | + self.e2e_speedup = -1 |
| 38 | + |
| 39 | + self.eager_gpu_time_ms = -1 |
| 40 | + self.compiled_gpu_time_ms = -1 |
| 41 | + self.gpu_speedup = -1 |
| 42 | + |
| 43 | + def get_model_name(self, args): |
| 44 | + model_name = None |
| 45 | + with open(os.path.join(args.model_path, "graph_net.json"), "r") as f: |
| 46 | + data = json.load(f) |
| 47 | + model_name = data.get("model_name", None) |
| 48 | + |
| 49 | + if model_name is not None: |
| 50 | + fields = args.model_path.split(os.sep) |
| 51 | + pattern = rf"^subgraph(_\d+)?$" |
| 52 | + model_name = fields[-2] if re.match(pattern, fields[-1]) else fields[-1] |
| 53 | + return model_name |
| 54 | + |
| 55 | + def get_subgraph_tag(self, args): |
| 56 | + fields = args.model_path.split(os.sep) |
| 57 | + pattern = rf"^subgraph(_\d+)?$" |
| 58 | + return fields[-1] if re.match(pattern, fields[-1]) else "" |
| 59 | + |
| 60 | + def update_model_info(self, num_ops, input_dtypes, param_dtypes): |
| 61 | + self.model_info["num_ops"] = num_ops |
| 62 | + self.model_info["input_dtypes"] = input_dtypes |
| 63 | + self.model_info["param_dtypes"] = param_dtypes |
| 64 | + |
| 65 | + def update_corrrectness(self, key, cmp_ret): |
| 66 | + self.correctness[key] = cmp_ret |
| 67 | + |
| 68 | + def update_performance(self, eager_stats, compiled_stats): |
| 69 | + self.performance["eager"] = eager_stats |
| 70 | + self.performance["compiled"] = compiled_stats |
| 71 | + |
| 72 | + self.eager_e2e_time_ms = eager_stats.get("e2e", {}).get("mean", -1) |
| 73 | + self.compiled_e2e_time_ms = compiled_stats.get("e2e", {}).get("mean", -1) |
| 74 | + if self.eager_e2e_time_ms > 0 and self.compiled_e2e_time_ms > 0: |
| 75 | + self.e2e_speedup = self.eager_e2e_time_ms / self.compiled_e2e_time_ms |
| 76 | + self.performance["speedup"]["e2e"] = float(f"{self.e2e_speedup:.6g}") |
| 77 | + |
| 78 | + if "cuda" in self.device: |
| 79 | + self.eager_gpu_time_ms = eager_stats.get("gpu", {}).get("mean", -1) |
| 80 | + self.compiled_gpu_time_ms = compiled_stats.get("gpu", {}).get("mean", -1) |
| 81 | + if self.eager_gpu_time_ms > 0 and self.compiled_gpu_time_ms > 0: |
| 82 | + self.gpu_speedup = self.eager_gpu_time_ms / self.compiled_gpu_time_ms |
| 83 | + self.performance["speedup"]["gpu"] = float(f"{self.gpu_speedup:.6g}") |
| 84 | + |
| 85 | + def write_to_json(self, output_dir): |
| 86 | + assert output_dir is not None |
| 87 | + os.makedirs(output_dir, exist_ok=True) |
| 88 | + result_data = { |
| 89 | + "configuration": self.configuration, |
| 90 | + "model_info": self.model_info, |
| 91 | + "correctness": self.correctness, |
| 92 | + "performance": self.performance, |
| 93 | + } |
| 94 | + model_name = self.configuration["model_name"] |
| 95 | + subgraph_tag = self.configuration["subgraph_tag"] |
| 96 | + compiler_name = self.configuration["compiler"] |
| 97 | + file_path = os.path.join( |
| 98 | + output_dir, f"{model_name}_{subgraph_tag}_{compiler_name}.json" |
| 99 | + ) |
| 100 | + with open(file_path, "w") as f: |
| 101 | + json.dump(result_data, f, indent=4) |
| 102 | + print(f"Result saved to {file_path}", file=sys.stderr) |
| 103 | + print(result_data) |
0 commit comments