Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

setup --backend hip: --vision cpu builds and uses the CPU image encoder (#304)

Niko12212282393234ed
diff --git a/setup.py b/setup.py
index e1c15ffa0..e15af279e 100644
--- a/setup.py
+++ b/setup.py
@@ -1012,19 +1012,24 @@ def hipblaslt_table(arch, lib_dirs):
return None
-def build_engine_hip(gpu, llama) -> Path:
+def build_engine_hip(gpu, llama, vision="none") -> Path:
"""Compile the HIP engine for this AMD GPU into engine/ (again only when its source changed: a `git pull`).
- gpu["archs"]: every architecture it needs code for (the cards of a layer split), else gpu["arch"]."""
+ gpu["archs"]: every architecture it needs code for (the cards of a layer split), else gpu["arch"]. vision "cpu"
+ (#304): the image encoder too, for the CPU (there is no HIP encoder build yet)."""
eng = ROOT / "engine"
eng.mkdir(exist_ok=True)
stamp = eng / "BUILD.json"
meta = json.loads(stamp.read_text()) if stamp.exists() else {}
- src = source_hash(ENGINE_SOURCES)
+ src, vsrc = source_hash(ENGINE_SOURCES), source_hash(VISION_SOURCES)
archs = sorted(set(gpu.get("archs") or [gpu["arch"]]))
has_archs = set(archs) <= set(meta.get("archs", []))
- if meta.get("backend") == "hip" and (eng / EXE).exists() and meta.get("src") == src and has_archs:
+ engine_ok = meta.get("backend") == "hip" and (eng / EXE).exists() and meta.get("src") == src and has_archs
+ vision_ok = vision == "none" or ((eng / VEXE).exists() and meta.get("vision_src") == vsrc)
+ if engine_ok and vision_ok:
ok("engine already built for this PC")
return eng
+ if engine_ok:
+ return build_vision_cpu(eng, stamp, meta, llama, vsrc)
if not (shutil.which("c++") or shutil.which("g++")) or not shutil.which("git"):
fail("a C++ compiler and git are needed to compile the AMD engine",
"Ubuntu/Debian: sudo apt install build-essential git Fedora: sudo dnf install gcc-c++ git")
@@ -1048,12 +1053,35 @@ def build_engine_hip(gpu, llama) -> Path:
f"-DCMAKE_HIP_FLAGS=--rocm-path={root} --rocm-device-lib-path={bitcode}",
f"-DSTRATA_GGML_DIR={llama}"], None, "")
shutil.copy2(ROOT / "build-hip" / EXE, eng / EXE)
- stamp.write_text(json.dumps({"source": "local-hip", "backend": "hip", "version": source_version(),
- "archs": archs, "vision": "none", "lib_dirs": dirs, "src": src}, indent=1))
+ meta = {"source": "local-hip", "backend": "hip", "version": source_version(), "archs": archs, "vision": "none",
+ "lib_dirs": dirs, "src": src}
+ if vision != "none":
+ return build_vision_cpu(eng, stamp, meta, llama, vsrc)
+ stamp.write_text(json.dumps(meta, indent=1))
ok(f"engine compiled: {eng / EXE}")
return eng
+def hip_vision(asked) -> str:
+ """The image encoder with the AMD backend (--vision): the CPU one when asked for (#304); a HIP (GPU) encoder build
+ is a later step, so `yes`/`gpu` leave images off, as before, and say how to get them."""
+ if asked in ("yes", "gpu"):
+ warn("the AMD backend has no GPU image encoder yet: images off (--vision cpu reads them on the CPU)")
+ return "cpu" if asked == "cpu" else "none"
+
+
+def build_vision_cpu(eng: Path, stamp: Path, meta: dict, llama, vsrc) -> Path:
+ """#304: the CPU image encoder beside the HIP engine (tools/vision without CUDA), recorded in its BUILD.json."""
+ if not ((eng / VEXE).exists() and meta.get("vision_src") == vsrc):
+ say(" Compiling the image encoder (for the CPU) ...")
+ cmake_build(ROOT / "tools" / "vision", ROOT / "build-vision", "strata-vision",
+ [f"-DLLAMA_DIR={llama}", "-DSTRATA_VISION_CUDA=OFF"], None, "")
+ shutil.copy2(ROOT / "build-vision" / "bin" / VEXE, eng / VEXE)
+ stamp.write_text(json.dumps({**meta, "vision": "cpu", "vision_src": vsrc}, indent=1))
+ ok(f"engine: {eng / EXE}, image encoder (CPU): {eng / VEXE}")
+ return eng
+
+
# ------------------------------------------------------------------------------------------------ the engine
def driver_major(gpu):
try:
@@ -1174,7 +1202,7 @@ def update_installed_engine(url_base) -> None:
raise RuntimeError("no supported AMD GPU found")
# every architecture it was built for (a layer split across two families keeps both)
build_engine_hip({**g, "archs": [x for x in meta.get("archs", []) if x in AMD_ARCHS] or [g["arch"]]},
- get_llama_cpp())
+ get_llama_cpp(), meta.get("vision") or "none")
except (Exception, SystemExit) as e:
warn(f"could not compile the updated engine{'' if isinstance(e, SystemExit) else f' ({e})'}: "
"starting the installed one")
@@ -2317,9 +2345,7 @@ def main() -> int:
if ctx > 8192:
ok(f"KV cache: {'8-bit' if kv == 'int8' else '4-bit (Hadamard-rotated)'}")
if hip:
- vision = "none"
- if a.vision not in (None, "no", "none"):
- warn("images are not available with the AMD backend yet: off")
+ vision = hip_vision(a.vision)
elif a.vision:
vision = {"yes": "gpu", "no": "none"}.get(a.vision, a.vision)
else:
@@ -2406,7 +2432,7 @@ def main() -> int:
else:
vision = prebuilt_vision(json.loads((eng / "BUILD.json").read_text()), gpu, vision)
if eng is None:
- eng = build_engine_hip(gpu, llama) if hip else build_engine(gpu, vision, a.yes, llama)
+ eng = build_engine_hip(gpu, llama, vision) if hip else build_engine(gpu, vision, a.yes, llama)
meta = json.loads((eng / "BUILD.json").read_text())
lib_dirs = meta.get("lib_dirs") or meta.get("cuda_dirs") or cuda_lib_dirs()
ok(f"engine: {eng / EXE}")
diff --git a/tools/test_setup_choices.py b/tools/test_setup_choices.py
index 3b178ca03..b024dd4b4 100644
--- a/tools/test_setup_choices.py
+++ b/tools/test_setup_choices.py
@@ -1,6 +1,6 @@
"""Tests for setup.py's choices that depend on the PC (no GPU, no network, nothing installed): the image encoder of a
ready-made engine that has no code for the card (#331), --gguf-dir's shard count (#305), the experimental
-Pascal/Volta build (#295).
+Pascal/Volta build (#295), the CPU image encoder with the AMD backend (#304).
python -m unittest tools.test_setup_choices
"""
@@ -8,6 +8,7 @@ from __future__ import annotations
import contextlib
import io
+import json
import os
import sys
import tempfile
@@ -156,5 +157,56 @@ class ExperimentalSm60(unittest.TestCase):
self.tools(archs, lambda below: (None, None) if below else ("nvcc13", (13, 0)))
+class HipVision(unittest.TestCase):
+ """#304: --vision cpu with --backend hip builds the CPU image encoder beside the HIP engine."""
+
+ def test_the_choice(self):
+ self.assertEqual(quiet(setup.hip_vision, "cpu"), ("cpu", ""))
+ for asked in (None, "no", "none"):
+ self.assertEqual(quiet(setup.hip_vision, asked), ("none", ""))
+ for asked in ("yes", "gpu"):
+ got, out = quiet(setup.hip_vision, asked)
+ self.assertEqual(got, "none")
+ self.assertIn("--vision cpu", out)
+
+ def build(self, meta, vision, vexe=False):
+ """build_engine_hip with an engine that is already built: only the encoder can be missing."""
+ with tempfile.TemporaryDirectory() as d:
+ root = Path(d)
+ eng = root / "engine"
+ eng.mkdir()
+ (eng / setup.EXE).write_bytes(b"engine")
+ if vexe:
+ (eng / setup.VEXE).write_bytes(b"vision")
+ src, vsrc = "S", "V"
+ (eng / "BUILD.json").write_text(json.dumps({"backend": "hip", "archs": ["gfx1201"], "src": src, **meta}))
+ built = []
+
+ def cmake_build(src_dir, bdir, target, defs, vcvars, bat):
+ built.append((target, defs))
+ (bdir / "bin").mkdir(parents=True)
+ (bdir / "bin" / setup.VEXE).write_bytes(b"vision")
+
+ with mock.patch.object(setup, "ROOT", root), mock.patch.object(setup, "cmake_build", cmake_build), \
+ mock.patch.object(setup, "source_hash", lambda paths: vsrc if paths == setup.VISION_SOURCES else src):
+ quiet(setup.build_engine_hip, {"arch": "gfx1201"}, "llama", vision)
+ return built, json.loads((eng / "BUILD.json").read_text()), (eng / setup.VEXE).exists()
+
+ def test_the_cpu_encoder_is_built_once(self):
+ built, meta, have = self.build({}, "cpu")
+ self.assertEqual([t for t, _ in built], ["strata-vision"])
+ self.assertIn("-DSTRATA_VISION_CUDA=OFF", built[0][1])
+ self.assertEqual((meta["vision"], meta["vision_src"], have), ("cpu", "V", True))
+ built, meta, _ = self.build({"vision": "cpu", "vision_src": "V"}, "cpu", vexe=True)
+ self.assertEqual(built, []) # built and unchanged: nothing to do
+ built, meta, _ = self.build({"vision": "cpu", "vision_src": "old"}, "cpu", vexe=True)
+ self.assertEqual([t for t, _ in built], ["strata-vision"]) # its source changed: again
+
+ def test_without_images_nothing_changes(self):
+ built, meta, have = self.build({}, "none")
+ self.assertEqual((built, have), ([], False))
+ self.assertNotIn("vision_src", meta)
+
+
if __name__ == "__main__":
unittest.main()