File size: 3,257 Bytes
4a1ea7b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | """Verify the text-encoder IRs against the CPU references (te_ref.npz): isolated per part + chained.
Usage: python verify_te.py --device CPU|GPU [--precision f16|f32|bf16] [--parts 0,1,2,3] [--prompts scene,empty]
Writes /opt/mossimage/work/te_verify_<device>_<precision>.json
"""
import argparse
import gc
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, 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="CPU")
ap.add_argument("--precision", default=None)
ap.add_argument("--parts", default="0,1,2,3")
ap.add_argument("--prompts", default="scene")
ap.add_argument("--cache", type=int, default=1)
ap.add_argument("--repeat", type=int, default=3)
ap.add_argument("--te-dir", default="te")
args = ap.parse_args()
ref = np.load(f"{WORK}/te_ref.npz")
core = make_core(bool(args.cache))
parts = [int(x) for x in args.parts.split(",")]
out = {"device": args.device, "precision": args.precision, "results": []}
chains = {p: None for p in args.prompts.split(",")}
for k in parts:
cm, tinfo = compile_model(core, f"{MODELS}/{args.te_dir}/te_part{k}.xml", args.device, args.precision, label=f"te_part{k}")
if args.device == "GPU":
print("[gpu mem MB]", gpu_mem(core), flush=True)
for p in chains:
L = len(ref[f"{p}_ids"])
cos, sin = text_rope(L)
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=args.repeat)
r32 = cmp(ref[f"{p}_f32_part{k}"], res[0], f"{p} part{k} isolated vs fp32")
rbf = cmp(ref[f"{p}_bf16_part{k}"], res[0], f"{p} part{k} isolated vs bf16 pipeline")
r_bf_vs_32 = cmp(ref[f"{p}_f32_part{k}"], ref[f"{p}_bf16_part{k}"], f"{p} part{k} (bf16 pipeline vs fp32 ref)")
# chained
ch_in = (ref[f"{p}_embeds"][None].astype(np.float32) if k == 0 else chains[p])
if ch_in is not None:
res_ch, _ = infer(cm, {"hidden": ch_in, "cos": cos, "sin": sin})
chains[p] = res_ch
rch = cmp(ref[f"{p}_f32_part{k}"], res_ch[0], f"{p} part{k} CHAINED vs fp32")
else:
rch = None
out["results"].append({"part": k, "prompt": p, "L": L, "compile": tinfo, "infer_s": [round(t, 3) for t in times],
"iso_vs_f32": r32, "iso_vs_bf16": rbf, "bf16pipe_vs_f32": r_bf_vs_32, "chained_vs_f32": rch,
"gpu_mem_mb": gpu_mem(core) if args.device == "GPU" else None, "hwm_gb": round(hwm_gb(), 1)})
print(f"[infer] part{k} {p} L={L}: {[round(t, 3) for t in times]}", flush=True)
del cm
gc.collect()
json.dump(out, open(f"{WORK}/te_verify_{args.te_dir}_{args.device}_{args.precision or 'default'}.json", "w"), indent=1)
print("saved result json")
if __name__ == "__main__":
main()
|