File size: 4,620 Bytes
37351b6 | 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 | #!/usr/bin/env python3
from __future__ import annotations
import argparse
import json
from typing import Any, Dict, List, Optional, Tuple
def parse_args() -> argparse.Namespace:
p = argparse.ArgumentParser(description="Compare saved generations across two backends.")
p.add_argument("--generations", required=True, help="Path to generations.jsonl from benchmark_longctx.py")
p.add_argument("--backend-a", required=True)
p.add_argument("--backend-b", required=True)
p.add_argument("--phase", choices=["warmup", "timed"], default=None)
p.add_argument("--run-idx", type=int, default=None)
return p.parse_args()
def normalize_text(x: Any) -> str:
return str(x or "").strip()
def load_rows(path: str, *, phase: Optional[str], run_idx: Optional[int]) -> List[Dict[str, Any]]:
rows: List[Dict[str, Any]] = []
with open(path, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line:
continue
row = json.loads(line)
if phase is not None and row.get("phase") != phase:
continue
row_run_idx = row.get("run_idx", row.get("run_index"))
if run_idx is not None and row_run_idx != run_idx:
continue
rows.append(row)
return rows
def key_for_row(row: Dict[str, Any]) -> Tuple[Any, ...]:
return (
row.get("phase", row.get("run_kind")),
row.get("run_idx", row.get("run_index")),
row.get("batch_id"),
row.get("example_index"),
)
def main() -> None:
args = parse_args()
rows = load_rows(args.generations, phase=args.phase, run_idx=args.run_idx)
by_backend: Dict[str, Dict[Tuple[Any, ...], Dict[str, Any]]] = {
args.backend_a: {},
args.backend_b: {},
}
for row in rows:
backend = str(row.get("backend"))
if backend in by_backend:
by_backend[backend][key_for_row(row)] = row
keys = sorted(set(by_backend[args.backend_a].keys()) & set(by_backend[args.backend_b].keys()))
if not keys:
raise RuntimeError(
f"No overlapping rows found between backend_a={args.backend_a} and backend_b={args.backend_b}."
)
total = 0
exact_text_match = 0
exact_visible_id_match = 0
exact_all_id_match = 0
em_a = 0
em_b = 0
differing_examples: List[Dict[str, Any]] = []
for key in keys:
ra = by_backend[args.backend_a][key]
rb = by_backend[args.backend_b][key]
total += 1
ta = normalize_text(ra.get("generated_text"))
tb = normalize_text(rb.get("generated_text"))
if ta == tb:
exact_text_match += 1
ida = list(ra.get("generated_token_ids_visible", []))
idb = list(rb.get("generated_token_ids_visible", []))
if ida == idb:
exact_visible_id_match += 1
idaa = list(ra.get("generated_token_ids_all", []))
idba = list(rb.get("generated_token_ids_all", []))
if idaa == idba:
exact_all_id_match += 1
if bool(ra.get("exact_match")):
em_a += 1
if bool(rb.get("exact_match")):
em_b += 1
if ta != tb:
differing_examples.append(
{
"key": list(key),
"example_index": ra.get("example_index"),
"batch_id": ra.get("batch_id"),
"text_a": ta,
"text_b": tb,
"exact_match_a": bool(ra.get("exact_match")),
"exact_match_b": bool(rb.get("exact_match")),
}
)
out = {
"backend_a": args.backend_a,
"backend_b": args.backend_b,
"phase": args.phase,
"run_idx": args.run_idx,
"overlapping_examples": total,
"text_match_count": exact_text_match,
"text_match_rate": exact_text_match / total,
"visible_token_id_match_count": exact_visible_id_match,
"visible_token_id_match_rate": exact_visible_id_match / total,
"all_token_id_match_count": exact_all_id_match,
"all_token_id_match_rate": exact_all_id_match / total,
"backend_a_exact_match_count": em_a,
"backend_a_exact_match_rate": em_a / total,
"backend_b_exact_match_count": em_b,
"backend_b_exact_match_rate": em_b / total,
"num_differing_text_examples": len(differing_examples),
"sample_differences": differing_examples[:20],
}
print(json.dumps(out, indent=2, ensure_ascii=False))
if __name__ == "__main__":
main()
|