Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.
Python amdapple-siliconcudadeepseekexl3exllamav3glminferencellmllm-inferencelocal-llmnvidiaopenai-apiquantizationqwenrocmstrix-halo
Strix scripts: find the HSA runtime through rocm-sdk, fail loudly in the builds
YanissAmzb5716c2c1ebb
diff --git a/hipsp/build.sh b/hipsp/build.shindex 02572de..2b073da 100755--- a/hipsp/build.sh+++ b/hipsp/build.sh@@ -1,9 +1,14 @@#!/bin/bash# build.sh SRC.cu OUT.so [VENV] : HIP library for gfx1151 (SDK toolchain that matches torch). Prints resource usage.-SRC=$1; OUT=$2; VENV=${3:-${VIRTUAL_ENV:-.venv}}+# Fails loudly: any error stops the script with a non-zero status, and the old OUT is removed first so a failed build never leaves a stale library behind.+set -euo pipefail+SRC=${1:?usage: build.sh SRC.cu OUT.so [VENV]}; OUT=${2:?usage: build.sh SRC.cu OUT.so [VENV]}; VENV=${3:-${VIRTUAL_ENV:-.venv}}SDK=$(PATH=$VENV/bin:$PATH rocm-sdk path --root)+[ -x "$SDK/bin/hipcc" ] || { echo "build.sh: no hipcc in '$SDK' (rocm-sdk of the venv '$VENV'): install the ROCm devel wheel as in doc/install.md" >&2; exit 1; }GCCD=$(dirname "$(gcc -print-libgcc-file-name)")-BC=$SDK/lib/llvm/amdgcn/bitcode; [ -d $BC ] || BC=$SDK/amdgcn/bitcode+BC=$SDK/lib/llvm/amdgcn/bitcode; [ -d "$BC" ] || BC=$SDK/amdgcn/bitcodeexport ROCM_PATH=$SDK HIP_PATH=$SDK-$SDK/bin/hipcc -x hip --offload-arch=gfx1151 -O3 -std=c++17 -shared -fPIC --gcc-install-dir=$GCCD --rocm-device-lib-path=$BC --rocm-path=$SDK -I$(dirname $SRC) -Rpass-analysis=kernel-resource-usage "$SRC" -o "$OUT" 2>&1 | grep -E "error|VGPRs:|Spill|LDS Size|Occupancy|Function Name|warning: " | head -60+rm -f "$OUT"+"$SDK/bin/hipcc" -x hip --offload-arch=gfx1151 -O3 -std=c++17 -shared -fPIC --gcc-install-dir="$GCCD" --rocm-device-lib-path="$BC" --rocm-path="$SDK" -I"$(dirname "$SRC")" -Rpass-analysis=kernel-resource-usage "$SRC" -o "$OUT" 2>&1+[ -s "$OUT" ] || { echo "build.sh: hipcc left no library at $OUT" >&2; exit 1; }ls -la "$OUT"diff --git a/tests/test_strix_scripts_cpu.py b/tests/test_strix_scripts_cpu.pynew file mode 100644index 0000000..f2c09fc--- /dev/null+++ b/tests/test_strix_scripts_cpu.py@@ -0,0 +1,140 @@+"""CPU tests of the Strix Halo shell scripts that failed silently: tools/strix_halo/env.sh (the HSA runtime that torch needs on gfx1151: found through the+rocm-sdk wheel when EXL3_ROCM_SDK is unset, a clear error instead of a later segfault when there is none) and hipsp/build.sh (a failed hipcc must fail the+script and leave no stale library). Fake rocm-sdk / hipcc / gcc scripts stand in for the toolchain: no GPU, no ROCm.+Run: python -m pytest tests/test_strix_scripts_cpu.py"""+import os+import shutil+import stat+import subprocess+import tempfile+import unittest++ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+BASH = shutil.which("bash")+++def script(path, body):+ os.makedirs(os.path.dirname(path), exist_ok=True)+ with open(path, "w") as f:+ f.write("#!/bin/bash\n" + body)+ os.chmod(path, os.stat(path).st_mode | stat.S_IEXEC)++[email protected](BASH is None, "bash not found")+class EnvSh(unittest.TestCase):+ def setUp(self):+ self.tmp = tempfile.mkdtemp()+ self.addCleanup(shutil.rmtree, self.tmp, True)+ self.venv = os.path.join(self.tmp, "venv")+ self.sdk = os.path.join(self.tmp, "sdk")++ def fake_rocm_sdk(self, root):+ script(os.path.join(self.venv, "bin", "rocm-sdk"), f'[ "$1 $2" = "path --root" ] && echo "{root}" || exit 1\n' if root else "exit 1\n")++ def source(self, **env):+ base = {k: v for k, v in os.environ.items() if not k.startswith(("EXL3_", "LD_PRELOAD"))}+ base.update({"EXL3_VENV": self.venv, "EXL3_HSA_SYSTEM_DIRS": os.path.join(self.tmp, "no-system-rocm"), "HOME": self.tmp, **env})+ return subprocess.run([BASH, "-c", 'source tools/strix_halo/env.sh >/dev/null; rc=$?; echo "LD_PRELOAD=$LD_PRELOAD"; exit $rc'], cwd=ROOT, env=base,+ capture_output=True, text=True, timeout=60)++ def make_lib(self, directory):+ os.makedirs(directory, exist_ok=True)+ lib = os.path.join(directory, "libhsa-runtime64.so.1")+ open(lib, "w").close()+ return lib++ def test_the_rocm_sdk_wheel_is_the_fallback_when_exl3_rocm_sdk_is_unset(self):+ lib = self.make_lib(os.path.join(self.sdk, "lib"))+ self.fake_rocm_sdk(self.sdk)+ r = self.source()+ self.assertEqual(r.returncode, 0, r.stderr)+ self.assertIn(f"LD_PRELOAD={lib}", r.stdout)++ def test_exl3_rocm_sdk_wins_over_the_wheel(self):+ mine = self.make_lib(os.path.join(self.tmp, "mine", "lib"))+ self.make_lib(os.path.join(self.sdk, "lib"))+ self.fake_rocm_sdk(self.sdk)+ r = self.source(EXL3_ROCM_SDK=os.path.join(self.tmp, "mine"))+ self.assertIn(f"LD_PRELOAD={mine}", r.stdout)++ def test_exl3_hsa_lib_wins_over_everything(self):+ pinned = self.make_lib(os.path.join(self.tmp, "pinned"))+ self.make_lib(os.path.join(self.sdk, "lib"))+ self.fake_rocm_sdk(self.sdk)+ r = self.source(EXL3_HSA_LIB=pinned)+ self.assertIn(f"LD_PRELOAD={pinned}", r.stdout)++ def test_the_system_rocm_is_tried_last(self):+ lib = self.make_lib(os.path.join(self.tmp, "system-rocm"))+ self.fake_rocm_sdk(None)+ r = self.source(EXL3_HSA_SYSTEM_DIRS=os.path.dirname(lib))+ self.assertEqual(r.returncode, 0, r.stderr)+ self.assertIn(f"LD_PRELOAD={lib}", r.stdout)++ def test_no_runtime_is_a_clear_error_not_a_segfault_later(self):+ self.fake_rocm_sdk(None) # rocm-sdk present but failing, no EXL3_ROCM_SDK, nothing on the system+ r = self.source()+ self.assertNotEqual(r.returncode, 0)+ self.assertIn("no libhsa-runtime64.so.1", r.stderr)+ self.assertIn("segfault", r.stderr)+ self.assertIn("LD_PRELOAD=\n", r.stdout)++ def test_no_runtime_without_rocm_sdk_at_all_is_the_same_error(self):+ r = self.source(PATH=os.path.dirname(BASH) + os.pathsep + "/usr/bin" + os.pathsep + "/bin")+ self.assertNotEqual(r.returncode, 0)+ self.assertIn("no libhsa-runtime64.so.1", r.stderr)++ def test_allow_no_hsa_only_warns(self):+ self.fake_rocm_sdk(None)+ r = self.source(EXL3_ALLOW_NO_HSA="1")+ self.assertEqual(r.returncode, 0, r.stderr)+ self.assertIn("WARNING", r.stderr)++[email protected](BASH is None, "bash not found")+class HipspBuild(unittest.TestCase):+ def setUp(self):+ self.tmp = tempfile.mkdtemp()+ self.addCleanup(shutil.rmtree, self.tmp, True)+ self.venv, self.sdk = os.path.join(self.tmp, "venv"), os.path.join(self.tmp, "sdk")+ script(os.path.join(self.venv, "bin", "rocm-sdk"), f'echo "{self.sdk}"\n')+ script(os.path.join(self.tmp, "bin", "gcc"), 'echo /usr/lib/gcc/x86_64-linux-gnu/13/libgcc.a\n')+ self.src, self.out = os.path.join(self.tmp, "k.cu"), os.path.join(self.tmp, "libk.so")+ open(self.src, "w").close()++ def hipcc(self, body):+ script(os.path.join(self.sdk, "bin", "hipcc"), body)++ def build(self):+ env = dict(os.environ, PATH=os.path.join(self.tmp, "bin") + os.pathsep + os.environ["PATH"])+ return subprocess.run([BASH, os.path.join(ROOT, "hipsp", "build.sh"), self.src, self.out, self.venv], env=env, capture_output=True, text=True, timeout=60)++ def test_a_failed_compile_fails_the_script_and_leaves_no_stale_library(self):+ open(self.out, "w").write("old library")+ self.hipcc('echo "error: kernel does not compile"; exit 1\n')+ r = self.build()+ self.assertNotEqual(r.returncode, 0)+ self.assertIn("does not compile", r.stdout)+ self.assertFalse(os.path.exists(self.out))++ def test_a_compiler_that_writes_nothing_fails_the_script(self):+ self.hipcc("exit 0\n")+ r = self.build()+ self.assertNotEqual(r.returncode, 0)+ self.assertIn("left no library", r.stderr)++ def test_a_good_build_prints_the_library(self):+ self.hipcc('while [ "$1" != "-o" ]; do shift; done; echo built > "$2"\n')+ r = self.build()+ self.assertEqual(r.returncode, 0, r.stderr)+ self.assertIn("libk.so", r.stdout)+ self.assertTrue(os.path.getsize(self.out) > 0)++ def test_a_missing_sdk_is_a_clear_error(self):+ r = self.build() # no hipcc in the fake SDK+ self.assertNotEqual(r.returncode, 0)+ self.assertIn("no hipcc", r.stderr)+++if __name__ == "__main__":+ unittest.main()diff --git a/tools/strix_halo/ck_fmha/build_mla.sh b/tools/strix_halo/ck_fmha/build_mla.shindex bc06938..73f7971 100755--- a/tools/strix_halo/ck_fmha/build_mla.sh+++ b/tools/strix_halo/ck_fmha/build_mla.sh@@ -14,6 +14,7 @@ GCC_DIR=${GCC_DIR:-$(dirname "$(gcc -print-libgcc-file-name)")}F=$CK_SRC/example/ck_tile/01_fmhaB=$(mktemp -d); trap 'rm -rf "$B"' EXITmkdir -p "$B/gen" "$B/obj" "$(dirname "$OUT")"+rm -f "$OUT" # a failed build must not leave an old library to be foundpython3 "$F/generate.py" --targets gfx1151 --api fwd --optdim ${CKMLA_HD:-256} -r 0 \-f "*fp16*batch*_nlogits_*_nlse_ndropout_nskip_nqscale*" -o "$B/gen" >/dev/nullFLAGS="--gcc-install-dir=$GCC_DIR -std=c++20 -O3 -fPIC -x hip --offload-arch=gfx1151 -I$CK_SRC/include -I$F -I$B/gen \@@ -21,4 +22,5 @@ FLAGS="--gcc-install-dir=$GCC_DIR -std=c++20 -O3 -fPIC -x hip --offload-arch=gfx-DCK_TILE_FMHA_FWD_PAGEDKV_API=0 -DCK_TILE_FMHA_FWD_BATCH_PREFILL_API=0 -w"ls "$B"/gen/*.cpp "$HERE/ckmla_shim.cpp" | xargs -P "${CK_JOBS:-8}" -I{} sh -c "$CXX $FLAGS -c {} -o $B/obj/\$(basename {} .cpp).o""$CXX" --gcc-install-dir="$GCC_DIR" -shared -fPIC --offload-arch=gfx1151 "$B"/obj/*.o -o "$OUT"+[ -s "$OUT" ] || { echo "build_mla.sh: no library at $OUT" >&2; exit 1; }echo "built $OUT ($(ls "$B"/gen/*.cpp | wc -l) instance files)"diff --git a/tools/strix_halo/env.sh b/tools/strix_halo/env.shindex 1a88c12..c7db24d 100755--- a/tools/strix_halo/env.sh+++ b/tools/strix_halo/env.sh@@ -41,25 +41,35 @@# the Trap 1 segfault. Build-time only, hence the `build` guard below.# ===========================================================================+# Repository root = two levels above this file (works for any clone location).+_SELF="${BASH_SOURCE[0]:-$0}"+EXL3_ROOT="${EXL3_ROOT:-$(cd "$(dirname "$_SELF")/../.." && pwd)}"+EXL3_VENV="${EXL3_VENV:-$EXL3_ROOT/.venv}"+export PATH="$EXL3_VENV/bin:$HOME/.local/bin:$PATH"+export PYTHONPATH="$EXL3_ROOT:${PYTHONPATH:-}" # exllamav3 and the built exllamav3_ext*.so live in the repo root+# Needs a ROCm 7 libhsa-runtime64.so.1 (HSA 1.18 or newer). Ubuntu 24.04's own libhsa-runtime64-1 is ROCm 5.7 (1.11)-# and is too old: torch then fails with "undefined symbol: hsa_amd_enable_logging". Install ROCm 7 from AMD's apt-# repository (it provides /opt/rocm/lib/libhsa-runtime64.so.1), or point EXL3_HSA_LIB at your copy. The rocm-sdk-devel wheel ships one too (EXL3_ROCM_SDK/lib). It is tried first because it matches the ROCm torch wheel: a system /opt/rocm with an older HSA fails with "undefined symbol: hsa_ext_image_create_v2".+# and is too old: torch then fails with "undefined symbol: hsa_amd_enable_logging". Looked for, in this order: EXL3_HSA_LIB; the rocm-sdk wheel+# (EXL3_ROCM_SDK/lib, else the root `rocm-sdk path --root` of the venv prints; it matches the torch wheel, so it is tried first); the system ROCm 7+# (EXL3_HSA_SYSTEM_DIRS, default /opt/rocm/lib /usr/lib/x86_64-linux-gnu: install ROCm 7 from AMD's apt repository).+# When none exists this stops with an error: torch would segfault on the first GPU allocation (Trap 1). EXL3_ALLOW_NO_HSA=1 only warns (CPU-only use).+_SDK_ROOT="${EXL3_ROCM_SDK:-}"+if [ -z "$_SDK_ROOT" ] && command -v rocm-sdk >/dev/null 2>&1; then+ _SDK_ROOT="$(rocm-sdk path --root 2>/dev/null || true)"+fi_HSA=""-for _c in "${EXL3_HSA_LIB:-}" "${EXL3_ROCM_SDK:-/nonexistent}/lib/libhsa-runtime64.so.1" /opt/rocm/lib/libhsa-runtime64.so.1 /usr/lib/x86_64-linux-gnu/libhsa-runtime64.so.1; do+for _c in "${EXL3_HSA_LIB:-}" "${_SDK_ROOT:-/nonexistent}/lib/libhsa-runtime64.so.1" $(for _d in ${EXL3_HSA_SYSTEM_DIRS:-/opt/rocm/lib /usr/lib/x86_64-linux-gnu}; do echo "$_d/libhsa-runtime64.so.1"; done); doif [ -n "$_c" ] && [ -e "$_c" ]; then _HSA="$_c"; break; fidoneif [ -n "$_HSA" ]; thenexport LD_PRELOAD="$_HSA"+elif [ "${EXL3_ALLOW_NO_HSA:-0}" = "1" ]; then+ echo "[env.sh] WARNING: no libhsa-runtime64.so.1 found (EXL3_ALLOW_NO_HSA=1): any GPU use of torch will segfault on gfx1151 (Trap 1)." >&2else- echo "[env.sh] WARNING: no libhsa-runtime64.so.1 found. Install ROCm 7, set EXL3_ROCM_SDK or set EXL3_HSA_LIB, or torch will segfault on gfx1151 (Trap 1)." >&2+ echo "[env.sh] ERROR: no libhsa-runtime64.so.1 (ROCm 7) found: torch would segfault on the first GPU allocation on gfx1151 (Trap 1). Install ROCm 7 (/opt/rocm), activate the venv that holds the rocm-sdk wheel (rocm-sdk path --root), or set EXL3_ROCM_SDK or EXL3_HSA_LIB. Set EXL3_ALLOW_NO_HSA=1 for CPU-only use." >&2+ return 1 2>/dev/null || exit 1fi--# Repository root = two levels above this file (works for any clone location).-_SELF="${BASH_SOURCE[0]:-$0}"-EXL3_ROOT="${EXL3_ROOT:-$(cd "$(dirname "$_SELF")/../.." && pwd)}"-EXL3_VENV="${EXL3_VENV:-$EXL3_ROOT/.venv}"-export PATH="$EXL3_VENV/bin:$HOME/.local/bin:$PATH"-export PYTHONPATH="$EXL3_ROOT:${PYTHONPATH:-}" # exllamav3 and the built exllamav3_ext*.so live in the repo root+unset _SDK_ROOT _HSA _c _d# Uncomment to force reconstruct+hgemm instead of HIP GEMV (debugging):# export EXL3_GEMV=0