Files

81 lines
4.0 KiB
Python

#!/usr/bin/env python3
"""Offline: join the client's per-turn timings of `cp_replay --live` with the servers' logs (experiments only).
live_report.py LIVE.txt ARM=SERVER_LOG [ARM=SERVER_LOG ...]
LIVE.txt holds `LIVE {json}` lines (one per request, with the client's unix-ms send/done times). Each SERVER_LOG is the stderr of a
`leleka-api serve` run with STRATA_STAGE_TRACE=log: `stage NAME MS t_unix_ms=END`, `generation start: t_unix_ms=T ...` and
`generation done: ... prompt_ms=P ...` lines. A turn is joined with the lines of its arm's log that fall inside [send, done].
Per turn the client's time to the first token splits into
pre = send -> `generation start` (everything the control plane does before it hands the prompt to the engine)
engine= `generation start` -> first text delta as the worker saw it (`prompt_ms`: engine start call, prefill, first round)
post = the rest (first delta at the worker -> first token event at the client)
"""
import json
import re
import statistics
import sys
PRE_STAGES = ["http_body_json", "request_decode", "request_validate", "render", "tokenize", "enqueue", "worker_prompt_text", "engine_start"]
def load_log(path):
stages, starts, dones = [], [], []
for line in open(path, errors="replace"):
m = re.match(r"stage (\S+) ([\d.]+) t_unix_ms=(\d+)", line)
if m:
stages.append((int(m.group(3)), m.group(1), float(m.group(2))))
continue
m = re.match(r"generation start: t_unix_ms=(\d+)", line)
if m:
starts.append(int(m.group(1)))
continue
m = re.match(r"generation done: t_unix_ms=(\d+).*prompt_tokens=(\d+) reused=(\d+).*prompt_ms=([\d.]+) decode_ms=([\d.]+)", line)
if m:
dones.append((int(m.group(1)), int(m.group(2)), int(m.group(3)), float(m.group(4))))
return stages, starts, dones
def pct(values, q):
v = sorted(values)
return v[round(q * (len(v) - 1))] if v else float("nan")
def main():
live = [json.loads(l[5:]) for l in open(sys.argv[1]) if l.startswith("LIVE ")]
logs = {}
for spec in sys.argv[2:]:
arm, path = spec.split("=", 1)
logs[arm] = load_log(path)
rows = []
for turn in live:
stages, starts, dones = logs[turn["arm"]]
lo, hi = turn["sent_unix_ms"] - 5, turn["done_unix_ms"] + 50
window = [(n, ms) for t, n, ms in stages if lo <= t <= hi]
start = next((t for t in starts if lo <= t <= hi), None)
done = next((d for d in dones if lo <= d[0] <= hi), None)
stage_sum = {name: sum(ms for n, ms in window if n == name) for name in PRE_STAGES}
pre = (start - turn["sent_unix_ms"]) if start else float("nan")
engine = done[3] if done else float("nan")
post = turn["ttft_ms"] - pre - engine
rows.append({**turn, "pre": pre, "engine": engine, "post": post, "stages": stage_sum,
"reused": done[2] if done else 0, "prompt_tokens": done[1] if done else 0})
print("turn arm round prompt reused ttft_ms created pre_ms engine_ms post_ms total_ms terminal")
for r in rows:
print(f"{r['turn']:>4} {r['arm']:<5} {r['round']:>5} {r['prompt_tokens']:>7} {r['reused']:>7} {r['ttft_ms']:>8.1f} {r['created_ms']:>8.1f} {r['pre']:>7.1f} {r['engine']:>10.1f} {r['post']:>8.1f} {r['total_ms']:>9.1f} {r['terminal']}")
print("\nper arm, turns after the first of each arm (median / p95), ms:")
for arm in logs:
mine = [r for r in rows if r["arm"] == arm][1:]
if not mine:
continue
def cell(key):
values = [r[key] for r in mine]
return f"{statistics.median(values):8.1f} / {pct(values, 0.95):8.1f}"
print(f" {arm:<6} n={len(mine):<3} ttft {cell('ttft_ms')} created {cell('created_ms')} pre {cell('pre')} engine {cell('engine')} post {cell('post')}")
names = ", ".join(f"{k}={statistics.median(r['stages'][k] for r in mine):.2f}" for k in PRE_STAGES)
print(f" median pre-engine stages: {names}")
main()