okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e4309aaf3
main
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 6python -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 ): 19spec = importlib .util .spec_from_file_location (name ,path ) 20m = importlib .util .module_from_spec (spec ) 21spec .loader .exec_module (m ) 22return 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 : 33sd = d / "stable-diffusion.cpp" 34for 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 ) 38if native : 39N .apply (sd ) 40return sd 41 42 43class DitApply (unittest .TestCase ): 44def test_apply (self ): 45with tempfile .TemporaryDirectory ()as td : 46sd = fake_tree (Path (td )) 47D .apply (sd ) 48for src ,rel in D .FILES : 49self .assertEqual ((sd / "src" / "strata_dit" / rel ).read_bytes (), (ROOT / src ).read_bytes (),rel ) 50self .assertTrue ((sd / "src" / "strata_dit" / D .HIP_COMPAT / "cublas_v2.h" ).is_file ()) 51nat = (sd / "src" / "model" / "diffusion" / "qwen_image_2_1_native.hpp" ).read_text () 52self .assertEqual (nat .count ('#include "strata/image/dit_gemm.hpp"' ),1 ) 53self .assertEqual (nat .count ('std::strstr(model_args, "strata_dit_gemm=1")' ),1 ) 54self .assertEqual (nat .count ("strata::image::dit_gemm(" ),2 )# gemm() and the prelude / head helper 55for part in ("timestep MLP, modulation and the final scale" ,": txt_in" ,": the output head" ): 56self .assertEqual (nat .count (part ),1 ,part ) 57self .assertEqual (nat .count ("} else\n#endif\n" ),3 )# each falls back to the ggml block 58self .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 60self .assertIn ("#endif\n ggml_tensor* y = ggml_mul_mat(g.ctx, weight, rows(g.ctx, src, n));" ,nat ) 61cm = (sd / "CMakeLists.txt" ).read_text () 62self .assertEqual (cm .count ("add_library(strata_dit STATIC" ),2 )# CUDA and HIP branches 63self .assertEqual (cm .count ("target_compile_definitions(${SD_LIB} PRIVATE SD_STRATA_DIT)" ),1 ) 64before = {p :p .read_bytes ()for p in sd .rglob ("*" )if p .is_file ()} 65D .apply (sd ) 66after = {p :p .read_bytes ()for p in sd .rglob ("*" )if p .is_file ()} 67self .assertEqual (before ,after ) 68 69def test_needs_native_runner (self ): 70with tempfile .TemporaryDirectory ()as td : 71sd = fake_tree (Path (td ),native = False ) 72with self .assertRaises (SystemExit ): 73D .apply (sd ) 74 75 76if __name__ == "__main__" : 77unittest .main ()