"""Interleaved speed benchmark: stock Swar (22 blocks) vs pruned Swar (16 blocks), same items, same recipe. Who/why: Claude Code session d1a584fd (Opus 5.5), 2026-11-22, for the public `ampixa/pruned-swar` release README, at the owner's request ("README should only show how fast we are"). Recipe: SwarEngine.synth_takes(n=1) — the single-pass path of Swar's recipe (NFE 16, CFG 2.0, sway -1, 2.0 s minimum generated length, fp32 EMA, vocos 24 kHz), seed 0. synth (F5 chunked infer) aborts on MPS for multi-chunk texts (command-encoder assertion, run 1 log kept), so texts needing more than one chunk are skipped and listed. Timing: wall time of CFM sampling + vocoder, excl. model load, device sync before/after. RTF = gen_s / generated s. Order alternates per item (A,B then B,A) so drift and shared-host load hit both models equally. Live-call safety: run under busy_guard.sh (SIGSTOP while the Kura agent has calls). An item is timed again if active_calls was non-zero before or after it, so paused time never enters a measurement. Usage: SWAR_DEVICE=mps ZF_ROOT= python bench_speed.py OUT_DIR N_PER_SET """ import json import os import sys import time import urllib.request from pathlib import Path ROOT = Path(os.environ["ZF_ROOT"]) sys.path.insert(0, str(ROOT / "code")) from swar_engine import SwarEngine # noqa: E402 MODELS = { "swar_stock": (ROOT / "hf/base/podvoices_ema.pt", "b932ea211288795a4a07749b84b51622b2c181cd29343f56f2a5c85598875b53"), "pruned_swar": (ROOT / "hf/ckpt/B16/ema_010000.pt", "970b5d0a57bcc195d2d6ed6c9f7668adcb782a6f8e3febf832ce4375cfbd3992"), } def active_calls() -> int: try: with urllib.request.urlopen("http://localhost:8820/busy", timeout=3) as r: return int(json.load(r).get("active_calls", 0)) except Exception as e: # unknown state counts as busy so the item is re-timed print("busy probe failed:", e, flush=True) return -1 def main() -> None: out, n = Path(sys.argv[1]), int(sys.argv[2]) out.mkdir(parents=True, exist_ok=True) dev = ROOT / "hf/dev80" items = json.loads((dev / "items.json").read_text()) sel = [i for i in items if i["set"] == "short"][:n] + [i for i in items if i["set"] == "long"][:n] eng, meta = {}, {} for name, (ck, sh) in MODELS.items(): e = SwarEngine(ckpt=str(ck), verify_sha=sh) params = sum(p.numel() for p in e.model.transformer.parameters()) eng[name] = e meta[name] = dict(ckpt=str(ck), sha256=sh, depth=e.depth, transformer_params=params, load_s=round(e.load_s, 2), fp32_weight_gb=round(params * 4 / 1e9, 3)) print(name, meta[name], flush=True) warm = sel[0] for name, e in eng.items(): # warm-up, not recorded for k in range(2): e.synth_takes(dev / warm["prompt_file"], warm["prompt_text"], warm["target_text"], 1, 0, [out / f"warm_{name}_{k}.wav"], nfe=16, floor_s=2.0) rows, skipped = [], [] for idx, it in enumerate(sel): order = list(eng) if idx % 2 == 0 else list(reversed(eng)) for name in order: for attempt in range(5): b0 = active_calls() wav = out / f"{name}_{it['id']}.wav" r = eng[name].synth_takes(dev / it["prompt_file"], it["prompt_text"], it["target_text"], 1, 0, [wav], nfe=16, floor_s=2.0) if r is None: break b1 = active_calls() if b0 == 0 and b1 == 0: break print(f"re-time {name} {it['id']} (active_calls {b0}/{b1})", flush=True) time.sleep(30) else: raise RuntimeError(f"could not time {name} {it['id']} without live calls") if r is None: skipped.append(dict(model=name, id=it["id"], set=it["set"], reason="multi-chunk text")) print("skip multi-chunk", name, it["id"], flush=True) continue dur = r["raw_gen_seconds"][0] rows.append(dict(model=name, id=it["id"], set=it["set"], gen_s=r["gen_s"], out_s=dur, rtf=r["gen_s"] / dur, attempts=attempt + 1)) print(json.dumps(rows[-1]), flush=True) (out / "skipped.json").write_text(json.dumps(skipped, indent=1) + "\n") (out / "rows.jsonl").write_text("".join(json.dumps(x) + "\n" for x in rows)) summ = {"meta": meta, "device": os.environ.get("SWAR_DEVICE"), "items_per_set": n, "recipe": "SwarEngine.synth_takes n=1 NFE16 CFG2 sway-1 floor2.0s fp32 seed0", "skipped": len(skipped), "by_model": {}} for name in eng: for s in ("short", "long", "all"): rr = [x for x in rows if x["model"] == name and (s == "all" or x["set"] == s)] g, o = sum(x["gen_s"] for x in rr), sum(x["out_s"] for x in rr) summ["by_model"].setdefault(name, {})[s] = dict(n=len(rr), total_gen_s=round(g, 2), total_out_s=round(o, 2), rtf=round(g / o, 4), mean_gen_s=round(g / len(rr), 3)) a, b = summ["by_model"]["swar_stock"], summ["by_model"]["pruned_swar"] summ["speedup_rtf"] = {s: round(a[s]["rtf"] / b[s]["rtf"], 3) for s in ("short", "long", "all")} summ["speedup_wall"] = {s: round(a[s]["total_gen_s"] / b[s]["total_gen_s"], 3) for s in ("short", "long", "all")} (out / "summary.json").write_text(json.dumps(summ, indent=1) + "\n") print("SUMMARY", json.dumps(summ), flush=True) if __name__ == "__main__": main()