Files
2026-11-13 07:08:50 +01:00

94 lines
4.4 KiB
Python

#!/usr/bin/env python3
"""Host spans of the decode round per arm (offline analysis, stdlib only; never part of serving).
host_round_spans.py RUN_DIR BASE VAR [--skip 3] [--spans a,b,c]
RUN_DIR holds one directory per block named NAME.BLOCK (written by host_round_ab_job.sh, profile on: engine.log has one
`DBG profile request: name=total_ms/count ...` line per request). For every block the spans are summed over the requests after
the first --skip (warm-up) and divided by the block's rounds (the count of `round.verify_commit`): ms per round. The report gives, per span,
the mean of BASE blocks, the mean of VAR blocks, the paired difference VAR - BASE (blocks paired by number) with a two-sided 95 % t interval,
and the spread between the BASE blocks themselves (the noise the difference has to be read against).
"""
import math
import os
import re
import sys
T975 = {1: 12.706, 2: 4.303, 3: 3.182, 4: 2.776, 5: 2.571, 6: 2.447, 7: 2.365, 8: 2.306, 9: 2.262}
DEFAULT = ("round.apply_pending", "window.ple", "ple.ahead", "ple.ahead_hit", "round.plan", "round.checkpoints", "window.verifier",
"round.run_window", "round.verify_commit", "round.emit", "draft.launch", "draft.finish", "round.draft_committed",
"commit.wait", "commit.publish", "round.adapt_deferred", "round.adopt", "round.finish", "round.T", "round.acc")
TOKEN = re.compile(r"([A-Za-z0-9_.]+)=(-?[0-9.]+)/(\d+)")
def block_sums(path, skip):
"""Total ms and count of every span over the requests after `skip`."""
sums, counts, seen = {}, {}, 0
for line in open(path, errors="replace"):
if "DBG profile request:" not in line:
continue
seen += 1
if seen <= skip:
continue
for name, total, count in TOKEN.findall(line.split("DBG profile request:", 1)[1]):
sums[name] = sums.get(name, 0.0) + float(total)
counts[name] = counts.get(name, 0) + int(count)
return sums, counts
def per_round(directory, skip):
sums, counts = block_sums(os.path.join(directory, "engine.log"), skip)
rounds = counts.get("round.verify_commit", 0)
if rounds == 0:
return None
out = {name: total / rounds for name, total in sums.items()}
out["rounds"] = rounds
out["tokens_per_round"] = (sums.get("round.acc", 0.0) + rounds) / rounds if "round.acc" in sums else float("nan")
return out
def interval(xs):
n = len(xs)
mean = sum(xs) / n
if n < 2:
return mean, float("nan")
sd = math.sqrt(sum((x - mean) ** 2 for x in xs) / (n - 1))
return mean, T975.get(n - 1, 1.96) * sd / math.sqrt(n)
def blocks(run_dir, name):
return {d.split(".", 1)[1]: os.path.join(run_dir, d) for d in sorted(os.listdir(run_dir))
if d.startswith(name + ".") and os.path.exists(os.path.join(run_dir, d, "engine.log"))
and not os.path.exists(os.path.join(run_dir, d, "INVALID"))}
def main(argv):
skip, spans = 3, DEFAULT
args = [a for a in argv[1:] if not a.startswith("--")]
if "--skip" in argv:
skip = int(argv[argv.index("--skip") + 1])
args = [a for a in args if a != str(skip)]
if "--spans" in argv:
spans = tuple(argv[argv.index("--spans") + 1].split(","))
args = [a for a in args if a != argv[argv.index("--spans") + 1]]
run_dir, base_name, var_name = args[:3]
base, var = blocks(run_dir, base_name), blocks(run_dir, var_name)
paired = sorted(set(base) & set(var))
print(f"{run_dir}: {base_name} blocks {sorted(base)}, {var_name} blocks {sorted(var)}, paired {paired}; first {skip} requests dropped")
base_rows = {k: per_round(v, skip) for k, v in base.items()}
var_rows = {k: per_round(v, skip) for k, v in var.items()}
print(f"{'span (ms per round)':<26}{base_name:>10}{var_name:>10}{'diff':>10}{'+-95%':>9}{'base spread':>13}")
for name in spans + ("tokens_per_round", "rounds"):
b = [r[name] for r in base_rows.values() if r and name in r]
v = [r[name] for r in var_rows.values() if r and name in r]
if not b or not v:
continue
diffs = [var_rows[k][name] - base_rows[k][name] for k in paired if base_rows[k] and var_rows[k] and name in base_rows[k] and name in var_rows[k]]
mean, half = interval(diffs) if diffs else (float("nan"), float("nan"))
spread = (max(b) - min(b)) if len(b) > 1 else float("nan")
print(f"{name:<26}{sum(b) / len(b):>10.3f}{sum(v) / len(v):>10.3f}{mean:>10.3f}{half:>9.3f}{spread:>13.3f}")
if __name__ == "__main__":
main(sys.argv)