Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio diffusion: use an installed HIP SDK for the Windows ROCm sd.cpp build when its bin dir is not on PATH (#15853)

oobabooga35492deddc3d
diff --git a/studio/backend/core/inference/sd_cpp_backend.py b/studio/backend/core/inference/sd_cpp_backend.py
index 235c087768e..281094ca460 100644
--- a/studio/backend/core/inference/sd_cpp_backend.py
+++ b/studio/backend/core/inference/sd_cpp_backend.py
@@ -1339,8 +1339,8 @@ def accelerator_fallback_reason(accelerator: Optional[str]) -> str:
# The loader cannot be asked ahead of time on Windows; a missing DLL is exit 0xC0000135 with no output.
return (
"the ROCm build needs the AMD HIP SDK 7.x runtime DLLs (amdhip64_7.dll, hipblas.dll, rocblas.dll) on "
- "PATH; without them Windows stops it before it prints anything (exit 0xC0000135), or the card's gfx "
- "target is missing from the prebuilt"
+ "PATH or under HIP_PATH; without them Windows stops it before it prints anything (exit 0xC0000135), or "
+ "the card's gfx target is missing from the prebuilt"
)
missing = rocm_runtime_missing_sonames()
if missing is None:
@@ -2119,7 +2119,7 @@ def _on_windows() -> bool:
def windows_rocm_build_mode() -> str:
- """``UNSLOTH_SD_CPP_WINDOWS_ROCM``: ``auto`` (ROCm only if the HIP SDK is on PATH), ``1`` always, ``0`` never."""
+ """``UNSLOTH_SD_CPP_WINDOWS_ROCM``: ``auto`` (ROCm only if the HIP SDK is found), ``1`` always, ``0`` never."""
raw = (os.environ.get("UNSLOTH_SD_CPP_WINDOWS_ROCM", "auto") or "").strip().lower()
if raw in ("1", "on", "true", "yes", "force"):
return "force"
@@ -2134,6 +2134,10 @@ def _windows_dll_search_dirs() -> list[str]:
dirs.append(os.path.join(system_root, "System32"))
dirs.append(system_root)
dirs.extend(d.strip().strip('"') for d in os.environ.get("PATH", "").split(os.pathsep))
+ try:
+ dirs.extend(_installer_module().windows_hip_sdk_bin_dirs())
+ except Exception as exc: # noqa: BLE001 -- PATH alone still decides
+ logger.debug("HIP SDK lookup failed: %s", exc)
return [d for d in dirs if d]
@@ -2149,6 +2153,13 @@ def windows_hip_runtime_missing_dlls() -> Optional[list[str]]:
return missing
+def _hip_sdk_url() -> str:
+ try:
+ return _installer_module().HIP_SDK_URL
+ except Exception: # noqa: BLE001 -- the log line still names the SDK
+ return "AMD's HIP SDK page"
+
+
def windows_rocm_build_skipped() -> bool:
"""Whether a Windows ROCm request goes to Vulkan (HIP SDK missing would fail with 0xC0000135)."""
if not _on_windows() or not sd_cpp_vulkan_fallback_enabled():
@@ -2173,9 +2184,10 @@ def preferred_accelerator(accelerator: Optional[str], card: Optional[str] = None
_windows_rocm_skip_logged = True
logger.info(
"sd_cpp.windows_rocm_skipped: using the Vulkan stable-diffusion.cpp build; the ROCm "
- "build needs the AMD HIP SDK runtime (%s) on PATH (UNSLOTH_SD_CPP_WINDOWS_ROCM=1 "
- "to use it anyway)",
+ "build needs the AMD HIP SDK 7.x runtime (%s), found on neither PATH nor HIP_PATH. "
+ "Install it from %s to use the ROCm build (UNSLOTH_SD_CPP_WINDOWS_ROCM=1 to use it anyway)",
", ".join(windows_hip_runtime_missing_dlls() or _WINDOWS_ROCM_HOST_DLLS),
+ _hip_sdk_url(),
)
chosen = klass = "vulkan"
seen: set[str] = set()
diff --git a/studio/backend/core/inference/sd_cpp_engine.py b/studio/backend/core/inference/sd_cpp_engine.py
index aae49a59b33..bd9edb5849d 100644
--- a/studio/backend/core/inference/sd_cpp_engine.py
+++ b/studio/backend/core/inference/sd_cpp_engine.py
@@ -300,10 +300,21 @@ def runtime_env(binary: str, base_env: Optional[dict[str, str]] = None) -> dict[
if _needs_host_cuda_runtime(bindir):
dirs.extend(d for d in host_cuda_runtime_dirs() if d not in dirs)
existing = env.get(var, "")
- env[var] = os.pathsep.join(dirs) + (os.pathsep + existing if existing else "")
+ # After the caller's PATH, so an SDK the user put on PATH still wins.
+ env[var] = os.pathsep.join(
+ [*dirs, *([existing] if existing else []), *host_hip_sdk_dirs(bindir)]
+ )
return env
+def host_hip_sdk_dirs(bindir: str) -> list[str]:
+ try:
+ from core.inference import sd_cpp_backend # noqa: PLC0415 - the backend imports this module
+ return sd_cpp_backend._installer_module().hip_sdk_dirs_for_bundle(bindir)
+ except Exception: # noqa: BLE001 - without the installer the child keeps the caller's PATH
+ return []
+
+
def _needs_host_cuda_runtime(bindir: str) -> bool:
try:
names = [n.lower() for n in os.listdir(bindir)]
diff --git a/studio/backend/tests/test_sd_cpp_install_verify.py b/studio/backend/tests/test_sd_cpp_install_verify.py
index 392d138c47f..6d53a8f8079 100644
--- a/studio/backend/tests/test_sd_cpp_install_verify.py
+++ b/studio/backend/tests/test_sd_cpp_install_verify.py
@@ -321,3 +321,101 @@ def test_the_refusal_record_survives_a_fallback_from_another_release(
assert sdmod.read_install_record(root).get("tag") != "master-982-0518b34-ud331c66"
assert backend.accelerator_runtime_failed("rocm") is True
assert backend.preferred_accelerator("rocm") == "vulkan"
+
+
+def _dll_dir(path: Path, *dlls: str) -> Path:
+ path.mkdir(parents = True, exist_ok = True)
+ for dll in dlls:
+ (path / dll).write_bytes(b"")
+ return path
+
+
+def hip_sdk(tmp_path, monkeypatch):
+ """HIP SDK 7.2 as its MSI leaves it: HIP_PATH (with its trailing separator) set, bin dir not on PATH."""
+ program_files = tmp_path / "Program Files"
+ root = program_files / "AMD" / "ROCm" / "7.2"
+ bindir = _dll_dir(root / "bin", "hipblas.dll", "rocblas.dll", "amdhip64_7.dll")
+ monkeypatch.setenv("ProgramFiles", str(program_files))
+ monkeypatch.setenv("HIP_PATH", str(root) + os.sep)
+ monkeypatch.delenv("ROCM_PATH", raising = False)
+ return types.SimpleNamespace(bin = bindir, program_files = program_files)
+
+
+def _rocm_bundle(tmp_path: Path) -> Path:
+ """The upstream win-rocm layout: amdhip64_7.dll beside sd-cli, hipblas.dll left to the SDK."""
+ return _dll_dir(tmp_path / "bundle", _CLI, "stable-diffusion.dll", "amdhip64_7.dll") / _CLI
+
+
+def test_the_hip_sdk_is_found_through_hip_path(hip_sdk):
+ assert sdmod.windows_hip_sdk_bin_dirs() == [str(hip_sdk.bin)]
+
+
+def test_without_hip_path_the_default_location_is_searched_newest_7x_first(hip_sdk, monkeypatch):
+ monkeypatch.delenv("HIP_PATH")
+ rocm = hip_sdk.program_files / "AMD" / "ROCm"
+ older = _dll_dir(rocm / "7.1" / "bin", "hipblas.dll", "amdhip64_7.dll")
+ # A 6.x hipblas.dll binds amdhip64_6.dll, not the bundle's HIP 7 runtime.
+ _dll_dir(rocm / "6.4" / "bin", "hipblas.dll", "amdhip64_6.dll")
+ assert sdmod.windows_hip_sdk_bin_dirs() == [str(hip_sdk.bin), str(older)]
+
+
+def test_no_hip_sdk_finds_nothing(tmp_path, monkeypatch):
+ monkeypatch.setenv("ProgramFiles", str(tmp_path / "Program Files"))
+ monkeypatch.delenv("HIP_PATH", raising = False)
+ monkeypatch.delenv("ROCM_PATH", raising = False)
+ assert sdmod.windows_hip_sdk_bin_dirs() == []
+
+
+def _search_path(env: dict, var: str) -> list[str]:
+ return env[var].split(os.pathsep)
+
+
+def test_the_rocm_bundle_child_gets_the_hip_sdk_after_the_callers_path(tmp_path, hip_sdk):
+ from core.inference import sd_cpp_engine
+
+ binary = _rocm_bundle(tmp_path)
+ var = sd_cpp_engine._lib_path_var()
+ env = sd_cpp_engine.runtime_env(str(binary), {var: "caller-dir"})
+ assert _search_path(env, var) == [str(binary.parent.resolve()), "caller-dir", str(hip_sdk.bin)]
+ probe = sdmod._probe_env(binary)
+ assert _search_path(probe, var)[0] == str(binary.parent)
+ assert _search_path(probe, var)[-1] == str(hip_sdk.bin)
+
+
+def test_other_bundles_and_a_bundled_hipblas_get_no_hip_sdk(tmp_path, hip_sdk):
+ from core.inference import sd_cpp_engine
+
+ vulkan = _dll_dir(tmp_path / "vk", _CLI, "stable-diffusion.dll") / _CLI
+ rocm_with_hipblas = _dll_dir(tmp_path / "full", _CLI, "amdhip64_7.dll", "hipblas.dll") / _CLI
+ var = sd_cpp_engine._lib_path_var()
+ for binary in (vulkan, rocm_with_hipblas):
+ assert sdmod.hip_sdk_dirs_for_bundle(binary.parent) == []
+ env = sd_cpp_engine.runtime_env(str(binary), {var: "caller-dir"})
+ assert str(hip_sdk.bin) not in _search_path(env, var)
+
+
+def windows_path_only(tmp_path, monkeypatch, backend):
+ system_root = tmp_path / "Windows"
+ (system_root / "System32").mkdir(parents = True)
+ monkeypatch.setenv("SystemRoot", str(system_root))
+ monkeypatch.setenv("PATH", str(_dll_dir(tmp_path / "on-path")))
+ monkeypatch.delenv("UNSLOTH_SD_CPP_WINDOWS_ROCM", raising = False)
+ monkeypatch.setattr(backend, "_on_windows", lambda: True, raising = False)
+ return backend
+
+
+def test_windows_rocm_with_the_hip_sdk_installed_off_path_keeps_rocm(windows_path_only, hip_sdk):
+ assert windows_path_only.windows_hip_runtime_missing_dlls() == []
+ assert windows_path_only.preferred_accelerator("rocm") == "rocm"
+
+
+def test_windows_rocm_without_any_hip_sdk_still_starts_at_vulkan(
+ windows_path_only, tmp_path, monkeypatch
+):
+ monkeypatch.setenv("ProgramFiles", str(tmp_path / "Program Files"))
+ monkeypatch.delenv("HIP_PATH", raising = False)
+ monkeypatch.delenv("ROCM_PATH", raising = False)
+ assert windows_path_only.windows_hip_runtime_missing_dlls() == ["hipblas.dll"]
+ assert windows_path_only.preferred_accelerator("rocm") == "vulkan"
diff --git a/studio/install_sd_cpp_prebuilt.py b/studio/install_sd_cpp_prebuilt.py
index 7cb4a714598..96b0bdf02b7 100644
--- a/studio/install_sd_cpp_prebuilt.py
+++ b/studio/install_sd_cpp_prebuilt.py
@@ -725,6 +725,46 @@ def loader_refusal(returncode: Optional[int], output: str = "") -> Optional[str]
return None
+HIP_SDK_URL = "https://www.amd.com/en/developer/resources/rocm-hub/hip-sdk.html"
+
+
+def _rocm_version_key(name: str) -> tuple:
+ return tuple((0, int(p)) if p.isdigit() else (1, p) for p in name.split("."))
+
+
+def windows_hip_sdk_bin_dirs() -> list[str]:
+ """HIP SDK 7.x bin dirs with hipblas.dll: HIP_PATH / ROCM_PATH, then Program Files newest first (installer never adds bin to PATH)."""
+ roots = [os.environ.get("HIP_PATH"), os.environ.get("ROCM_PATH")]
+ base = Path(os.environ.get("ProgramFiles") or r"C:\Program Files") / "AMD" / "ROCm"
+ try:
+ roots.extend(
+ str(p)
+ for p in sorted(base.iterdir(), key = lambda p: _rocm_version_key(p.name), reverse = True)
+ )
+ except OSError:
+ pass
+ dirs: list[str] = []
+ for root in roots:
+ root = (root or "").strip().strip('"')
+ if not root:
+ continue
+ bindir = os.path.join(root, "bin")
+ # amdhip64_7.dll marks a 7.x SDK: a 6.x hipblas.dll binds the other HIP runtime.
+ if bindir not in dirs and all(
+ os.path.isfile(os.path.join(bindir, dll)) for dll in ("hipblas.dll", "amdhip64_7.dll")
+ ):
+ dirs.append(bindir)
+ return dirs
+
+
+def hip_sdk_dirs_for_bundle(bindir: Path | str) -> list[str]:
+ """SDK dirs for a ROCm bundle shipping amdhip64_7.dll but importing the SDK's hipblas.dll; else []."""
+ bindir = Path(bindir)
+ if not (bindir / "amdhip64_7.dll").is_file() or (bindir / "hipblas.dll").is_file():
+ return []
+ return windows_hip_sdk_bin_dirs()
+
+
def _probe_env(binary: Path) -> dict:
env = dict(os.environ)
if sys.platform == "win32":
@@ -734,7 +774,12 @@ def _probe_env(binary: Path) -> dict:
else:
var = "LD_LIBRARY_PATH"
existing = env.get(var, "")
- env[var] = str(binary.parent) + (os.pathsep + existing if existing else "")
+ parts = [
+ str(binary.parent),
+ *([existing] if existing else []),
+ *hip_sdk_dirs_for_bundle(binary.parent),
+ ]
+ env[var] = os.pathsep.join(parts)
return env
@@ -837,7 +882,8 @@ def _verify_bundle_starts(
hint = ""
if sys.platform == "win32" and accelerator_class(accelerator) == "rocm":
hint = (
- " The ROCm build needs the AMD HIP SDK runtime (hipblas.dll, rocblas.dll) on PATH."
+ " The ROCm build needs the AMD HIP SDK 7.x runtime (hipblas.dll, rocblas.dll), found on PATH or "
+ f"under HIP_PATH; install it from {HIP_SDK_URL}."
)
raise UnrunnableBuildError(
f"{chosen}: {name} cannot start on this host: {why}. Kept the existing install.{hint}",