okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-5b adfcf9028 into f-own-int (round 16: the strata-image test program and its parts, DiT runner, sampler, noise, PNG writer; hooked into no build)e4309aaf3

main
3.6 KiB77 linesraw
1"""Tests for the patch that puts Strata's DiT GEMMs into the native Qwen-Image-2.1 runner of sd-cli
2(tools/image/sdcpp_dit/apply.py), on a stand-in checkout with the native runner applied by its own patch: the sources it
3copies (byte for byte), the CMake target, the runner's switch (strata_dit_gemm=1, off without it) and its gemm() branch,
4that a second run changes nothing, and that a checkout without the native runner is refused.
5
6    python -m unittest tools.test_sdcpp_dit
7"""
8from __future__ import annotations
9
10import importlib.util
11import tempfile
12import unittest
13from pathlib import Path
14
15ROOT = Path(__file__).resolve().parents[1]
16
17
18def load(name, path):
19    spec = importlib.util.spec_from_file_location(name, path)
20    m = importlib.util.module_from_spec(spec)
21    spec.loader.exec_module(m)
22    return m
23
24
25D = load("strata_sdcpp_dit_apply_test", ROOT / "tools" / "image" / "sdcpp_dit" / "apply.py")
26N = load("strata_sdcpp_native_apply_for_dit_test", ROOT / "tools" / "image" / "sdcpp_native" / "apply.py")
27
28CMAKE = "add_library(${SD_LIB} STATIC ${SD_LIB_SOURCES})\ntarget_include_directories(${SD_LIB} PUBLIC . src include)\n"
29BUILDERS = '#include "model/diffusion/minit2i.hpp"\n' + N.BUILDER_OLD
30
31
32def fake_tree(d: Path, native: bool = True) -> Path:
33    sd = d / "stable-diffusion.cpp"
34    for p in ("src/pipeline", "src/model/diffusion", "ggml/src/ggml-cuda"):
35        (sd / p).mkdir(parents=True)
36    (sd / "src" / "pipeline" / "model_builders.cpp").write_text(BUILDERS)
37    (sd / "CMakeLists.txt").write_text(CMAKE)
38    if native:
39        N.apply(sd)
40    return sd
41
42
43class DitApply(unittest.TestCase):
44    def test_apply(self):
45        with tempfile.TemporaryDirectory() as td:
46            sd = fake_tree(Path(td))
47            D.apply(sd)
48            for src, rel in D.FILES:
49                self.assertEqual((sd / "src" / "strata_dit" / rel).read_bytes(), (ROOT / src).read_bytes(), rel)
50            self.assertTrue((sd / "src" / "strata_dit" / D.HIP_COMPAT / "cublas_v2.h").is_file())
51            nat = (sd / "src" / "model" / "diffusion" / "qwen_image_2_1_native.hpp").read_text()
52            self.assertEqual(nat.count('#include "strata/image/dit_gemm.hpp"'), 1)
53            self.assertEqual(nat.count('std::strstr(model_args, "strata_dit_gemm=1")'), 1)
54            self.assertEqual(nat.count("strata::image::dit_gemm("), 2)          # gemm() and the prelude / head helper
55            for part in ("timestep MLP, modulation and the final scale", ": txt_in", ": the output head"):
56                self.assertEqual(nat.count(part), 1, part)
57            self.assertEqual(nat.count("} else\n#endif\n"), 3)                  # each falls back to the ggml block
58            self.assertEqual(nat.count("bool strata_gemm = false;"), 1)
59            # without the argument the old ggml_mul_mat path is still there, right after the branch
60            self.assertIn("#endif\n            ggml_tensor* y = ggml_mul_mat(g.ctx, weight, rows(g.ctx, src, n));", nat)
61            cm = (sd / "CMakeLists.txt").read_text()
62            self.assertEqual(cm.count("add_library(strata_dit STATIC"), 2)          # CUDA and HIP branches
63            self.assertEqual(cm.count("target_compile_definitions(${SD_LIB} PRIVATE SD_STRATA_DIT)"), 1)
64            before = {p: p.read_bytes() for p in sd.rglob("*") if p.is_file()}
65            D.apply(sd)
66            after = {p: p.read_bytes() for p in sd.rglob("*") if p.is_file()}
67            self.assertEqual(before, after)
68
69    def test_needs_native_runner(self):
70        with tempfile.TemporaryDirectory() as td:
71            sd = fake_tree(Path(td), native=False)
72            with self.assertRaises(SystemExit):
73                D.apply(sd)
74
75
76if __name__ == "__main__":
77    unittest.main()