MOSS-Image-SFT-OpenVINO / code /bench_static.py
aqualaguna's picture
Add files using upload-large-folder tool
565b868 verified
Raw History Blame Contribute Delete
2.8 kB
"""Dynamic vs static-shape compile of the DiT segments on the GPU: memory and per-call time (random inputs, timing only).
Usage: python bench_static.py [--n 4096] [--l 56] [--mode static|dynamic]
"""
import argparse
import gc
import sys
import time
import numpy as np
import openvino as ov
sys.path.insert(0, __import__("os").path.dirname(__import__("os").path.abspath(__file__)))
from common import MODELS # noqa: E402
from ovrun import gpu_mem, make_core # noqa: E402
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--n", type=int, default=4096)
ap.add_argument("--l", type=int, default=56)
ap.add_argument("--mode", default="static")
ap.add_argument("--cache", type=int, default=1)
args = ap.parse_args()
N, L = args.n, args.l
core = make_core(bool(args.cache))
d = f"{MODELS}/dit_sft"
shapes = {
"dit_a": {"tokens": [1, N, 128], "text": [1, L, 3072], "rope": [1, N + L, 128], "mod": [1, 6, 3072]},
"dit_b": {"x": [1, N + L, 3072], "rope": [1, N + L, 128], "mod": [1, 6, 3072]},
"dit_c": {"x": [1, N + L, 3072], "rope": [1, N + L, 128], "mod": [1, 6, 3072], "head": [1, 2, 3072], "n_img": [1, N, 1]},
}
rng = np.random.default_rng(0)
cms, reqs = {}, {}
for name, sh in shapes.items():
m = core.read_model(f"{d}/{name}.xml")
if args.mode == "static":
m.reshape({k: ov.PartialShape(v) for k, v in sh.items()})
t = time.time()
cms[name] = core.compile_model(m, "GPU")
print(f"[compile] {name} {args.mode}: {time.time() - t:.1f}s", flush=True)
print("[gpu mem after compile]", gpu_mem(core), flush=True)
x = None
for name in ("dit_a", "dit_b", "dit_c"):
req = cms[name].create_infer_request()
inp = {}
for k, v in shapes[name].items():
if k == "n_img":
inp[k] = np.zeros(v, np.float32)
elif k == "x" and x is not None:
inp[k] = x
else:
inp[k] = (rng.standard_normal(v) * 0.5).astype(np.float32)
ts = []
for _ in range(3):
t = time.time()
r = req.infer(inp)
ts.append(time.time() - t)
x = np.array(next(iter(r.values())))
print(f"[infer] {name} {args.mode} N={N} L={L}: {[round(t, 3) for t in ts]} finite={bool(np.isfinite(x).all())}", flush=True)
reqs[name] = req
print("[gpu mem]", gpu_mem(core), flush=True)
if name == "dit_a":
x = x # feed dit_b/dit_c with a_out of the right shape
print("[gpu mem final]", gpu_mem(core), flush=True)
for r in list(reqs):
reqs[r] = None
gc.collect()
print("[gpu mem after dropping requests]", gpu_mem(core), flush=True)
if __name__ == "__main__":
main()