-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_eval.py
More file actions
289 lines (245 loc) · 10 KB
/
Copy pathrun_eval.py
File metadata and controls
289 lines (245 loc) · 10 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
"""
run_eval.py -- Run the pipeline against SWE-bench Lite tasks and report results.
Usage:
python run_eval.py # 5 tasks from psf/requests
python run_eval.py --n 10 # 10 tasks from psf/requests
python run_eval.py --repo django/django --n 3 # from a different repo
python run_eval.py --tasks psf__requests-1734,psf__requests-1789
"""
import argparse
import json
import os
import subprocess
import sys
import time
# Windows consoles default to CP1252; MLflow emits emoji — force UTF-8 throughout.
if hasattr(sys.stdout, "reconfigure"):
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
if hasattr(sys.stderr, "reconfigure"):
sys.stderr.reconfigure(encoding="utf-8", errors="replace")
PREDICTIONS_DIR = os.path.join("evals", "predictions")
RESULTS_PATH = os.path.join("evals", "results.json")
# Repos confirmed working — used when --repos flag is passed
WORKING_REPOS = [
"psf/requests", # 3/6 approved, avg 0.68 — best performer
"astropy/astropy", # 4/4 coder success
"mwaskom/seaborn", # 3/3 coder success
"pylint-dev/pylint", # ~2/4 coder success
"pallets/flask", # 1/2 coder success
"django/django", # large pool, mixed success
]
def load_tasks(n: int, repo: str, task_ids: list[str] | None, force: bool = False, repos: list[str] | None = None) -> list[dict]:
from datasets import load_dataset
print("Loading SWE-bench Lite dataset from HuggingFace...")
ds = load_dataset("princeton-nlp/SWE-bench_Lite", split="test")
tasks = list(ds)
if task_ids:
tasks = [t for t in tasks if t["instance_id"] in task_ids]
elif repos:
tasks = [t for t in tasks if t["repo"] in repos]
# Interleave by repo for spread
by_repo: dict[str, list] = {}
for t in tasks:
by_repo.setdefault(t["repo"], []).append(t)
interleaved: list[dict] = []
while any(by_repo.values()):
for r in list(by_repo.keys()):
if by_repo[r]:
interleaved.append(by_repo[r].pop(0))
if not by_repo[r]:
del by_repo[r]
tasks = interleaved
else:
tasks = [t for t in tasks if t["repo"] == repo]
# Skip tasks that already have a prediction (unless --force)
if not force:
existing = {
f.replace(".diff", "")
for f in os.listdir(PREDICTIONS_DIR)
if f.endswith(".diff")
}
tasks = [t for t in tasks if t["instance_id"] not in existing]
return tasks[:n]
def run_task(task: dict) -> dict:
instance_id = task["instance_id"]
repo = task["repo"]
problem_statement = task["problem_statement"]
# Derive a usable issue URL -- repo identity is what matters for cloning
number = instance_id.split("-")[-1]
issue_url = f"https://github.com/{repo}/issues/{number}"
print(f"\n{'='*60}")
print(f"Task : {instance_id}")
print(f"Repo : {repo}")
print(f"{'='*60}")
base_commit = task.get("base_commit", "")
fail_to_pass = task.get("FAIL_TO_PASS", "[]")
cmd = [
sys.executable, "main.py",
"--issue-url", issue_url,
"--issue-body", problem_statement,
"--instance-id", instance_id,
"--eval",
"--fail-to-pass", fail_to_pass if isinstance(fail_to_pass, str) else json.dumps(fail_to_pass),
]
if base_commit:
cmd += ["--base-commit", base_commit]
start = time.time()
proc = subprocess.run(
cmd,
capture_output=True,
text=True,
encoding="utf-8",
errors="replace",
timeout=600,
)
elapsed = round(time.time() - start, 1)
# Echo output so the user can follow along
print(proc.stdout)
if proc.stderr:
print(proc.stderr, file=sys.stderr)
parsed = _parse_stdout(proc.stdout)
parsed["instance_id"] = instance_id
parsed["repo"] = repo
parsed["elapsed_s"] = elapsed
parsed["gold_patch"] = task.get("patch", "")
parsed["fail_to_pass"] = task.get("FAIL_TO_PASS", "[]")
return parsed
def _parse_stdout(output: str | None) -> dict:
result = {"fix_score": 0.0, "error": None, "broken_file": "", "pr_url": ""}
for line in (output or "").splitlines():
if ":" not in line:
continue
key, _, val = line.partition(":")
key = key.strip()
val = val.strip()
if key == "fix_score":
try:
result["fix_score"] = float(val)
except ValueError:
pass
elif key == "error":
result["error"] = None if val == "None" else val
elif key == "broken_file":
result["broken_file"] = val
elif key == "pr_url":
result["pr_url"] = val
return result
def patch_similarity(pred_path: str, gold_patch: str) -> float:
"""Fraction of gold-patched files also touched by prediction (path-prefix agnostic)."""
if not os.path.exists(pred_path) or not gold_patch:
return 0.0
_PREFIX_STRIP = ("src/", "lib/", "source/")
def normalise(path: str) -> str:
for prefix in _PREFIX_STRIP:
if path.startswith(prefix):
return path[len(prefix):]
return path
def files_in_patch(text: str) -> set[str]:
return {
normalise(line[6:]) # strip "--- a/" then normalise
for line in text.splitlines()
if line.startswith("--- a/")
}
with open(pred_path) as f:
pred_text = f.read()
pred_files = files_in_patch(pred_text)
gold_files = files_in_patch(gold_patch)
if not gold_files:
return 0.0
return len(pred_files & gold_files) / len(gold_files)
def save_results(results: list[dict]) -> None:
"""Upsert results into results.json — safe to call after every task."""
os.makedirs("evals", exist_ok=True)
existing = []
if os.path.exists(RESULTS_PATH):
with open(RESULTS_PATH) as f:
existing = json.load(f)
by_id = {r["instance_id"]: r for r in existing}
for r in results:
by_id[r["instance_id"]] = {k: v for k, v in r.items() if k != "gold_patch"}
with open(RESULTS_PATH, "w") as f:
json.dump(list(by_id.values()), f, indent=2)
def print_summary(results: list[dict]) -> None:
print(f"\n{'='*75}")
print(f"{'EVAL SUMMARY':^75}")
print(f"{'='*75}")
header = f"{'Instance':<35} {'Score':>5} {'File%':>5} {'Time':>6} Error"
print(header)
print("-" * 75)
for r in results:
pred_path = os.path.join(PREDICTIONS_DIR, f"{r['instance_id']}.diff")
file_pct = patch_similarity(pred_path, r.get("gold_patch", ""))
error = (r["error"] or "")[:18] if r["error"] else "-"
print(
f"{r['instance_id']:<35} {r['fix_score']:>5.2f} "
f"{file_pct:>4.0%} {r['elapsed_s']:>5}s {error}"
)
scores = [r["fix_score"] for r in results]
approved = sum(1 for r in results if r["fix_score"] >= 0.6 and not r["error"])
avg = sum(scores) / len(scores) if scores else 0.0
print("-" * 75)
print(
f"Tasks: {len(results)} | Approved (score>=0.6): {approved}/{len(results)} "
f"| Avg score: {avg:.3f}"
)
print("=" * 75)
save_results(results)
print(f"\nResults saved -> {RESULTS_PATH}")
def main() -> None:
parser = argparse.ArgumentParser(description="Eval runner for SWE-bench Lite")
parser.add_argument("--n", type=int, default=5, help="Max tasks to run")
parser.add_argument("--repo", type=str, default="psf/requests", help="Filter by single repo")
parser.add_argument("--tasks", type=str, default=None, help="Comma-separated instance IDs")
parser.add_argument("--force", action="store_true", help="Re-run even if prediction already exists")
parser.add_argument("--working", action="store_true", help="Use only confirmed-working repos (WORKING_REPOS list)")
args = parser.parse_args()
os.makedirs(PREDICTIONS_DIR, exist_ok=True)
task_ids = [t.strip() for t in args.tasks.split(",")] if args.tasks else None
repos = WORKING_REPOS if args.working else None
repo = None if (task_ids or repos) else args.repo
tasks = load_tasks(args.n, repo=repo, task_ids=task_ids, force=args.force, repos=repos)
if not tasks:
print("No tasks to run -- all already predicted or no matches found.")
existing = []
if os.path.exists(RESULTS_PATH):
with open(RESULTS_PATH) as f:
existing = json.load(f)
if task_ids:
existing = [r for r in existing if r["instance_id"] in task_ids]
elif repo:
existing = [r for r in existing if r.get("repo") == repo]
if existing:
print_summary(existing)
return
print(f"Running {len(tasks)} task(s)...\n")
results = []
for i, task in enumerate(tasks):
if i > 0:
time.sleep(15) # avoid TPM rate limit between tasks
try:
results.append(run_task(task))
except subprocess.TimeoutExpired:
print(f"TIMEOUT: {task['instance_id']}")
results.append({
"instance_id": task["instance_id"],
"repo": task["repo"],
"fix_score": 0.0,
"error": "timeout (600s)",
"elapsed_s": 600,
"gold_patch": task.get("patch", ""),
})
except Exception as e:
print(f"ERROR: {task['instance_id']} -- {e}")
results.append({
"instance_id": task["instance_id"],
"repo": task["repo"],
"fix_score": 0.0,
"error": str(e),
"elapsed_s": 0,
"gold_patch": task.get("patch", ""),
})
# Save after every task so interrupts don't lose progress
save_results(results)
print_summary(results)
if __name__ == "__main__":
main()