#!/usr/bin/env python3 """Replay the largest matched-reference IQ8R KLD rows as one-token requests. Uses the exact token prefixes recorded with --kld-record and verifies the server consumed each entire prefix. Run against one candidate server configured with the same TP, BF16 KV, chunk limit, no speculation, and no prefix cache as the KLD scoring run. The report stores only row metadata and top candidates, not the prompt token sequence. """ import argparse import json from pathlib import Path import struct import sys import urllib.error import urllib.request def post(url, body, timeout): req = urllib.request.Request(url.rstrip("/") + "/v1/completions", json.dumps(body).encode(), {"Content-Type": "application/json"}) with urllib.request.urlopen(req, timeout=timeout) as response: return json.load(response) def main(): p = argparse.ArgumentParser(description=__doc__) p.add_argument("--reference", required=True, type=Path, help="directory from --kld-record (top-128 + tail)") p.add_argument("--report", required=True, type=Path, help="candidate --kld-out JSON; matching .rows file must be present") p.add_argument("--url", required=True, help="candidate Radiance base URL, e.g. http://127.0.0.1:5914") p.add_argument("--model", default="spark", help="served model name") p.add_argument("--out", required=True, type=Path) p.add_argument("--limit", type=int, default=20, help="replay this many largest-KL rows") p.add_argument("--threshold", type=float, default=1.0, help="also include every row above this coarsened KL threshold") p.add_argument("--timeout", type=float, default=120) args = p.parse_args() ref_dir, report_path, out = args.reference.resolve(), args.report.resolve(), args.out.resolve() ref = json.loads((ref_dir / "kld.json").read_text()) report = json.loads(report_path.read_text()) if ref.get("format") != "radiance-kld-top-1" or ref.get("top") != 128: p.error("reference must be radiance-kld-top-1 with top=128") if report.get("ref_form") != "top-128 + tail": p.error("candidate report was not scored against the top-128-plus-tail reference") n = int(ref["rows"]) if report.get("all", {}).get("positions") != n: p.error("candidate report position count differs from the reference") raw_rows = Path(str(report_path) + ".rows").read_bytes() if len(raw_rows) != n * 4 * 4: p.error("candidate .rows file has an unexpected size") values = struct.unpack("<" + "f" * (n * 4), raw_rows) kls = [values[4 * i] for i in range(n)] above = [i for i, kl in enumerate(kls) if kl > args.threshold] selected = set(sorted(range(n), key=kls.__getitem__, reverse=True)[:args.limit]) selected.update(above) selected = sorted(selected, key=kls.__getitem__, reverse=True) docs = ref["docs"] tokens_raw = (ref_dir / "tokens.i32").read_bytes() if len(tokens_raw) % 4: p.error("tokens.i32 has an unexpected size") tokens = struct.unpack("<" + "i" * (len(tokens_raw) // 4), tokens_raw) token_starts = [] total = 0 for doc in docs: token_starts.append(total) total += int(doc["tokens"]) if total != len(tokens): p.error("tokens.i32 length does not match the reference manifest") spans = [(int(d["row0"]), int(d["row0"]) + int(d["tokens"]) - int(d["score_from"]) - 1) for d in docs] top_count = int(ref["top"]) top_raw = (ref_dir / "top.bin").read_bytes() if len(top_raw) != n * top_count * 8: p.error("top.bin has an unexpected size") results = [] for row in selected: doc_index = next((i for i, (lo, hi) in enumerate(spans) if lo <= row < hi), None) if doc_index is None: p.error(f"row {row} is outside every reference document") doc = docs[doc_index] token_pos = int(doc["score_from"]) + row - int(doc["row0"]) begin = token_starts[doc_index] prompt = list(tokens[begin:begin + token_pos + 1]) if not prompt: p.error(f"row {row} produced an empty replay prompt") ref_entries = [struct.unpack_from("