#!/usr/bin/env python3 """Inputs of the 245k equivalence test. Offline; no runtime use. q245_prep.py prep CAPTURE_DIR OUT split a capture dir (req-NNNNNN.json with prompt_ids / generated_ids) into OUT/p.ids (prompt), OUT/r_port.ids (the recorded reply) and OUT/meta.json q245_prep.py mkcap OUT SRC NAME [--max-new N] OUT/cap-NAME/req-00000k.json: the prompts of OUT with the replies of source SRC (OUT/r_.ids) as the recorded reply; for the port's replay smoke (LELEKA_REPLAY_DIR). With several SRC (comma list) the requests of the sources follow each other (all prompts of SRC1, then all of SRC2). q245_prep.py sha DIR sha256 of every file under DIR (for MANIFEST.sha256) A request is the prompt (ends with the assistant header `<|im_start|>assistant\\n\\n`, 5 tokens) and a forced reply. The same token path is scored by the Original (prompt batched up to the last `<|im_start|>`, header and reply through the verify windows) and by the port (prompt prefilled, reply decoded along the forced path). """ import glob, hashlib, json, os, sys IM_START = 248045 def ids_of(doc, key): return [int(x) for x in doc[key]] def prep(capture, out): os.makedirs(out, exist_ok=True) files = sorted(glob.glob(os.path.join(capture, "req-*.json"))) meta = [] for k, path in enumerate(files, 1): doc = json.load(open(path)) prompt, reply = ids_of(doc, "prompt_ids"), ids_of(doc, "generated_ids") turn_at = max(i for i, t in enumerate(prompt) if t == IM_START) open(os.path.join(out, f"p{k}.ids"), "w").write(",".join(map(str, prompt))) open(os.path.join(out, f"r_port{k}.ids"), "w").write(",".join(map(str, reply))) meta.append({"k": k, "prompt_len": len(prompt), "reply_len": len(reply), "turn_at": turn_at, "header_len": len(prompt) - turn_at, "source_file": os.path.basename(path)}) json.dump({"capture": os.path.abspath(capture), "requests": meta}, open(os.path.join(out, "meta.json"), "w"), indent=1) for m in meta: print(m) def mkcap(out, sources, name, max_new=None): meta = json.load(open(os.path.join(out, "meta.json")))["requests"] cap = os.path.join(out, "cap-" + name) os.makedirs(cap, exist_ok=True) n = 0 for src in sources.split(","): for m in meta: k = m["k"] prompt = [int(x) for x in open(os.path.join(out, f"p{k}.ids")).read().strip().split(",")] reply = [int(x) for x in open(os.path.join(out, f"r_{src}{k}.ids")).read().strip().split(",") if x] n += 1 doc = {"index": n, "source": src, "k": k, "max_new": max_new or len(reply), "temperature_milli": 0, "top_p_micro": None, "top_k": None, "min_p_micro": None, "repetition_penalty_micro": None, "frequency_penalty_micro": None, "presence_penalty_micro": None, "penalty_last_n": None, "seed": None, "prompt_tokens": len(prompt), "reused": 0, "rounds": 0, "produced": len(reply), "prompt_ids": prompt, "generated_ids": reply} json.dump(doc, open(os.path.join(cap, "req-%06d.json" % n), "w")) print(n, "requests ->", cap) def sha(directory): for root, _, names in sorted(os.walk(directory)): for f in sorted(names): p = os.path.join(root, f) h = hashlib.sha256() with open(p, "rb") as fh: for block in iter(lambda: fh.read(1 << 20), b""): h.update(block) print(h.hexdigest() + " " + p) def main(argv): if not argv: print(__doc__) return 2 cmd = argv[0] if cmd == "prep": prep(argv[1], argv[2]) elif cmd == "mkcap": mx = int(argv[argv.index("--max-new") + 1]) if "--max-new" in argv else None mkcap(argv[1], argv[2], argv[3], mx) elif cmd == "sha": sha(argv[1]) else: print(__doc__) return 2 return 0 if __name__ == "__main__": sys.exit(main(sys.argv[1:]))