From a2edcb12f974be20a41ecbc9d890e665bd4fc495 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Fri, 2 Oct 2026 09:31:18 +0200 Subject: [PATCH] Windows AMD: the HIP runtime next to strata.exe, not an AMD driver's System32 copy (#468, #461) Windows looks for an imported DLL in the exe's folder, then System32, then PATH (where the zip's rocm/bin is): a driver that installs its own amdhip64_7.dll in System32 won, and the bundled rocBLAS/hipBLAS ran on that runtime - an access violation on a W7900, hipErrorInvalidDeviceFunction on a 7900 XTX, on the first prompt. setup copies amdhip64_7.dll and amd_comgr.dll from engine/rocm/bin next to the exes after the unzip, before the device probe and on every start (so a 0.1.34 install is fixed too); package_windows.py ships them there; the engine and strata-device log which runtime they loaded. rocBLAS/hipBLASLt stay in rocm/bin (their kernel libraries and .kpack are found from there). --- docs/AMD_HIP.md | 5 +++++ setup.py | 29 +++++++++++++++++++++++++++++ src/core/device_main.cpp | 13 +++++++++++++ src/program/generate.cpp | 8 ++++++++ tools/hip/package_windows.py | 7 +++++++ tools/test_setup_amd.py | 27 +++++++++++++++++++++++++++ 6 files changed, 89 insertions(+) diff --git a/docs/AMD_HIP.md b/docs/AMD_HIP.md index 5b993876..da311bc2 100644 --- a/docs/AMD_HIP.md +++ b/docs/AMD_HIP.md @@ -68,6 +68,11 @@ On a PC with no NVIDIA card Strata can use, the AMD card is chosen by itself; wi amd_comgr and the Microsoft C++ runtime; ROCm 10.2.0a20260930 from AMD's TheRock builds, licenses in `engine\rocm\licenses`). The HIP runtime works through the AMD driver's own components, so the driver is the one thing it needs from the PC. +- **The HIP runtime next to `strata.exe` (0.1.35, #468 #461):** `amdhip64_7.dll` and `amd_comgr.dll` are also put in + `engine\` (setup copies them there on every start). Windows looks in the program's folder before System32, where + some AMD drivers install their own `amdhip64_7.dll`; with that one, the bundled libraries crashed on the first + prompt (an access violation, or `hipErrorInvalidDeviceFunction`). The engine's log names the runtime it loaded + (`strata generate: HIP runtime ...`). - **Before the ~60 GB model download** setup runs `engine\strata-device.exe --list-devices` (with `engine\rocm\bin` on the PATH): if the HIP runtime does not see the card, setup stops there and points to the driver. It also gives the card's HIP number: with an integrated Radeon that is device 1, not 0 (#325). From then on setup lists the AMD cards diff --git a/setup.py b/setup.py index 67fdd4f5..f2548ac2 100644 --- a/setup.py +++ b/setup.py @@ -1192,6 +1192,7 @@ def hip_devices(probe: Path | None = None, text: str | None = None) -> list[dict if not probe.exists() or not hip_engine: return None try: + hip_runtime_beside_exe(probe.parent) # #468 #461: not the driver's System32 copy env = dict(os.environ) # the ready-made engine's ROCm DLLs (rocm/bin beside it) env["PATH"] = os.pathsep.join([str(d) for d in hip_lib_dirs(probe.parent)] + [env.get("PATH", "")]) r = subprocess.run([str(probe), "--list-devices"], capture_output=True, text=True, timeout=120, @@ -1232,6 +1233,31 @@ def hip_lib_dirs(eng: Path) -> list[Path]: return [eng / d for d in rel if (eng / d).is_dir()] +# #468 #461: the HIP runtime the ready-made engine was built with, next to strata.exe. Windows looks for an imported +# DLL in the exe's folder, then System32, and only then on PATH (where rocm/bin is): an AMD driver that installs its own +# amdhip64_7.dll in System32 won, and the bundled rocBLAS/hipBLAS ran on that runtime - an access violation (W7900) or +# hipErrorInvalidDeviceFunction (7900 XTX) on the first prompt. Only the runtime and the compiler it loads by name: +# rocBLAS/hipBLASLt stay in rocm/bin, where they find their kernel libraries and ../.kpack. +HIP_RUNTIME_DLLS = ("amdhip64_*.dll", "amd_comgr*.dll") + + +def hip_runtime_beside_exe(eng: Path) -> None: + """Copy the bundled HIP runtime DLLs from rocm/bin next to the engine's exes when missing or different (a 0.1.34 + install, whose zip had them in rocm/bin only, is fixed on its next start).""" + for d in hip_lib_dirs(eng): + for pat in HIP_RUNTIME_DLLS: + for src in d.glob(pat): + dst = eng / src.name + try: + if dst.exists() and dst.stat().st_size == src.stat().st_size and \ + dst.stat().st_mtime >= src.stat().st_mtime: + continue + shutil.copy2(src, dst) + except OSError as e: # e.g. the engine is running and holds the old copy + warn(f"could not put {src.name} next to the AMD engine ({e}); if the engine stops on its first " + "request, close Strata and run START-HERE.bat again") + + def hip_match(card: dict, listed: list[dict], hip: list[dict]) -> dict | None: """The HIP device that is setup's `card` (from `listed`, the display-adapter order): the k-th device of the same architecture, k = the card's rank among the listed cards of that architecture. None when HIP has no such card.""" @@ -1324,6 +1350,7 @@ def get_prebuilt_hip(url_base, gpu, updating=False) -> Path | None: p.replace(dst) shutil.rmtree(tmp, ignore_errors=True) drop_archive(z) + hip_runtime_beside_exe(eng) # #468 #461 ok(f"ready-made AMD engine {meta.get('version', '')} for {', '.join(meta.get('archs', []))} " f"(ROCm {meta.get('rocm', '?')})") return eng @@ -2324,6 +2351,8 @@ def start(cfg_path: Path, port: int | None, gpu: int | list | None = None, open_ cmd = [sys.executable, str(ROOT / "serve" / "server.py"), "--engine", "strata", "--config", str(cfg_path), "--port", str(port or cfg.get("port", 8080))] if cfg.get("backend") == "hip": # AMD, numbered as HIP numbers them (setup's KFD order) + if WIN: + hip_runtime_beside_exe(Path(cfg["exe"]).parent) # #468 #461: also fixes a 0.1.34 install amd = amd_gpus() if isinstance(gpu, list): # --gpus: saved, this model runs on these cards from now on cards = amd_parse_gpus(",".join(str(i) for i in gpu), amd) diff --git a/src/core/device_main.cpp b/src/core/device_main.cpp index ff5adf51..c6872afc 100644 --- a/src/core/device_main.cpp +++ b/src/core/device_main.cpp @@ -9,6 +9,12 @@ #include #include #include +#if defined(STRATA_USE_HIP) && defined(_WIN32) +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include +#endif static std::string human(uint64_t b) { char buf[64]; @@ -68,6 +74,13 @@ int main(int argc, char** argv) { std::printf(" multiprocessors %d\n", d.multi_processor_count); std::printf(" VRAM total / free %s / %s\n", human(d.total_bytes).c_str(), human(d.free_bytes).c_str()); std::printf(" driver / runtime %d / %d\n", d.driver_version, d.runtime_version); +#if defined(STRATA_USE_HIP) && defined(_WIN32) + // #468 #461: which HIP runtime this process loaded - the one beside the exe, or an AMD driver's System32 copy + if (HMODULE h = GetModuleHandleA("amdhip64_7.dll")) { + char path[MAX_PATH] = {}; + if (GetModuleFileNameA(h, path, MAX_PATH) > 0) std::printf(" HIP runtime %s\n", path); + } +#endif // The planner's view against the card's. A plan that does not fit in what is actually FREE is the // failure this print exists to make visible at startup rather than at token 4000. diff --git a/src/program/generate.cpp b/src/program/generate.cpp index fa52c6bc..e7769028 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -2553,6 +2553,14 @@ int main(int argc, char** argv) { const char* name = named && p.name[0] ? p.name : "(an unnamed GPU)"; #if defined(STRATA_USE_HIP) std::fprintf(stderr, "strata generate: GPU %d: %s (%s)\n", dev, name, named ? p.gcnArchName : "?"); +#if defined(_WIN32) + // #468 #461: which HIP runtime was loaded - the bundled one beside the exe, or an AMD driver's System32 copy + if (HMODULE h = GetModuleHandleA("amdhip64_7.dll")) { + char path[MAX_PATH] = {}; + if (GetModuleFileNameA(h, path, MAX_PATH) > 0) + std::fprintf(stderr, "strata generate: HIP runtime %s\n", path); + } +#endif #else std::fprintf(stderr, "strata generate: GPU %d: %s, compute capability %d.%d%s\n", dev, name, strata::cc_major_of(p.major), strata::cc_minor_of(p.minor), diff --git a/tools/hip/package_windows.py b/tools/hip/package_windows.py index b7ee3d59..e6298815 100644 --- a/tools/hip/package_windows.py +++ b/tools/hip/package_windows.py @@ -7,6 +7,7 @@ tools/hip/build_windows.bat runs it after the build. The zip holds: strata.exe, strata-device.exe, BUILD.json (backend "hip", the archs, the ROCm and hipBLASLt versions) + amdhip64_7.dll, amd_comgr.dll the HIP runtime beside the exes too (#468 #461: before System32) rocm/bin/*.dll the ROCm DLLs the two programs load (their import tables, followed through the ROCm DLLs, + amd_comgr.dll, which the HIP runtime loads by name) and the Microsoft C++ runtime they import @@ -104,6 +105,12 @@ def main() -> int: sys.exit("VCToolsRedistDir is not set: run this from a Visual Studio x64 developer prompt (vcvars64.bat)") for d in sorted(crt): shutil.copy2(cdir / d, stage / "rocm" / "bin" / d) + # #468 #461: the HIP runtime (and the compiler it loads by name) next to the exes as well - Windows searches the + # exe's folder before System32, where an AMD driver may install its own amdhip64_7.dll (found before PATH's + # rocm/bin); rocBLAS/hipBLASLt stay in rocm/bin, which they resolve their kernel libraries and ../.kpack from + for pat in ("amdhip64_*.dll", "amd_comgr*.dll"): + for f in (stage / "rocm" / "bin").glob(pat): + shutil.copy2(f, stage / f.name) # the GEMM kernels and the libraries' own device code, for these archs lib = stage / "rocm" / "bin" / "rocblas" / "library" diff --git a/tools/test_setup_amd.py b/tools/test_setup_amd.py index fdca0eb4..ceb9d565 100644 --- a/tools/test_setup_amd.py +++ b/tools/test_setup_amd.py @@ -311,5 +311,32 @@ class WindowsHipVision(unittest.TestCase): self.assertEqual(setup.hip_vision("cpu"), "cpu") +class HipRuntimeBesideExe(unittest.TestCase): + """#468 #461: the bundled HIP runtime (and amd_comgr) goes next to strata.exe, so an AMD driver's System32 copy is + not found first; rocBLAS and the rest stay in rocm/bin.""" + + def test_copies_only_the_runtime(self): + import json + with tempfile.TemporaryDirectory() as d: + eng = Path(d) + rb = eng / "rocm" / "bin" + rb.mkdir(parents=True) + (eng / "BUILD.json").write_text(json.dumps({"backend": "hip", "lib_dirs": ["rocm/bin"]})) + for n, data in (("amdhip64_7.dll", b"hip-3686"), ("amd_comgr.dll", b"comgr"), ("rocblas.dll", b"blas")): + (rb / n).write_bytes(data) + setup.hip_runtime_beside_exe(eng) + self.assertEqual((eng / "amdhip64_7.dll").read_bytes(), b"hip-3686") + self.assertEqual((eng / "amd_comgr.dll").read_bytes(), b"comgr") + self.assertFalse((eng / "rocblas.dll").exists()) # finds its kernels relative to rocm/bin + (rb / "amdhip64_7.dll").write_bytes(b"hip-3690-newer") # a newer zip: replaced on the next start + setup.hip_runtime_beside_exe(eng) + self.assertEqual((eng / "amdhip64_7.dll").read_bytes(), b"hip-3690-newer") + + def test_no_rocm_bin_is_a_no_op(self): + with tempfile.TemporaryDirectory() as d: + setup.hip_runtime_beside_exe(Path(d)) # no BUILD.json (a CUDA or Linux engine) + self.assertEqual(list(Path(d).iterdir()), []) + + if __name__ == "__main__": unittest.main()