Download code/verify_dit.py from aqualaguna/MOSS-Image-SFT-OpenVINO: direct link, hf CLI and curl.
- Browser
- Download file 4.95 kB
-
https://huggingface.co/aqualaguna/MOSS-Image-SFT-OpenVINO/resolve/main/code/verify_dit.py
- Command line
-
hf download hf://aqualaguna/MOSS-Image-SFT-OpenVINO/code/verify_dit.py
-
curl -L -o verify_dit.py https://huggingface.co/aqualaguna/MOSS-Image-SFT-OpenVINO/resolve/main/code/verify_dit.py
4.95 kB
| """Verify the DiT IRs (text_refine, dit_a, dit_b, dit_c) against dit_ref_<which>.npz (fp32 CPU, original classes). | |
| Usage: python verify_dit.py --device CPU|GPU [--precision f16|f32|bf16] [--which sft] [--cases A,B,C] | |
| Isolated = each segment fed the fp32 reference input of that point; chained = whole path through the IRs + host glue. | |
| """ | |
| 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 # noqa: E402 | |
| from dit_ops import load_orig_module, rope_table, time_mod # noqa: E402 | |
| from ovrun import compile_model, gpu_mem, infer, make_core # noqa: E402 | |
| T = {"A": 1000.0, "B": 500.0, "C": 100.0} | |
| SKIP = {"A": False, "B": True, "C": False} | |
| CTX = {"A": "ctx_scene", "B": "ctx_empty", "C": "ctx_scene"} | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--device", default="CPU") | |
| ap.add_argument("--precision", default=None) | |
| ap.add_argument("--which", default="sft") | |
| ap.add_argument("--suffix", default="", help="IR dir is models/dit_<which><suffix>, e.g. _int8") | |
| ap.add_argument("--cases", default="A,B,C") | |
| ap.add_argument("--repeat", type=int, default=2) | |
| ap.add_argument("--cache", type=int, default=1) | |
| args = ap.parse_args() | |
| ref = np.load(f"{WORK}/dit_ref_{args.which}.npz") | |
| d = f"{MODELS}/dit_{args.which}{args.suffix}" | |
| hw = dict(np.load(f"{d}/host_weights.npz")) | |
| lat = ref["latents"] | |
| h, w = lat.shape[2:] | |
| N = h * w | |
| tokens = lat.reshape(1, 128, N).transpose(0, 2, 1).astype(np.float32) | |
| core = make_core(bool(args.cache)) | |
| out = {"device": args.device, "precision": args.precision, "cases": {}} | |
| # host glue check vs original code | |
| import torch | |
| orig = load_orig_module() | |
| L0 = ref["ctx_scene"].shape[1] | |
| r_t = orig._RoPE(128, 10000.0)(h, w, L0, "cpu").numpy() | |
| print("rope_table vs original _RoPE: max abs diff", float(np.abs(r_t - rope_table(h, w, L0)).max()), flush=True) | |
| cms = {} | |
| for name in ["text_refine", "dit_a", "dit_b", "dit_c"]: | |
| cms[name], tinfo = compile_model(core, f"{d}/{name}.xml", args.device, args.precision, label=name) | |
| out.setdefault("compile", {})[name] = tinfo | |
| if args.device == "GPU": | |
| print("[gpu mem MB]", gpu_mem(core), flush=True) | |
| for case in args.cases.split(","): | |
| ctx = ref[CTX[case]].astype(np.float32) | |
| L = ctx.shape[1] | |
| rope = rope_table(h, w, L) | |
| mod, head = time_mod(hw, T[case]) | |
| res = {"L": L, "t": T[case], "skip22": SKIP[case]} | |
| times = {} | |
| # ---- isolated | |
| text, tt = infer(cms["text_refine"], {"ctx": ctx, "rope_t": rope[:, N:]}, args.repeat) | |
| res["text_refine_iso"] = cmp(ref[f"text_refine_{case}"], text, f"{case} text_refine") | |
| times["text_refine"] = tt | |
| x_a, tt = infer(cms["dit_a"], {"tokens": tokens, "text": ref[f"text_refine_{case}"], "rope": rope, "mod": mod}, args.repeat) | |
| res["dit_a_iso"] = cmp(ref[f"after_21_{case}"], x_a, f"{case} dit_a (blocks 0-21) isolated") | |
| times["dit_a"] = tt | |
| if not SKIP[case]: | |
| x_b, tt = infer(cms["dit_b"], {"x": ref[f"after_21_{case}"], "rope": rope, "mod": mod}, args.repeat) | |
| res["dit_b_iso"] = cmp(ref[f"after_22_{case}"], x_b, f"{case} dit_b (block 22) isolated") | |
| times["dit_b"] = tt | |
| x_in = ref[f"after_22_{case}"] | |
| else: | |
| x_in = ref[f"after_21_{case}"] | |
| v, tt = infer(cms["dit_c"], {"x": x_in, "rope": rope, "mod": mod, "head": head, "n_img": np.zeros((1, N, 1), np.float32)}, args.repeat) | |
| v_img = v.transpose(0, 2, 1).reshape(1, 128, h, w) | |
| res["dit_c_iso"] = cmp(ref[f"v_{case}"], v_img, f"{case} dit_c (blocks 23-41 + head) isolated") | |
| times["dit_c"] = tt | |
| # ---- chained (own outputs all the way) | |
| x = infer(cms["dit_a"], {"tokens": tokens, "text": text, "rope": rope, "mod": mod})[0] | |
| if not SKIP[case]: | |
| x = infer(cms["dit_b"], {"x": x, "rope": rope, "mod": mod})[0] | |
| v = infer(cms["dit_c"], {"x": x, "rope": rope, "mod": mod, "head": head, "n_img": np.zeros((1, N, 1), np.float32)})[0] | |
| v_img = v.transpose(0, 2, 1).reshape(1, 128, h, w) | |
| res["chained_velocity"] = cmp(ref[f"v_{case}"], v_img, f"{case} CHAINED velocity vs fp32 ref") | |
| res["infer_s"] = {k: [round(x, 3) for x in v_] for k, v_ in times.items()} | |
| print(f"[infer] case {case}: {res['infer_s']}", flush=True) | |
| out["cases"][case] = res | |
| if args.device == "GPU": | |
| out["gpu_mem_mb"] = gpu_mem(core) | |
| print("[gpu mem MB] final", out["gpu_mem_mb"], flush=True) | |
| out["hwm_gb"] = round(hwm_gb(), 1) | |
| json.dump(out, open(f"{WORK}/dit_verify_{args.which}{args.suffix}_{args.device}_{args.precision or 'default'}.json", "w"), indent=1) | |
| print("saved result json") | |
| if __name__ == "__main__": | |
| main() | |