File size: 2,602 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 | """Verify the VAE decoder IR against vae_ref_<size>.npz (fp32 torch CPU decode of real latents).
Usage: python verify_vae.py --device CPU|GPU [--variant f16|f32] [--precision f16|f32] [--sizes 512,1024]
"""
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, cmp, hwm_gb, psnr # noqa: E402
from ovrun import compile_model, gpu_mem, infer, make_core # noqa: E402
def to_u8(a):
return ((a[0].transpose(1, 2, 0) + 1) * 127.5).round().clip(0, 255).astype(np.uint8)
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--device", default="CPU")
ap.add_argument("--variant", default="f16")
ap.add_argument("--precision", default=None)
ap.add_argument("--sizes", default="512,1024")
ap.add_argument("--repeat", type=int, default=2)
args = ap.parse_args()
core = make_core(True)
cm, tinfo = compile_model(core, f"{MODELS}/vae/vae_decoder_{args.variant}.xml", args.device, args.precision, label=f"vae_{args.variant}")
out = {"device": args.device, "variant": args.variant, "precision": args.precision, "compile": tinfo, "sizes": {}}
for s in [int(x) for x in args.sizes.split(",")]:
ref = np.load(f"{WORK}/vae_ref_{s}.npz")
y, times = infer(cm, {"z": ref["z"].astype(np.float32)}, args.repeat)
r = cmp(ref["dec"], np.clip(y, -1, 1), f"vae {s} {args.device} {args.variant}/{args.precision}")
p_ref = psnr(to_u8(ref["dec"]), to_u8(np.clip(y, -1, 1)))
p_src = psnr(ref["src"], to_u8(np.clip(y, -1, 1)))
p_src_ref = psnr(ref["src"], to_u8(ref["dec"]))
print(f"[psnr] size {s}: OV vs fp32-torch decode {p_ref:.2f} dB; OV vs source {p_src:.2f} dB (fp32 torch vs source {p_src_ref:.2f} dB); infer {[round(t, 3) for t in times]}", flush=True)
out["sizes"][s] = {"cmp": r, "psnr_vs_fp32_decode_db": round(p_ref, 2), "psnr_vs_source_db": round(p_src, 2), "psnr_fp32_vs_source_db": round(p_src_ref, 2), "infer_s": [round(t, 3) for t in times]}
from PIL import Image
Image.fromarray(to_u8(np.clip(y, -1, 1))).save(f"{WORK}/vae_ov_{args.device}_{args.variant}_{args.precision or 'default'}_{s}.png")
if args.device == "GPU":
out["gpu_mem_mb"] = gpu_mem(core)
print("[gpu mem MB]", out["gpu_mem_mb"], flush=True)
out["hwm_gb"] = round(hwm_gb(), 1)
json.dump(out, open(f"{WORK}/vae_verify_{args.device}_{args.variant}_{args.precision or 'default'}.json", "w"), indent=1)
if __name__ == "__main__":
main()
|