forked from MakazhanAlpamys/Soup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathship.py
More file actions
1337 lines (1155 loc) · 53.2 KB
/
Copy pathship.py
File metadata and controls
1337 lines (1155 loc) · 53.2 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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""soup ship — the SHIP / DON'T-SHIP verdict (v0.71.25).
Top-level CLI command (NOT a sub-group) — operators type::
soup ship --base <m> --adapter <lora> --task-eval tasks.jsonl
soup ship --evidence ev.json # offline, pre-computed scores
soup ship ... --output verdict.json
soup ship ... --config soup.yaml # read eval.ship defaults + bind provenance
soup ship ... --emit-evidence ev.json # re-serialise scores as replayable input
soup ship --evidence ev.json --push owner/repo#42 # verdict as a PR comment
After fine-tuning, answer ONE question: did the model get better, or did I
break it? The decision fuses two legs (task win + catastrophic-forgetting
guard) into a single binary verdict — see ``utils/ship_verdict.py`` for the
moat (``decide_ship``).
Exit codes so CI can gate on the result:
**0 = SHIP, 2 = DON'T SHIP, 3 = usage/validation error, 1 = runtime error**.
Usage errors moved off ``2`` in v0.71.38 — a typo'd flag was previously
indistinguishable from a caught regression (both exited ``2``); ``3`` mirrors
``soup plan`` / ``soup env check``. Offline ``--evidence`` read/parse errors
stay ``1``.
Leg 1 (task win) modes: ``metric`` (reuses ``eval/custom.run_eval`` accuracy),
``judge_score`` (reuses ``eval/judge.JudgeEvaluator``), and ``pairwise`` (true
judge win-rate, v0.71.31). Leg 2 (general suite) defaults to the bundled offline
suite (``eval/gate_suites`` — MCQ/arithmetic + tool-call/JSON/safety, scored by
the pure diagnose/custom scorers, v0.71.38); ``--general-suite`` with any
non-bundled name routes through the existing lm-eval runner.
"""
from __future__ import annotations
import json
import os
import re
from typing import (
TYPE_CHECKING,
Callable,
Dict,
List,
Mapping,
NoReturn,
Optional,
Tuple,
)
import typer
from rich.console import Console
from rich.markup import escape
from soup_cli.utils.paths import atomic_write_text, enforce_under_cwd_and_no_symlink
if TYPE_CHECKING: # pydantic models — import for typing only (no eager cost)
from soup_cli.config.schema import ShipConfig, SoupConfig
from soup_cli.utils.ship_verdict import (
DECISION_SHIP,
DEFAULT_FORGETTING_THRESHOLD,
MAX_NOISE_FLOOR_RUNS,
MIN_NOISE_FLOOR_RUNS,
SUPPORTED_TASK_MODES,
TASK_AXIS,
TASK_MODES,
NoiseFloor,
ShipVerdict,
TaskWin,
build_task_win,
compute_benchmark_deltas,
compute_noise_floor,
decide_ship,
floor_exceeds_threshold,
for_terminal,
noise_floor_from_evidence,
render_ship_panel,
verdict_to_dict,
verdict_to_evidence,
)
console = Console()
app = typer.Typer(no_args_is_help=False)
# Exit-code taxonomy (v0.71.38): keep DON'T-SHIP distinct from a config typo.
_EXIT_RUNTIME = 1 # something went wrong actually running (IO, model load, ...)
_EXIT_DONT_SHIP = 2 # a verdict: leg 1 or leg 2 said don't ship
_EXIT_USAGE = 3 # bad flags / validation (mirrors `soup plan` / `env check`)
# 16 MiB cap on evidence JSON (mirrors `soup diagnose` — prevents a
# multi-GB / symlink-pointed file from OOMing at json.load time).
_MAX_EVIDENCE_BYTES = 16 * 1024 * 1024
# 4 MiB cap on a soup.yaml passed via --config (configs are small).
_MAX_CONFIG_BYTES = 4 * 1024 * 1024
# 8 GiB cap on the training file we fingerprint for provenance.data_sha
# (best-effort — skipped above this, never fatal).
_MAX_DATA_SHA_BYTES = 8 * 1024 * 1024 * 1024
# A canonical hex SHA-256 digest — used to sanity-check the config_sha we read
# out of an untrusted evidence file before echoing it in an error message (a
# raw value could smuggle terminal ESC bytes past rich.markup.escape).
_SHA256_RE = re.compile(r"^[0-9a-f]{64}$")
# lm-eval override defaults (kept minimal; the override is for users who
# already run lm-eval — they can tune via a future flag if needed).
_LM_EVAL_BATCH_SIZE = 1
# Bounds on the leg-2 general suite (DoS / input-hygiene guards).
_MAX_SUITE_BENCHMARKS = 50
_MAX_BENCHMARK_NAME_CHARS = 256
# ---------------------------------------------------------------------------
# Small helpers
# ---------------------------------------------------------------------------
def _fail(message: str, code: int) -> NoReturn:
"""Print a friendly red error and raise ``typer.Exit(code)``."""
console.print(f"[red]Error:[/] {escape(message)}")
raise typer.Exit(code=code)
def _validate_threshold_flag(value: float) -> float:
# Fast-fail a bad flag with a usage error (exit 3) here; the engine's own
# _validate_threshold raises ValueError (-> exit 1 runtime), which is the
# wrong exit code for a CLI typo. Intentional, narrow duplication.
if not isinstance(value, (int, float)) or isinstance(value, bool):
_fail("--forgetting-threshold must be a number", _EXIT_USAGE)
fvalue = float(value)
# NaN fails both comparisons -> rejected.
if not (0.0 <= fvalue <= 1.0):
_fail("--forgetting-threshold must be in [0.0, 1.0]", _EXIT_USAGE)
return fvalue
def _validate_noise_floor_flag(value: Optional[int]) -> Optional[int]:
"""``--noise-floor N`` — repeats of the BASE run, or ``None`` when unset.
Bounded below because a floor is a spread and one sample has none, and
above because every repeat is a full pass over the base model.
"""
if value is None:
return None
if isinstance(value, bool) or not isinstance(value, int):
_fail("--noise-floor must be an integer", _EXIT_USAGE)
if not (MIN_NOISE_FLOOR_RUNS <= value <= MAX_NOISE_FLOOR_RUNS):
_fail(
f"--noise-floor must be in "
f"[{MIN_NOISE_FLOOR_RUNS}, {MAX_NOISE_FLOOR_RUNS}]; got {value}",
_EXIT_USAGE,
)
return value
def _validate_task_mode_flag(task_mode: str) -> None:
# All three modes (metric / judge_score / pairwise) ship as of v0.71.31, so
# SUPPORTED_TASK_MODES == TASK_MODES and the old "pairwise reserved" gate is
# gone (it was dead code).
if task_mode not in TASK_MODES:
_fail(
f"--task-mode must be one of {', '.join(TASK_MODES)}; got {task_mode!r}",
_EXIT_USAGE,
)
def _validate_judge_model_url(url: str) -> None:
"""SSRF guard for --judge-model: urlparse hostname check (not startswith).
Blocks the ``http://localhost.attacker.com`` prefix-bypass that a bare
``startswith("http://localhost")`` check would allow through.
"""
from urllib.parse import urlparse
parsed = urlparse(url)
if parsed.scheme in ("ollama", "https"):
return
if parsed.scheme == "http" and parsed.hostname in ("localhost", "127.0.0.1"):
return
_fail(
f"--judge-model {url!r} uses a disallowed scheme/host; "
"use ollama://, https://, or http://localhost",
_EXIT_USAGE,
)
def _reject_lm_eval_injection(value: str, field: str) -> None:
"""Block ',' / '=' in an lm-eval model id (model_args injection guard).
lm-eval parses ``model_args`` as comma-separated ``key=value`` pairs, so an
adapter path like ``lora,trust_remote_code=True`` would otherwise smuggle
extra args (e.g. remote-code execution) into the harness.
"""
if "," in value or "=" in value:
raise ValueError(
f"{field} must not contain ',' or '=' "
f"(lm-eval model_args injection guard): {value!r}"
)
# ---------------------------------------------------------------------------
# --config — read leg-1/leg-2 defaults from a committed soup.yaml (v0.71.39)
# ---------------------------------------------------------------------------
def _safe_read_text(path: str, field: str, max_bytes: int) -> str:
"""O_NOFOLLOW + fstat-capped read of a cwd-contained file.
Shared TOCTOU-safe reader (mirrors v0.71.22 ``load_audio_mono``): opens with
``O_NOFOLLOW`` where available and fstats the open fd, so a symlink swapped
in after the containment check cannot redirect the read. Raises ``ValueError``
(incl. via ``enforce_under_cwd_and_no_symlink``) on any failure; callers map
it to the right exit code.
"""
enforce_under_cwd_and_no_symlink(path, field)
flags = os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0)
try:
fd = os.open(path, flags)
except OSError as exc:
raise ValueError(f"{field} unreadable: {type(exc).__name__}") from exc
with os.fdopen(fd, "r", encoding="utf-8") as handle:
if os.fstat(handle.fileno()).st_size > max_bytes:
raise ValueError(f"{field} exceeds {max_bytes} bytes")
return handle.read()
def _parse_ship_config(path: str) -> "Tuple[SoupConfig, Optional[ShipConfig]]":
"""Load a soup.yaml and return ``(SoupConfig, ShipConfig | None)``.
A read / parse / validation failure is a USAGE error (exit 3), mirroring
``soup plan`` / ``soup env check``.
"""
import yaml
from soup_cli.config.loader import load_config_from_string
try:
text = _safe_read_text(path, "--config path", _MAX_CONFIG_BYTES)
cfg = load_config_from_string(text)
except (ValueError, TypeError, yaml.YAMLError) as exc:
_fail(f"--config: {exc}", _EXIT_USAGE)
ship_cfg = cfg.eval.ship if cfg.eval is not None else None
return cfg, ship_cfg
def _config_sha_of(cfg: "SoupConfig") -> str:
"""Canonical (order/whitespace-insensitive) SHA-256 of the training recipe.
Semantic, not textual: a reformatted soup.yaml keeps the same sha but a real
recipe change does not. Cheap — hashes only the config dict, never the data
file (that's the ``data_sha`` in the full provenance).
The gate's own read-time policy (``eval.ship`` — threshold / suite / judge)
is EXCLUDED: it is applied at verdict time, not training time, so loosening
``forgetting_threshold`` must NOT invalidate evidence about an unchanged
model (the staleness gate fingerprints the recipe, not the gate config).
"""
from soup_cli.registry.hashing import hash_config
return hash_config(cfg.model_dump(mode="json", exclude={"eval": {"ship"}}))
def _safe_hash_file(path: str, max_bytes: int) -> Optional[str]:
"""SHA-256 of a cwd-local file via an O_NOFOLLOW fd (TOCTOU + size capped).
``data.train`` comes from a parsed ``--config`` YAML, so it must not follow a
symlink out of the tree or stream an unbounded file. This mirrors
``_safe_read_text`` but hashes bytes and is best-effort — returns ``None``
(never raises) for an absent / oversized / unreadable / out-of-cwd path,
because ``data_sha`` is informational provenance, not a hard requirement.
"""
import hashlib
try:
enforce_under_cwd_and_no_symlink(path, "data.train")
except (ValueError, TypeError):
return None
flags = os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0)
try:
fd = os.open(path, flags)
except OSError:
return None
try:
with os.fdopen(fd, "rb") as handle:
if os.fstat(handle.fileno()).st_size > max_bytes:
return None
digest = hashlib.sha256()
for chunk in iter(lambda: handle.read(1024 * 1024), b""):
digest.update(chunk)
except OSError:
return None
return digest.hexdigest()
def _compute_provenance(cfg: "SoupConfig") -> Dict[str, object]:
"""Bind emitted evidence to the exact config that produced it (v0.71.39).
``config_sha`` (semantic recipe hash) + ``base_model`` + a best-effort
``data_sha`` over a cwd-local training file. Built only when we actually
``--emit-evidence`` (the ``data_sha`` streams the whole training file).
Also carries the #404 scorer/version stamp so a later ``--baseline``
consumer can detect scale drift.
"""
from soup_cli.eval.gate import current_baseline_stamp
prov: Dict[str, object] = dict(current_baseline_stamp())
prov["config_sha"] = _config_sha_of(cfg)
base = cfg.base
if base:
prov["base_model"] = base
data = cfg.data.train
if data:
if isinstance(data, list):
# #443 — data.interleave: combine per-file hashes into one
# data_sha. Order preserved (not sorted) since reordering the
# list is a real semantic change — it realigns data.interleave
# .probs to different files.
import hashlib
shas = [
sha for p in data
if (sha := _safe_hash_file(p, _MAX_DATA_SHA_BYTES)) is not None
]
data_sha = (
hashlib.sha256("\x1e".join(shas).encode()).hexdigest()
if shas
else None
)
else:
data_sha = _safe_hash_file(data, _MAX_DATA_SHA_BYTES)
if data_sha is not None:
prov["data_sha"] = data_sha
return prov
def _check_evidence_staleness(payload: dict, expected_sha: str) -> None:
"""Refuse evidence whose ``config_sha`` != the committed config's (exit 3).
Catches DRIFT: a PR that changed ``soup.yaml`` but forgot to recompute its
``ship_evidence.json`` is caught here instead of shipping a verdict about a
*different* recipe than the one in the diff. This is staleness detection,
NOT tamper-resistance — ``config_sha`` is an unkeyed hash, so it verifies
"this evidence claims to describe the config at HEAD", not "these scores were
actually produced by that config" (the ``--evidence`` trust model has always
assumed a trusted artifact from your own pipeline; ``soup attest`` /
``adapters sign`` provide ed25519 signing if forgery is in scope). Pure — the
payload is already loaded (read once per invocation).
"""
prov = payload.get("provenance")
got = prov.get("config_sha") if isinstance(prov, dict) else None
# Validate the SHAPE before ever printing it: a non-hex value is both
# malformed provenance AND a terminal-escape vector (rich.markup.escape does
# not strip raw C0/ESC bytes). Never echo an unvalidated value.
if not isinstance(got, str) or not _SHA256_RE.match(got):
_fail(
"evidence has no valid provenance.config_sha to verify against "
"--config; re-produce it with "
"`soup ship ... --config <cfg> --emit-evidence <ev>`",
_EXIT_USAGE,
)
if got != expected_sha:
# Both sides are now guaranteed [0-9a-f]{64}, so slicing is print-safe.
_fail(
"stale evidence: its config_sha does not match --config "
f"(evidence={got[:12]}..., config={expected_sha[:12]}...). "
"Re-run training + emit evidence against the current config.",
_EXIT_USAGE,
)
def _flag_is_default(ctx: typer.Context, name: str) -> bool:
"""True when ``name`` was left at its default (so --config may fill it).
Uses Click's parameter-source tracking so an explicit CLI flag (or env var)
always wins over the config value (CLI > config > hard default). Only a
genuine ``DEFAULT`` source returns True; an untrackable / unknown name
(source is None) returns False, so a future param rename cannot silently
make a flag config-overridable.
"""
try:
from click.core import ParameterSource
source = ctx.get_parameter_source(name)
except (ImportError, AttributeError): # pragma: no cover — defensive
return False
return source == ParameterSource.DEFAULT
# ---------------------------------------------------------------------------
# Offline path — --evidence
# ---------------------------------------------------------------------------
def _load_evidence(path: str) -> dict:
"""Load an evidence JSON (cwd-contained, symlink-rejected, size-capped)."""
payload = json.loads(_safe_read_text(path, "evidence path", _MAX_EVIDENCE_BYTES))
if not isinstance(payload, dict):
raise ValueError("evidence file must contain a JSON object")
return payload
def _verdict_from_evidence(payload: dict, *, forgetting_threshold: float) -> ShipVerdict:
"""Build a verdict from an already-loaded evidence payload (no model load)."""
task = payload.get("task")
if not isinstance(task, dict):
_fail("evidence.task must be an object with 'mode', 'base', 'tuned'", _EXIT_RUNTIME)
mode = task.get("mode", "metric")
if mode not in SUPPORTED_TASK_MODES:
_fail(
f"evidence.task.mode must be one of {', '.join(SUPPORTED_TASK_MODES)}; "
f"got {mode!r}",
_EXIT_RUNTIME,
)
if "base" not in task or "tuned" not in task:
_fail("evidence.task needs both 'base' and 'tuned' scores", _EXIT_RUNTIME)
# A floor recorded by --emit-evidence must be honoured on read, or the same
# scores replay to a DIFFERENT decision than the run that produced them.
try:
stored_floor = noise_floor_from_evidence(payload.get("noise_floor"))
except (TypeError, ValueError) as exc:
_fail(f"invalid evidence.noise_floor: {exc}", _EXIT_RUNTIME)
_warn_if_floor_widens(stored_floor, forgetting_threshold, source="evidence-supplied")
try:
task_win = build_task_win(
mode, task["base"], task["tuned"], noise_floor=stored_floor
)
except (TypeError, ValueError) as exc:
_fail(f"invalid evidence.task: {exc}", _EXIT_RUNTIME)
raw_benchmarks = payload.get("benchmarks", {})
if not isinstance(raw_benchmarks, dict):
_fail("evidence.benchmarks must be an object of {name: {base, tuned}}", _EXIT_RUNTIME)
base_scores: Dict[str, object] = {}
tuned_scores: Dict[str, object] = {}
for name, entry in raw_benchmarks.items():
if not isinstance(entry, dict) or "base" not in entry or "tuned" not in entry:
_fail(f"evidence.benchmarks[{name!r}] needs 'base' and 'tuned'", _EXIT_RUNTIME)
base_scores[str(name)] = entry["base"]
tuned_scores[str(name)] = entry["tuned"]
try:
deltas = compute_benchmark_deltas(
base_scores,
tuned_scores,
forgetting_threshold=forgetting_threshold,
noise_floor=stored_floor,
)
return decide_ship(
task_win,
deltas,
forgetting_threshold=forgetting_threshold,
noise_floor=stored_floor,
)
except (TypeError, ValueError) as exc:
_fail(f"invalid evidence.benchmarks: {exc}", _EXIT_RUNTIME)
# ---------------------------------------------------------------------------
# Live path — load base + tuned, evaluate both legs
# ---------------------------------------------------------------------------
# live_eval only builds a BitsAndBytesConfig for these two (#367); the other
# quant_menu formats (gptq/awq/hqq/...) need a full TrainingConfig, so those
# still fall back to bf16 here, same as no --config at all.
_LIVE_EVAL_QUANTIZATION_FORMATS = frozenset({"4bit", "8bit"})
def _live_eval_quantization_from_config(soup_config: Optional["SoupConfig"]) -> Optional[str]:
"""Reuse the training run's own quantization for the live eval load.
Returns ``None`` (unchanged bf16 default) when no ``--config`` was given,
or the run used a format live_eval cannot build directly.
"""
if soup_config is None:
return None
quant = soup_config.training.quantization
return quant if quant in _LIVE_EVAL_QUANTIZATION_FORMATS else None
def _resolve_generators(
base: str,
tuned: Optional[str],
adapter: Optional[str],
device: Optional[str],
quantization: Optional[str] = None,
) -> Tuple[Callable[[str], str], Callable[[str], str]]:
"""Build ``(base_gen, tuned_gen)`` from live_eval (greedy decode)."""
# #316 — the behavioural suites need a budget that fits a real tool call.
# At make_generator's default of 64, 31 of 40 tool calls and 15 of 40 JSON
# fences were truncated ONE CLOSING BRACE short and scored 0.000 on a model
# that produces them correctly. Extraction cannot repair truncated JSON and
# must not try — a decoder lenient enough to guess the missing brace would
# credit incidental braces in prose instead.
from soup_cli.eval.gate_suites import BEHAVIOURAL_MAX_NEW_TOKENS
from soup_cli.utils import live_eval
if quantization:
console.print(
f"[dim]Live eval: loading base/tuned at {quantization} "
"(reused from --config training.quantization).[/]"
)
dtype = None
else:
# Match the fallback message: bf16 on CUDA (this codebase's other live
# loaders use the same cuda-else-fp32 split, e.g. mole_routing.py/prm.py),
# fp32 elsewhere. Previously left unset, so from_pretrained fell through
# to its own default instead of the precision this message promised.
resolved_device = live_eval.resolve_device(device)
# startswith, not ==: an explicit --device cuda:0 (or any indexed
# CUDA device) resolves verbatim (live_eval.resolve_device returns
# it unchanged), and a bare "cuda" equality check would miss it and
# silently fall back to float32.
dtype = "bfloat16" if resolved_device.startswith("cuda") else "float32"
console.print(
f"[dim]Live eval: loading base/tuned at full precision ({dtype}); "
"pass --config to reuse the training run's own quantization.[/]"
)
base_gen = live_eval.make_generator(
base, device=device, max_new_tokens=BEHAVIOURAL_MAX_NEW_TOKENS,
dtype=dtype, quantization=quantization,
)
if adapter:
tuned_gen = live_eval.make_generator(
base, adapter=adapter, device=device,
max_new_tokens=BEHAVIOURAL_MAX_NEW_TOKENS,
dtype=dtype, quantization=quantization,
)
elif tuned:
tuned_gen = live_eval.make_generator(
tuned, device=device, max_new_tokens=BEHAVIOURAL_MAX_NEW_TOKENS,
dtype=dtype, quantization=quantization,
)
else: # pragma: no cover — _verdict_live guarantees one of tuned/adapter
raise ValueError("need --tuned or --adapter")
return base_gen, tuned_gen
def _leg1_metric(
base_gen: Callable[[str], str],
tuned_gen: Callable[[str], str],
base_id: str,
tuned_id: str,
task_eval: str,
) -> TaskWin:
from soup_cli.eval.custom import load_eval_tasks, run_eval
tasks = load_eval_tasks(task_eval)
if not tasks:
raise ValueError(f"task-eval file {task_eval!r} has no tasks")
base_acc = run_eval(base_id, tasks, generate_fn=base_gen).accuracy
tuned_acc = run_eval(tuned_id, tasks, generate_fn=tuned_gen).accuracy
return build_task_win("metric", base_acc, tuned_acc)
def _build_judge_scorer(
task_eval: str, judge_model: str
) -> Callable[[Callable[[str], str]], float]:
"""A ``score(gen) -> [0, 1]`` scorer for one side of a judge_score leg.
Lifted out of ``_leg1_judge`` (was an inner closure) so the base side can be
scored on its own N times for the noise floor without also scoring the tuned
side (#403). The tasks are loaded and the evaluator built once, then reused
across every call.
"""
from soup_cli.eval.custom import load_eval_tasks
from soup_cli.eval.gate import _parse_judge_url
from soup_cli.eval.judge import JudgeEvaluator
tasks = load_eval_tasks(task_eval)
if not tasks:
raise ValueError(f"task-eval file {task_eval!r} has no tasks")
provider, model, api_base = _parse_judge_url(judge_model)
evaluator = JudgeEvaluator(provider=provider, model=model, api_base=api_base)
# Normalise to [0, 1] using the judge's ACTUAL rubric scale (DEFAULT_RUBRIC
# is 1-5, not 1-10), via min-max so the rubric floor maps to 0.0. The
# verdict is monotonic-safe either way, but the stored/displayed numbers
# must be honest.
scale = evaluator.rubric.get("scale", {}) if isinstance(evaluator.rubric, dict) else {}
if not isinstance(scale, dict):
scale = {}
def _num(value: object, default: float) -> float:
if isinstance(value, (int, float)) and not isinstance(value, bool):
return float(value)
return default
scale_min = _num(scale.get("min", 1), 1.0)
scale_max = _num(scale.get("max", 5), 5.0)
span = (scale_max - scale_min) or 1.0
def _score(gen: Callable[[str], str]) -> float:
items = [
{"prompt": t.prompt, "response": gen(t.prompt), "category": t.category}
for t in tasks
]
overall = float(
getattr(evaluator.evaluate_batch(items), "overall_score", scale_min)
)
return max(0.0, min(1.0, (overall - scale_min) / span))
return _score
def _leg1_judge(
base_gen: Callable[[str], str],
tuned_gen: Callable[[str], str],
task_eval: str,
judge_model: str,
) -> TaskWin:
score = _build_judge_scorer(task_eval, judge_model)
return build_task_win("judge_score", score(base_gen), score(tuned_gen))
def _build_pairwise_scorer(
task_eval: str, judge_model: str
) -> Callable[[Callable[[str], str], Callable[[str], str]], float]:
"""A ``winrate(gen_a, gen_b) -> [0, 1]`` scorer for a pairwise leg (#284).
Factored out of ``_leg1_pairwise`` so the noise floor can measure the base
model judged against ITSELF (``winrate(base_gen, base_gen)``), whose expected
value is 0.5 by construction — its spread over repeats is the combined
decode + judge noise, directly measured rather than inferred (#403). Tasks
and evaluator are built once and reused.
"""
from soup_cli.eval.custom import load_eval_tasks
from soup_cli.eval.gate import _parse_judge_url
from soup_cli.eval.judge import JudgeEvaluator, pairwise_winrate
tasks = load_eval_tasks(task_eval)
if not tasks:
raise ValueError(f"task-eval file {task_eval!r} has no tasks")
provider, model, api_base = _parse_judge_url(judge_model)
evaluator = JudgeEvaluator(provider=provider, model=model, api_base=api_base)
def _winrate(
gen_a: Callable[[str], str], gen_b: Callable[[str], str]
) -> float:
pairs = [(t.prompt, gen_a(t.prompt), gen_b(t.prompt)) for t in tasks]
return pairwise_winrate(pairs, evaluator)
return _winrate
def _leg1_pairwise(
base_gen: Callable[[str], str],
tuned_gen: Callable[[str], str],
task_eval: str,
judge_model: str,
) -> TaskWin:
"""Leg-1 via a true pairwise judge win-rate (#284).
For each task prompt, generate a base and a tuned response and ask the judge
which is better (swap-debiased). The tuned win-rate becomes leg 1, framed as
``TaskWin(base=0.5 coin-flip, tuned=win-rate)`` so ``won <=> win-rate > 0.5``.
"""
winrate = _build_pairwise_scorer(task_eval, judge_model)
return build_task_win("pairwise", 0.5, winrate(base_gen, tuned_gen))
def _extract_lm_score(bench_data: Mapping[str, object]) -> Optional[float]:
"""Pull a single accuracy metric from an lm-eval per-task result block."""
for key in ("acc,none", "acc_norm,none", "exact_match,none", "em,none"):
if key in bench_data:
val = bench_data[key]
if isinstance(val, (int, float)) and not isinstance(val, bool):
return float(val)
for key, val in bench_data.items():
key_str = str(key)
if "stderr" in key_str or key_str.startswith("alias"):
continue
if isinstance(val, (int, float)) and not isinstance(val, bool):
return float(val)
return None
def _lm_eval_leg2(
names: List[str],
base_id: str,
tuned_id: Optional[str],
adapter: Optional[str],
baseline_scores: Mapping[str, float],
device: Optional[str],
) -> Tuple[Dict[str, object], Dict[str, object]]:
"""Score non-mini benchmarks via the existing lm-eval harness runner."""
from soup_cli.commands import eval as eval_cmd
dev = device or "cpu"
_reject_lm_eval_injection(base_id, "--base")
base_arg = f"pretrained={base_id}"
if adapter:
_reject_lm_eval_injection(adapter, "--adapter")
tuned_arg = f"pretrained={base_id},peft={adapter}"
else:
_reject_lm_eval_injection(str(tuned_id), "--tuned")
tuned_arg = f"pretrained={tuned_id}"
base_map: Dict[str, object] = {}
tuned_map: Dict[str, object] = {}
tuned_results = eval_cmd._run_lm_eval(
tuned_arg, names, None, _LM_EVAL_BATCH_SIZE, dev
)
tuned_blocks = tuned_results.get("results", {})
for name in names:
score = _extract_lm_score(tuned_blocks.get(name, {}))
if score is not None:
tuned_map[name] = score
base_to_run: List[str] = []
for name in names:
if name in baseline_scores:
base_map[name] = float(baseline_scores[name])
else:
base_to_run.append(name)
if base_to_run:
base_results = eval_cmd._run_lm_eval(
base_arg, base_to_run, None, _LM_EVAL_BATCH_SIZE, dev
)
base_blocks = base_results.get("results", {})
for name in base_to_run:
score = _extract_lm_score(base_blocks.get(name, {}))
if score is not None:
base_map[name] = score
return base_map, tuned_map
def _leg2_scores(
suite_names: List[str],
base_gen: Callable[[str], str],
tuned_gen: Callable[[str], str],
*,
base_id: str,
tuned_id: Optional[str],
adapter: Optional[str],
baseline_scores: Mapping[str, float],
device: Optional[str],
) -> Tuple[Dict[str, object], Dict[str, object]]:
"""Compute leg-2 ``(base_scores, tuned_scores)`` maps over the general suite.
Bundled suite names (v0.71.38 — the MCQ/arithmetic *and* the behavioural
tool-call / JSON-format / safety suites) are scored offline via
``gate_suites.score_bundled_suite``; any other name routes through the
lm-eval override. ``baseline_scores`` supplies base scores directly
(skipping the base run) for any name it covers.
"""
from soup_cli.eval.gate_suites import (
is_bundled_suite,
score_bundled_suite,
)
bundled_names = [n for n in suite_names if is_bundled_suite(n)]
other_names = [n for n in suite_names if not is_bundled_suite(n)]
base_map: Dict[str, object] = {}
tuned_map: Dict[str, object] = {}
for name in bundled_names:
tuned_map[name] = score_bundled_suite(name, tuned_gen)
if name in baseline_scores:
base_map[name] = float(baseline_scores[name])
else:
base_map[name] = score_bundled_suite(name, base_gen)
if other_names:
lm_base, lm_tuned = _lm_eval_leg2(
other_names, base_id, tuned_id, adapter, baseline_scores, device
)
base_map.update(lm_base)
tuned_map.update(lm_tuned)
# Never silently drop a requested benchmark: a name missing on either side
# would vanish at the delta intersection and the moat would not see a
# possible regression. Refuse loudly instead (-> exit 1).
missing = [n for n in suite_names if n not in base_map or n not in tuned_map]
if missing:
raise ValueError(
f"could not score benchmark(s) on both base and tuned: "
f"{', '.join(sorted(missing))}"
)
return base_map, tuned_map
def _build_task_floor_scorer(
task_mode: str,
base_gen: Callable[[str], str],
*,
base_id: str,
task_eval: str,
judge_model: Optional[str],
) -> Callable[[], float]:
"""A ``() -> float`` closure scoring the BASE side's leg-1 task axis once.
Built once (tasks / evaluator resolved a single time) and called per
noise-floor repeat so the spread reflects run-to-run variance, not setup.
The judge modes require a judge model; its presence is validated upstream in
``_verdict_live``, so a missing one here is a programming error.
"""
if task_mode == "metric":
from soup_cli.eval.custom import load_eval_tasks, run_eval
tasks = load_eval_tasks(task_eval)
if not tasks:
raise ValueError(f"task-eval file {task_eval!r} has no tasks")
def _metric_score() -> float:
return run_eval(base_id, tasks, generate_fn=base_gen).accuracy
return _metric_score
if not judge_model:
raise ValueError(f"--task-mode {task_mode} needs a judge model")
if task_mode == "judge_score":
judge_scorer = _build_judge_scorer(task_eval, judge_model)
def _judge_score() -> float:
return judge_scorer(base_gen)
return _judge_score
if task_mode == "pairwise":
pairwise_scorer = _build_pairwise_scorer(task_eval, judge_model)
def _pairwise_score() -> float:
# The base judged against itself: expected 0.5 by construction, so
# the spread over repeats is the combined decode + judge noise (#403).
return pairwise_scorer(base_gen, base_gen)
return _pairwise_score
raise ValueError(f"unknown task mode {task_mode!r}")
def _measure_noise_floor(
runs: int,
suite_names: List[str],
base_gen: Callable[[str], str],
*,
base_id: str,
task_mode: str,
task_eval: str,
judge_model: Optional[str],
forgetting_threshold: float,
) -> NoiseFloor:
"""Re-run the BASE model ``runs`` times and return the measured spread.
Greedy decoding is not deterministic on GPU: measured on an H100, the same
model with no adapter over five runs spread **0.015 strict / 0.020
format-blind**, against a gate threshold of 0.05. Four of six paired deltas
in that session sat inside the floor, so the gate was calling differences
it could not resolve.
Leg-2 axes are always measured (decode-only). The leg-1 task axis is now
measured in every mode (#403):
- ``metric``: the offline scorer, re-run — decode-only noise.
- ``judge_score``: the base side scored N times through the judge.
- ``pairwise``: the base model judged against ITSELF, whose expected
win-rate is 0.5 by construction, so the spread is a directly measured
quantity, not an inference.
In the two judge modes the spread folds the judge's own sampling noise into
the number, so the returned floor is stamped ``judge_inclusive`` and never
presented as decode-only. That is why a judge-scored win smaller than the
judge's own noise no longer counts.
A ``--baseline`` file is deliberately NOT consulted here even though the
verdict path uses one: a stored number is not a repeat of this instrument,
and folding it in would report a spread that was never measured.
"""
from soup_cli.eval.gate_suites import is_bundled_suite, score_bundled_suite
bundled = [name for name in suite_names if is_bundled_suite(name)]
skipped = [name for name in suite_names if not is_bundled_suite(name)]
if skipped:
console.print(
"[yellow]Warning:[/] --noise-floor measures bundled suites only; "
f"no floor for {escape(', '.join(sorted(skipped)))}"
)
judge_inclusive = task_mode in ("judge_score", "pairwise")
task_score = _build_task_floor_scorer(
task_mode,
base_gen,
base_id=base_id,
task_eval=task_eval,
judge_model=judge_model,
)
samples: List[Dict[str, float]] = []
for index in range(runs):
console.print(f"[dim]noise floor: base repeat {index + 1}/{runs}[/]")
run: Dict[str, float] = {}
for name in bundled:
run[name] = score_bundled_suite(name, base_gen)
run[TASK_AXIS] = task_score()
samples.append(run)
floor = compute_noise_floor(samples, judge_inclusive=judge_inclusive)
if floor.floors and all(value == 0.0 for _name, value in floor.floors):
console.print(
"[dim]noise floor: every axis repeated exactly — this instrument "
"was deterministic over these runs.[/]"
)
_warn_if_floor_widens(floor, forgetting_threshold, source="measured")
return floor
def _warn_if_floor_widens(
floor: Optional[NoiseFloor], threshold: float, *, source: str
) -> None:
"""Announce any axis whose floor loosens the gate past ``threshold``.
Shared by the live path and the ``--evidence`` reader on purpose: an
evidence-supplied floor widens the gate exactly as much as a measured one,
and an evidence file is untrusted input, so the quieter of the two paths is
the one an attacker would choose.
"""
widened = floor_exceeds_threshold(floor, threshold)
if not widened:
return
detail = ", ".join(f"{for_terminal(name)} {value:.4f}" for name, value in widened)
console.print(
f"[yellow]Warning:[/] the {escape(source)} noise floor exceeds "
f"--forgetting-threshold ({threshold:.4f}) on: {escape(detail)}. "
"Those axes are gated at their floor, so the gate is LOOSER there "
"than you asked."
)
def _parse_suite(general_suite: Optional[str]) -> List[str]:
from soup_cli.eval.gate_suites import DEFAULT_GENERAL_SUITE
if not general_suite:
return list(DEFAULT_GENERAL_SUITE)
names = [chunk.strip() for chunk in general_suite.split(",")]
return [name for name in names if name]
def _verdict_live(
*,
base: Optional[str],
tuned: Optional[str],
adapter: Optional[str],
task_eval: Optional[str],
task_mode: str,
judge_model: Optional[str],
general_suite: Optional[str],
baseline_spec: Optional[str],
device: Optional[str],
forgetting_threshold: float,
noise_floor_runs: Optional[int] = None,
quantization: Optional[str] = None,
) -> ShipVerdict:
"""Run a live verdict — validate flags (exit 2), then evaluate (exit 1)."""
if not base:
_fail("live run needs --base <model>", _EXIT_USAGE)
if adapter and tuned:
_fail("pass --adapter OR --tuned, not both", _EXIT_USAGE)
if not adapter and not tuned:
_fail("live run needs --tuned <model> or --adapter <adapter-path>", _EXIT_USAGE)
if not task_eval:
_fail("live run needs --task-eval <tasks.jsonl> for the leg-1 task win", _EXIT_USAGE)
try:
enforce_under_cwd_and_no_symlink(task_eval, "--task-eval path")
except (ValueError, TypeError) as exc:
_fail(str(exc), _EXIT_USAGE)
suite_names = _parse_suite(general_suite)
if not suite_names:
_fail("--general-suite resolved to no benchmarks", _EXIT_USAGE)
if len(suite_names) > _MAX_SUITE_BENCHMARKS:
_fail(
f"--general-suite has too many benchmarks (max {_MAX_SUITE_BENCHMARKS})",
_EXIT_USAGE,
)
for _name in suite_names:
if "\x00" in _name or len(_name) > _MAX_BENCHMARK_NAME_CHARS:
_fail(
"--general-suite names must be null-free and "
f"< {_MAX_BENCHMARK_NAME_CHARS} chars",
_EXIT_USAGE,
)
# Resolve --baseline up front so a bad spec (outside cwd / missing file /
# unknown registry id) is a USAGE error (exit 2), not a runtime error (1).
baseline_scores: Dict[str, float] = {}
if baseline_spec:
from soup_cli.eval.gate import resolve_baseline
try:
baseline_scores = resolve_baseline(
baseline_spec,
warn=lambda msg: console.print(
f"[yellow]Warning:[/] {escape(msg)}"
),
)
except (ValueError, FileNotFoundError, OSError) as exc:
_fail(f"--baseline: {exc}", _EXIT_USAGE)