Files
Strata/tools/image/gate/test_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

62 lines
2.6 KiB
Python

"""tools/image/gate/test_dit_step_check.py - TEST-ONLY: dit_step_check.py on a fake folder (no model, no GPU).
python -m pytest tools/image/gate/test_dit_step_check.py"""
import contextlib
import io
import sys
import tempfile
import unittest
from pathlib import Path
import numpy as np
sys.path.insert(0, str(Path(__file__).resolve().parent))
import dit_step_check as K # noqa: E402
def put(path: Path, a: np.ndarray, npy: bool = False) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
if npy:
np.save(path, a)
else:
a.astype("<f4").tofile(path)
class StepCheck(unittest.TestCase):
def test_table_and_rule(self):
rng = np.random.default_rng(3)
with tempfile.TemporaryDirectory() as td:
root, ours = Path(td) / "own5b", Path(td) / "ours"
for p, scale in ((0, 1.0), (5, 3.0)):
P = K.case_paths(root, f"512 p{p}")
ref = rng.standard_normal((64, 4, 4)).astype(np.float32)
noise = lambda s: ref + s * rng.standard_normal(ref.shape).astype(np.float32) # noqa: E731
put(Path(str(P["ref"]) + "_f32.npy"), ref, True)
put(Path(str(P["ref"]) + "_f16op.npy"), noise(1e-3), True)
put(P["acc32"], noise(1e-3 * scale), True)
put(P["stock"], noise(1e-2))
put(P["native"], noise(1e-2))
put(P["d1d2"], noise(1e-3))
put(ours / P["dump"].parent.name / P["dump"].name / "vel.bin", noise(1e-3 * scale))
out = io.StringIO()
with contextlib.redirect_stdout(out):
K.main(["--root", str(root), "--ours", str(ours)])
text = out.getvalue()
self.assertIn("| 512 p0 |", text)
self.assertIn("| 512 p5 |", text)
f32_rule = next(line for line in text.splitlines() if line.startswith("RULE vs the float32"))
exact_rule = next(line for line in text.splitlines() if line.startswith("RULE vs the exact"))
self.assertIn("(iii) PASS", f32_rule) # 1e-3 vs 1e-2: ours closer than stock and native
self.assertIn("(ii) largest", f32_rule)
self.assertIn("FAIL", exact_rule) # p5 ours ~3x the exact-sum emulation
def test_case_paths(self):
P = K.case_paths(Path("/r"), "1024 p0")
self.assertEqual(P["acc32"].name, "1024_p0_acc32.npy")
self.assertEqual(K.case_paths(Path("/r"), "512 p6")["d1d2"].parent.parent.name, "z_ours_512")
self.assertEqual(K.case_paths(Path("/r"), "512 p2")["d1d2"].parent.parent.name, "d2_g15")
if __name__ == "__main__":
unittest.main()