"""Text-encoder IRs on the card (fp16) against the CPU fp32 reference for long prompts (te_ref_long.npz): isolated per part + chained. Usage: python verify_te_long.py [--device GPU] [--precision f16] """ import argparse import gc import json import sys import numpy as np sys.path.insert(0, __import__("os").path.dirname(__import__("os").path.abspath(__file__))) from common import MODELS, WORK, cmp, hwm_gb, text_rope # noqa: E402 from ovrun import compile_model, gpu_mem, infer, make_core # noqa: E402 def main(): ap = argparse.ArgumentParser() ap.add_argument("--device", default="GPU") ap.add_argument("--precision", default=None) ap.add_argument("--prompts", default="layout,long") ap.add_argument("--cache", type=int, default=1) ap.add_argument("--te-dir", default="te") args = ap.parse_args() ref = np.load(f"{WORK}/te_ref_long.npz") core = make_core(bool(args.cache)) out = {"device": args.device, "precision": args.precision, "results": []} chains = {p: None for p in args.prompts.split(",")} for k in range(4): cm, tinfo = compile_model(core, f"{MODELS}/{args.te_dir}/te_part{k}.xml", args.device, args.precision, label=f"te_part{k}") for p in chains: L = len(ref[f"{p}_ids"]) cos, sin = text_rope(L) # (a) what the service does: ONE call per part and shape (a new prompt = a new L), chained -> FIRST-run kernels ch_in = ref[f"{p}_embeds"][None].astype(np.float32) if k == 0 else chains[p] res_first, t1 = infer(cm, {"hidden": ch_in, "cos": cos, "sin": sin}) chains[p] = res_first rfirst = cmp(ref[f"{p}_f32_part{k}"], res_first[0], f"{p} L={L} part{k} CHAINED first-run vs fp32 ({t1[0]:.3f}s)") # (b) the same shape again, with the exact fp32 input of the reference: settled kernels inp_iso = ref[f"{p}_embeds"] if k == 0 else ref[f"{p}_f32_part{k - 1}"] res, times = infer(cm, {"hidden": inp_iso[None].astype(np.float32), "cos": cos, "sin": sin}, repeat=2) r32 = cmp(ref[f"{p}_f32_part{k}"], res[0], f"{p} L={L} part{k} isolated settled vs fp32") if f"{p}_bf16_part{k}" in ref.files: cmp(ref[f"{p}_f32_part{k}"], ref[f"{p}_bf16_part{k}"], f"{p} L={L} part{k} (bf16 pipeline vs fp32 ref)") cmp(ref[f"{p}_bf16_part{k}"], res_first[0], f"{p} L={L} part{k} CHAINED first-run vs bf16 pipeline") out["results"].append({"part": k, "prompt": p, "L": L, "infer_s": [round(t, 3) for t in times], "chained_first_vs_f32": rfirst, "iso_settled_vs_f32": r32, "gpu_mem_mb": gpu_mem(core) if args.device == "GPU" else None}) print(f"[infer] part{k} {p} L={L}: first {t1[0]:.3f} then {[round(t, 3) for t in times]}", flush=True) del cm gc.collect() out["hwm_gb"] = round(hwm_gb(), 1) json.dump(out, open(f"{WORK}/te_verify_long_{args.te_dir}_{args.device}_{args.precision or 'default'}.json", "w"), indent=1) print("saved result json") if __name__ == "__main__": main()