Repository navigation
Expand file tree
/
Copy pathrun_inference.py
More file actions
380 lines (323 loc) · 12.8 KB
/
Copy pathrun_inference.py
File metadata and controls
380 lines (323 loc) · 12.8 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/env python3
"""
Run LLM inference on benchmark questions in parallel and save results.
Usage (run from repo root):
python run_inference.py --config oai_gpt52
python run_inference.py --domain math --config oai_gpt52
python run_inference.py --domain logic --difficulty hard --config or_deepseek
python run_inference.py --difficulty longcot --config anthropic_sonnet --max-questions 5 --dry-run
"""
from __future__ import annotations
import argparse
import json
import os
import sys
import time
from concurrent.futures import FIRST_COMPLETED, ThreadPoolExecutor, wait
from pathlib import Path
from typing import Any, Dict, List, Set
import yaml
# Support running both with the installed `longcot` package (via `uv sync`)
# and directly from the repo root without installation.
try:
from longcot._llm import LLMProvider, call_with_retry, create_provider
except ModuleNotFoundError:
sys.path.insert(0, str(Path(__file__).resolve().parent / "src"))
from _llm import LLMProvider, call_with_retry, create_provider # type: ignore[import-untyped]
REPO_ROOT = Path(__file__).resolve().parent
DATA_DIR = REPO_ROOT / "src" / "data"
CONFIGS_DIR = REPO_ROOT / "src" / "configs"
DOMAINS = ("logic", "cs", "chemistry", "chess", "math")
DIFFICULTIES = ("easy", "medium", "hard")
# Benchmark aliases: "longcot-mini" == easy, "longcot" == medium + hard.
DIFFICULTY_CHOICES = (*DIFFICULTIES, "longcot-mini", "longcot")
# ---------------------------------------------------------------------------
# I/O helpers
# ---------------------------------------------------------------------------
def load_questions(
domain: str | None = None,
difficulty: str | None = None,
max_questions: int | None = None,
) -> List[Dict[str, Any]]:
"""Load questions, optionally filtered by domain and/or difficulty."""
domains = [domain] if domain else list(DOMAINS)
if difficulty == "longcot":
diffs = ["medium", "hard"]
elif difficulty == "longcot-mini":
diffs = ["easy"]
elif difficulty:
diffs = [difficulty]
else:
diffs = list(DIFFICULTIES)
questions: List[Dict[str, Any]] = []
for dom in domains:
for diff in diffs:
path = DATA_DIR / dom / f"{diff}.json"
if not path.exists():
continue
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
for q in data.get("questions", []):
if "prompt" not in q:
continue
question = dict(q)
question["domain"] = dom
question["difficulty"] = diff
questions.append(question)
if not questions:
raise ValueError(f"No questions found for domain={domain!r}, difficulty={difficulty!r}")
if max_questions is not None:
questions = questions[:max_questions]
return questions
def find_config(name: str) -> Path:
"""Resolve a config name to a path in src/configs/."""
candidates = [CONFIGS_DIR / name, CONFIGS_DIR / f"{name}.yaml"]
for p in candidates:
if p.exists():
return p
available = [f.stem for f in CONFIGS_DIR.glob("*.yaml")]
raise FileNotFoundError(
f"Config {name!r} not found in {CONFIGS_DIR}. Available: {', '.join(sorted(available))}"
)
def load_config(path: Path, *, resolve_env: bool = True) -> Dict[str, Any]:
"""Load a YAML config file, optionally resolving ${ENV_VAR} placeholders."""
with open(path, "r", encoding="utf-8") as f:
cfg = yaml.safe_load(f) or {}
return _resolve_env(cfg) if resolve_env else cfg
def _resolve_env(value: Any) -> Any:
if isinstance(value, str):
v = value.strip()
if v.startswith("${") and v.endswith("}"):
env_var = v[2:-1].strip()
resolved = os.environ.get(env_var)
if not resolved:
raise ValueError(f"Environment variable {env_var} is not set")
return resolved
return value
if isinstance(value, dict):
return {k: _resolve_env(v) for k, v in value.items()}
if isinstance(value, list):
return [_resolve_env(v) for v in value]
return value
# ---------------------------------------------------------------------------
# Core parallel runner
# ---------------------------------------------------------------------------
def call_one(
provider: LLMProvider,
question: Dict[str, Any],
*,
max_retries: int,
retry_timeouts: bool,
llm_kwargs: Dict[str, Any],
) -> Dict[str, Any]:
"""Call the provider for a single question and return a result dict."""
qid = question.get("question_id", "unknown")
domain = question.get("domain")
difficulty = question.get("difficulty")
prompt = question["prompt"]
resp, errors, attempts = call_with_retry(
provider,
prompt,
max_retries=max_retries,
retry_timeouts=retry_timeouts,
**llm_kwargs,
)
if resp is None:
return {
"question_id": qid,
"domain": domain,
"difficulty": difficulty,
"successful": False,
"attempts": attempts,
"errors": errors,
}
result = {
"question_id": qid,
"domain": domain,
"difficulty": difficulty,
"successful": True,
"attempts": attempts,
"response_text": resp.content,
"model": resp.model,
"usage": resp.usage,
}
if resp.reasoning:
result["reasoning"] = resp.reasoning
return result
def run_parallel(
provider: LLMProvider,
questions: List[Dict[str, Any]],
*,
num_workers: int = 8,
max_retries: int = 2,
retry_timeouts: bool = False,
llm_kwargs: Dict[str, Any],
output_path: str,
stop_on_codes: tuple[int, ...] = (401, 402, 403),
stop_threshold: int = 3,
) -> str:
"""Run inference in parallel, writing results to JSONL as they complete."""
out = Path(output_path)
out.parent.mkdir(parents=True, exist_ok=True)
total = len(questions)
idx = 0
completed = 0
consecutive_fatal = 0
stop_submitting = False
in_flight: Set[Any] = set()
future_to_q: Dict[Any, Dict[str, Any]] = {}
with ThreadPoolExecutor(max_workers=num_workers) as executor, open(out, "w", encoding="utf-8") as f:
def submit(q: Dict[str, Any]):
fut = executor.submit(
call_one, provider, q,
max_retries=max_retries,
retry_timeouts=retry_timeouts,
llm_kwargs=llm_kwargs,
)
in_flight.add(fut)
future_to_q[fut] = q
return fut
# Seed initial batch
while idx < total and len(in_flight) < num_workers:
submit(questions[idx])
idx += 1
while in_flight:
done_futures, _ = wait(in_flight, timeout=0.5, return_when=FIRST_COMPLETED)
if not done_futures:
continue
for fut in done_futures:
in_flight.remove(fut)
q = future_to_q.pop(fut, {})
result = fut.result()
f.write(json.dumps(result) + "\n")
f.flush()
completed += 1
qid = result.get("question_id", "?")
ok = result.get("successful", False)
status = "ok" if ok else "FAIL"
print(f" [{completed}/{total}] {qid}: {status}")
# Check for fatal status codes
errors = result.get("errors", [])
fatal = any(
e.get("status_code") in stop_on_codes
for e in errors
if isinstance(e.get("status_code"), int)
)
if fatal:
consecutive_fatal += 1
if consecutive_fatal >= stop_threshold:
print(f"Stopping: {consecutive_fatal} consecutive fatal errors (codes {stop_on_codes})")
stop_submitting = True
elif ok:
consecutive_fatal = 0
# Replenish
while not stop_submitting and idx < total and len(in_flight) < num_workers:
submit(questions[idx])
idx += 1
return output_path
# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------
def main():
available_configs = [f.stem for f in sorted(CONFIGS_DIR.glob("*.yaml"))]
parser = argparse.ArgumentParser(
description="Run LLM inference on benchmark questions",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=f"""
Available configs: {', '.join(available_configs)}
Available domains: {', '.join(DOMAINS)}
Examples:
python run_inference.py --config oai_gpt52 # all domains, all difficulties
python run_inference.py --domain math --config oai_gpt52 # one domain, all difficulties
python run_inference.py --difficulty longcot --config or_deepseek # full LongCoT (medium + hard)
python run_inference.py --difficulty longcot-mini --config or_deepseek # LongCoT-Mini (easy)
python run_inference.py --domain logic --difficulty hard --config or_deepseek
""",
)
parser.add_argument("--domain", choices=DOMAINS, help="Question domain (default: all)")
parser.add_argument("--difficulty", choices=DIFFICULTY_CHOICES, help="Difficulty filter (default: all)")
parser.add_argument("--config", required=True, help="Config name (from src/configs/)")
parser.add_argument("--max-questions", type=int, help="Only use the first N questions")
parser.add_argument("--output", help="Output JSONL path (default: responses/<domain>_<difficulty>_<model>_<ts>.jsonl)")
parser.add_argument("--dry-run", action="store_true", help="Show what would be submitted without calling the API")
args = parser.parse_args()
# --- Load config ---
config_path = find_config(args.config)
cfg = load_config(config_path, resolve_env=not args.dry_run)
provider_name: str = cfg["provider"]
model: str = cfg["model"]
api_key = cfg.get("api_key")
timeout = cfg.get("timeout", 900.0)
num_workers = cfg.get("num_workers", 8)
max_retries = cfg.get("max_retries", 2)
retry_timeouts = cfg.get("retry_timeouts", False)
llm_kwargs: Dict[str, Any] = dict(cfg.get("llm_kwargs") or {})
headers = cfg.get("headers")
# --- Load questions ---
questions = load_questions(args.domain, args.difficulty, args.max_questions)
domain_label = args.domain or "all"
diff_label = args.difficulty or "all"
print(f"Loaded {len(questions)} questions ({domain_label}/{diff_label})")
# --- Output path ---
if args.output:
output_path = args.output
else:
model_slug = model.replace("/", "_").replace(":", "_")
ts = time.strftime("%Y%m%d_%H%M%S")
output_path = f"responses/{domain_label}_{diff_label}_{model_slug}_{ts}.jsonl"
# --- Dry run ---
if args.dry_run:
print(f"\nDry run — would submit {len(questions)} questions")
print(f" Config: {config_path.stem}")
print(f" Provider: {provider_name}")
print(f" Model: {model}")
print(f" Workers: {num_workers}")
print(f" Max retries: {max_retries}")
print(f" Output: {output_path}")
print(f" LLM kwargs: {json.dumps(llm_kwargs, indent=2)}")
print(f"\nFirst question ID: {questions[0].get('question_id', '?')}")
print(f"Last question ID: {questions[-1].get('question_id', '?')}")
return
# --- Create provider ---
provider = create_provider(
provider_name,
model=model,
api_key=api_key,
timeout=timeout,
headers=headers,
)
# --- Run ---
print(f"\nConfig: {config_path.stem}")
print(f"Provider: {provider_name}")
print(f"Model: {model}")
print(f"Workers: {num_workers}")
print(f"Max retries: {max_retries}")
print(f"Output: {output_path}")
print()
start = time.time()
run_parallel(
provider,
questions,
num_workers=num_workers,
max_retries=max_retries,
retry_timeouts=retry_timeouts,
llm_kwargs=llm_kwargs,
output_path=output_path,
)
elapsed = time.time() - start
# --- Summary ---
results = []
with open(output_path, "r", encoding="utf-8") as f:
for line in f:
if line.strip():
results.append(json.loads(line))
total = len(results)
succeeded = sum(1 for r in results if r.get("successful"))
failed = total - succeeded
print(f"\nDone in {elapsed:.1f}s")
print(f" Total: {total}")
print(f" Succeeded: {succeeded}")
print(f" Failed: {failed}")
print(f" Output: {output_path}")
if __name__ == "__main__":
main()