#!/usr/bin/env python3 """Private one-pair CPU KAR vs bounded CUDA PLE serving smoke. The source config supplies model paths and normal engine options. Only its `args` list is read. Raw engine output stays in an external private directory. This is a functional pair, not a throughput claim or quality gate. """ import argparse import hashlib import json import os from pathlib import Path import re import subprocess import time def replace_arg(args, flag, value): at = args.index(flag) args[at + 1] = value def run(engine, args, out, name): started = time.monotonic() proc = subprocess.run([str(engine), *args], capture_output=True, timeout=900) wall = time.monotonic() - started (out / f"{name}.stdout").write_bytes(proc.stdout) (out / f"{name}.stderr").write_bytes(proc.stderr) stdout = proc.stdout.decode("utf-8", "replace") output = re.search(r"^output :(.*)$", stdout, re.MULTILINE) rates = {} for phase in ("prefill", "decode"): match = re.search(rf"^{phase}\s+\d+ tokens in ([0-9.]+) ms\s+->\s+([0-9.]+) tok/s", stdout, re.MULTILINE) if match: rates[f"{phase}_ms"] = float(match.group(1)) rates[f"{phase}_tokens_per_second"] = float(match.group(2)) ids = output.group(1).strip().split() if output else [] result = {"returncode": proc.returncode, "wall_seconds": wall, "output_count": len(ids), "output_ids_sha256": hashlib.sha256(" ".join(ids).encode()).hexdigest() if ids else None, **rates} if proc.returncode != 0 or not ids: tail = proc.stderr.decode("utf-8", "replace").splitlines()[-8:] result["error_tail"] = tail return result, ids def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--engine", type=Path, required=True) parser.add_argument("--config", type=Path, required=True) parser.add_argument("--archive", type=Path, required=True) parser.add_argument("--tokens-file", type=Path, required=True) parser.add_argument("--out", type=Path, required=True) parser.add_argument("--cache-mib", type=int, default=64) parser.add_argument("--expert-slots", type=int, default=10000) parser.add_argument("--max-new", type=int, default=16) parser.add_argument("--order", default="cpu,gpu", help="comma-separated arm order, for example cpu,gpu,gpu,cpu") parser.add_argument("--expert-cache-per-layer", action="store_true") options = parser.parse_args() if not 1 <= options.cache_mib <= 1024 or options.expert_slots < 1: parser.error("invalid cache size or expert slots") order = options.order.split(",") if not order or any(arm not in {"cpu", "gpu"} for arm in order) or set(order) != {"cpu", "gpu"}: parser.error("order must contain both cpu and gpu arms") if options.out.exists(): parser.error("output directory already exists") options.out.mkdir(mode=0o700, parents=True) os.chmod(options.out, 0o700) source = json.loads(options.config.read_text()) common = list(source["args"]) if "--ple-kar" in common or "--ple-kar-gpu-cache-mib" in common: parser.error("source config already selects a KAR experiment") replace_arg(common, "--expert-cache", str(options.expert_slots)) if options.expert_cache_per_layer: common.append("--expert-cache-per-layer") common += ["--tokens-file", str(options.tokens_file), "--max-new", str(options.max_new), "--ple-kar", str(options.archive), "--ple-kar-level", "2", "--stats"] results = {} outputs = {} for index, arm in enumerate(order, 1): name = f"{index:02d}_{arm}" extra = ["--ple-kar-gpu-cache-mib", str(options.cache_mib)] if arm == "gpu" else [] results[name], outputs[name] = run(options.engine, common + extra, options.out, name) (options.out / "summary.json").write_text(json.dumps(results, indent=2) + "\n") if results[name]["returncode"] != 0 or not outputs[name]: break if len(outputs) == len(order) and all(outputs.values()): results["greedy_ids_exact"] = all(ids == next(iter(outputs.values())) for ids in outputs.values()) (options.out / "summary.json").write_text(json.dumps(results, indent=2) + "\n") print(json.dumps(results, indent=2)) if __name__ == "__main__": main()