Files
Strata/tools/test_sdcpp_dit.py
T
Niko1221 09d67a33d4 image: own DiT GEMMs for the native Qwen-Image-2.1 runner (D1+D2, test builds only)
D1 and D2 together: every multiply of the native runner's DiT call on our own GEMM
(src/image/dit/dit_gemm.cu: weights decoded to f16 by te_dequant, activations to f16,
cuBLAS / hipBLAS GemmEx with float32 sums). Groups: blocks + img_in (D1); timestep MLP,
modulation, norm_out; txt_in; the head (D2). Wired into the pinned stable-diffusion.cpp
by tools/image/sdcpp_dit/apply.py (build_variant.py --dit) behind
--model-args strata_dit_gemm=1. No build or product path uses it; sd.cpp stays the
default and the fallback.

Measured together, not D1 alone: D1 alone was 0.00331 from the f32 reference against
the emulation's 0.00065 (5x); only all four groups together reach the emulation's level,
because the multiplies left on ggml's MMQ (8-bit activations) then dominate.
vel_0 rel L2 to f32, 512 prompts 0-3 (mean), per group switched on:
  none (native runner) 0.00609 | all (D1+D2) 0.00065 | D1 0.00331 |
  D1 + timestep/modulation/norm_out 0.00271 | D1 + txt_in 0.00342 | D1 + head 0.00200 |
  f16 emulation 0.00065 | stock 0.00678

Stage rule (coordinator's restated rule: mean within 1.25x the emulation, no case above
1.5x, every case closer than stock and native). Nine cases (prompts 0-7 at 512, prompt 0
at 1024), velocity rel L2 to f32:
  against the emulation with float32 sums (dit_ref mode f16acc32): mean ratio 1.039,
  largest 1.177 (512 p6); against the emulation with exact sums (mode f16op): mean
  ratio 1.116, 512 p5 at 2.078. Every case 0.00048-0.00114 from f32; stock 0.0049-0.0090,
  native 0.0052-0.0083.
The comparison was changed to the float32-sum emulation after the measurement
(coordinator, 11-11): on prompt 5 the exact-sum emulation moves from 0.00055 to 0.00086
when started from inputs that differ by 3e-6; a per-block trace found no step that differs
from the emulation's arithmetic (block 20 replayed from our input agrees to 0.0002). Both
modes stay in dit_ref.py; tools/image/gate/dit_acc32_cases.py reproduces the table.

Gate (a): with the argument off, 16 pictures (prompts 0-7, seeds 42 and 1234, 1024)
byte-identical to the build before D1: 16 of 16 (dgxspark-20261111-0513-8147).
Pictures, 16 against stock sd.cpp, PSNR mean / min, SSIM mean / min (GB10):
  sd.cpp cuBLAS-forced 33.35 / 23.06 dB, 0.979 / 0.937
  sd.cpp native runner 34.32 / 21.32 dB, 0.982 / 0.923
  ours (D1+D2)         34.45 / 23.74 dB, 0.982 / 0.948
  ours vs native       35.90 / 20.71 dB, 0.982 / 0.912
Sampling median at 1024: stock 19.57 s, native 12.32 s, ours 12.54 s.

Also: te_type_supported / te_row_bytes moved into te_dequant.cu (needed without the
host decoder); run_set.py --model-arg; test-only trace hooks in apply.py
(STRATA_DIT_TRACE_DIR / STRATA_DIT_TRACE_BLOCK, read after a backend sync, change no
value); dit_replay.py / dit_replay2.py (the prompt-5 trace tools).
Tests: tools/test_sdcpp_dit.py, tools/image/gate/test_dit_ref.py (modes).
2026-11-11 05:26:49 +01:00

78 lines
3.6 KiB
Python

"""Tests for the patch that puts Strata's DiT GEMMs into the native Qwen-Image-2.1 runner of sd-cli
(tools/image/sdcpp_dit/apply.py), on a stand-in checkout with the native runner applied by its own patch: the sources it
copies (byte for byte), the CMake target, the runner's switch (strata_dit_gemm=1, off without it) and its gemm() branch,
that a second run changes nothing, and that a checkout without the native runner is refused.
python -m unittest tools.test_sdcpp_dit
"""
from __future__ import annotations
import importlib.util
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
def load(name, path):
spec = importlib.util.spec_from_file_location(name, path)
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
return m
D = load("strata_sdcpp_dit_apply_test", ROOT / "tools" / "image" / "sdcpp_dit" / "apply.py")
N = load("strata_sdcpp_native_apply_for_dit_test", ROOT / "tools" / "image" / "sdcpp_native" / "apply.py")
CMAKE = "add_library(${SD_LIB} STATIC ${SD_LIB_SOURCES})\ntarget_include_directories(${SD_LIB} PUBLIC . src include)\n"
BUILDERS = '#include "model/diffusion/minit2i.hpp"\n' + N.BUILDER_OLD
def fake_tree(d: Path, native: bool = True) -> Path:
sd = d / "stable-diffusion.cpp"
for p in ("src/pipeline", "src/model/diffusion", "ggml/src/ggml-cuda"):
(sd / p).mkdir(parents=True)
(sd / "src" / "pipeline" / "model_builders.cpp").write_text(BUILDERS)
(sd / "CMakeLists.txt").write_text(CMAKE)
if native:
N.apply(sd)
return sd
class DitApply(unittest.TestCase):
def test_apply(self):
with tempfile.TemporaryDirectory() as td:
sd = fake_tree(Path(td))
D.apply(sd)
for src, rel in D.FILES:
self.assertEqual((sd / "src" / "strata_dit" / rel).read_bytes(), (ROOT / src).read_bytes(), rel)
self.assertTrue((sd / "src" / "strata_dit" / D.HIP_COMPAT / "cublas_v2.h").is_file())
nat = (sd / "src" / "model" / "diffusion" / "qwen_image_2_1_native.hpp").read_text()
self.assertEqual(nat.count('#include "strata/image/dit_gemm.hpp"'), 1)
self.assertEqual(nat.count('std::strstr(model_args, "strata_dit_gemm=1")'), 1)
self.assertEqual(nat.count("strata::image::dit_gemm("), 2) # gemm() and the prelude / head helper
for part in ("timestep MLP, modulation and the final scale", ": txt_in", ": the output head"):
self.assertEqual(nat.count(part), 1, part)
self.assertEqual(nat.count("} else\n#endif\n"), 3) # each falls back to the ggml block
self.assertEqual(nat.count("bool strata_gemm = false;"), 1)
# without the argument the old ggml_mul_mat path is still there, right after the branch
self.assertIn("#endif\n ggml_tensor* y = ggml_mul_mat(g.ctx, weight, rows(g.ctx, src, n));", nat)
cm = (sd / "CMakeLists.txt").read_text()
self.assertEqual(cm.count("add_library(strata_dit STATIC"), 2) # CUDA and HIP branches
self.assertEqual(cm.count("target_compile_definitions(${SD_LIB} PRIVATE SD_STRATA_DIT)"), 1)
before = {p: p.read_bytes() for p in sd.rglob("*") if p.is_file()}
D.apply(sd)
after = {p: p.read_bytes() for p in sd.rglob("*") if p.is_file()}
self.assertEqual(before, after)
def test_needs_native_runner(self):
with tempfile.TemporaryDirectory() as td:
sd = fake_tree(Path(td), native=False)
with self.assertRaises(SystemExit):
D.apply(sd)
if __name__ == "__main__":
unittest.main()