diff --git a/scripts/quality/q_analyze.py b/scripts/quality/q_analyze.py index 1e563dad..663f3c99 100644 --- a/scripts/quality/q_analyze.py +++ b/scripts/quality/q_analyze.py @@ -180,6 +180,7 @@ def main(argv): q_port.verdict(q, out, env, result) q_port.pooled(q, out, result, ref_run) q_port.long_section(q, out, result) + q_port.retrieval_section(q, out, result) open_md ="\n".join(out) print(open_md) if md_out: open(md_out, "w").write(open_md + "\n") diff --git a/scripts/quality/q_port.py b/scripts/quality/q_port.py index ed22bc70..8180d669 100644 --- a/scripts/quality/q_port.py +++ b/scripts/quality/q_port.py @@ -321,3 +321,74 @@ def pooled(q, out, result, ref_run="R1-static"): if pp_rows: result["pool_port_port"] = line(f"port arms vs {base} (port's own envelope)", pp_rows) out.append("") + + +def parse_long_multi(path): + """[(prompt_tokens, windows, output)] of a port long-smoke log with several prompts.""" + import re + from q_lib import parse_row + segs, cur, win = [], None, None + for line in open(path, errors="replace"): + m = re.match(r"long-smoke prompt tokens=(\d+)", line) + if m: + cur = {"n": int(m[1]), "windows": [], "output": None} + segs.append(cur) + win = None + continue + if cur is None: + continue + m = re.match(r"DBG logprobs window position=(\d+) inputs=\[([0-9, -]*)\]", line) + if m: + win = (int(m[1]), [int(x) for x in m[2].split(",") if x.strip()], []) + cur["windows"].append(win) + continue + m = re.match(r"long-smoke output ids \(\d+\): ([0-9,]*)", line) + if m and cur["output"] is None: + cur["output"] = [int(x) for x in m[1].split(",") if x] + continue + r = parse_row(line) + if r is not None and win is not None and len(win[2]) < len(win[1]): + win[2].append(r) + return segs + + +def retrieval_section(q, out, result): + """Teacher-forced comparison on the first <=120 tokens of the Original's 100k / 180k answers, and the divergence rows.""" + import os + from q_lib import orig_reads + run = "RET-orig-read" + pf = os.path.join(q.d, "runs", "RET-port-forced", "port.log") + if run not in q.reads or not os.path.exists(pf): + return + import re + reqs = [n for n in open(os.path.join(q.d, "runs", run, "reqs.txt")).read().split("\n") if n and n != "QUIT"] + from q_lib import load_orig_logpos + segs, _ = load_orig_logpos(os.path.join(q.d, "runs", run)) + # only the answer reads log rows (the batched prompt reads do not): segments are 100k+answer, 180k+answer + orig = {100000: segs[0] if segs else None, 180000: segs[1] if len(segs) > 1 else None} + ports = {s["n"]: s for s in parse_long_multi(pf)} + out.append("## R. Long-context retrieval: teacher-forced along the Original's answers\n") + out.append("Original: prompt read batched (chunk 8192), the answer through the verify windows; port: forced pass, int8 KV streamed, chunk 8192. Rows from the last prompt token on.\n") + out.append("| prompt | answer tokens forced | rows compared | d_top1 med / p99 / max | dmax16 p99 / max | flips | Original margin at the flips | stop token lp/rank (Original; port) |") + out.append("|---|---|---|---|---|---|---|---|") + for n in (100000, 180000): + o, p = orig.get(n), ports.get(n) + if not o or not p: + continue + ans = [int(x) for x in open(os.path.join(q.d, "long-force", f"answer{n}.ids")).read().split(",")] if os.path.exists(os.path.join(q.d, "long-force", f"answer{n}.ids")) else [] + # the path: prompt ids from the long-180k dir are not in cases; take them from the Original request line + line = [l for l in open(os.path.join(q.d, "runs", run, "reqs.txt")).read().split("\n") if l.startswith("GEN 1 ")][1 if n == 100000 else 3] + path = [int(x) for x in line[len("GEN 1 "):].split(",")] + rows = valid_rows(p["windows"], path) + lo, hi = n - 1, len(path) - 2 + cmp_ = compare(o, rows, lo, hi) + if not cmp_: + continue + s = summary(cmp_) + result[f"retrieval_{n}"] = s + stop = len(path) - 2 + eos = (o.get(stop), rows.get(stop)) + cell = " ; ".join(f"{fmt(r.lp(path[-1])) if r else '-'} / {r.rank(path[-1]) if r else '-'}" for r in eos) + out.append(f"| {n} | {len(path) - n} | {s['n']} | {fmt(s['d_top1']['median'])} / {fmt(s['d_top1']['p99'])} / {fmt(s['d_top1']['max'])} | " + f"{fmt(s['dmax']['p99'])} / {fmt(s['dmax']['max'])} | {s['flips']} | {', '.join(map(str, s['flip_margins'])) or '-'} | {cell} |") + out.append("")