okami.mom/strata

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

Niko1221Merge f-qimage2 into f-144-relc106e9629

main
4.5 KiB75 linesraw
1"""tools/image/sdcpp_native/apply.py - put Strata's native Qwen-Image-2.1 DiT runner into a stable-diffusion.cpp
2checkout at the pinned commit (tools/image/qimage.py SD_CPP_COMMIT).  Idempotent; the stock runner stays the
3default: sd-cli uses the native one only with --model-args qwen_image_2_1_native=1 (serve/images.py adds it when the
4config's "native" is true), and only in CUDA / HIP builds.
5
6    python tools/image/sdcpp_native/apply.py third_party/stable-diffusion.cpp
7
8Two new files (the kernels into ggml/src/ggml-cuda/, which ggml-cuda and ggml-hip compile by glob; the runner into
9src/model/diffusion/) and two small edits (model_builders.cpp picks the runner; CMakeLists.txt defines SD_QI_NATIVE
10for CUDA / HIP builds)."""
11from __future__ import annotations
12
13import shutil
14import sys
15from pathlib import Path
16
17HERE = Path(__file__).resolve().parent
18MARK = "// strata: native Qwen-Image-2.1 runner"
19
20BUILDER_OLD = """            if (version == VERSION_QWEN_IMAGE_2_1) {
21                result.diffusion = std::make_shared<Qwen::QwenImage21Runner>(ctx.backends.runtime_backend(SDBackendModule::DIFFUSION),
22                                                                             tensor_storage_map,
23                                                                             "model.diffusion_model",
24                                                                             weight_manager,
25                                                                             sd_ctx_params->model_args);
26"""
27BUILDER_NEW = """            if (version == VERSION_QWEN_IMAGE_2_1) {
28#ifdef SD_QI_NATIVE
29                """ + MARK + """ (--model-args qwen_image_2_1_native=1)
30                if (sd_ctx_params->model_args != nullptr &&
31                    std::strstr(sd_ctx_params->model_args, "qwen_image_2_1_native=1") != nullptr &&
32                    sd_ctx_params->diffusion_model_path != nullptr && sd_ctx_params->diffusion_model_path[0] != 0) {
33                    result.diffusion = std::make_shared<Qwen::QwenImage21NativeRunner>(ctx.backends.runtime_backend(SDBackendModule::DIFFUSION),
34                                                                                       tensor_storage_map,
35                                                                                       "model.diffusion_model",
36                                                                                       weight_manager,
37                                                                                       sd_ctx_params->model_args,
38                                                                                       sd_ctx_params->diffusion_model_path);
39                } else
40#endif
41                result.diffusion = std::make_shared<Qwen::QwenImage21Runner>(ctx.backends.runtime_backend(SDBackendModule::DIFFUSION),
42                                                                             tensor_storage_map,
43                                                                             "model.diffusion_model",
44                                                                             weight_manager,
45                                                                             sd_ctx_params->model_args);
46"""
47INCLUDE_OLD = '#include "model/diffusion/minit2i.hpp"\n'
48INCLUDE_NEW = INCLUDE_OLD + "#ifdef SD_QI_NATIVE\n#include \"model/diffusion/qwen_image_2_1_native.hpp\"  " + MARK + "\n#endif\n"
49CMAKE_OLD = "target_include_directories(${SD_LIB} PUBLIC . src include)\n"
50CMAKE_NEW = ("if (SD_CUDA OR SD_HIPBLAS)  # " + MARK.lstrip("/ ") + "\n"
51             "    target_compile_definitions(${SD_LIB} PRIVATE SD_QI_NATIVE)\nendif()\n" + CMAKE_OLD)
52
53
54def edit(path: Path, old: str, new: str) -> None:
55    text = path.read_text(encoding="utf-8")
56    if new in text:
57        return
58    if text.count(old) != 1:
59        raise SystemExit(f"{path}: the expected code is not there (another stable-diffusion.cpp commit?)")
60    path.write_text(text.replace(old, new), encoding="utf-8")
61
62
63def apply(sd: Path) -> None:
64    sd = Path(sd)
65    shutil.copy2(HERE / "qi21-native.cu", sd / "ggml" / "src" / "ggml-cuda" / "qi21-native.cu")
66    shutil.copy2(HERE / "qwen_image_2_1_native.hpp", sd / "src" / "model" / "diffusion" / "qwen_image_2_1_native.hpp")
67    builders = sd / "src" / "pipeline" / "model_builders.cpp"
68    edit(builders, INCLUDE_OLD, INCLUDE_NEW)
69    edit(builders, BUILDER_OLD, BUILDER_NEW)
70    edit(sd / "CMakeLists.txt", CMAKE_OLD, CMAKE_NEW)
71
72
73if __name__ == "__main__":
74    apply(Path(sys.argv[1]))
75    print("native Qwen-Image-2.1 runner applied")