-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_swebench_pro_agent.py
More file actions
348 lines (293 loc) · 11.9 KB
/
Copy pathrun_swebench_pro_agent.py
File metadata and controls
348 lines (293 loc) · 11.9 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
"""Run mini-SWE-agent on SWE-bench Pro instances with Modal sandboxes.
Each instance gets a fresh Modal sandbox loaded with its per-instance Docker
image from jefzda/sweap-images (the repo is already at /app). A single vLLM
server stays up for the full job; all parallel agent threads share it.
Prerequisite: clone the official harness repo next to this project (or point
--scripts-dir at it):
git clone https://github.com/scaleapi/SWE-bench_Pro-os
Usage (single shard):
export MODAL_TOKEN_ID="..."
export MODAL_TOKEN_SECRET="..."
uv run python run_swebench_pro_agent.py \\
--run-config configs/runs/qwen36_27b_swebench_pro_test.yaml
Usage (array job — driven by swebench_pro_run.sh):
uv run python run_swebench_pro_agent.py \\
--run-config configs/runs/qwen36_27b_swebench_pro_test.yaml \\
--shard-rank 2 --num-shards 8
"""
from __future__ import annotations
import argparse
import re
import threading
import urllib.request
from concurrent.futures import ThreadPoolExecutor, as_completed
from pathlib import Path
import wandb
from src.agents.mini_swe import MiniSweAgentAdapter
from src.agents.swe_bench_pro_environment import SWEBenchProModalEnvironment
from src.agents.vllm_server import VllmServer
from src.configs import SWEBenchRunConfig, load_config
from src.tasks.swe_bench_pro import SWEBenchProAdapter
def _parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument("--run-config", required=True, help="Path to SWEBenchRunConfig YAML")
parser.add_argument("--shard-rank", type=int, default=0)
parser.add_argument("--num-shards", type=int, default=1)
parser.add_argument("--resume", action="store_true", default=True)
parser.add_argument("--no-resume", dest="resume", action="store_false")
return parser.parse_args()
def _out_path(output_dir: Path, instance_id: str, run_idx: int) -> Path:
if run_idx == 0:
return output_dir / f"{instance_id}.json"
return output_dir / f"{instance_id}_run{run_idx:02d}.json"
def _extract_agent_metrics(agent: MiniSweAgentAdapter) -> dict:
messages = agent.last_messages or []
result = agent.last_result or {}
prompt_tokens = 0
completion_tokens = 0
n_steps = 0
timestamps: list[float] = []
for msg in messages:
extra = msg.get("extra") or {}
if "actions" not in extra:
continue
n_steps += 1
usage = (extra.get("response") or {}).get("usage") or {}
prompt_tokens += usage.get("prompt_tokens", 0)
completion_tokens += usage.get("completion_tokens", 0)
ts = extra.get("timestamp")
if ts is not None:
timestamps.append(ts)
duration_s = (max(timestamps) - min(timestamps)) if len(timestamps) >= 2 else 0.0
return {
"agent/n_steps": n_steps,
"agent/exit_status": result.get("exit_status", "unknown"),
"agent/prompt_tokens": prompt_tokens,
"agent/completion_tokens": completion_tokens,
"agent/total_tokens": prompt_tokens + completion_tokens,
"agent/tool_calls": len(agent.command_history),
"agent/duration_s": duration_s,
}
_VLLM_METRICS = {
"vllm:num_requests_running": "vllm/requests_running",
"vllm:num_requests_waiting": "vllm/requests_waiting",
"vllm:gpu_cache_usage_perc": "vllm/gpu_cache_usage_perc",
"vllm:prompt_tokens_total": "vllm/prompt_tokens_total",
"vllm:generation_tokens_total": "vllm/generation_tokens_total",
}
def _scrape_vllm_metrics(base_url: str) -> dict[str, float]:
try:
url = f"{base_url.rstrip('/')}/metrics"
with urllib.request.urlopen(url, timeout=5) as resp:
text = resp.read().decode()
except Exception:
return {}
out: dict[str, float] = {}
for line in text.splitlines():
if line.startswith("#"):
continue
m = re.match(r"^([\w:]+)(?:\{[^}]*\})?\s+([\d.e+\-]+)", line)
if m and m.group(1) in _VLLM_METRICS:
try:
out[_VLLM_METRICS[m.group(1)]] = float(m.group(2))
except ValueError:
pass
return out
def _vllm_metrics_poller(base_url: str, stop: threading.Event, interval: int = 30) -> None:
while not stop.wait(interval):
metrics = _scrape_vllm_metrics(base_url)
if metrics:
wandb.log(metrics)
def _run_one(
instance: dict,
run_idx: int,
*,
cfg: SWEBenchRunConfig,
server: VllmServer,
output_dir: Path,
) -> dict:
instance_id = instance["instance_id"]
label = f"{instance_id}[run{run_idx}]"
out_path = _out_path(output_dir, instance_id, run_idx)
env = SWEBenchProModalEnvironment(
instance,
scripts_dir=cfg.swe_bench_pro_scripts_dir,
timeout=cfg.modal_timeout,
app_name=cfg.modal_app_name,
)
agent = MiniSweAgentAdapter(
model_name=server.model_name,
base_url=server.base_url,
api_key="EMPTY",
agent_config=cfg.to_pro_agent_config(),
generation_config=cfg.to_generation_config(),
environment=env,
)
outcome = None
patch = ""
error = None
eval_log = ""
agent_metrics: dict = {}
try:
task = (
f"Repository: {instance.get('repo', '')}\n\n"
f"Issue:\n{instance['problem_statement']}"
)
agent.run_task(task)
agent_metrics = _extract_agent_metrics(agent)
patch = env.get_patch()
outcome, eval_log = env.evaluate(patch=patch)
print(
f"[{label}] outcome={'PASS' if outcome else 'FAIL'} "
f"patch={len(patch)}chars steps={agent_metrics.get('agent/n_steps', '?')} "
f"exit={agent_metrics.get('agent/exit_status', '?')}",
flush=True,
)
except Exception as exc:
error = str(exc)
print(f"[{label}] ERROR: {exc}", flush=True)
finally:
env.close()
if error is None:
agent.save_trajectory(
out_path,
tokenizer_name=cfg.model_id,
metadata={
"instance_id": instance_id,
"run_idx": run_idx,
"repo": instance.get("repo", ""),
"base_commit": instance.get("base_commit", ""),
"outcome": outcome,
"eval_log": eval_log[-5000:] if eval_log else "",
"patch": patch,
"run_config": cfg.model_dump(),
},
)
return {
"instance_id": instance_id,
"run_idx": run_idx,
"outcome": outcome,
"patch_len": len(patch),
**agent_metrics,
**({"error": error} if error else {}),
}
def main() -> None:
args = _parse_args()
cfg = load_config(args.run_config, SWEBenchRunConfig)
assert isinstance(cfg, SWEBenchRunConfig)
output_dir = Path(cfg.output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
from src.configs import TaskConfig
task_cfg = TaskConfig(dataset="ScaleAI/SWE-bench_Pro", adapter="swe_bench_pro")
adapter = SWEBenchProAdapter()
all_instances = adapter.load_dataset(task_cfg)
instances = [inst for i, inst in enumerate(all_instances) if i % args.num_shards == args.shard_rank]
print(f"[swe-bench-pro] shard {args.shard_rank}/{args.num_shards}: {len(instances)} instances", flush=True)
jobs = [
(inst, run_idx)
for inst in instances
for run_idx in range(cfg.n_runs)
]
if args.resume:
n_before = len(jobs)
jobs = [
(inst, run_idx) for inst, run_idx in jobs
if not _out_path(output_dir, inst["instance_id"], run_idx).exists()
]
print(f"[swe-bench-pro] resume: skipped {n_before - len(jobs)}, {len(jobs)} remaining", flush=True)
if cfg.n_instances > 0:
seen: set[str] = set()
limited = []
for inst, run_idx in jobs:
seen.add(inst["instance_id"])
if len(seen) <= cfg.n_instances:
limited.append((inst, run_idx))
jobs = limited
if not jobs:
print("[swe-bench-pro] nothing to do.", flush=True)
return
print(f"[swe-bench-pro] {len(jobs)} jobs, {cfg.n_workers} workers", flush=True)
run_name = f"{Path(args.run_config).stem}_shard{args.shard_rank}of{args.num_shards}"
wandb.init(
project="program-probes",
name=run_name,
config={**cfg.model_dump(), "shard_rank": args.shard_rank, "num_shards": args.num_shards},
resume="allow",
)
vllm_cfg = cfg.to_vllm_server_config()
if vllm_cfg.log_path:
stem = vllm_cfg.log_path.replace(".log", "")
vllm_cfg = vllm_cfg.model_copy(update={"log_path": f"{stem}_shard{args.shard_rank}.log"})
model_cfg = cfg.to_model_config()
n_total = len(jobs)
n_done = n_passed = n_failed = n_errored = 0
with VllmServer(model_cfg, vllm_cfg) as server:
print(f"[swe-bench-pro] vLLM ready at {server.base_url} ({server.model_name})", flush=True)
stop_poller = threading.Event()
poller = threading.Thread(
target=_vllm_metrics_poller,
args=(server.base_url, stop_poller),
daemon=True,
)
poller.start()
try:
with ThreadPoolExecutor(max_workers=cfg.n_workers) as pool:
futures = {
pool.submit(
_run_one,
inst,
run_idx,
cfg=cfg,
server=server,
output_dir=output_dir,
): (inst["instance_id"], run_idx)
for inst, run_idx in jobs
}
for future in as_completed(futures):
instance_id, run_idx = futures[future]
try:
summary = future.result()
except Exception as exc:
print(f"[swe-bench-pro] unhandled exception for {instance_id}[run{run_idx}]: {exc}", flush=True)
summary = {"instance_id": instance_id, "run_idx": run_idx, "outcome": None, "patch_len": 0, "error": str(exc)}
n_done += 1
errored = "error" in summary
passed = (not errored) and bool(summary.get("outcome"))
if errored:
n_errored += 1
elif passed:
n_passed += 1
else:
n_failed += 1
wandb.log({
"instance/outcome": 1 if passed else 0,
"instance/patch_len": summary.get("patch_len", 0),
"instance/errored": int(errored),
"instance/n_steps": summary.get("agent/n_steps", 0),
"instance/prompt_tokens": summary.get("agent/prompt_tokens", 0),
"instance/completion_tokens": summary.get("agent/completion_tokens", 0),
"instance/total_tokens": summary.get("agent/total_tokens", 0),
"instance/tool_calls": summary.get("agent/tool_calls", 0),
"instance/duration_s": summary.get("agent/duration_s", 0),
"instance/exit_status": summary.get("agent/exit_status", "unknown"),
"progress/completed": n_done,
"progress/passed": n_passed,
"progress/failed": n_failed,
"progress/errored": n_errored,
"progress/pass_rate": n_passed / n_done,
"progress/remaining": n_total - n_done,
})
finally:
stop_poller.set()
poller.join(timeout=5)
wandb.summary.update({
"total": n_total,
"passed": n_passed,
"failed": n_failed,
"errored": n_errored,
"pass_rate": n_passed / n_done if n_done else 0.0,
})
wandb.finish()
print(f"\n[swe-bench-pro] done.", flush=True)
if __name__ == "__main__":
main()