#!/usr/bin/env python3 """Decode-only A/B at the depth of the last turn of long_session.py: one prompt of about --target-tokens tokens is read once (warm-up request, its prefill is the cost), then the SAME prompt is sent again for every arm; the prefix cache holds it, so each request re-reads a few tokens and decodes --max-output tokens at that depth. An arm is a set of files whose existence the engine checks (bring-up switches that need no restart): the script creates exactly the files of the arm before the request and removes the others. Output: one JSON line per request (arm, rep, decode tok/s, ttft, cached tokens) on stdout and $E2E_OUT_DIR/decode-ab.json; each line also gets a wall-clock stamp so the engine log can be cut. decode_ab.py --src DIR --arm NAME=FILE,FILE ... --order A,B,B,A [--target-tokens N] [--max-output N] The first arm named in --arm with no files is the baseline. Needs E2E_BASE_URL, E2E_MODEL, E2E_API_KEY. """ import argparse import json import os import sys import time sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import long_session as ls def main(): ap = argparse.ArgumentParser() ap.add_argument("--src", required=True) ap.add_argument("--target-tokens", type=int, default=242000) ap.add_argument("--max-output", type=int, default=300) ap.add_argument("--context", type=int, default=262144) ap.add_argument("--arm", action="append", required=True, help="NAME=FILE,FILE (empty list: no files)") ap.add_argument("--order", required=True, help="comma separated arm names, one request each") a = ap.parse_args() base, model, key = os.environ["E2E_BASE_URL"], os.environ["E2E_MODEL"], os.environ["E2E_API_KEY"] out_dir = os.environ.get("E2E_OUT_DIR", ".") arms = {} for spec in a.arm: name, _, files = spec.partition("=") arms[name] = [f for f in files.split(",") if f] every = sorted({f for fs in arms.values() for f in fs}) files = ls.collect(a.src) room = int((a.context - a.max_output - 8 - 400) * 0.96) chunk, _ = ls.take(files, 0, int(min(a.target_tokens, room) * 3.3)) items = [{"role": "user", "content": [{"type": "input_text", "text": chunk + "\n\n" + ls.QUESTIONS[0]}]}] for f in every: if os.path.exists(f): os.remove(f) results = [] def one(label, arm, rep): want = set(arms.get(arm, [])) for f in every: if f in want: open(f, "w").close() elif os.path.exists(f): os.remove(f) stamp = time.strftime("%H:%M:%S") info, _reply = ls.stream(base, key, model, items, a.max_output) info.update({"label": label, "arm": arm, "rep": rep, "start": stamp}) results.append(info) print(json.dumps(info), flush=True) return info one("warmup", next(iter(arms)), 0) # reads the whole prompt seen = {} for arm in a.order.split(","): seen[arm] = seen.get(arm, 0) + 1 info = one("ab", arm, seen[arm]) if "error" in info: break for f in every: if os.path.exists(f): os.remove(f) with open(os.path.join(out_dir, "decode-ab.json"), "w") as fh: json.dump(results, fh, indent=1) return 0 if __name__ == "__main__": sys.exit(main())