Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
116 lines
6.1 KiB
Python
116 lines
6.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Per-request split of real Codex sessions of the codex-compare run (offline analysis, stdlib only; never part of serving).
|
|
|
|
session_split.py RUNS_ROOT VARIANT [VARIANT ...]
|
|
|
|
For every cell RUNS_ROOT/<VARIANT>/<task>-r<k> (tasks ttt and nestng-crm) the proxy log `proxy/rounds.jsonl` gives per request the wall time in the proxy (t_end - t_start),
|
|
the output tokens and the pause before the next request (tool execution + Codex); the engine log of the stack, sliced from `engine_log_start_offset` of the cell, gives the
|
|
engine's own prefill and decode milliseconds (LEL: JSON `request` events; vanilla: `strata serve: prompt N tokens = R reused + P read in X ms ..., G generated in Y ms` lines).
|
|
The two are joined by order. Printed per variant and task: mean ms per request of proxy time, prefill, decode, remainder (= proxy - prefill - decode: tokenizer, HTTP, prefix-cache
|
|
work, SSE, response end), pause between requests, and the same per output token.
|
|
"""
|
|
import glob
|
|
import json
|
|
import os
|
|
import re
|
|
import sys
|
|
|
|
VAN = re.compile(r"strata serve: prompt (\d+) tokens = (\d+) reused \+ (\d+) read in (\d+) ms.*?, (\d+) generated in (\d+) ms")
|
|
|
|
|
|
def engine_events(path, offset, variant, n):
|
|
out = []
|
|
with open(path, "rb") as f:
|
|
f.seek(offset)
|
|
for raw in f:
|
|
line = raw.decode("utf-8", "replace")
|
|
if variant == "LEL":
|
|
if '"ev":"request"' in line and '"comp":"engine"' in line:
|
|
j = json.loads(line)
|
|
out.append({"ptok": j["ptok"], "cached": j["ctok"], "otok": j["otok"], "pf": j["pf_ms"], "dec": j["dec_ms"]})
|
|
else:
|
|
m = VAN.search(line)
|
|
if m:
|
|
out.append({"ptok": int(m[1]) , "cached": int(m[2]), "otok": int(m[5]), "pf": float(m[4]), "dec": float(m[6])})
|
|
if len(out) >= n:
|
|
break
|
|
return out
|
|
|
|
|
|
def cell(d, variant):
|
|
rounds = [json.loads(l) for l in open(os.path.join(d, "proxy", "rounds.jsonl"))]
|
|
rounds = [r for r in rounds if r.get("path", "").endswith("/responses") and r.get("status") == 200]
|
|
info = json.load(open(os.path.join(d, "cell.json")))
|
|
rep = info["rep"]
|
|
stack = os.path.join(os.path.dirname(d), "stack-r%s" % rep)
|
|
ev = engine_events(os.path.join(stack, "engine.log"), info["engine_log_start_offset"], variant, len(rounds))
|
|
return rounds, ev, info
|
|
|
|
|
|
def buckets(root, variants):
|
|
"""Engine decode tok/s and prefill ms per request by prompt size (ptok, both tasks together), pause excluded."""
|
|
edges = [0, 20000, 40000, 70000, 100000, 10**9]
|
|
print("prompt tokens bucket | " + " | ".join("%s: n, dec tok/s, pf ms/req, uncached/req" % v for v in variants))
|
|
acc = {v: [dict(n=0, o=0, d=0.0, p=0.0, u=0) for _ in edges[:-1]] for v in variants}
|
|
for v in variants:
|
|
for d in sorted(glob.glob(os.path.join(root, v, "*-r[0-9]"))):
|
|
if not os.path.basename(d).startswith(("ttt", "nestng-crm")):
|
|
continue
|
|
rounds, ev, _ = cell(d, v)
|
|
if len(ev) != len(rounds):
|
|
continue
|
|
for e in ev:
|
|
if e["ptok"] < 200:
|
|
continue
|
|
for i in range(len(edges) - 1):
|
|
if edges[i] <= e["ptok"] < edges[i + 1]:
|
|
a = acc[v][i]
|
|
a["n"] += 1; a["o"] += e["otok"]; a["d"] += e["dec"]; a["p"] += e["pf"]; a["u"] += e["ptok"] - e["cached"]
|
|
for i in range(len(edges) - 1):
|
|
cols = []
|
|
for v in variants:
|
|
a = acc[v][i]
|
|
cols.append("%4d %6.1f %6.0f %6.0f" % (a["n"], a["o"] / a["d"] * 1e3 if a["d"] else 0, a["p"] / a["n"] if a["n"] else 0, a["u"] / a["n"] if a["n"] else 0))
|
|
print("%6d-%-9d | %s" % (edges[i], edges[i + 1], " | ".join(cols)))
|
|
|
|
|
|
def main():
|
|
if sys.argv[1] == "--buckets":
|
|
return buckets(sys.argv[2], sys.argv[3:])
|
|
root, variants = sys.argv[1], sys.argv[2:]
|
|
for v in variants:
|
|
for task in ("nestng-crm", "ttt"):
|
|
agg = dict(n=0, proxy=0.0, pf=0.0, dec=0.0, pause=0.0, otok=0, ptok=0, uncached=0, cells=0, mismatch=0, pausen=0, wall=0.0, first=0.0)
|
|
for d in sorted(glob.glob(os.path.join(root, v, task + "-r[0-9]"))):
|
|
rounds, ev, info = cell(d, v)
|
|
if len(ev) != len(rounds):
|
|
agg["mismatch"] += 1
|
|
continue
|
|
agg["cells"] += 1
|
|
agg["wall"] += info["wall_s"] * 1e3
|
|
agg["first"] += rounds[0]["t_start_ms"]
|
|
for i, (r, e) in enumerate(zip(rounds, ev)):
|
|
agg["n"] += 1
|
|
agg["proxy"] += r["t_end_ms"] - r["t_start_ms"]
|
|
agg["pf"] += e["pf"]
|
|
agg["dec"] += e["dec"]
|
|
agg["otok"] += r["resp"]["output_tokens"] if r.get("resp") else e["otok"]
|
|
agg["ptok"] += e["ptok"]
|
|
agg["uncached"] += e["ptok"] - e["cached"]
|
|
if i + 1 < len(rounds):
|
|
agg["pause"] += rounds[i + 1]["t_start_ms"] - r["t_end_ms"]
|
|
agg["pausen"] += 1
|
|
n = agg["n"]
|
|
if not n:
|
|
print(v, task, "no joined cells (order mismatch in %d)" % agg["mismatch"])
|
|
continue
|
|
rest = agg["proxy"] - agg["pf"] - agg["dec"]
|
|
print("%-5s %-10s cells %d requests %3d | per request ms: proxy %7.0f = prefill %6.0f + decode %6.0f + rest %5.0f | pause %6.0f | per cell: wall %5.0f s, model %5.0f s, pauses %5.0f s | out tok/req %5.1f, uncached/req %6.0f | ms/out tok: proxy %.2f pf %.2f dec %.2f rest %.2f | proxy tok/s %.1f, engine decode tok/s %.1f" % (
|
|
v, task, agg["cells"], n, agg["proxy"] / n, agg["pf"] / n, agg["dec"] / n, rest / n, agg["pause"] / max(agg["pausen"], 1),
|
|
agg["wall"] / agg["cells"] / 1e3, agg["proxy"] / agg["cells"] / 1e3, agg["pause"] / agg["cells"] / 1e3, agg["otok"] / n, agg["uncached"] / n,
|
|
agg["proxy"] / agg["otok"], agg["pf"] / agg["otok"], agg["dec"] / agg["otok"], rest / agg["otok"], agg["otok"] / agg["proxy"] * 1e3, agg["otok"] / agg["dec"] * 1e3))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|