Files
Strata/tools/image/gate/dit_step_check.py
T
Niko1221 1705c8b9f0 image: Qwen-Image-2.1 DiT step without ggml, and the strata-dit-step test program (D4, first piece)
One DiT step (latent + text condition + timestep -> velocity) in our own code, CUDA:
- src/image/dit/qi21_dit.cu, include/strata/image/qi21_dit.hpp: the weights read with
  strata/artifact/gguf_reader.hpp into one device buffer as the file's bytes (q, k, v of a
  block one after the other: one GEMM), 1-D norms as f32; own buffers and stream; every
  multiply through dit_gemm (D1+D2); the text prefix once per condition with its K / V kept,
  as the native runner does.
- src/image/dit/qi21_kernels.cu: the native runner's kernels moved out of the sd.cpp patch
  (LayerNorm + modulate + gated residual, q/k RMSNorm + RoPE + f16 K/V, SwiGLU, the causal /
  exact attention, the mma.sync attention for sm_80+, the gfx11 WMMA attention), plus the
  steps ggml still did: timestep sinusoid, SiLU, GELU (tanh form), txt_in's RMSNorm.
  The sd.cpp patch keeps its own copy for the test builds.
- src/image/dit/qi21_dit_step_main.cpp: strata-dit-step <dit.gguf> <out> <dump dirs>
  [--repeat N], on the stage-hook dumps of what sd.cpp fed its DiT.
- tools/image/gate/dit_step_check.py (+ test_dit_step_check.py, CPU): the velocity against
  dit_ref's f32 result, both emulations, the D1+D2 test build, stock and native.
Not used by any build or by sd-cli; sd.cpp stays the default and the fallback. HIP is
written but not compiled (no card tonight).

Measured on the GB10 (dgxspark-20261111-0538-11fc), Q8_0, velocity rel L2 to f32:
  512 p0: ours 0.00055 | float32-sum emulation 0.00052 (1.061) | exact-sum 0.00046 (1.179)
          | D1+D2 test build 0.00050 | stock 0.00658 | native 0.00570 | ours vs D1+D2 0.00026
  512 p5: ours 0.00058 | 0.00098 (0.594) | 0.00055 (1.061) | 0.00114 | 0.00492 | 0.00612
          | ours vs D1+D2 0.00107
  1024 p0: ours 0.00045 | 0.00050 (0.901) | 0.00056 (0.809) | 0.00049 | 0.00535 | 0.00518
          | ours vs D1+D2 0.00023
Three runs per case (the first with the text prefix pass) byte-identical.
Step time: 512 369-391 ms (first run with the prefix 588-716 ms), 1024 1316-1326 ms
(first 1524 ms). sd-cli sampling at 1024 divided by 8 steps (host side included): native
1.54 s, D1+D2 1.57 s, stock 2.45 s.
Device memory of the DiT's own buffers: 8.12 GB at 512, 8.91 GB at 1024 (weights 7.64 GB
= the GGUF's size, work 0.48 / 1.27 GB incl. the GEMM's f16 buffers).
2026-11-11 05:39:36 +01:00

90 lines
4.5 KiB
Python

"""tools/image/gate/dit_step_check.py - TEST-ONLY (OWN-5B D4): strata-dit-step's velocities against the reference.
For each case: velocity rel L2 to the f32 reference (dit_ref.py) for ours (strata-dit-step), the emulation with
float32 sums (dit_ref mode f16acc32, the stage rule's comparison since 11-11) and with exact sums (f16op), the D1+D2
test build (sd.cpp's native runner with our GEMMs), stock sd.cpp and its native runner; and ours against the D1+D2
build directly. Then the stage rule over the cases given: (i) mean ours <= 1.25 x mean emulation, (ii) no case above
1.5 x its emulation value, (iii) every case closer than stock and native - against both emulations.
python -I -B dit_step_check.py --root R --ours O [--cases "512 p0,512 p5"]
R = the Spark own5b folder (pics/, d0ref/ with d0ref/acc32 from dit_acc32_cases.py); O = strata-dit-step's out dir.
"""
from __future__ import annotations
import argparse
import sys
from pathlib import Path
import numpy as np
_HERE = Path(__file__).resolve().parent
if str(_HERE) not in sys.path:
sys.path.insert(0, str(_HERE))
from te_ref import compare # noqa: E402
def vel(path) -> np.ndarray:
"""a velocity file ([C][H][W] float32, sd.cpp's vel_<k>.bin layout) as [H*W][C] rows"""
return np.fromfile(path, dtype="<f4").reshape(64, -1).T
def case_paths(root: Path, name: str) -> dict:
size, p = name.split()
p = int(p[1:])
dn = f"dump_p{p}_s42"
if size == "1024":
return {"dump": root / "pics/d0_stock_1024" / dn, "ref": root / "d0ref/r1024" / dn,
"acc32": root / "d0ref/acc32/1024_p0_acc32.npy", "stock": root / "pics/d0_stock_1024" / dn / "vel_0.bin",
"native": root / "pics/d0_native_1024" / dn / "vel_0.bin",
"d1d2": root / "pics/z_ours_1024" / dn / "vel_0.bin"}
lo = "d0" if p < 4 else "z"
return {"dump": root / f"pics/{lo}_stock_512" / dn, "ref": root / "d0ref" / dn,
"acc32": root / f"d0ref/acc32/512_p{p}_acc32.npy", "stock": root / f"pics/{lo}_stock_512" / dn / "vel_0.bin",
"native": root / f"pics/{lo}_native_512" / dn / "vel_0.bin",
"d1d2": root / ("pics/d2_g15" if p < 4 else "pics/z_ours_512") / dn / "vel_0.bin"}
def rule(rows: list, k: int) -> str:
O = np.mean([r["ours"] for r in rows])
E = np.mean([r[k] for r in rows])
worst = max(rows, key=lambda r: r["ours"] / r[k])
ii = all(r["ours"] <= 1.5 * r[k] for r in rows)
iii = all(r["ours"] < r["stock"] and r["ours"] < r["native"] for r in rows)
return (f"(i) mean {O:.5f} / {E:.5f} = {O / E:.3f} ({'PASS' if O <= 1.25 * E else 'FAIL'}); (ii) largest "
f"{worst['ours'] / worst[k]:.3f} at {worst['name']} ({'PASS' if ii else 'FAIL'}); (iii) "
f"{'PASS' if iii else 'FAIL'}")
def main(argv=None) -> int:
ap = argparse.ArgumentParser()
ap.add_argument("--root", required=True)
ap.add_argument("--ours", required=True)
ap.add_argument("--cases", default="512 p0,512 p5")
a = ap.parse_args(argv)
root, ours_dir = Path(a.root), Path(a.ours)
rows = []
for name in [c.strip() for c in a.cases.split(",") if c.strip()]:
P = case_paths(root, name)
f32 = np.load(str(P["ref"]) + "_f32.npy").reshape(64, -1).T
o = vel(ours_dir / P["dump"].parent.name / P["dump"].name / "vel.bin")
r = {"name": name, "ours": compare(o, f32)["rel_l2"],
"f16acc32": compare(np.load(P["acc32"]).reshape(64, -1).T, f32)["rel_l2"],
"f16op": compare(np.load(str(P["ref"]) + "_f16op.npy").reshape(64, -1).T, f32)["rel_l2"],
"d1d2": compare(vel(P["d1d2"]), f32)["rel_l2"], "stock": compare(vel(P["stock"]), f32)["rel_l2"],
"native": compare(vel(P["native"]), f32)["rel_l2"], "ours_vs_d1d2": compare(o, vel(P["d1d2"]))["rel_l2"]}
rows.append(r)
print("| case | ours (strata-dit-step) | f32-sum emulation | ratio | exact-sum emulation | ratio | D1+D2 test build "
"| stock | native | ours vs D1+D2 build |")
print("|---|---|---|---|---|---|---|---|---|---|")
for r in rows:
print(f"| {r['name']} | {r['ours']:.5f} | {r['f16acc32']:.5f} | {r['ours'] / r['f16acc32']:.3f} | "
f"{r['f16op']:.5f} | {r['ours'] / r['f16op']:.3f} | {r['d1d2']:.5f} | {r['stock']:.5f} | "
f"{r['native']:.5f} | {r['ours_vs_d1d2']:.5f} |")
print("RULE vs the float32-sum emulation: " + rule(rows, "f16acc32"))
print("RULE vs the exact-sum emulation: " + rule(rows, "f16op"))
return 0
if __name__ == "__main__":
sys.exit(main())