"""Plot total and per-request decode throughput vs batch size from results.json.""" import json import re import sys from pathlib import Path import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt res = json.loads(Path(sys.argv[1] if len(sys.argv) > 1 else "results.json").read_text()) out = sys.argv[2] if len(sys.argv) > 2 else "throughput.png" M = res["metrics"] kinds = {n: v["kind"] for n, v in res["environment"]["models"].items()} fig, axes = plt.subplots(1, 3, figsize=(16, 5)) for name in kinds: series = {} for k, v in M.items(): m = re.fullmatch(rf"{re.escape(name)}\.b(\d+)\.(total_decode_tps|per_request_decode_tps|speedup_vs_b1)", k) if m: series.setdefault(m.group(2), {})[int(m.group(1))] = v if not series: continue style = "-o" if kinds[name] == "dense" else "--s" label = f"{name} ({kinds[name]})" for ax, key in zip(axes, ["total_decode_tps", "per_request_decode_tps", "speedup_vs_b1"]): pts = series.get(key, {}) bs = sorted(pts) y = [pts[b]["value"] for b in bs] lo = [pts[b]["value"] - min(pts[b]["samples"] or [pts[b]["value"]]) for b in bs] hi = [max(pts[b]["samples"] or [pts[b]["value"]]) - pts[b]["value"] for b in bs] line = ax.errorbar(bs, y, yerr=[lo, hi], fmt=style, capsize=3, label=label) knee = M.get(f"{name}.knee_batch", {}).get("value") if key == "total_decode_tps" and knee in pts: ax.scatter([knee], [pts[knee]["value"]], s=180, facecolors="none", edgecolors=line[0].get_color(), linewidths=2, zorder=5) # Crossovers: dashed vertical line halfway between the batch sizes where the MoE/dense ratio crosses 1. for k in M: m = re.fullmatch(r"(.+)_vs_(.+)\.b(\d+)\.total_tps_ratio", k) if not m: continue moe, dense, b = m.group(1), m.group(2), int(m.group(3)) ratios = {int(re.search(r"\.b(\d+)\.", kk).group(1)): vv["value"] for kk, vv in M.items() if kk.startswith(f"{moe}_vs_{dense}.b")} nxt = [c for c in sorted(ratios) if c > b][:1] if nxt and (ratios[b] > 1) != (ratios[nxt[0]] > 1): axes[0].axvline((b + nxt[0]) / 2, color="grey", ls=":", lw=1) axes[0].text((b + nxt[0]) / 2, axes[0].get_ylim()[1] * 0.95, f"{moe}\n= dense", fontsize=7, ha="center", va="top") titles = ["Total decode throughput (tok/s)", "Per-request decode throughput (tok/s)", "Speedup vs batch 1 (x)"] for ax, t in zip(axes, titles): ax.set_title(t) ax.set_xlabel("concurrent requests (batch size)") ax.set_xticks([1, 2, 4, 8, 12, 16]) ax.grid(alpha=0.3) axes[0].legend(fontsize=8) fig.suptitle("mlx_lm batched greedy decode, 256 new tokens, ~290-token prompts. Median of repeats, bars = min/max, circle = knee", fontsize=10) fig.tight_layout() fig.savefig(out, dpi=150) print(f"wrote {out}")