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: fix WSL Strix Halo GPU on reinstall (ROCDXG drop-in + system HIP before bundle) (#6227)

Daniel Han240c0c350027
diff --git a/install.sh b/install.sh
index eba92d07465..af062ae0573 100755
--- a/install.sh
+++ b/install.sh
@@ -2047,6 +2047,37 @@ _pick_radeon_wheel() {
# CPU, non-Strix WSL) skips it and normal detection runs unchanged. NEVER aborts
# the installer -- always returns 0. Runs the idempotent helper (ROCm 7.2 +
# librocdxg), then sources the env it persisted so detection finds the GPU.
+# Export the ROCm-on-WSL env into this process and persist it to /etc/profile.d
+# so non-login Studio/llama launches inherit it. Idempotent (writes only when
+# the drop-in is missing); no-op without librocdxg, so never fires off WSL.
+# /etc/profile.d is root-owned -- sudo-tee when not root, else ROCm vanishes
+# after this shell on a non-root reinstall. Best-effort either way.
+_persist_rocm_wsl_dropin() {
+ [ -e /opt/rocm/lib/librocdxg.so ] || [ -e /opt/rocm/lib64/librocdxg.so ] || return 0
+ _rw_rocm=/opt/rocm
+ export HSA_ENABLE_DXG_DETECTION=1
+ export TORCH_ROCM_AOTRITON_ENABLE_EXPERIMENTAL=1
+ case ":${PATH}:" in
+ *":${_rw_rocm}/bin:"*) ;;
+ *) export PATH="${_rw_rocm}/bin:${PATH}" ;;
+ esac
+ export LD_LIBRARY_PATH="${_rw_rocm}/lib:${LD_LIBRARY_PATH:-}"
+ [ -r /etc/profile.d/unsloth-rocm-wsl.sh ] && return 0
+ _rw_dropin="$(
+ printf '# >>> Unsloth ROCm-on-WSL (gfx1151) >>>\n'
+ printf 'export HSA_ENABLE_DXG_DETECTION=1\n'
+ printf 'export TORCH_ROCM_AOTRITON_ENABLE_EXPERIMENTAL=1\n'
+ printf 'export PATH="%s/bin:${PATH}"\n' "${_rw_rocm}"
+ printf 'export LD_LIBRARY_PATH="%s/lib:${LD_LIBRARY_PATH:-}"\n' "${_rw_rocm}"
+ printf '# <<< Unsloth ROCm-on-WSL (gfx1151) <<<\n'
+ )"
+ if [ "$(id -u)" = "0" ]; then
+ printf '%s\n' "$_rw_dropin" > /etc/profile.d/unsloth-rocm-wsl.sh 2>/dev/null || true
+ elif command -v sudo >/dev/null 2>&1; then
+ printf '%s\n' "$_rw_dropin" | sudo tee /etc/profile.d/unsloth-rocm-wsl.sh >/dev/null 2>&1 || true
+ fi
+}
+
_maybe_bootstrap_rocm_wsl() {
[ "${OS:-}" = "wsl" ] || return 0
[ "${SKIP_TORCH:-false}" = "false" ] || return 0
@@ -2060,6 +2091,11 @@ _maybe_bootstrap_rocm_wsl() {
_ensure_rocm_probe_env
if command -v rocminfo >/dev/null 2>&1 && \
rocminfo 2>/dev/null | awk '/Name:[[:space:]]*gfx1151/{found=1} END{exit !found}'; then
+ # rocminfo may work only via the transient env _ensure_rocm_probe_env
+ # just set, which dies with the installer. Persist the drop-in so login
+ # shells (Studio, llama.cpp) inherit it -- else a reinstall over an
+ # existing /opt/rocm (uninstall keeps ROCm but drops it) loses the GPU.
+ _persist_rocm_wsl_dropin
return 0
fi
# WSL GPU passthrough device must exist (present on any WSL2 GPU host).
@@ -2077,31 +2113,8 @@ _maybe_bootstrap_rocm_wsl() {
. /etc/profile.d/unsloth-rocm-wsl.sh || true
else
# librocdxg present but the env drop-in is gone (e.g. a Studio
- # uninstall removed it while keeping shared ROCm). Restore the FULL
- # env inline (so rocminfo is on PATH) and recreate the drop-in.
- _rw_rocm=/opt/rocm
- export HSA_ENABLE_DXG_DETECTION=1
- export TORCH_ROCM_AOTRITON_ENABLE_EXPERIMENTAL=1
- export PATH="${_rw_rocm}/bin:${PATH}"
- export LD_LIBRARY_PATH="${_rw_rocm}/lib:${LD_LIBRARY_PATH:-}"
- # Persist the drop-in so later non-login Studio launches get the env
- # too. /etc/profile.d is root-owned: a plain redirect fails for a
- # non-root reinstall (ROCm would silently disappear after this shell),
- # so tee through sudo when not root. Best-effort -- the current shell
- # already has the env, so the install proceeds either way.
- _rw_dropin="$(
- printf '# >>> Unsloth ROCm-on-WSL (gfx1151) >>>\n'
- printf 'export HSA_ENABLE_DXG_DETECTION=1\n'
- printf 'export TORCH_ROCM_AOTRITON_ENABLE_EXPERIMENTAL=1\n'
- printf 'export PATH="%s/bin:${PATH}"\n' "${_rw_rocm}"
- printf 'export LD_LIBRARY_PATH="%s/lib:${LD_LIBRARY_PATH:-}"\n' "${_rw_rocm}"
- printf '# <<< Unsloth ROCm-on-WSL (gfx1151) <<<\n'
- )"
- if [ "$(id -u)" = "0" ]; then
- printf '%s\n' "$_rw_dropin" > /etc/profile.d/unsloth-rocm-wsl.sh 2>/dev/null || true
- elif command -v sudo >/dev/null 2>&1; then
- printf '%s\n' "$_rw_dropin" | sudo tee /etc/profile.d/unsloth-rocm-wsl.sh >/dev/null 2>&1 || true
- fi
+ # uninstall removed it while keeping shared ROCm). Restore the env.
+ _persist_rocm_wsl_dropin
fi
return 0
fi
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.py
index 3cc7514ff54..e2de5fc3228 100644
--- a/studio/backend/core/inference/llama_cpp.py
+++ b/studio/backend/core/inference/llama_cpp.py
@@ -75,6 +75,33 @@ from state.tool_approvals import (
logger = get_logger(__name__)
+def _wsl_system_rocm_lib_dirs() -> "list[str]":
+ """System ROCm lib dir(s) to load before a prebuilt's bundled HIP, on WSL.
+
+ The bundled bare-metal HIP can't drive WSL's /dev/dxg and segfaults on the
+ first GPU call; the system ROCm libs (libamdhip64 + librocdxg) can, while
+ the bundle still supplies libggml-hip / librocblas (gfx1151 kernels).
+ Mirrors install_llama_prebuilt._wsl_system_rocm_lib_dirs so a prebuilt that
+ passed install validation runs the same at serve time. No-op off a ROCDXG
+ WSL host (needs /dev/dxg, "microsoft" /proc/version, librocdxg in /opt/rocm).
+ """
+ try:
+ if not os.path.exists("/dev/dxg"):
+ return []
+ with open("/proc/version", encoding = "utf-8", errors = "replace") as fh:
+ if "microsoft" not in fh.read().lower():
+ return []
+ except OSError:
+ return []
+ out: "list[str]" = []
+ for d in ("/opt/rocm/lib", "/opt/rocm/lib64"):
+ if os.path.exists(os.path.join(d, "librocdxg.so")) or os.path.exists(
+ os.path.join(d, "librocdxg.so.1")
+ ):
+ out.append(d)
+ return out
+
+
# ── Pre-compiled patterns for plan-without-action re-prompt ──
# Forward-looking intent signals: the model is describing what it *will*
# do rather than giving a final answer.
@@ -3789,7 +3816,15 @@ class LlamaCppBackend:
# plus CUDA runtime libs (libcudart, libcublas, etc.)
import platform
- lib_dirs = [binary_dir]
+ lib_dirs = []
+ # WSL: system HIP before the bundle's (which segfaults on
+ # /dev/dxg). Mirror install_llama_prebuilt.binary_env, which
+ # validates the prebuilt with this same ordering.
+ for _wsl_rocm in _wsl_system_rocm_lib_dirs():
+ lib_dirs.append(_wsl_rocm)
+ if lib_dirs:
+ env.setdefault("HSA_ENABLE_DXG_DETECTION", "1")
+ lib_dirs.append(binary_dir)
_arch = platform.machine() # x86_64, aarch64, etc.
# Pip-installed nvidia CUDA runtime libs. The prebuilt
diff --git a/studio/install_llama_prebuilt.py b/studio/install_llama_prebuilt.py
index e3b2d493fb3..680b621b18a 100644
--- a/studio/install_llama_prebuilt.py
+++ b/studio/install_llama_prebuilt.py
@@ -5662,6 +5662,33 @@ def windows_runtime_dirs_for_runtime_line(runtime_line: str | None) -> list[str]
return windows_runtime_dirs_for_patterns(patterns)
+def _wsl_system_rocm_lib_dirs() -> list[str]:
+ """System ROCm lib dir(s) for binary_env to load before a prebuilt's HIP.
+
+ A prebuilt bundles a bare-metal HIP runtime that can't drive WSL's /dev/dxg
+ and segfaults on the first GPU call -> validation fails, install falls back
+ to a CPU build. Putting the system ROCm libs first loads the WSL-capable
+ HIP (libamdhip64 + librocdxg) while the bundle still supplies libggml-hip /
+ librocblas with the gfx1151 kernels. Strict no-op off WSL (needs /dev/dxg, a
+ "microsoft" /proc/version, and a librocdxg-providing ROCm).
+ """
+ try:
+ if not os.path.exists("/dev/dxg"):
+ return []
+ with open("/proc/version", encoding = "utf-8", errors = "replace") as fh:
+ if "microsoft" not in fh.read().lower():
+ return []
+ except OSError:
+ return []
+ out: list[str] = []
+ for d in ("/opt/rocm/lib", "/opt/rocm/lib64"):
+ if os.path.exists(os.path.join(d, "librocdxg.so")) or os.path.exists(
+ os.path.join(d, "librocdxg.so.1")
+ ):
+ out.append(d)
+ return out
+
+
def binary_env(
binary_path: Path,
install_dir: Path,
@@ -5683,6 +5710,11 @@ def binary_env(
str(install_dir),
*linux_runtime_dirs(binary_path),
]
+ # WSL: system HIP before the bundle's (which segfaults on /dev/dxg).
+ _wsl_rocm = _wsl_system_rocm_lib_dirs()
+ if _wsl_rocm:
+ ld_dirs = [*_wsl_rocm, *ld_dirs]
+ env.setdefault("HSA_ENABLE_DXG_DETECTION", "1")
existing = [part for part in env.get("LD_LIBRARY_PATH", "").split(os.pathsep) if part]
env["LD_LIBRARY_PATH"] = os.pathsep.join(dedupe_existing_dirs([*ld_dirs, *existing]))
elif host.is_macos:
diff --git a/tests/studio/install/test_rocm_support.py b/tests/studio/install/test_rocm_support.py
index 9aa06b6ff76..04463fb720a 100644
--- a/tests/studio/install/test_rocm_support.py
+++ b/tests/studio/install/test_rocm_support.py
@@ -11,7 +11,7 @@ import os
import subprocess
import sys
from pathlib import Path
-from unittest.mock import MagicMock, patch, PropertyMock
+from unittest.mock import MagicMock, mock_open, patch, PropertyMock
import pytest
@@ -3207,5 +3207,157 @@ class TestRocmGfxForwarding:
assert "$script:ROCmGfxArch" in source
+# TEST: WSL ROCDXG fixes -- drop-in persistence + system-HIP-before-bundle
+
+
+_INSTALL_SH_PATH = PACKAGE_ROOT / "install.sh"
+_LLAMA_CPP_PATH = PACKAGE_ROOT / "studio" / "backend" / "core" / "inference" / "llama_cpp.py"
+
+
+class TestWslSystemRocmLibDirs:
+ """install_llama_prebuilt._wsl_system_rocm_lib_dirs: a strict no-op off a
+ ROCDXG WSL host; returns the system ROCm lib dir there so binary_env can
+ load the WSL-capable HIP runtime before a prebuilt's bundled one."""
+
+ def test_empty_without_dev_dxg(self):
+ with patch("os.path.exists", return_value = False):
+ assert prebuilt_mod._wsl_system_rocm_lib_dirs() == []
+
+ def test_empty_on_bare_metal_linux(self):
+ # /dev/dxg present but /proc/version is not a WSL kernel.
+ with patch("os.path.exists", lambda p: p == "/dev/dxg"):
+ with patch(
+ "builtins.open",
+ mock_open(read_data = "Linux version 6.8.0-generic"),
+ ):
+ assert prebuilt_mod._wsl_system_rocm_lib_dirs() == []
+
+ def test_returns_system_lib_on_wsl_with_librocdxg(self):
+ # Normalize separators: os.path.join uses "\" on the Windows test host
+ # though the target path is Linux.
+ def _exists(p):
+ p = str(p).replace("\\", "/")
+ return p in ("/dev/dxg", "/opt/rocm/lib/librocdxg.so")
+
+ with patch("os.path.exists", _exists):
+ with patch(
+ "builtins.open",
+ mock_open(read_data = "Linux version 5.15.0-microsoft-standard-WSL2"),
+ ):
+ assert prebuilt_mod._wsl_system_rocm_lib_dirs() == ["/opt/rocm/lib"]
+
+ def test_empty_on_wsl_without_librocdxg(self):
+ # WSL kernel + /dev/dxg but no librocdxg -> not a ROCDXG ROCm install.
+ with patch("os.path.exists", lambda p: p == "/dev/dxg"):
+ with patch(
+ "builtins.open",
+ mock_open(read_data = "microsoft-standard-WSL2"),
+ ):
+ assert prebuilt_mod._wsl_system_rocm_lib_dirs() == []
+
+
+class TestBinaryEnvWslOrdering:
+ """binary_env must put the system ROCm lib dir AHEAD of the prebuilt's
+ bundle dir on a ROCDXG WSL host, and set HSA_ENABLE_DXG_DETECTION; no-op
+ on bare-metal Linux."""
+
+ @staticmethod
+ def _linux_host():
+ return HostInfo(
+ system = "Linux",
+ machine = "x86_64",
+ is_windows = False,
+ is_linux = True,
+ is_macos = False,
+ is_x86_64 = True,
+ is_arm64 = False,
+ nvidia_smi = None,
+ driver_cuda_version = None,
+ compute_caps = [],
+ visible_cuda_devices = None,
+ has_physical_nvidia = False,
+ has_usable_nvidia = False,
+ has_rocm = True,
+ )
+
+ def test_wsl_prepends_system_rocm_and_sets_hsa(self, tmp_path):
+ binary = tmp_path / "bundle" / "llama-server"
+ binary.parent.mkdir(parents = True)
+ binary.write_text("")
+ # binary_env's dedupe_existing_dirs drops non-existent dirs, so use a
+ # real dir to stand in for the system ROCm lib path.
+ sys_rocm = tmp_path / "sysrocm"
+ sys_rocm.mkdir()
+ with patch.object(prebuilt_mod, "_wsl_system_rocm_lib_dirs", return_value = [str(sys_rocm)]):
+ with patch.dict(os.environ, {}, clear = True):
+ env = prebuilt_mod.binary_env(binary, tmp_path, self._linux_host())
+ ld = env["LD_LIBRARY_PATH"].split(os.pathsep)
+ # Compare resolved paths (dedupe_existing_dirs calls Path.resolve()).
+ ld_resolved = [str(Path(p).resolve()) for p in ld]
+ assert ld_resolved[0] == str(sys_rocm.resolve())
+ assert str(binary.parent.resolve()) in ld_resolved
+ assert ld_resolved.index(str(sys_rocm.resolve())) < ld_resolved.index(
+ str(binary.parent.resolve())
+ )
+ assert env.get("HSA_ENABLE_DXG_DETECTION") == "1"
+
+ def test_bare_metal_linux_unchanged(self, tmp_path):
+ binary = tmp_path / "bundle" / "llama-server"
+ binary.parent.mkdir(parents = True)
+ binary.write_text("")
+ with patch.object(prebuilt_mod, "_wsl_system_rocm_lib_dirs", return_value = []):
+ with patch.dict(os.environ, {}, clear = True):
+ env = prebuilt_mod.binary_env(binary, tmp_path, self._linux_host())
+ ld = env["LD_LIBRARY_PATH"].split(os.pathsep)
+ assert ld[0] == str(binary.parent) # bundle dir first, as before
+ assert "HSA_ENABLE_DXG_DETECTION" not in env
+
+
+class TestInstallShDropinPersistence:
+ """install.sh must persist the ROCm-on-WSL drop-in even when rocminfo
+ already enumerates the GPU (via the transient probe env), so a reinstall
+ over an existing /opt/rocm doesn't leave login shells without the env."""
+
+ def test_has_persist_helper(self):
+ source = _INSTALL_SH_PATH.read_text(encoding = "utf-8")
+ assert "_persist_rocm_wsl_dropin()" in source
+
+ def test_gate5_early_return_persists_dropin(self):
+ """The rocminfo-already-works early return must call the persist helper
+ BEFORE returning."""
+ source = _INSTALL_SH_PATH.read_text(encoding = "utf-8")
+ # Find the rocminfo gfx1151 gate and assert the persist call precedes
+ # its `return 0`.
+ gate = source.find("Name:[[:space:]]*gfx1151")
+ assert gate != -1
+ window = source[gate : gate + 900]
+ assert "_persist_rocm_wsl_dropin" in window
+ assert window.find("_persist_rocm_wsl_dropin") < window.find("return 0")
+
+ def test_persist_helper_gated_on_librocdxg(self):
+ source = _INSTALL_SH_PATH.read_text(encoding = "utf-8")
+ body_start = source.find("_persist_rocm_wsl_dropin()")
+ body = source[body_start : body_start + 1200]
+ assert "librocdxg.so" in body
+ assert "profile.d/unsloth-rocm-wsl.sh" in body
+
+
+class TestLlamaCppRuntimeWslOrdering:
+ """The serve-time launcher must mirror binary_env: system HIP before the
+ bundle dir on WSL, so a prebuilt that passed install validation runs."""
+
+ def test_has_wsl_helper(self):
+ source = _LLAMA_CPP_PATH.read_text(encoding = "utf-8")
+ assert "_wsl_system_rocm_lib_dirs" in source
+
+ def test_prepends_before_binary_dir(self):
+ source = _LLAMA_CPP_PATH.read_text(encoding = "utf-8")
+ # The Linux lib_dirs build must add WSL rocm dirs before binary_dir.
+ idx_helper = source.find("for _wsl_rocm in _wsl_system_rocm_lib_dirs()")
+ idx_binary = source.find("lib_dirs.append(binary_dir)")
+ assert idx_helper != -1 and idx_binary != -1
+ assert idx_helper < idx_binary
+
+
if __name__ == "__main__":
pytest.main([__file__, "-v"])