#!/usr/bin/env python3 """Analysis of windowpolicy-ab.sh runs (arms a, b and optionally c): per run all-turn tok/s (tokens over seconds), tok/round, ms/round, verified rows per round, accepted per round, chain steps per round; per arm mean/sd; block-paired differences b - a, c - a, c - b with the per-block values. python3 -I windowpolicy-analyze.py [ ...] A run directory is /bp

-[tag]; VALIDITY.txt must start with VALID or the run is listed and left out (a block missing an arm drops out of the pairs it needs). tok/s = total output tokens / total decode seconds (decode seconds of a turn = tokens / the client's decode_tok_s) tok/round = tokens / rounds ('rounds=' of the request outcome lines); ms/round = decode seconds / rounds * 1000 rows/round, accepted/round, chain steps/round: the profile spans round.T, round.acc, mtp.chain_step (count) of the request profile lines, summed over the requests / rounds """ import glob, json, math, os, re, statistics, sys SPANS = ["round.run_window", "round.apply_pending", "round.draft_committed", "commit.wait", "mtp.finish_first_step", "mtp.chain_step"] def run_stats(d): j = json.load(open(os.path.join(d, "long-session.json"))) toks = [r["output_tokens"] for r in j] secs = [r["output_tokens"] / r["decode_tok_s"] for r in j] rounds, prof = [], [] cur = None for ln in open(os.path.join(d, "inference.log"), errors="replace"): if ln.startswith("DBG profile request:"): cur = {m[0]: (float(m[1]), int(m[2])) for m in re.findall(r"(\S+?)=([\d.]+)/(\d+)", ln)} elif "leleka-serve request outcome" in ln: rounds.append(int(re.search(r"rounds=(\d+)", ln).group(1))) prof.append(cur or {}) cur = None R = sum(rounds) out = {"turns": [r["decode_tok_s"] for r in j], "tok_s": sum(toks) / sum(secs), "tok_round": sum(toks) / R, "ms_round": sum(secs) / R * 1000, "rounds": R} out["rows_round"] = sum(p.get("round.T", (0, 0))[0] for p in prof) / R out["acc_round"] = sum(p.get("round.acc", (0, 0))[0] for p in prof) / R out["steps_round"] = sum(p.get("mtp.chain_step", (0, 0))[1] for p in prof) / R for s in SPANS: out[s] = sum(p.get(s, (0.0, 0))[0] for p in prof) / R return out def sd(v): return statistics.stdev(v) if len(v) > 1 else float("nan") def main(): runs = {} for root in sys.argv[1:]: for d in sorted(glob.glob(os.path.join(root, "b*p*-*"))): m = re.match(r"b(\d+)p(\d+)-([abc])(.*)", os.path.basename(d)) if not os.path.isdir(d) or not m: continue ver = open(os.path.join(d, "VALIDITY.txt")).read().strip() if os.path.exists(os.path.join(d, "VALIDITY.txt")) else "no VALIDITY" if not ver.startswith("VALID"): print("%s %s (left out)" % (os.path.basename(d), ver)) continue try: runs[(root, int(m.group(1)), int(m.group(2)), m.group(3))] = run_stats(d) except Exception as e: # noqa print("%s analysis failed: %s" % (os.path.basename(d), e)) print("%-26s %-5s %-26s %8s %8s %8s %8s" % ("run", "pos", "turns tok/s", "tok/s", "tok/rnd", "ms/rnd", "rows/rnd")) for (root, b, p, arm), s in sorted(runs.items(), key=lambda kv: (kv[0][1], kv[0][2])): print("%-26s b%02dp%d %-26s %8.1f %8.3f %8.3f %8.3f" % (os.path.basename(root.rstrip("/")) + "/" + arm, b, p, " ".join("%.0f" % t for t in s["turns"]), s["tok_s"], s["tok_round"], s["ms_round"], s["rows_round"])) arms = {} for (root, b, p, arm), s in runs.items(): arms.setdefault(arm, []).append(s) print() for arm, ss in sorted(arms.items()): print("arm %s n=%d tok/s %.2f (sd %.2f, %.1f..%.1f) tok/round %.3f (sd %.3f) ms/round %.3f (sd %.3f) rows/round %.3f accepted/round %.3f chain steps/round %.3f" % ( arm, len(ss), statistics.mean(s["tok_s"] for s in ss), sd([s["tok_s"] for s in ss]), min(s["tok_s"] for s in ss), max(s["tok_s"] for s in ss), statistics.mean(s["tok_round"] for s in ss), sd([s["tok_round"] for s in ss]), statistics.mean(s["ms_round"] for s in ss), sd([s["ms_round"] for s in ss]), statistics.mean(s["rows_round"] for s in ss), statistics.mean(s["acc_round"] for s in ss), statistics.mean(s["steps_round"] for s in ss))) print("\nspans, ms per round (mean over the arm's runs):") print("%-26s" % "span", *["%10s" % a for a in sorted(arms)]) for sp in SPANS: print("%-26s" % sp, *["%10.3f" % statistics.mean(s[sp] for s in ss) for a, ss in sorted(arms.items())]) byb = {} for (root, b, p, arm), s in runs.items(): byb.setdefault((root, b), {})[arm] = s for x, y in (("a", "b"), ("a", "c"), ("b", "c")): pairs = [(k, v[x], v[y]) for k, v in sorted(byb.items(), key=lambda kv: kv[0][1]) if x in v and y in v] if len(pairs) < 2: continue print("\nblock-paired, %s - %s (n=%d blocks: %s):" % (y, x, len(pairs), " ".join(str(k[1]) for k, _, _ in pairs))) for key, name in (("tok_s", "tok/s"), ("tok_round", "tok/round"), ("ms_round", "ms/round"), ("rows_round", "rows/round"), ("steps_round", "steps/round")): d = [pb[key] - pa[key] for _, pa, pb in pairs] rel = [100.0 * (pb[key] - pa[key]) / pa[key] for _, pa, pb in pairs] se = sd(d) / math.sqrt(len(d)) print(" %-11s per block %s mean %+.3f (sd %.3f, se %.3f) %+.2f %% %s better in %d of %d" % ( name, " ".join("%+.3f" % v for v in d), statistics.mean(d), sd(d), se, statistics.mean(rel), y, sum(1 for v in d if v > 0), len(d))) if __name__ == "__main__": main()