"""Text-encoder speed vs prompt length on the GPU (random embeddings of the real scale; timing only). Usage: python bench_te.py [--lengths 64,256,512,1024,2048] [--parts 0,1,2,3] """ import argparse import json import sys import time import numpy as np sys.path.insert(0, __import__("os").path.dirname(__import__("os").path.abspath(__file__))) from common import MODELS, WORK, hwm_gb, text_rope # noqa: E402 from ovrun import compile_model, gpu_mem, make_core # noqa: E402 def main(): ap = argparse.ArgumentParser() ap.add_argument("--lengths", default="64,256,512,1024,2048") ap.add_argument("--parts", default="0") ap.add_argument("--device", default="GPU") ap.add_argument("--precision", default=None) args = ap.parse_args() core = make_core(True) out = {} rng = np.random.default_rng(0) for k in [int(x) for x in args.parts.split(",")]: cm, _ = compile_model(core, f"{MODELS}/te/te_part{k}.xml", args.device, args.precision, label=f"te_part{k}") req = cm.create_infer_request() for L in [int(x) for x in args.lengths.split(",")]: cos, sin = text_rope(L) h = (rng.standard_normal((1, L, 4096)) * 0.5).astype(np.float32) ts = [] for _ in range(2): t = time.time() r = req.infer({"hidden": h, "cos": cos, "sin": sin}) ts.append(time.time() - t) y = np.array(list(r.values())[0]) print(f"[bench] part{k} L={L}: {[round(x, 3) for x in ts]} finite={bool(np.isfinite(y).all())} absmax={float(np.abs(y).max()):.1f}", flush=True) out[f"part{k}_L{L}"] = [round(x, 3) for x in ts] print("[gpu mem MB]", gpu_mem(core), flush=True) del req, cm json.dump(out, open(f"{WORK}/bench_te_{args.device}.json", "w"), indent=1) if __name__ == "__main__": main()