-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_benchmark.py
More file actions
executable file
·221 lines (186 loc) · 7.51 KB
/
Copy pathrun_benchmark.py
File metadata and controls
executable file
·221 lines (186 loc) · 7.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python3
import argparse
import json
import os
import subprocess
import sys
from pathlib import Path
script_dir = Path(__file__).parent.absolute()
tvm_python_dir = script_dir / "tvm" / "python"
if str(tvm_python_dir) not in sys.path:
sys.path.insert(0, str(tvm_python_dir))
def find_baseline_files(kernelbench_root):
baseline_files = []
root = Path(kernelbench_root)
for py_file in root.rglob("*.py"):
if py_file.name == "__init__.py":
continue
if ".ipynb_checkpoints" in str(py_file):
continue
baseline_files.append(py_file)
return sorted(baseline_files)
def _run_single_baseline(baseline_file, config_path, kernelbench_root, res_root, gpu_id):
"""Run process_baseline for one file (used in subprocess)."""
from core.pipeline import process_baseline
return process_baseline(
str(baseline_file), str(config_path), str(kernelbench_root), str(res_root), gpu_id=gpu_id
)
def _run_in_subprocess(baseline_file, gpu_id, log_file=None, timeout=5000):
"""Spawn subprocess for one baseline; stream output in real-time. Returns exit code (-99 on timeout)."""
import selectors
import time as _time
real_stdout = sys.__stdout__
real_stderr = sys.__stderr__
proc = subprocess.Popen(
[
sys.executable, "-u",
str(Path(__file__).resolve()),
"--run-single", str(baseline_file.resolve()),
"--gpu", str(gpu_id),
],
cwd=str(script_dir),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
sel = selectors.DefaultSelector()
sel.register(proc.stdout, selectors.EVENT_READ)
sel.register(proc.stderr, selectors.EVENT_READ)
deadline = _time.monotonic() + timeout
open_streams = 2
while open_streams > 0:
remaining = deadline - _time.monotonic()
if remaining <= 0:
proc.kill()
proc.wait()
sel.close()
return -99
for key, _ in sel.select(timeout=min(remaining, 5.0)):
chunk = key.fileobj.read1(8192) if hasattr(key.fileobj, "read1") else key.fileobj.read(8192)
if not chunk:
sel.unregister(key.fileobj)
open_streams -= 1
continue
text = chunk.decode("utf-8", errors="replace")
target = real_stdout if key.fileobj is proc.stdout else real_stderr
target.write(text)
target.flush()
if log_file:
log_file.write(text)
log_file.flush()
sel.close()
proc.wait()
return proc.returncode
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--gpu", type=int, default=0, help="CUDA device index (default: 0)")
parser.add_argument("--skip-errors", action="store_true", help="Skip failed tasks instead of crashing")
parser.add_argument("--run-single", type=str, metavar="BASELINE_FILE", help="Run one baseline in subprocess (internal)")
args = parser.parse_args()
kernelbench_root = script_dir / "KernelBench"
res_root = script_dir / "res"
config_path = script_dir / "config.yaml"
if args.run_single:
baseline_file = Path(args.run_single)
_run_single_baseline(baseline_file, config_path, kernelbench_root, res_root, args.gpu)
return
os.makedirs(res_root, exist_ok=True)
output_log_path = res_root / "output.log"
class _Tee:
def __init__(self, *streams):
self._streams = streams
def write(self, data: str) -> None:
for s in self._streams:
s.write(data)
s.flush()
def flush(self) -> None:
for s in self._streams:
s.flush()
log_file = open(output_log_path, "a", encoding="utf-8")
_log_file = log_file
sys.stdout = _Tee(sys.stdout, log_file)
sys.stderr = _Tee(sys.stderr, log_file)
if not config_path.exists():
print(f"Error: config.yaml not found at {config_path}")
sys.exit(1)
baseline_files = find_baseline_files(kernelbench_root)
if not baseline_files:
print("No baseline files found in KernelBench/")
sys.exit(1)
print("=" * 70)
print("TVM Compiler Benchmark")
print("=" * 70)
print(f"Found {len(baseline_files)} baseline file(s)")
print(f"GPU: {args.gpu}")
print(f"Config: {config_path}")
print(f"Results directory: {res_root}")
print("=" * 70)
print()
results = []
error_list = []
start_time = __import__("time").time()
for i, baseline_file in enumerate(baseline_files, 1):
model_path_rel = baseline_file.relative_to(kernelbench_root)
task_name = str(model_path_rel).replace(".py", "")
metrics_path = res_root / task_name / "metrics.json"
print("=" * 70)
print(f"[{i}/{len(baseline_files)}] Processing: {model_path_rel}")
print("=" * 70)
if metrics_path.exists():
with open(metrics_path) as _f:
metrics = json.load(_f)
results.append(metrics)
print(" [SKIP] Already processed, metrics.json exists")
print()
continue
returncode = _run_in_subprocess(baseline_file, args.gpu, log_file=_log_file)
if returncode != 0:
err_msg = f"exit code {returncode}" if returncode != -99 else "timeout (600s)"
error_list.append({"task": str(model_path_rel), "error": err_msg})
print(f" [ERROR] Subprocess failed: {err_msg}")
print()
continue
if metrics_path.exists():
with open(metrics_path) as _f:
metrics = json.load(_f)
results.append(metrics)
print()
print(" Summary:")
print(f" Model: {metrics.get('name', 'unknown')}")
if "latency" in metrics:
if "after_tir" in metrics["latency"]:
at = metrics["latency"]["after_tir"]
if at.get("skipped"):
print(f" Latency (after TIR): skipped ({at.get('reason', 'llm_failed')})")
else:
print(f" Latency (after TIR): {at['mean_ms']:.3f} ms")
elif "after_relax" in metrics["latency"]:
print(f" Latency (after Relax): {metrics['latency']['after_relax']['mean_ms']:.3f} ms")
if "correctness" in metrics:
status = "PASS" if metrics["correctness"]["is_correct"] else "FAIL"
print(f" Correctness: {status}")
print(f" Results saved to: {res_root / task_name}")
print()
elapsed_time = __import__('time').time() - start_time
print("=" * 70)
print("Benchmark Complete")
print("=" * 70)
print(f"Processed: {len(results)}/{len(baseline_files)} baselines successfully")
if error_list:
print(f"Errors: {len(error_list)}")
for err in error_list:
print(f" - {err['task']}: {err['error']}")
print(f"Total time: {elapsed_time:.2f} seconds")
print(f"Average time per model: {elapsed_time/len(baseline_files):.2f} seconds")
print()
summary_file = res_root / "summary.json"
with open(summary_file, "w") as f:
json.dump(results, f, indent=2)
print(f"Summary saved to: {summary_file}")
if error_list:
errors_file = res_root / "errors.json"
with open(errors_file, "w") as f:
json.dump(error_list, f, indent=2)
print(f"Errors saved to: {errors_file}")
print("=" * 70)
if __name__ == "__main__":
main()