MOSS-Image-Edit-OpenVINO / code /verify_dit_cpu.py
aqualaguna's picture
Add files using upload-large-folder tool
4a1ea7b verified
Raw History Blame Contribute Delete
3.28 kB
"""Isolated per-segment check of the DiT IRs on the CPU plugin (f32 compute, one segment compiled at a time to bound host RAM:
dit_a alone is ~11 GB once the fp16 weights are expanded to f32), against dit_ref_<which>.npz (original classes, fp32).
Usage: python verify_dit_cpu.py [--which sft] [--cases A,B,C] [--segments text_refine,dit_a,dit_b,dit_c]
"""
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 rope_table, time_mod # noqa: E402
from ovrun import compile_model, 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("--which", default="sft")
ap.add_argument("--cases", default="A,B,C")
ap.add_argument("--segments", default="text_refine,dit_a,dit_b,dit_c")
args = ap.parse_args()
ref = np.load(f"{WORK}/dit_ref_{args.which}.npz")
d = f"{MODELS}/dit_{args.which}"
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(False)
out = {"device": "CPU", "precision": "f32", "cases": {}}
for name in args.segments.split(","):
cm, tinfo = compile_model(core, f"{d}/{name}.xml", "CPU", "f32", label=name)
out.setdefault("compile", {})[name] = tinfo
for case in args.cases.split(","):
if name == "dit_b" and SKIP[case]:
continue
ctx = ref[CTX[case]].astype(np.float32)
L = ctx.shape[1]
rope = rope_table(h, w, L)
mod, head = time_mod(hw, T[case])
if name == "text_refine":
y, tt = infer(cm, {"ctx": ctx, "rope_t": rope[:, N:]}, 1)
r = cmp(ref[f"text_refine_{case}"], y, f"{case} text_refine CPU")
elif name == "dit_a":
y, tt = infer(cm, {"tokens": tokens, "text": ref[f"text_refine_{case}"], "rope": rope, "mod": mod}, 1)
r = cmp(ref[f"after_21_{case}"], y, f"{case} dit_a CPU")
elif name == "dit_b":
y, tt = infer(cm, {"x": ref[f"after_21_{case}"], "rope": rope, "mod": mod}, 1)
r = cmp(ref[f"after_22_{case}"], y, f"{case} dit_b CPU")
else:
x_in = ref[f"after_21_{case}"] if SKIP[case] else ref[f"after_22_{case}"]
y, tt = infer(cm, {"x": x_in, "rope": rope, "mod": mod, "head": head, "n_img": np.zeros((1, N, 1), np.float32)}, 1)
r = cmp(ref[f"v_{case}"], y.transpose(0, 2, 1).reshape(1, 128, h, w), f"{case} dit_c CPU")
out["cases"].setdefault(case, {})[name] = {"cmp": r, "infer_s": round(tt[0], 2)}
del cm
gc.collect()
print(f"[hwm] {hwm_gb():.1f} GB after {name}", flush=True)
out["hwm_gb"] = round(hwm_gb(), 1)
json.dump(out, open(f"{WORK}/dit_verify_cpu_isolated_{args.which}.json", "w"), indent=1)
print("saved result json")
if __name__ == "__main__":
main()