"""Evaluation CLI (CONTRACTS section 8). python -m macjev.eval.cli --weights --readout verdict|letter --suites --out [--sealed-ok] [--temperatures temperatures.json] [--device cuda|mps|cpu] [--dtype bf16|fp32] [--base ] [--max-len 25600] [--laya english|typed|multilingual] [--limit N] [--sample N --seed S] Writes ``/.jsonl`` (prediction rows, raw logits) and ``/metrics.json`` (per suite: raw and, with --temperatures, calibrated metrics plus the suite bundles of ``macjev.eval.aggregate``), and ``/run.json`` (scorer description, timings, file hashes). Sealed suites need --sealed-ok; for our own model the CLI never prints sealed metrics to the console (they go to metrics.json only). ``--laya`` scores an official Laya checkpoint instead of --weights. """ from __future__ import annotations import argparse import json import random import sys import time from pathlib import Path from . import suites as U from .aggregate import suite_metrics from .metrics import latency_summary from .predictions import write_jsonl from ..render import CONTEXT_LIMIT def build_suite(name: str, cache: dict) -> list[dict]: if name == "flip": if "t4" not in cache: cache["t4"] = U.build_t4() return U.build_flip(cache["t4"]) if name not in cache: cache[name] = U.SUITES[name].build() return cache[name] def make_scorer(args): if args.laya: from .laya_scorer import LayaScorer return LayaScorer(args.laya, device=args.device) from .torch_scorer import TorchScorer return TorchScorer(args.weights, readout=args.readout, device=args.device, dtype=args.dtype, base=args.base, max_len=args.max_len, head_max=args.head_max, max_tokens=args.max_batch_tokens, attn_chunk=args.attn_chunk if args.attn_chunk == "auto" else int(args.attn_chunk)) def main(argv=None): ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) ap.add_argument("--weights", default=None) ap.add_argument("--readout", choices=["verdict", "letter"], default="verdict") ap.add_argument("--suites", required=True) ap.add_argument("--out", required=True) ap.add_argument("--sealed-ok", action="store_true") ap.add_argument("--temperatures", default=None) ap.add_argument("--device", default=None) ap.add_argument("--dtype", default=None, choices=[None, "bf16", "fp32", "fp16"]) ap.add_argument("--base", default="Qwen/Qwen3.5-0.8B-Base") ap.add_argument("--max-len", type=int, default=CONTEXT_LIMIT, help="total input budget (default: the 25,600-token context limit, macjev.render.CONTEXT_LIMIT); " "rows over it are 'unsupported', never truncated") ap.add_argument("--head-max", type=int, default=2048, help="header token budget; 2048 = the largest header the training pool uses (long tables, wide intent)") ap.add_argument("--max-batch-tokens", type=int, default=8192, help="padded tokens per forward batch; a longer row is scored alone") ap.add_argument("--attn-chunk", default="auto", help="query-chunked sdpa for long rows: auto (MPS/CPU only), 0 = off, or a chunk size") ap.add_argument("--laya", choices=["english", "typed", "multilingual"], default=None) ap.add_argument("--limit", type=int, default=None, help="first N records per suite (smoke tests)") ap.add_argument("--sample", type=int, default=None, help="random N records per suite (dev smoke tests)") ap.add_argument("--seed", type=int, default=0) args = ap.parse_args(argv) if not args.laya and not args.weights: ap.error("--weights is required unless --laya is given") names = U.resolve(args.suites, args.sealed_ok) temps = json.loads(Path(args.temperatures).read_text()) if args.temperatures else None out = Path(args.out) out.mkdir(parents=True, exist_ok=True) scorer = make_scorer(args) ours = not args.laya run = {"argv": sys.argv if argv is None else list(argv), "scorer": scorer.describe(), "suites": {}, "temperatures": args.temperatures, "started_unix": time.time()} metrics = {} cache: dict = {} for name in names: records = build_suite(name, cache) if args.sample is not None and args.sample < len(records): records = random.Random(args.seed).sample(records, args.sample) if args.limit is not None: records = records[:args.limit] t0 = time.time() rows = scorer.predict(records, progress=name if sys.stderr.isatty() else "") secs = time.time() - t0 sha = write_jsonl(out / f"{name}.jsonl", rows) n_ok = sum(r["status"] == "ok" for r in rows) run["suites"][name] = {"rows": len(rows), "ok": n_ok, "unsupported": len(rows) - n_ok, "sha256": sha, "seconds": round(secs, 2), "rows_per_second": round(len(rows) / max(secs, 1e-9), 2), "sealed": U.SUITES[name].sealed} metrics[name] = suite_metrics(name, rows, temps) metrics[name]["latency_batched"] = latency_summary([1000 * secs / max(1, len(rows))]) (out / "metrics.json").write_text(json.dumps(metrics, indent=1, allow_nan=True) + "\n") (out / "run.json").write_text(json.dumps(run, indent=1) + "\n") shown = {k: v for k, v in run["suites"][name].items() if k != "sha256"} if ours and U.SUITES[name].sealed: print(json.dumps({"suite": name, **shown, "metrics": "written to metrics.json (sealed; not printed)"})) else: acc = {s: round(m["raw"].get("accuracy", float("nan")), 4) for s, m in metrics[name]["suites"].items()} print(json.dumps({"suite": name, **shown, "accuracy_raw": acc})) run["finished_unix"] = time.time() (out / "run.json").write_text(json.dumps(run, indent=1) + "\n") if __name__ == "__main__": main()