#!/usr/bin/env python3 """Per-arm sums of the PREFILL-PROFILE chunk stages of a replay log, and a paired summary of the arms of one kind. usage: arm_prof.py FULL_LOG [STAGE ...] (default stages: gdn qsa moe inject) Arms are split by `REPLAY-ARM start label=` lines; every `PREFILL-PROFILE first=F tokens=N total_ms=T =ms ...` line is one chunk of the arm that is running. Prints per arm: chunks, tokens, the sum of total_ms and of each stage, and the same for the chunks that start past 100,000 tokens (the position dependent part). The last table pools the arms by the label's letter (B1/B2/B3 -> B) and prints mean and spread over the pooled arms. (docs/upstream-port-plan.md rows 2.4 / 2.5.) """ import re import statistics import sys log = sys.argv[1] stages = sys.argv[2:] or ["gdn", "qsa", "moe", "inject"] arms = [] cur = None for line in open(log, errors="replace"): m = re.search(r"REPLAY-ARM start label=(\S+)", line) if m: cur = {"label": m[1], "chunks": []} arms.append(cur) continue m = re.match(r"PREFILL-PROFILE first=(\d+) tokens=(\d+) total_ms=([\d.]+)", line) if m and cur is not None: row = {"first": int(m[1]), "tokens": int(m[2]), "total": float(m[3])} for s in stages: v = re.search(rf" {re.escape(s)}=([\d.]+)", line) row[s] = float(v[1]) if v else 0.0 cur["chunks"].append(row) def sums(chunks): out = {"chunks": len(chunks), "tokens": sum(c["tokens"] for c in chunks), "total": sum(c["total"] for c in chunks)} for s in stages: out[s] = sum(c[s] for c in chunks) return out cols = ["chunks", "tokens", "total"] + stages print("per arm (ms): " + " ".join(f"{c:>10}" for c in cols)) for a in arms: for name, ch in (("all", a["chunks"]), ("pos>=100k", [c for c in a["chunks"] if c["first"] >= 100_000])): s = sums(ch) print(f"{a['label']:<8} {name:<14} " + " ".join(f"{s[c]:>10.0f}" for c in cols)) print("\npooled by arm letter (mean +- sd over the arms, ms):") kinds = {} for a in arms: kinds.setdefault(re.sub(r"\d+$", "", a["label"]), []).append(a) for k, group in kinds.items(): for name, pick in (("all", lambda a: a["chunks"]), ("pos>=100k", lambda a: [c for c in a["chunks"] if c["first"] >= 100_000])): vals = [sums(pick(a)) for a in group] cells = [] for c in ["total"] + stages: xs = [v[c] for v in vals] cells.append(f"{c} {statistics.mean(xs):.0f}+-{(statistics.stdev(xs) if len(xs) > 1 else 0):.0f}") print(f"{k:<4} n={len(group)} {name:<10} " + " ".join(cells))