Repository navigation
feat(windows): load the ROCm runtime from a relocatable directory - #2607
Conversation
A Windows rocm build could only run on a machine with the same ROCm SDK at the path recorded at build time. The delay-load hook now picks one runtime directory per process: MLXCEL_ROCM_RUNTIME_DIR (absolute, authoritative, no fallback when wrong), else the executable's directory when it holds the HIP runtime named in the image's delay-import table, else the baked SDK bin. Every ROCm DLL and preload comes from that directory only, a missing DLL fails with its expected path, and the choice is printed once. The pure selection lives in the new header-only runtime_dir.h. The hiprtc JIT takes headers from the include folder in or beside the runtime directory before the baked SDK include. scripts/package_windows_rocm_runtime.py builds the runtime bundle (ten DLLs, arch-filtered rocBLAS and hipBLASLt kernels, .kpack archives, include/hip, licenses) in the SDK layout, because rocBLAS, hipBLASLt and rocFFT open ../.kpack relative to their DLL. A runtime path over 130 characters crashed a bf16 GEMM under MAX_PATH, so the hook warns about it. Validated on gfx1151 with the SDK renamed away: generate and all three server endpoints from the bundle bin and through MLXCEL_ROCM_RUNTIME_DIR, module origins, decoys and the missing-DLL failure. Host tests cover both selection functions and the packaging script. Closes #2606
…me dir A runtime directory without amd_comgr.dll was accepted: the preload loaded nothing, the HIP runtime's plain-name LoadLibrary resolved to the display driver's System32 amd_comgr.dll (confirmed from the loaded-module list), HIP reported no device, mlxcel fell back to the CPU and then crashed with 0xC0000409 and no message. That is the cross-version pairing the one-directory rule exists to prevent. The delay-load hook now lists the chosen directory before loading any ROCm DLL and requires at least one amd_comgr*.dll and one hiprtc-builtins*.dll (missing_runtime_preloads() in runtime_dir.h, covered by the host test). A missing pattern, or a preload Windows refuses, stops the binary with a message naming the directory and the pattern. Directory entries are read as UTF-8 so a non-ANSI file name cannot throw inside the hook. Docs and LOCAL_FIXES item 58 describe the check. Verified on gfx1151 with ROCm 10.1.0: a bundle without amd_comgr.dll (exe directory and MLXCEL_ROCM_RUNTIME_DIR) and one without hiprtc-builtins0716.dll exit 0xC06D007E with that message and load no ROCm DLL; the complete bundle generates "Paris" with every ROCm DLL from its bin; the developer build still uses the SDK bin. Host test, Python tests, mlxcel-mlx-pin tests, fmt, clippy --features rocm, overlay drift/records and check_env_var_docs.py pass. Refs #2606
Implementation Review SummaryIntent
Findings Addressed
Remaining ItemsNone at CRITICAL/HIGH. The rest of the PR (selection order, no fallback after a bad override, JIT include order, bundle script, docs including the llama.cpp folder rule) matches the issue and its comment. Verification
Rebuilt |
--rocm-version went into the zip file name and the top folder of every entry without the check applied to .info/version, so a value such as ../../x wrote the zip outside --out and produced entries with .. components. It is now held to the same pattern, which also must start with a letter or digit, so ".." is rejected. Also: duplicate destination detection uses a Counter instead of a quadratic list scan, and --stage-dir naming an existing file fails with a packaging error instead of an uncaught NotADirectoryError. Unit tests cover the three cases. Refs #2606
The hook printed directories with printf %ls. The MSVC CRT converts %ls through the default C locale, which stops at the first non-ASCII character and returns -1, so a runtime directory under a non-ASCII user name (for example C:\Users\<Korean name>\Downloads\mlxcel\bin) printed a cut-off "[mlx-rocm] ROCm runtime:" line and error messages that lost the path and the advice after it. A probe built with the same toolchain printed "C:\Users\" and nothing more. Paths are now converted with WideCharToMultiByte(CP_UTF8) and printed with %s. The same non-throwing conversion replaces path::u8string() for the directory listing in the hook, so a file name that is not valid UTF-16 cannot throw inside the delay-load helper. The JIT's runtime include lookup catches a failed conversion and skips the candidate. Refs #2606
Security and performance reviewNo CRITICAL or HIGH findings. DLL search order holds: every ROCm DLL and both plain-name preloads come from the one chosen directory, loaded by full path with Fixed:
Not changed (spec order): Verification: package script unit tests (18), |
ROCM_HOME, ROCM_PATH and HIP_PATH take precedence over the bundle's include folder for the JIT headers. The AMD HIP SDK installer sets HIP_PATH system-wide, so on such a machine the kernel compiler would use the SDK's headers, possibly from a different ROCm version than the bundle's runtime. Document clearing HIP_PATH for mlxcel, or pointing it at the bundle root, in the Windows ROCm install rules and in the environment variable reference. Validation: - python3 scripts/ci/check_env_var_docs.py: OK - cargo fmt --all --check, clippy -p mlxcel-core --release --features rocm -- -D warnings: clean - rocm_runtime_dir_test.sh (with the SDK clang++), tests.test_package_windows_rocm_runtime (18 tests), overlay drift and records: pass Refs #2606
Both scripts start with a shebang, and test_script_executable_modes requires Python entry points to carry executable Git mode; the python tooling tests job failed on scripts/package_windows_rocm_runtime.py. The host test script gets the same mode as rocm_overlay_test.sh. Refs #2606
…#2651) ## Summary Windows `--features cuda` binaries now load every CUDA, cuDNN and NVRTC DLL from one runtime directory and can run from a relocatable bundle, following the ROCm design of #2606/#2607. No NVIDIA GPU was available, so this PR is verified by building and by GPU-less runtime checks only (see below); runs on an NVIDIA host are still open. - `overlays/cuda/.../runtime_dir.h` (new, header-only, host-tested): `choose_cuda_runtime_dir()` picks `MLXCEL_CUDA_RUNTIME_DIR` (absolute, must hold the runtime, no fallback), else the executable directory when it holds the CUDA runtime DLL named in the binary's own delay-import table, else the build-time layout. nvcc links the static cudart, so the marker is `cublasLt64_13.dll`. - `delayload.cpp`: in a bundle, the DLLs the libraries open by plain name (`nvrtc64_*`, `nvrtc-builtins*`, `cublas64_*`, `cublasLt64_*`, `nvJitLink_*`, seven required `cudnn_*` sub-libraries; `cusparse64_*` checked only) are checked before any CUDA DLL loads and preloaded by full path; a failed load raises the delay-load exception naming the expected path instead of returning null. The build-time path keeps upstream's per-library lookup and its fallback; it only adds the `[mlx-cuda] CUDA runtime:` line, plus a message naming the missing marker path when a bundle-looking exe directory lacks it. - `jit_module.cpp`: NVRTC headers, CCCL and CUTLASS come from the bundle's `include\` (in or beside the runtime directory), ranked after `CUDA_HOME` and ahead of `CUDA_PATH`. - `scripts/package_windows_cuda_runtime.py`: stages or zips `bin\` (DLLs + OpenBLAS), `include\` (JIT headers, CCCL from the build's `_deps`, CUTLASS), `licenses\` (CUDA EULA, cuDNN, CCCL, CUTLASS); fails naming every missing DLL, header or license. Licenses are told apart by content, because the redistributable archives all ship a `LICENSE` that overwrites the others when unpacked into one directory. - `make verify-cuda-runtime-dir` (in `make verify`), docs in `installation.md` and `environment-variables.md`, overlay README record. ## Decisions and open points - The DLL closure is derived statically (import tables and embedded DLL names of the x64 CUDA 13.4.3 / cuDNN 9.27 DLLs), not from a loaded-module list of a generate run; to be confirmed on hardware. - The JIT header set was found by compiling each of the 17 headers MLX embeds for the JIT with NVRTC 13.4 against only the staged bundle's `include\` (needs no GPU). That added `cuda_runtime.h`, `mma.h` and `crt/mma.h(pp)`, which CUTLASS includes. Several of these headers (`cooperative_groups.h`, `math_constants.h`, `cuda_runtime.h`, `mma.h`, ...) are not on the CUDA EULA Attachment A list; the docs say so and advise confirming terms with NVIDIA before publishing a bundle. Prebuilt archives stay out of scope. - `delayload.cpp` is 595 lines, over the 500-line guideline; it is an overlay of an upstream file (like `jit_module.cpp` at 805), so it was not split. ## Changes during review - `CUDA_HOME` now really outranks the bundle's headers: the bundle `include\` was placed ahead of the chosen CUDA header dir on the NVRTC include list, so NVRTC took every CUDA header from the bundle even with `CUDA_HOME` set. With `CUDA_HOME` set the bundle include now goes last and only supplies CUTLASS/CuTe (implementation review, HIGH). - `cusparse64_*`, which only cuSOLVER imports, is checked before any CUDA DLL loads, so a missing cuSPARSE is reported under its own name instead of as cuSOLVER (implementation review). - `MLXCEL_CUDA_RUNTIME_DIR` is normalized (`.`/`..`, trailing separator) before use; `C:\b\bin\` previously made the header lookup miss the bundle's `include\` and fall back to `CUDA_PATH` (security review). - The baked-mode cuDNN log line no longer converts through the ANSI code page unguarded inside the delay-load hook (security review). - Documented, not fixed: the CUDA libraries also open `nvcuda.dll`, `nvcudart_hybrid64.dll`, `zlibwapi.dll`, `cask_profile_interface.dll`, `nvtileiras64.dll` and the two optional cuDNN sub-libraries by plain name; the bundle neither ships nor checks them, so the standard search order (application directory, System32, then current directory and `PATH`) applies, as it does in developer builds. Closing that needs a decision and is left to the DLL-planting follow-up (#2641). - On Linux the only behavior difference is that an empty `CUDA_HOME` now counts as unset. ## Test plan Toolkit: NVIDIA redistributables for windows-x86_64, CUDA 13.4.3 (cudart/nvcc/crt/nvrtc/nvjitlink/nvvm 13.4.92, cccl 13.3.4.3.1, cublas 13.8.0.4, cufft 12.4.0.43, cusolver 12.3.5.3, cusparse 12.8.6.72) and cuDNN 9.27.0.42 for CUDA 13, sha256-checked. Host: Windows 11 x64, Radeon 8060S, no NVIDIA GPU. - [x] x64 `cargo build --release --features cuda` (`MLX_CUDA_ARCHITECTURES=120`): cold build 32 min, rebuilt after the review fixes (17 min 37 s, `mlxcel.exe` 133,756,928 bytes) with the harness re-run, no build-glue change needed; `mlxcel.exe` 128 MiB, `mlxcel-server.exe` 118 MiB. `llvm-readobj --coff-imports`: cuBLASLt, cuFFT, cuSOLVER, cuDNN and NVRTC are DelayImport, no CUDA DLL in the regular import table. - [x] `cargo clippy --release --features cuda -- -D warnings` (x64), `cargo fmt --all --check`, `cargo test -p mlxcel-mlx-pin --lib` (49 passed). - [x] `make verify-cuda-runtime-dir` host test (clang++), checked to fail on mutated selection logic; 25 unit tests in `tests/test_package_windows_cuda_runtime.py`; env-var docs check; license header check. - [x] Packaging on the real archives: 1830 files, 1968 MiB; fails naming the file when one is renamed, and refuses cuDNN's `LICENSE` as the CUDA EULA. - [x] Hook exercised with no GPU: a harness linking this `delayload.cpp` unchanged calls `cublasLtGetVersion`, `nvrtcVersion`, `cufftGetVersion` and `cudnnGetVersion` through delay-loaded DLLs. With the toolkit renamed away and `CUDA_PATH` unset, every CUDA/cuDNN/NVRTC module loads from the bundle (exe directory and `MLXCEL_CUDA_RUNTIME_DIR`), `nvcuda.dll` from System32; relative and wrong-folder overrides, a removed delay-loaded DLL, a removed `cudnn_ops` and a removed marker each fail with the expected path. With the toolkit present the harness loads from the toolkit as before. - [x] `mlxcel generate` and `mlxcel-server` from the bundle (toolkit hidden) and the developer build both stop with 0xC0000409 at the first device query after `Detected 1 GPU(s).`, before any delay-loaded CUDA DLL is requested (this host has a stale `nvcuda.dll` but no NVIDIA GPU). Everything after device initialization is unverified. - [ ] On a Windows NVIDIA host (RTX Spark N1X or x64): generate (including the first NVRTC JIT) and the server from a fresh bundle folder with the toolkit moved away; module list; removed-DLL failure; developer build unchanged. Closes #2640
…#2652) ## Summary - `overlay-host-tests` (ubuntu-latest, about 20 s) runs `make verify-rocm-overlay verify-cuda-runtime-dir`: the ROCm overlay record checks and the host tests of both overlays' runtime and include directory choice (#2606/#2607, #2640). The record checks read only the checkout and a synthetic git history they build, so the whole target passes on a shallow clone. It runs unconditionally, like `python-tooling`, because several files it runs (`rocm_overlay.py`, `allocator_ownership_test.*`, the Makefile targets) are in no path filter. - `windows-build` (windows-latest, compile-only, no GPU) runs the `mlxcel-mlx-pin` lib tests (adds the `#[cfg(windows)]` test), the `tests/` Python suites, and `cargo check -p mlxcel-core --features rocm` against the ROCm 10.1.0 `gfx1151` TheRock tarball, then asserts `mlx_rocm_kernels.lib` exists so a skipped HIP build cannot pass. Hosted runners only. It is gated on a new `windows_build` filter: `build.rs`, `src/lib/mlxcel-core/build.rs` and `build_support/**`, `src/lib/mlxcel-mlx-pin/**`, the ROCm and CUDA overlays' `delayload.cpp`/`runtime_dir.h`/`jit_module.cpp`, the MLX pin (`src/lib/mlxcelverse/CMakeLists.txt`), both runtime-dir test scripts, `scripts/package_windows_*.py` and their Python tests, and `tests/test_rocm_arch_validation.py`. The tarball is pinned by sha256 (AMD publishes no checksum), cached per version, family and digest, saved right after verification, and verified again after every restore. - The bash-driven classes of the five suites named in the issue skip on `win32`; their pure-Python classes still run there. Two other Windows failures were encoding bugs (locale-code-page reads, emoji prints in `insert_apache_header.py` crashing on cp1252/cp949 consoles) and are fixed instead of skipped. - `docs/installation.md` replaces "no Windows CI job" with what the job covers; `CONTRIBUTING.md` and `docs/mlxcelverse/rocm-overlay.md` name the new jobs. ## Windows ROCm check: measured, full check kept Measured on windows-latest (4 vCPU, image windows-2025-vs2026), first on a scratch branch before opening this PR ([run 40403292712](https://github.com/lablup/mlxcel/actions/runs/40403292712); the cache-miss download and save timings come from [run 40402876890](https://github.com/lablup/mlxcel/actions/runs/40402876890)), then on this PR: | | | |---|---| | TheRock tarball (`gfx1151`, 10.1.0) | 1,649,447,497 bytes | | Cache entry (`gh cache list`) | 1,625,900,605 bytes | | Job, cache hit | 14.5 min (scratch), about 20 min three times on this PR ([PR run](https://github.com/lablup/mlxcel/actions/runs/40408890440/job/121873850081)); restore 22-24 s, verify 5-7 s, extract 54-57 s, `cargo check` 12-17.5 min | | Job, cache miss | 14 min ([PR run](https://github.com/lablup/mlxcel/actions/runs/40406365843/job/121865140156)); download 4-6 s, save 10-11 s | | Extracted SDK after excludes | 3.8 GB of 8.4 GB | | `CARGO_TARGET_DIR` after the check | 2.2 GB, HIP kernel archive 190.1 MiB | All within the bounds set for this issue (under 45 min on a hit, under 4 GB of cache, one download per key), so the full `rocm` check runs rather than a subset. The CUDA variant is not added: it is optional in the issue and was not measured. Repository cache usage was already about 10.5 GB before this entry, so GitHub's LRU eviction applies across all of them. ## Test plan - [x] Windows 11 host: `python -m unittest discover -s tests -p 'test_*.py'` (188 tests, 76 skipped), `cargo test -p mlxcel-mlx-pin --lib` (49 passed), both host tests with ROCm's `clang++`, actionlint 1.7.7 clean on `ci.yml`. - [x] Deliberately broken order (78f4552 swaps the HIP_PATH and bundle branches of `choose_rocm_include_dir()`): [run 40406365843](https://github.com/lablup/mlxcel/actions/runs/40406365843) fails `overlay runtime-dir host tests` with `FAIL: include: bundle beats HIP_PATH` and one more check ([job](https://github.com/lablup/mlxcel/actions/runs/40406365843/job/121865140004)). Reverted in 9ab81fc. - [x] Final tree: [run 40415050086](https://github.com/lablup/mlxcel/actions/runs/40415050086) passes `overlay runtime-dir host tests` (21 s) and `Windows build (compile-only)` (19 min 50 s, cache hit). The squash commit touches gated paths, so `windows-build` also runs on `main` after merge, which creates the `main`-scoped cache entry later PRs restore. ## Changes during review - `overlay-host-tests` became unconditional; it was gated on filters that missed several of the files it tests. - The MLX pin joined the `windows_build` filter, so a pin bump that breaks the Windows build fails on its own PR. - The docs say the `ci:full` label referenced by `ci.yml` is not defined in the repository yet. Until a maintainer creates it, a PR that edits only the `windows-build` definition (for example a tarball digest bump) cannot run that job before merge. Closes #2642
Summary
MLXCEL_ROCM_RUNTIME_DIR(absolute; when set it is the only candidate, and a relative path or a folder without the HIP runtime is a clear error), else the executable's directory when it holds the HIP runtime named in the image's delay-import table, else the baked SDKbin. The choice is printed as[mlx-rocm] ROCm runtime: <dir> (<source>). The selection is a pure header-only function (runtime_dir.h).amd_comgr*.dlland ahiprtc-builtins*.dll, which the HIP runtime and hiprtc load by plain name. A missing file, or a DLL missing from the chosen directory, fails with the expected path and never falls back to another ROCm installation (including the display driver's System32 copies).<runtime dir>\includeor<runtime dir>\..\includeafterROCM_HOME/ROCM_PATH/HIP_PATHand before the baked SDK include.scripts/package_windows_rocm_runtime.pybuildsmlxcel-rocm-runtime-<ver>-win-x64.zip(gfx1151, ROCm 10.1.0: 359 files, 225 MiB, 76 MiB zipped) or stages a tree;--add-binarymakes a full app folder.HIP_PATHset by the HIP SDK installer overrides the bundle headers),MLXCEL_ROCM_RUNTIME_DIR.LOCAL_FIXES.mditem 58.Deviations from the issue, found while testing
bin\,.kpack\,include\) instead of being flat: rocBLAS, hipBLASLt and rocFFT load device code from<dll dir>\..\.kpack\<lib>_<arch>.kpack. The binaries go in the bundle'sbin\, orMLXCEL_ROCM_RUNTIME_DIRnames thatbin\.libopenblas.dllis an eager import of both binaries and must sit beside them.Changes during review
amd_comgr.dllsilently loaded the driver'sSystem32\amd_comgr.dllnext to the bundle's HIP runtime, fell back to CPU and crashed with 0xC0000409. The comgr/hiprtc-builtins presence check above fixes it.%ls, which the MSVC CRT truncates at the first non-ASCII character; paths are now printed as UTF-8, and directory listing no longer throws on unconvertible names.--rocm-versionis validated like.info/version(a../value could write outside--out).Verification (gfx1151, ROCm 10.1.0, final build at eb1c47a; SDK renamed away, PATH=System32 only, no ROCm env vars, cold JIT cache, cwd holding decoy ROCm DLLs)
bin\: generate 1.7B-bf16bin\/v1/messages[DONE]MLXCEL_ROCM_RUNTIME_DIRnaming the bundleamd_comgr.dll/hiprtc-builtins*.dll/rocblas.dllC:\rocm\10.1.0\bincargo test -p mlxcel-mlx-pin --lib(49), fmt, clippy--features rocm -D warnings, overlay drift/records,check_env_var_docs.py, host C++ test,tests/test_package_windows_rocm_runtime.py(18)Informational: llama.cpp b11889's ROCm 10.0
amdhip64_7.dll,amd_comgr.dllandrocm_kpack.dllplus the 10.1 math libraries ran both models correctly throughMLXCEL_ROCM_RUNTIME_DIR; one host only, so the docs still forbid mixing.Not verified: other drivers and GPUs, Windows long-path mode, FFT (hipFFT/rocFFT are shipped but no FFT ran). The host C++ test is not run by CI.
Closes #2606