Download scripts/bench/score_exp2.py from jaytonde05/kv-cache-quantization-commands: direct link, hf CLI and curl.
- Browser
- Download file 4.72 kB
-
https://huggingface.co/datasets/jaytonde05/kv-cache-quantization-commands/resolve/main/scripts/bench/score_exp2.py
- Command line
-
hf download hf://datasets/jaytonde05/kv-cache-quantization-commands/scripts/bench/score_exp2.py
-
curl -L -o score_exp2.py https://huggingface.co/datasets/jaytonde05/kv-cache-quantization-commands/resolve/main/scripts/bench/score_exp2.py
4.72 kB
| """Score Exp 2 (RULER 6 tasks: 4K/32K/128K x 180) and compare runs with paired statistics. | |
| Usage: python score_exp2.py # all comparisons whose result files exist | |
| python score_exp2.py --by-task # also break RULER down by task | |
| Each run folder ~/bench/exp2/<RUN>/ holds vllm bench serve results whose | |
| generated_texts[i] belongs to JSONL row i (inputs were unshuffled). | |
| """ | |
| import argparse | |
| import json | |
| import os | |
| import random | |
| import re | |
| from pathlib import Path | |
| B = Path.home() / "bench" | |
| EXP = B / os.environ.get("KVQ_EXP_OUT", "exp2") # results folder (exp2_gemma for Gemma) | |
| DATA = {"ruler_4k": "ruler_4k_6t.jsonl", "ruler_32k": "ruler_32k_6t.jsonl", "ruler_128k": "ruler_128k_6t.jsonl", | |
| "docvqa": "docvqa_1000.jsonl"} | |
| # (treatment, baseline, what the difference measures) | |
| PAIRS = [("BF16_rep2", "BF16", "noise floor (FA)"), | |
| ("BF16-FI_rep2", "BF16-FI", "noise floor (FI)"), | |
| ("BF16-FI", "BF16", "kernel effect"), | |
| ("FP8", "BF16", "FP8 KV effect"), | |
| ("FP4", "BF16-FI", "NVFP4 KV effect"), | |
| ("FP8", "BF16-TFA", "FP8 KV, same kernel")] # Gemma DocVQA: both on TRITON_FLASH_ATTN | |
| def lev(a, b): | |
| prev = list(range(len(b) + 1)) | |
| for i, ca in enumerate(a, 1): | |
| cur = [i] | |
| for j, cb in enumerate(b, 1): | |
| cur.append(min(prev[j] + 1, cur[j - 1] + 1, prev[j - 1] + (ca != cb))) | |
| prev = cur | |
| return prev[-1] | |
| def anls(pred, answers): | |
| p = pred.strip().lower() | |
| best = 0.0 | |
| for g in answers: | |
| g = g.strip().lower() | |
| nl = lev(p, g) / max(len(p), len(g), 1) | |
| best = max(best, 1 - nl if nl < 0.5 else 0.0) | |
| return best | |
| def score(ds, row, text): | |
| if ds.startswith("ruler"): | |
| return sum(a.lower() in text.lower() for a in row["answers"]) / len(row["answers"]) | |
| lines = [l for l in text.strip().split("\n") if l.strip()] | |
| return anls(re.sub(r"[*`]", "", lines[-1]) if lines else "", row["answers"]) | |
| _cache = {} | |
| def scores(run, ds): | |
| key = (run, ds) | |
| if key not in _cache: | |
| f = EXP / run / f"{ds}.json" | |
| if not f.exists(): | |
| _cache[key] = None | |
| else: | |
| rows = [json.loads(l) for l in open(B / os.environ.get("KVQ_DATA", "data") / DATA[ds])] | |
| res = json.loads(f.read_text()) | |
| errs = sum(1 for e in res.get("errors", []) if e) | |
| s = [score(ds, r, t) for r, t in zip(rows, res["generated_texts"])] | |
| _cache[key] = (s, rows, errs) | |
| return _cache[key] | |
| def paired_ci(a, b, iters=10000, seed=0): | |
| d = [x - y for x, y in zip(a, b)] | |
| n = len(d) | |
| rng = random.Random(seed) | |
| means = sorted(sum(d[rng.randrange(n)] for _ in range(n)) / n for _ in range(iters)) | |
| return sum(d) / n, means[int(0.025 * iters)], means[int(0.975 * iters)] | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--by-task", action="store_true") | |
| a = ap.parse_args() | |
| runs = sorted(p.name for p in EXP.iterdir() if p.is_dir()) | |
| print("Absolute scores (RULER: fraction of answers found; DocVQA: ANLS)") | |
| print(f"{'run':<16}" + "".join(f"{d:>11}" for d in DATA)) | |
| for r in runs: | |
| line = f"{r:<16}" | |
| for d in DATA: | |
| s = scores(r, d) | |
| line += f"{'-':>11}" if s is None else f"{100 * sum(s[0]) / len(s[0]):>9.2f}" + (f"e{s[2]}" if s[2] else " ") | |
| print(line) | |
| print("\nPaired differences (treatment - baseline, points), 95% bootstrap CI, flips right->wrong / wrong->right") | |
| for t, base, label in PAIRS: | |
| for d in DATA: | |
| st, sb = scores(t, d), scores(base, d) | |
| if not st or not sb: | |
| continue | |
| diff, lo, hi = paired_ci(st[0], sb[0]) | |
| rw = sum(1 for x, y in zip(st[0], sb[0]) if y == 1 and x < 1) | |
| wr = sum(1 for x, y in zip(st[0], sb[0]) if y < 1 and x == 1) | |
| print(f"{label:<17} {t:>15} vs {base:<10} {d:<11} {100 * diff:+6.2f} [{100 * lo:+6.2f}, {100 * hi:+6.2f}] flips {rw}/{wr}") | |
| if a.by_task: | |
| print("\nRULER by task (mean over all lengths)") | |
| tasks = sorted({row["task"] for row in scores(runs[0], "ruler_4k")[1]}) if scores(runs[0], "ruler_4k") else [] | |
| print(f"{'task':<18}" + "".join(f"{r:>16}" for r in runs)) | |
| for task in tasks: | |
| line = f"{task:<18}" | |
| for r in runs: | |
| vals = [] | |
| for d in [k for k in DATA if k.startswith("ruler")]: | |
| s = scores(r, d) | |
| if s: | |
| vals += [x for x, row in zip(s[0], s[1]) if row["task"] == task] | |
| line += f"{100 * sum(vals) / len(vals):>16.2f}" if vals else f"{'-':>16}" | |
| print(line) | |
| if __name__ == "__main__": | |
| main() | |