| """Render the baseline comparison figure (paper fig 7).""" |
|
|
| import argparse |
| import json |
| import os |
| import sys |
|
|
| import matplotlib as mpl |
| import matplotlib.pyplot as plt |
| import numpy as np |
|
|
| sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) |
|
|
| mpl.rcParams.update({ |
| "font.family": "sans-serif", |
| "font.sans-serif": ["Inter", "Helvetica Neue", "Arial", "DejaVu Sans"], |
| "font.size": 9, |
| "axes.labelsize": 10, |
| "xtick.labelsize": 8.5, |
| "ytick.labelsize": 8.5, |
| "legend.fontsize": 8, |
| "legend.frameon": False, |
| "figure.dpi": 200, |
| "savefig.dpi": 400, |
| "savefig.bbox": "tight", |
| "pdf.fonttype": 42, |
| "ps.fonttype": 42, |
| "axes.linewidth": 0.7, |
| "axes.spines.top": False, |
| "axes.spines.right": False, |
| }) |
|
|
| |
| STRATEGIES = [ |
| ("random", "Random\n(no diagnosis)", "#B0B7C3"), |
| ("failed_only", "Failed only\n(no type balancing)", "#F0A357"), |
| ("silver_bullet", "Silver bullet\n(ours)", "#2E7D32"), |
| ] |
|
|
| GREY_REF = "#999999" |
|
|
|
|
| def _bootstrap(jsonl_path, n_boot=5000): |
| if not os.path.exists(jsonl_path): |
| return None |
| v = [] |
| with open(jsonl_path) as f: |
| for line in f: |
| t = json.loads(line) |
| v.append(1.0 if t.get("is_correct_final") else 0.0) |
| if not v: |
| return None |
| v = np.array(v) |
| rng = np.random.default_rng(0) |
| s = np.empty(n_boot) |
| for i in range(n_boot): |
| idx = rng.integers(0, len(v), size=len(v)) |
| s[i] = v[idx].mean() |
| return float(v.mean()), float(np.percentile(s, 2.5)), float(np.percentile(s, 97.5)) |
|
|
|
|
| def _paired_p(base_out, rest_out, n_boot=5000): |
| common = sorted(set(base_out) & set(rest_out)) |
| if not common: |
| return None |
| b = np.array([base_out[k] for k in common]) |
| r = np.array([rest_out[k] for k in common]) |
| rng = np.random.default_rng(0) |
| deltas = np.empty(n_boot) |
| for i in range(n_boot): |
| idx = rng.integers(0, len(common), size=len(common)) |
| deltas[i] = r[idx].mean() - b[idx].mean() |
| return float(2 * min((deltas <= 0).mean(), (deltas >= 0).mean())) |
|
|
|
|
| def _load_outcomes(jsonl_path): |
| if not os.path.exists(jsonl_path): |
| return None |
| out = {} |
| with open(jsonl_path) as f: |
| for line in f: |
| t = json.loads(line) |
| out[t.get("problem_id")] = 1.0 if t.get("is_correct_final") else 0.0 |
| return out |
|
|
|
|
| def _stars(p): |
| if p is None: return "" |
| if p < 0.001: return "***" |
| if p < 0.01: return "**" |
| if p < 0.05: return "*" |
| return "" |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser() |
| parser.add_argument("--baseline-root", required=True) |
| parser.add_argument("--model", required=True) |
| parser.add_argument("--quant", required=True) |
| parser.add_argument("--benchmark", required=True) |
| parser.add_argument("--metrics", required=True) |
| parser.add_argument("--segmented", default="results/segmented") |
| parser.add_argument("--output", required=True) |
| args = parser.parse_args() |
|
|
| base_out = _load_outcomes(os.path.join("results", "diagnosis", |
| args.quant, args.model, |
| f"{args.benchmark}_run0.jsonl")) |
|
|
| names, means, los, his, colors, stars = [], [], [], [], [], [] |
| for strat_key, strat_label, color in STRATEGIES: |
| diag = os.path.join(args.baseline_root, strat_key, "diagnosis", |
| f"{args.benchmark}_run0.jsonl") |
| ci = _bootstrap(diag) |
| if ci is None: |
| continue |
| p = None |
| if base_out: |
| rest_out = _load_outcomes(diag) |
| if rest_out: |
| p = _paired_p(base_out, rest_out) |
| names.append(strat_label) |
| means.append(ci[0] * 100); los.append(ci[1] * 100); his.append(ci[2] * 100) |
| colors.append(color); stars.append(_stars(p)) |
|
|
| if not names: |
| print("No baseline results found.") |
| return |
|
|
| |
| base_path = os.path.join(args.metrics, |
| f"{args.model}_{args.quant}_{args.benchmark}_run0_metrics.json") |
| base_acc = json.load(open(base_path))["accuracy"] * 100 if os.path.exists(base_path) else None |
|
|
| from eval_accuracy import accuracy as _lv_acc |
| fp16_jsonl = os.path.join(args.segmented, "fp16", args.model, |
| f"{args.benchmark}_run0.jsonl") |
| fp16_v = _lv_acc(fp16_jsonl, args.benchmark) |
| fp16_acc = fp16_v * 100 if fp16_v else None |
|
|
| fig, ax = plt.subplots(figsize=(5.3, 3.2), constrained_layout=True) |
|
|
| x = np.arange(len(names)) |
| width = 0.5 |
|
|
| |
| if base_acc is not None and fp16_acc is not None: |
| ax.axhspan(base_acc, fp16_acc, color="#EEEEEE", alpha=1.0, zorder=0) |
|
|
| for i, (m, lo, hi, c, s) in enumerate(zip(means, los, his, colors, stars)): |
| ax.bar(x[i], m, width, color=c, edgecolor="white", linewidth=0.9, |
| zorder=2) |
| |
| ax.plot([x[i], x[i]], [lo, hi], color="#333333", linewidth=1.0, zorder=3, |
| solid_capstyle="butt") |
| |
| ax.annotate(f"{m:.1f}", xy=(x[i], m), xytext=(0, 5), |
| textcoords="offset points", ha="center", va="bottom", |
| fontsize=9.5, color="#222", fontweight="bold") |
| |
| if s: |
| ax.annotate(s, xy=(x[i], hi), xytext=(0, 4), |
| textcoords="offset points", ha="center", va="bottom", |
| fontsize=11, color=c, fontweight="bold") |
|
|
| |
| |
| |
| if base_acc is not None: |
| ax.axhline(base_acc, color=GREY_REF, linestyle=(0, (5, 3)), |
| linewidth=1.0, zorder=1) |
| ax.text(1.02, base_acc, f"Quantized\n{base_acc:.1f}%", |
| transform=ax.get_yaxis_transform(), |
| ha="left", va="center", fontsize=7.5, color=GREY_REF) |
| if fp16_acc is not None: |
| ax.axhline(fp16_acc, color="#333", linestyle=(0, (1, 2)), |
| linewidth=1.0, zorder=1) |
| ax.text(1.02, fp16_acc, f"FP16\n{fp16_acc:.1f}%", |
| transform=ax.get_yaxis_transform(), |
| ha="left", va="center", fontsize=7.5, color="#333") |
|
|
| ax.set_xticks(x) |
| ax.set_xticklabels(names) |
| ax.set_ylabel("Accuracy (%)") |
| ax.yaxis.grid(True, linewidth=0.4, color="#DDDDDD") |
| ax.set_axisbelow(True) |
|
|
| ys = means + los + his + [v for v in (base_acc, fp16_acc) if v is not None] |
| ax.set_ylim(min(ys) - 3, max(ys) + 5) |
| ax.set_xlim(-0.55, len(names) - 0.45) |
|
|
| pretty_quant = {"awq_w4": "AWQ w4", "gptq_w4": "GPTQ w4", "bnb_nf4_w4": "BnB NF4"}.get(args.quant, args.quant) |
| ax.text(1.0, 1.02, f"{args.model} · {pretty_quant} · {args.benchmark}", |
| transform=ax.transAxes, ha="right", va="bottom", |
| fontsize=8, color="#555") |
|
|
| |
| fig.text(0.02, -0.03, |
| r"Paired-bootstrap $p$ vs. quantized baseline: $*$: $p<.05$ $**$: $p<.01$ $***$: $p<.001$", |
| ha="left", va="top", fontsize=7, color="#555") |
|
|
| os.makedirs(os.path.dirname(args.output), exist_ok=True) |
| fig.savefig(args.output) |
| plt.close(fig) |
| print(f" Paper fig 7 (baselines) saved: {args.output}") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|