Files
leleka/engine/strata/tools/image/install.py
T
Pavlo RatushnyiandClaude Sonnet 5.5 87e2633afe engine/strata: bring the Strata subtree to upstream 41cf05a0 (engine 0.1.50) with the local patches re-applied
Documented tree replacement (the history has no git-subtree metadata): 3-way merge of the old subtree
(570201a:engine/strata) against upstream 41cf05a06a25dd29103b7b27a2279a3f60c21876 (2026-10-25), merge base =
the old pin d9ab8435 (tree-identical to upstream 5b734e42; d9ab8435 is not an ancestor of upstream main).

Local patches re-applied by hand where they conflicted (15 files): STRATA_CUDA_STATIC profile, experimental KAR
experts and KAR PLE arms, keyless V-only arm (now beside upstream's K8V4 hybrid KV and fused norm+rope). Details,
conflict table and left-out files are in docs/upstream.md.

Left out (weights, captures, logs, media): new bench/results (2022 files), binary media and app assets (61),
docs/work/evidence (24), *.bin / *.gguf data files, and the 7 files the first import already left out.

scripts/build-cpu.sh: build the 11 CPU tests upstream registered since the old pin (they ran as "Not Run").
Checked: build-cpu.sh 16/16 ctest in WSL; sm_86 CUDA objects of the patched files compile, also with the KAR
macros on. No GPU run, no quality gate.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-25 12:49:15 +01:00

102 lines
4.9 KiB
Python

"""tools/image/install.py - image generation (Qwen-Image-2.1-Turbo) for an existing Strata install, or on its own:
compiles sd-cli for the backend (setup.py's build_image_engine), downloads and checks the three model files, and
writes the "images" section into a run config (or prints it). setup.py --images qwen-image does the same inside
the normal setup; this is the way to add it without running the whole setup again.
python tools/image/install.py --config strata-q2_0.json (backend and size picked for this PC)
python tools/image/install.py --backend vulkan --quant Q8_0 --models-dir ~/models --print
python tools/image/install.py --backend hip --arch gfx1103 --build-only
--gpu-policy auto|keep|shrink|unload: what the text model does during an image job (serve/images.py).
"""
from __future__ import annotations
import argparse
import json
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT))
sys.path.insert(0, str(ROOT / "tools"))
sys.path.insert(0, str(ROOT / "tools" / "image"))
import qimage as QI # noqa: E402
def detect_gpu(S) -> tuple[dict | None, bool]:
"""setup.py's view of this PC's GPU: (gpu dict, AMD?) - an NVIDIA card first, else an AMD one, else none."""
try:
g = S.gpu_info()
if g:
return {**g, "vendor": "nvidia"}, False
except Exception: # noqa: BLE001 - no NVIDIA driver
pass
try:
amd = S.amd_gpus_win() if S.WIN else S.amd_gpus()
if amd:
g = min(amd, key=S.amd_rank)
return S.amd_apply_uma(dict(g)), True
except Exception: # noqa: BLE001
pass
return None, False
def main(argv=None) -> int:
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("--backend", choices=QI.BACKENDS)
ap.add_argument("--arch", help="the GPU architecture to compile for (CUDA: 86, ...; HIP: gfx1103, ...)")
ap.add_argument("--quant", choices=QI.QUANTS)
ap.add_argument("--gpu-gb", type=float, help="the GPU memory the quant is picked for (default: detected)")
ap.add_argument("--models-dir", help="where the model files go (default: the data folder's models)")
ap.add_argument("--config", help="a run config (strata-<model>.json) to write the \"images\" section into")
ap.add_argument("--gpu-policy", choices=["auto", "keep", "shrink", "unload"], default="auto")
ap.add_argument("--gpu", type=int, help="the GPU's number for the image job (default: the backend's first)")
ap.add_argument("--native", choices=["on", "off"], default="off",
help="EXPERIMENTAL: Strata's own DiT runner on CUDA / HIP (the config's \"native\")")
ap.add_argument("--build-only", action="store_true", help="compile sd-cli only")
ap.add_argument("--print", action="store_true", help="print the images section instead of writing it")
ap.add_argument("--yes", action="store_true")
a = ap.parse_args(argv)
import setup as S
gpu, amd = detect_gpu(S) if not (a.backend and (a.arch or a.backend in ("vulkan", "cpu"))) else (None, False)
gpu = dict(gpu or {})
if a.arch:
gpu["arch"] = a.arch
if a.gpu_gb is not None:
gpu["vram_gb"] = a.gpu_gb
ns = argparse.Namespace(images="qwen-image", image_backend=a.backend, image_quant=a.quant,
image_gpu_policy=a.gpu_policy, yes=a.yes)
backend, quant, gb = S.image_backend_choice(ns, gpu or None, amd or a.backend == "hip")
if backend in ("cuda", "hip") and not gpu.get("arch"):
S.fail(f"the {backend} build needs the GPU's architecture", "pass --arch (e.g. 86 or gfx1103)")
S.ok(f"image generation: {quant} on {backend}" + (f" ({gb:.0f} GB GPU memory)" if gb else ""))
exe, lib_dirs = S.build_image_engine(backend, gpu)
if a.build_only:
return 0
if a.models_dir:
models = Path(a.models_dir).expanduser()
else:
data, _ = S.data_folder(None)
models = data / "models"
S.say(" " + QI.LICENSE_NOTE)
for name in (quant, "vae", "llm"):
repo, path, size, sha = QI.FILES[name]
dst = QI.local_path(models, name)
S.download(QI.file_url(name, S.hf_endpoint()), dst, f"image model: {Path(path).name}")
S.verify_sha256(dst, size, sha)
section = QI.image_config(backend, quant, exe, models, uma=QI.is_apu(gpu), gpu=a.gpu, lib_dirs=lib_dirs,
policy=a.gpu_policy, native=a.native == "on")
if a.print or not a.config:
print(json.dumps({"images": section}, indent=1))
return 0
path = Path(a.config)
cfg = json.loads(path.read_text(encoding="utf-8-sig")) if path.is_file() else {}
cfg["images"] = section
S.write_config(path, cfg)
S.ok(f"{path.name}: image generation added (POST /v1/images/generations once the server starts again)")
return 0
if __name__ == "__main__":
sys.exit(main())