-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
154 lines (123 loc) · 4.17 KB
/
Copy pathrun.py
File metadata and controls
154 lines (123 loc) · 4.17 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
import argparse
import json
import os
import subprocess
import sys
from pathlib import Path
import yaml
from tabulate import tabulate
RESULTS_DIR = Path("evalplus_results")
VALID_DATASETS = {"humaneval", "mbpp"}
def load_config(path: Path) -> dict:
with open(path) as f:
config = yaml.safe_load(f)
if not config.get("models"):
raise ValueError("No models specified in config")
datasets = config.get("datasets", ["humaneval"])
if isinstance(datasets, str):
datasets = [datasets]
for ds in datasets:
if ds not in VALID_DATASETS:
raise ValueError(f"Invalid dataset '{ds}', must be one of {VALID_DATASETS}")
return config
def run_evalplus(
model: str,
dataset: str,
base_url: str,
max_tokens: int | None = None,
timeout: int | None = None,
) -> int:
env = os.environ.copy()
env["OPENAI_API_KEY"] = env.get("API_KEY", "none")
if timeout:
env["OPENAI_TIMEOUT"] = str(timeout)
cmd = [
sys.executable,
"-m",
"evalplus.evaluate",
"--model",
model,
"--dataset",
dataset,
"--backend",
"openai",
"--base-url",
base_url,
"--greedy",
"--root",
str(RESULTS_DIR),
]
if max_tokens:
cmd.extend(["--max-tokens", str(max_tokens)])
print(f" $ {' '.join(cmd)}")
result = subprocess.run(cmd, env=env)
return result.returncode
def find_eval_results(dataset: str) -> list[Path]:
dataset_dir = RESULTS_DIR / dataset
if not dataset_dir.exists():
return []
return sorted(dataset_dir.glob("*.eval_results.json"))
def parse_model_name(filename: str) -> str:
name = filename.replace(".eval_results", "")
name = name.replace("_openai_temp_0.0", "")
name = name.replace("--", "/")
return name
def report(dataset: str):
results_files = find_eval_results(dataset)
if not results_files:
print("\nNo results found. Run a benchmark first.")
return
rows = []
for path in results_files:
with open(path) as f:
data = json.load(f)
pass_at_k = data.get("pass_at_k", {})
if not pass_at_k:
continue
base_score = pass_at_k.get("base", {}).get("pass@1", 0)
plus_score = pass_at_k.get("plus", {}).get("pass@1", 0)
total = len(data.get("eval", {}))
model_name = parse_model_name(path.stem)
rows.append(
[
model_name,
total,
f"{base_score * 100:.1f}%",
f"{plus_score * 100:.1f}%",
]
)
rows.sort(key=lambda r: r[3], reverse=True)
headers = ["Model", "Tasks", f"{dataset} pass@1", f"{dataset}+ pass@1"]
print(f"\n{'=' * 60}")
print(f"RESULTS — {dataset}")
print(f"{'=' * 60}")
print(tabulate(rows, headers=headers, tablefmt="grid"))
def main():
parser = argparse.ArgumentParser(description="Benchmark LLMs with EvalPlus")
parser.add_argument("--config", type=Path, default=Path("config.yaml"))
parser.add_argument("--models", nargs="+", help="Override models from config")
parser.add_argument("--dataset", nargs="+", help="Override datasets from config")
parser.add_argument("--report-only", action="store_true")
args = parser.parse_args()
config = load_config(args.config)
datasets = args.dataset or config.get("datasets", ["humaneval"])
if isinstance(datasets, str):
datasets = [datasets]
base_url = config["server"]["base_url"]
bench_cfg = config.get("benchmark", {})
max_tokens = bench_cfg.get("max_tokens")
timeout = bench_cfg.get("timeout")
models = args.models or config["models"]
if not args.report_only:
for dataset in datasets:
for model in models:
print(f"\n{'=' * 60}")
print(f"Benchmarking: {model} on {dataset}")
print(f"{'=' * 60}")
rc = run_evalplus(model, dataset, base_url, max_tokens, timeout)
if rc != 0:
print(f" WARNING: evalplus exited with code {rc}")
for dataset in datasets:
report(dataset)
if __name__ == "__main__":
main()