forked from MakazhanAlpamys/Soup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_eval_v0650.py
More file actions
511 lines (461 loc) · 18.8 KB
/
Copy path_eval_v0650.py
File metadata and controls
511 lines (461 loc) · 18.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
"""v0.65.0 — `soup eval behavior` / `capability` / `checklist` / `irt-subset`.
Subcommand bundle attached to the existing ``soup eval`` Typer app via
:func:`register`. Mirrors the v0.55.0 / v0.61.0 registration pattern so
``commands/eval.py`` stays under length cap.
"""
from __future__ import annotations
import json
import os
import stat
from typing import Optional
import typer
from rich.console import Console
from rich.markup import escape
from rich.panel import Panel
from rich.table import Table
# v0.56.0 evidence-loader policy (review M6 fix).
_MAX_EVIDENCE_BYTES = 16 * 1024 * 1024 # 16 MiB
_MAX_RUN_ID_LEN = 256
def _validate_run_id(run_id: object) -> str:
"""Validate a CLI-passed run_id (review M5 fix).
Run IDs are echoed into output JSON payloads and used as report keys,
so reject null bytes / control chars / oversize before they propagate.
"""
if not isinstance(run_id, str):
raise typer.BadParameter("run_id must be a string")
if "\x00" in run_id:
raise typer.BadParameter("run_id must not contain null bytes")
if not run_id:
raise typer.BadParameter("run_id must not be empty")
if len(run_id) > _MAX_RUN_ID_LEN:
raise typer.BadParameter(
f"run_id too long ({len(run_id)} > {_MAX_RUN_ID_LEN})"
)
return run_id
def _read_evidence_json(path: str, *, console: Console) -> dict:
"""Read + parse an evidence JSON file with size cap (review M6 fix).
Uses ``O_NOFOLLOW`` (POSIX) + ``os.fstat`` on the SAME descriptor for
size enforcement (review H-NEW-2 fix — defends against an attacker
swapping the file between the helper's lstat and our open).
"""
from soup_cli.utils.paths import enforce_under_cwd_and_no_symlink
enforce_under_cwd_and_no_symlink(path, "--evidence")
flags = os.O_RDONLY
if hasattr(os, "O_NOFOLLOW"):
flags |= os.O_NOFOLLOW
try:
fd = os.open(path, flags)
except OSError as exc:
raise typer.BadParameter(
f"cannot open --evidence: {type(exc).__name__}"
) from exc
try:
st = os.fstat(fd)
if stat.S_ISLNK(st.st_mode): # impossible under O_NOFOLLOW, defence-in-depth
raise typer.BadParameter("--evidence must not be a symlink")
if st.st_size > _MAX_EVIDENCE_BYTES:
raise typer.BadParameter(
f"--evidence too large ({st.st_size} > {_MAX_EVIDENCE_BYTES})"
)
with os.fdopen(fd, "r", encoding="utf-8", closefd=True) as fh:
raw = fh.read()
fd = -1
finally:
if fd != -1:
try:
os.close(fd)
except OSError:
pass
data = json.loads(raw)
if not isinstance(data, dict):
console.print("[red]Evidence must be a JSON object.[/]")
raise typer.Exit(2)
return data
def _write_json_output(
payload: dict, output: str, *, console: Console, field: str = "--output",
) -> None:
"""Write ``payload`` to ``output`` atomically (review L6 dedup helper)."""
from soup_cli.utils.paths import (
atomic_write_text,
enforce_under_cwd_and_no_symlink,
)
try:
enforce_under_cwd_and_no_symlink(output, field)
except (TypeError, ValueError) as exc:
console.print(f"[red]Invalid {field}:[/] {escape(str(exc))}")
raise typer.Exit(2) from exc
atomic_write_text(json.dumps(payload, indent=2), output, field=field)
console.print(f"[green]Wrote {escape(output)}[/]")
def register(app: typer.Typer, console: Console) -> None:
"""Attach v0.65.0 subcommands to the existing ``soup eval`` app."""
@app.command(name="behavior")
def behavior_cmd(
run_id: str = typer.Argument(..., help="Run identifier."),
battery: str = typer.Option(
"xstest", "--battery", "-b",
help="Battery: xstest / harmbench / jailbreakbench / elephant / syceval.",
),
evidence: Optional[str] = typer.Option(
None, "--evidence", "-e",
help=(
"Path to a JSON file with "
"{pre_responses, post_responses, oracle} arrays."
),
),
output: Optional[str] = typer.Option(
None, "--output", "-o",
help="Where to write the rendered BehaviorDiffReport JSON.",
),
base_model: Optional[str] = typer.Option(
None, "--base-model",
help="Base model id for a LIVE diff (generates pre/post responses).",
),
adapter: Optional[str] = typer.Option(
None, "--adapter",
help="LoRA adapter path for the 'post' model in a live diff.",
),
device: Optional[str] = typer.Option(
None, "--device", help="Device for the live diff (cuda / cpu).",
),
) -> None:
"""Score a run on a bundled behaviour battery (pre/post diff).
With ``--base-model`` (optionally ``--adapter``) this LIVE-generates
pre/post responses on the bundled battery and scores them. Without it,
falls back to ``--evidence`` JSON, or a neutral OK report.
"""
from soup_cli.utils.behavior_battery import (
compute_behavior_diff,
get_battery_spec,
validate_battery_name,
)
# M5 fix — validate run_id BEFORE anything else.
try:
_validate_run_id(run_id)
except typer.BadParameter as exc:
console.print(f"[red]Invalid run_id:[/] {escape(str(exc))}")
raise typer.Exit(2) from exc
try:
canonical = validate_battery_name(battery)
except (TypeError, ValueError) as exc:
console.print(f"[red]Invalid battery:[/] {escape(str(exc))}")
raise typer.Exit(2) from exc
spec = get_battery_spec(canonical)
console.print(Panel(
f"[bold]{escape(spec.name)}[/]\n{escape(spec.description)}\n"
f"Axis: {escape(spec.primary_axis)}",
title="Behaviour Battery",
border_style="cyan",
))
if base_model is not None:
from soup_cli.utils.behavior_battery import run_behavior_live
try:
report = run_behavior_live(
run_id=run_id,
battery=canonical,
base_model=base_model,
adapter=adapter,
device=device,
)
except (RuntimeError, ValueError, TypeError, OSError) as exc:
console.print(f"[red]Live behaviour diff failed:[/] {escape(str(exc))}")
raise typer.Exit(2) from exc
table = Table(title=f"Behaviour Diff (live) — {canonical}")
table.add_column("Stage", style="bold")
table.add_column("Value", justify="right")
table.add_column("Verdict")
table.add_row("Pre", f"{report.pre.value:.3f}", report.pre.verdict)
table.add_row("Post", f"{report.post.value:.3f}", report.post.verdict)
table.add_row("Δ", f"{report.delta:+.3f}", report.overall)
console.print(table)
if output:
_write_json_output(report.to_dict(), output, console=console)
if report.overall == "MAJOR":
raise typer.Exit(2)
return
if evidence is None:
# No evidence: emit neutral OK report (matches v0.56.0 diagnose
# policy when no probes are supplied).
console.print(
"[yellow]No --evidence supplied; emitting neutral OK report.[/]"
)
payload = {
"run_id": run_id, "battery": canonical,
"pre": {"value": 1.0, "verdict": "OK", "num_probes": 0},
"post": {"value": 1.0, "verdict": "OK", "num_probes": 0},
"delta": 0.0, "overall": "OK",
}
if output:
_write_json_output(payload, output, console=console)
return
try:
data = _read_evidence_json(evidence, console=console)
except (typer.BadParameter, OSError, json.JSONDecodeError) as exc:
console.print(
f"[red]Failed to read evidence:[/] {escape(str(exc))}"
)
raise typer.Exit(2) from exc
try:
report = compute_behavior_diff(
run_id=run_id,
battery=canonical,
pre_responses=data.get("pre_responses") or [],
post_responses=data.get("post_responses") or [],
oracle=data.get("oracle") or [],
)
except (TypeError, ValueError) as exc:
console.print(f"[red]Diff failed:[/] {escape(str(exc))}")
raise typer.Exit(2) from exc
table = Table(title=f"Behaviour Diff — {canonical}")
table.add_column("Stage", style="bold")
table.add_column("Value", justify="right")
table.add_column("Verdict")
table.add_row("Pre", f"{report.pre.value:.3f}", report.pre.verdict)
table.add_row("Post", f"{report.post.value:.3f}", report.post.verdict)
table.add_row("Δ", f"{report.delta:+.3f}", report.overall)
console.print(table)
if output:
_write_json_output(report.to_dict(), output, console=console)
if report.overall == "MAJOR":
raise typer.Exit(2)
@app.command(name="capability")
def capability_cmd(
run_id: str = typer.Argument(..., help="Run identifier."),
suite: str = typer.Option(
"fast", "--suite", "-s",
help="Profile: full / fast / math / code.",
),
output: Optional[str] = typer.Option(
None, "--output", "-o",
help="Where to write the rendered CapabilityReport JSON.",
),
live: bool = typer.Option(
False, "--live",
help="Run the suite LIVE via lm-eval-harness against --model.",
),
model: Optional[str] = typer.Option(
None, "--model", "-m",
help="HF model id for a live run (required with --live).",
),
tasks: Optional[str] = typer.Option(
None, "--tasks",
help="Comma-separated lm-eval task override (live; bypasses --suite).",
),
limit: Optional[int] = typer.Option(
None, "--limit",
help="Cap eval examples per task (live; use 1-5 for a smoke).",
),
device: Optional[str] = typer.Option(
None, "--device", help="Device for the live run (cuda / cpu).",
),
) -> None:
"""Run a bundled capability profile (MMLU-Pro / GPQA / AIME / ...).
Without ``--live`` this emits a task manifest (no model load). With
``--live --model <id>`` it invokes lm-eval-harness per task.
"""
from soup_cli.utils.capability_suite import (
list_suites,
resolve_suite,
validate_suite_name,
)
try:
_validate_run_id(run_id)
except typer.BadParameter as exc:
console.print(f"[red]Invalid run_id:[/] {escape(str(exc))}")
raise typer.Exit(2) from exc
try:
canonical = validate_suite_name(suite)
except (TypeError, ValueError) as exc:
console.print(
f"[red]Invalid --suite:[/] {escape(str(exc))} "
f"(valid: {', '.join(list_suites())})"
)
raise typer.Exit(2) from exc
if live:
if not model:
console.print("[red]--live requires --model <hf-id>.[/]")
raise typer.Exit(2)
from soup_cli.utils.capability_suite import run_capability_suite
task_list = (
[t.strip() for t in tasks.split(",") if t.strip()] if tasks else None
)
try:
payload = run_capability_suite(
run_id=run_id,
model_id=model,
suite=None if task_list else canonical,
tasks=task_list,
device=device,
limit=limit,
)
except (RuntimeError, ValueError, TypeError) as exc:
console.print(f"[red]Capability run failed:[/] {escape(str(exc))}")
raise typer.Exit(2) from exc
table = Table(title=f"Capability Suite (live) — {canonical}")
table.add_column("Benchmark")
table.add_column("Metric")
table.add_column("Score", justify="right")
for r in payload["results"]:
if "error" in r:
table.add_row(
escape(str(r["benchmark"])),
"[red]error[/]",
escape(str(r["error"])),
)
else:
table.add_row(
escape(str(r["benchmark"])),
escape(str(r.get("metric", ""))),
f"{r.get('score', float('nan')):.4f}",
)
console.print(table)
if output:
_write_json_output(payload, output, console=console)
return
benchmarks = resolve_suite(canonical)
table = Table(title=f"Capability Suite — {canonical}")
table.add_column("Benchmark")
table.add_column("lm-eval task")
for b in benchmarks:
table.add_row(escape(b.name), escape(b.lm_eval_task))
console.print(table)
payload = {
"run_id": run_id,
"suite": canonical,
"benchmarks": [{"name": b.name, "task": b.lm_eval_task} for b in benchmarks],
"note": (
"Manifest only — pass --live --model <id> to invoke "
"lm-eval-harness against the listed tasks."
),
}
if output:
_write_json_output(payload, output, console=console)
@app.command(name="checklist")
def checklist_cmd(
spec_path: str = typer.Argument(
..., help="Path to CheckList DSL YAML.",
),
evidence: Optional[str] = typer.Option(
None, "--evidence", "-e",
help="Optional JSON with operator-supplied per-test responses.",
),
output: Optional[str] = typer.Option(
None, "--output", "-o",
help="Where to write the rendered CheckListReport JSON.",
),
) -> None:
"""Run CheckList MFT / INV / DIR behavioural tests."""
from soup_cli.utils.checklist_dsl import (
load_checklist_spec,
run_checklist_spec,
)
try:
spec = load_checklist_spec(spec_path)
except (TypeError, ValueError, OSError) as exc:
console.print(f"[red]Failed to load spec:[/] {escape(str(exc))}")
raise typer.Exit(2) from exc
evidence_map = None
if evidence is not None:
try:
evidence_map = _read_evidence_json(evidence, console=console)
except (typer.BadParameter, OSError, json.JSONDecodeError) as exc:
console.print(
f"[red]Failed to read evidence:[/] {escape(str(exc))}"
)
raise typer.Exit(2) from exc
try:
report = run_checklist_spec(spec, evidence=evidence_map)
except (TypeError, ValueError) as exc:
console.print(f"[red]CheckList run failed:[/] {escape(str(exc))}")
raise typer.Exit(2) from exc
table = Table(title="CheckList Tests")
table.add_column("Name", style="bold")
table.add_column("Kind")
table.add_column("Passed", justify="right")
table.add_column("Total", justify="right")
table.add_column("Verdict")
for result in report.results:
table.add_row(
escape(result.name),
result.kind,
str(result.passed),
str(result.total),
result.verdict,
)
console.print(table)
console.print(
f"Overall: [bold]{report.overall}[/]"
)
if output:
_write_json_output(report.to_dict(), output, console=console)
if report.overall == "MAJOR":
raise typer.Exit(2)
@app.command(name="irt-subset")
def irt_subset_cmd(
responses_path: str = typer.Argument(
...,
help=(
"Path to JSONL with rows {item_id, correct(bool), "
"respondent_id? (required for 2pl/3pl)}."
),
),
size: str = typer.Option(
"small", "--size", "-z",
help="Subset profile: full / small / tiny.",
),
model: str = typer.Option(
"1pl", "--model", "-m",
help="IRT model: 1pl (Rasch) / 2pl (discrimination) / 3pl (+ guessing).",
),
output: Optional[str] = typer.Option(
None, "--output", "-o",
help="Where to write the rendered IrtSubsetPlan JSON.",
),
) -> None:
"""Pick a minimum-cost eval subset that preserves ranking power."""
from soup_cli.utils.irt import (
IRT_PROFILES,
SUPPORTED_IRT_MODELS,
fit_difficulty,
fit_irt,
load_response_rows,
pick_irt_subset,
)
if size not in IRT_PROFILES:
console.print(
f"[red]Invalid --size: {escape(size)} "
f"(valid: {', '.join(sorted(IRT_PROFILES))})[/]"
)
raise typer.Exit(2)
if model not in SUPPORTED_IRT_MODELS:
console.print(
f"[red]Invalid --model: {escape(model)} "
f"(valid: {', '.join(SUPPORTED_IRT_MODELS)})[/]"
)
raise typer.Exit(2)
try:
rows = load_response_rows(responses_path)
except (TypeError, ValueError, OSError) as exc:
console.print(f"[red]Failed to load responses:[/] {escape(str(exc))}")
raise typer.Exit(2) from exc
try:
if model == "1pl":
# 1PL keeps the single-respondent correct-rate fit (back-compat;
# rows need only {item_id, correct}).
items = fit_difficulty(rows)
else:
# 2PL/3PL need a response matrix ({respondent_id, item_id, correct}).
items = fit_irt(rows, model=model)
plan = pick_irt_subset(items, size=size)
except (TypeError, ValueError) as exc:
console.print(f"[red]IRT fit failed:[/] {escape(str(exc))}")
raise typer.Exit(2) from exc
console.print(Panel(
f"Model: [bold]{escape(model)}[/]\n"
f"Profile: [bold]{escape(plan.size)}[/]\n"
f"Selected: {len(plan.item_ids)} / {plan.total_items}\n"
f"Approx cost cut: {plan.cost_ratio:.1%}",
title="IRT Subset",
border_style="green",
))
if output:
_write_json_output(plan.to_dict(), output, console=console)