-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlaunch_benchmark.py
More file actions
executable file
·359 lines (287 loc) · 13.4 KB
/
launch_benchmark.py
File metadata and controls
executable file
·359 lines (287 loc) · 13.4 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env python3
"""
launch_benchmark.py – resilient single-/multi-run launcher for ModelBenchmark.
• **Lists in YAML → multi-run** (Cartesian product)
• **Scalars only → single-run**
Each successful run writes three CSVs under `results_benchmark_<experiment_name>/…`
◦ run_report/<stem>.csv
◦ details/<stem>.csv
◦ readings/ts_<stem>.csv
Crashes are appended to `results_benchmark_<experiment_name>/failed_runs.log` **together with the
full traceback** and will be retried automatically (up to **3 attempts**) the next
time you invoke the launcher. Already‑completed runs are detected by their
hash‑based stem and are silently skipped.
"""
from __future__ import annotations
import argparse
import json
import hashlib
import os
import sys
import traceback
import warnings
from pathlib import Path
from typing import Any, Dict, List, Tuple
from rich.live import Live
from rich.panel import Panel
import yaml
from rich.console import Console
from rich.progress import (
BarColumn,
Progress,
SpinnerColumn,
TimeElapsedColumn,
TimeRemainingColumn,
)
from rich.table import Table
from benchmark.benchmark import ModelBenchmark
from benchmark.utils_multi import load_multi_cfg
# ── Silence noisy logs up‑front ─────────────────────────────────────────────
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
os.environ["TOKENIZERS_PARALLELISM"] = "false"
warnings.filterwarnings("ignore")
console = Console(stderr=True)
# ── Globals filled by _ensure_output_dirs() ─────────────────────────────────
FAILED_LOG: str = ""
BASE_DIR: str = ""
# ── Directory helpers ───────────────────────────────────────────────────────
def _ensure_output_dirs(exp_name: str) -> None:
"""Create required sub‑directories *iff* they do not yet exist.
If the experiment folder already exists we do **not** delete any content –
this enables incremental / resumed benchmarking.
"""
global FAILED_LOG, BASE_DIR
BASE_DIR = f"{exp_name}"
Path(BASE_DIR).mkdir(exist_ok=True)
for sub in ("details", "run_report", "readings"):
Path(BASE_DIR, sub).mkdir(exist_ok=True)
FAILED_LOG = os.path.join(BASE_DIR, "failed_runs.log")
# Ensure the file exists so open(..., "a") later never fails.
Path(FAILED_LOG).touch(exist_ok=True)
# ── Utility: result‑hash / stem / existence check ───────────────────────────
def _run_multi_cfgs(cfgs: List[Dict[str, Any]], verbose: bool = False, dump_server_output: bool = False, script_path: str = None):
prior_failures = _load_prior_failures()
has_prior_failures = len(prior_failures) > 0
# Combine + de-duplicate
all_runs = {json.dumps(c, sort_keys=True): c for c in cfgs + prior_failures}
pending_cfgs = [cfg for cfg in all_runs.values() if not _results_exist(cfg)]
MAX_ATTEMPTS = 3
attempt = 1
unresolved: List[Tuple[Dict[str, Any], str]] = []
while pending_cfgs and attempt <= MAX_ATTEMPTS:
# Only show attempt counter if retrying known failed configs
show_attempts = has_prior_failures or attempt > 1
failures = _multi_run_cycle(pending_cfgs, verbose, attempt, MAX_ATTEMPTS, show_attempts, dump_server_output, script_path)
pending_cfgs = [cfg for cfg in (f[0] for f in failures) if not _results_exist(cfg)]
unresolved = failures
attempt += 1
_rewrite_failed_log(unresolved)
if unresolved:
console.print(
f"[yellow]⚠ {len(unresolved)} runs still failing after {MAX_ATTEMPTS} attempts – see {FAILED_LOG} for details[/]"
)
def _cfg_hash(cfg: Dict[str, Any]) -> str:
return hashlib.md5(json.dumps(cfg, sort_keys=True).encode()).hexdigest()[:8]
def _param_bundle(cfg: Dict[str, Any]) -> str:
sc = cfg["scenario"]
if sc == "batch":
return f"bs{cfg['batch_size']}"
if sc == "server":
return f"cu{cfg['concurrent_users']}_rpm{cfg['requests_per_user_per_min']}"
return ""
def _stem(cfg: Dict[str, Any]) -> str:
parts = [
cfg["backend"],
cfg["model_name"],
cfg["task"],
cfg["scenario"],
]
bundle = _param_bundle(cfg)
if bundle:
parts.append(bundle)
parts.append(_cfg_hash(cfg))
return "_".join(parts)
def _results_exist(cfg: Dict[str, Any]) -> bool:
"""Return *True* if the main run_report CSV is already present."""
return Path(BASE_DIR, "run_report", f"{_stem(cfg)}.csv").is_file()
# ── Failure logging helpers ────────────────────────────────────────────────
def _log_failure(cfg: Dict[str, Any], err_trace: str) -> None:
"""Append the *cfg* and *err_trace* JSON‑encoded to FAILED_LOG."""
with open(FAILED_LOG, "a", encoding="utf-8") as fh:
fh.write(json.dumps({"cfg": cfg, "error": err_trace}, sort_keys=True) + "\n")
def _load_prior_failures() -> List[Dict[str, Any]]:
"""Load JSON records from FAILED_LOG. Each line may be either a raw cfg or
an object with a `cfg` key. We only return the cfg portion.
"""
runs: List[Dict[str, Any]] = []
if not Path(FAILED_LOG).is_file():
return runs
with open(FAILED_LOG, "r", encoding="utf-8") as fh:
for line in fh:
line = line.strip()
if not line:
continue
try:
obj = json.loads(line)
if isinstance(obj, dict) and "cfg" in obj:
runs.append(obj["cfg"])
else:
runs.append(obj)
except json.JSONDecodeError:
continue
return runs
def _rewrite_failed_log(entries: List[Tuple[Dict[str, Any], str]]) -> None:
"""Overwrite FAILED_LOG with *entries* (list of (cfg, error))."""
with open(FAILED_LOG, "w", encoding="utf-8") as fh:
for cfg, err in entries:
fh.write(json.dumps({"cfg": cfg, "error": err}, sort_keys=True) + "\n")
def _render_log(client):
if not hasattr(client, "_log_buf"):
return Panel("[dim]‹log tailer not started yet›", title="engine-logs", border_style="grey37", height=12)
lines = list(client._log_buf)
body = "\n".join(lines[-10:]) or "[dim]‹no output yet›"
return Panel(body, title="engine-logs", border_style="grey37", height=12)
# ── Pretty printing helper for single‑run summaries ─────────────────────────
def _display_vertical(df):
tbl = Table(show_header=False, box=None)
tbl.add_column("Metric", style="bold cyan", no_wrap=True)
tbl.add_column("Value", style="white")
for k, v in df.iloc[0].items():
tbl.add_row(k, f"{v:.6f}" if isinstance(v, float) else str(v))
console.print(tbl)
# ── Core: run one benchmark & store results ─────────────────────────────────
def _save_results(report, details, readings, cfg):
stem = _stem(cfg)
report.to_csv(Path(BASE_DIR, "run_report", f"{stem}.csv"), index=False)
details.to_csv(Path(BASE_DIR, "details", f"{stem}.csv"), index=False)
readings.to_csv(Path(BASE_DIR, "readings", f"ts_{stem}.csv"), index=False)
console.print(f"[green]Saved[/] {BASE_DIR}/run_report/{stem}.csv")
def _run_one(cfg: Dict[str, Any], bm: ModelBenchmark):
report, details, readings = bm.run(
task=cfg["task"],
scenario=cfg["scenario"],
samples=cfg.get("samples"),
batch_size=cfg.get("batch_size"),
run_time=cfg.get("run_time"),
concurrent_users=cfg.get("concurrent_users"),
requests_per_user_per_min=cfg.get("requests_per_user_per_min"),
sample_interval=cfg.get("sample_interval", 0.1),
quality_metric=cfg.get("quality_metric", True),
)
_save_results(report, details, readings, cfg)
return report
# ── Multi‑run orchestration with retry logic ────────────────────────────────
def _multi_run_cycle(
runs: List[Dict[str, Any]],
verbose: bool,
attempt: int,
total_attempts: int,
show_attempts: bool = True,
dump_server_output: bool = False,
script_path: str = None
) -> List[Tuple[Dict[str, Any], str]]:
"""Execute *runs*. Return list of tuples (cfg, error_trace) that failed."""
if not runs:
return []
if show_attempts:
console.rule(f"Attempt {attempt}/{total_attempts} • {len(runs)} pending")
else:
console.rule(f"Running {len(runs)} benchmark(s)…")
progress = Progress(
SpinnerColumn(style="cyan"),
"[progress.percentage]{task.percentage:>3.0f}%",
BarColumn(bar_width=None),
TimeElapsedColumn(),
"• ETA",
TimeRemainingColumn(compact=True),
console=console,
transient=True,
)
task_id = progress.add_task("[cyan]benchmarks", total=len(runs))
failures: List[Tuple[Dict[str, Any], str]] = []
current: ModelBenchmark | None = None
active_key = None # (backend, hf_model)
with progress:
for idx, cfg in enumerate(runs, 1):
hf_model_cfg = cfg["hf_model"]
model_repo = hf_model_cfg["name"]
temperature = hf_model_cfg.get("temperature", 0.6)
top_p = hf_model_cfg.get("top_p", 0.95)
max_tokens = hf_model_cfg.get("max_tokens", 100)
# ensure model_name is a simple string for filenames / logs
cfg["model_name"] = cfg.get("model_name", model_repo)
console.print(
f"Configuration: {cfg['backend']}/{cfg['model_name']} "
f"{cfg['task']}:{cfg['scenario']} {_param_bundle(cfg)}"
)
desc = f"{cfg['backend']}/{cfg['model_name']} {cfg['task']}:{cfg['scenario']} {_param_bundle(cfg)}"
progress.update(task_id, description=desc)
key = (cfg["backend"], model_repo) # use backend + repo id as cache key
if key != active_key:
if current is not None:
current.iec.close()
current.close()
current = ModelBenchmark(
backend=cfg["backend"],
model_name=cfg["model_name"],
model_path=model_repo,
temperature=temperature,
top_p=top_p,
max_tokens=max_tokens,
verbose=verbose,
dump_server_output=dump_server_output,
script_path=script_path
)
active_key = key
if hasattr(current.iec, "_start_log_tailer"):
current.iec._start_log_tailer()
try:
report = _run_one(cfg, current)
console.print(
f"[green]✓[/] {idx}/{len(runs)} {desc} → {report.iloc[0]['total_generation_time_s']:.1f}s"
)
except Exception:
err_trace = traceback.format_exc()
console.print(f"[red]✗[/] {idx}/{len(runs)} {desc}\n{err_trace}")
failures.append((cfg, err_trace))
_log_failure(cfg, err_trace)
# Print the latest logs right below the result
if hasattr(current.iec, "_log_buf"):
console.print(_render_log(current.iec))
progress.advance(task_id)
if current is not None:
current.iec.close()
current.close()
return failures
def _run_multi(yaml_path: str, verbose: bool = False, dump_server_output: bool = False, script_path: str = None):
all_cfgs = load_multi_cfg(yaml_path)
_run_multi_cfgs(all_cfgs, verbose, dump_server_output, script_path)
def _run_single(yaml_path: str, verbose: bool = False, dump_server_output: bool = False, script_path: str = None):
with open(yaml_path, encoding="utf-8") as f:
raw = yaml.safe_load(f)
run_cfg = raw | {"model_name": raw.get("model_name", "")}
_run_multi_cfgs([run_cfg], verbose, dump_server_output, script_path)
def main():
p = argparse.ArgumentParser(description="Run one or more benchmark configs")
p.add_argument("configs", nargs="+", help="One or more YAML config paths")
p.add_argument("-v", "--verbose", action="store_true")
p.add_argument("-d", "--dump-server-output", action="store_true")
p.add_argument("-s", "--script_path", default=None)
args = p.parse_args()
for yaml_path in args.configs:
try:
with open(yaml_path, encoding="utf-8") as f:
raw = yaml.safe_load(f)
exp_name = raw.get("experiment_name", Path(yaml_path).stem)
_ensure_output_dirs(exp_name)
is_multi = any(isinstance(v, list) for v in raw.values())
if is_multi:
console.print(f"[cyan]Multi-run mode detected[/] → {yaml_path}")
_run_multi(yaml_path, verbose=args.verbose, dump_server_output=args.dump_server_output, script_path=args.script_path)
else:
_run_single(yaml_path, verbose=args.verbose, dump_server_output=args.dump_server_output, script_path=args.script_path)
except Exception:
console.print(f"[red]✗ Failed to load or run config:[/] {yaml_path}")
traceback.print_exc()
if __name__ == "__main__":
main()