Skip to content

feat: build and run mlxcel natively on Windows with ROCm - #2603

Merged
inureyes merged 5 commits into
mainfrom
feature/issue-2601-windows-rocm
Oct 25, 2026
Merged

inureyes merged 5 commits into
mainfrom
feature/issue-2601-windows-rocm

Conversation

@inureyes

@inureyes inureyes commented Oct 25, 2026 •

Copy link
Copy Markdown
Member

Summary

--features rocm now builds and runs natively on Windows x86_64 MSVC with a ROCm 10 SDK (validated on gfx1151 with ROCm 10.1.0). Linux code paths are unchanged apart from the feature-gate panic text and three fixes that apply on all platforms: the host-shadow dirty flag, the shadow lifetime on free and reuse, and the DivMod duplicate.

  • Build script: the feature gate accepts Linux or Windows x86_64 MSVC. build_support/windows_rocm.rs (RocmLayout, resolve_rocm_root, parse_amdgpu_arch, reusing windows_cuda::cmake_path) is wired into build_mlx (Ninja, forward-slash ROCM_PATH/CMAKE_PREFIX_PATH/CMAKE_HIP_COMPILER), detect_rocm_arch (amdgpu-arch) and link_rocm (import libs, libhipblaslt.dll.a copied to OUT_DIR/hipblaslt.lib so the link reaches the final binaries). ROCm 10.1's tarball keeps amdgpu-arch.exe in lib\llvm\bin\, not bin\; the layout prefers bin\ and falls back. Tests live in mlxcel-mlx-pin.
  • /EHsc for MLX's own C++ on every MSVC build. cmake-rs replaced CMake's MSVC defaults, so MLX compiled without unwind semantics (C4530). This also applies to the Windows CUDA build; it is flag-only and unverified here (no NVIDIA GPU on this host).
  • Overlay (LOCAL_FIXES.md entries 53-56): a WIN32 CMake branch (no libstdc++ probing, no -fPIC, --rocm-path/--rocm-device-lib-path, /MD runtime, .obj, lib.exe archive mlx_rocm_kernels.lib), MSVC portability fixes (sysconf, weak symbol, dup2//dev/null, <iterator>, __builtin_clz shim), and an LLP64-safe hiprtc size_t. rocm_home() now also tries HIP_PATH on Windows and treats an empty ROCM_HOME/ROCM_PATH/HIP_PATH as unset; before, an empty value produced -I/include, the root of the current drive on Windows, where any local user could plant a header the slicing JIT includes.
  • Memory model: Windows WDDM does not map device memory into the CPU address space, even on an APU (CPU writes to hipMalloc and fine-grained memory fault; managed and pinned memory are capped near the OS-visible RAM). Windows therefore uses plain hipMalloc and routes every CPU access through the host shadow (device_memory_host_visible() in allocator.h, constexpr true on Linux). raw_ptr() marks the shadow dirty so gpu_ptr flushes CPU writes back before the next kernel; nothing set that flag before, so CPU writes on the existing discrete-GPU shadow path were never flushed. The shadow is freed with its buffer and its dirty flag cleared on free and on buffer-cache reuse, which closes a pinned-memory leak in the scalar pool and a stale shadow handed to a buffer's next owner.
  • DivMod: binary_two.hip is dropped and binary.hip keeps the only definition of DivMod::eval_gpu, which was the only non-template definition in binary_two.hip; nothing else references its kernels or templates. Linux resolved binary.o first (archive order follows HIP_SOURCES, and pulling both would have been a multiple-definition error), so Linux behavior is unchanged; this was reasoned, not traced with a Linux link.

Test plan

Windows 11, Radeon 8060S (gfx1151), Adrenalin 32.0.31041.1004, ROCm 10.1.0, VS 18 x64 developer environment, ROCM_PATH set:

  • cargo build --release --features rocm builds mlxcel.exe and mlxcel-server.exe; the -vv log has 0 C4530 and 0 LNK4006, with all HIP objects and mlx_rocm_kernels.lib rebuilt in that run. amdgpu-arch detection gives gfx1151.
  • mlxcel generate -m mlx-community/Qwen3-0.6B-4bit -p "What is the capital of France?" -n 60 --temp 0 --show-reasoning on the final head: correct ("France's capital is Paris"), 113 tok/s; the first token runs a hiprtc fused-kernel JIT.
  • Same with mlx-community/Qwen3-1.7B-bf16 -n 80 (hipBLASLt bf16): correct, 28.7 tok/s.
  • JIT with ROCM_HOME and ROCM_PATH empty and HIP_PATH set: generation succeeds through the HIP_PATH fallback.
  • cargo test -p mlxcel-mlx-pin --lib (44 pass, 11 new), cargo fmt --all --check, cargo clippy -p mlxcel-core --release --features rocm -- -D warnings, rocm_overlay.py drift and records, check_env_var_docs.py.
  • Linux ROCm build: not run. CI's "ROCm build and generate" job is parked (no rocm-gfx1151 runner, ci(rocm): self-hosted gfx1151 runner that builds, links and smoke-tests ROCm changes #1811), so nothing in CI builds --features rocm on Linux. The implementation and security reviews checked that every Linux-reachable change is either identical or one of the three intended fixes above.
  • Windows CUDA build with /EHsc: needs a CUDA host.

Running still needs the ROCm bin directory on PATH and ROCM_PATH (or HIP_PATH) set; #2602 removes both requirements.

Closes #2601

The rocm feature was Linux-only. ROCm 10 on Windows is an MSVC-ABI stack, so the backend can build with the same toolchain as MLX and the Rust binary once the build script, the overlay CMake and a few backend sources learn the Windows layout.

- Build script: the feature gate accepts Linux or Windows x86_64 MSVC. New build_support/windows_rocm.rs resolves the SDK from ROCM_PATH or HIP_PATH, checks hipcc.exe and lib/amdhip64.lib, detects gfx targets with amdgpu-arch (bin/ or lib/llvm/bin/), and reuses windows_cuda::cmake_path. build_mlx selects Ninja and passes forward-slash paths; link_rocm links the import libraries and copies libhipblaslt.dll.a to OUT_DIR/hipblaslt.lib. Tests run in mlxcel-mlx-pin.
- build_mlx adds /EHsc for every MSVC build, since cmake-rs replaces CMake's MSVC defaults (C4530). This also affects the Windows CUDA build.
- ROCm overlay: a WIN32 branch in the backend CMake, MSVC portability fixes, an LLP64-safe hiprtc size_t, and a host-shadow memory model for device memory the CPU cannot map on Windows. raw_ptr now marks the shadow dirty, which also fixes CPU writes never reaching VRAM on the discrete-GPU path.
- binary_two.hip is dropped: it duplicated DivMod::eval_gpu, which ld resolved to binary.hip and lib.exe reported as LNK4006.
- Docs: Windows with AMD ROCm in installation.md and the Windows build variables in environment-variables.md.

Verified on a Radeon 8060S (gfx1151) with ROCm 10.1.0: both binaries build and generate is correct on Qwen3-0.6B-4bit and Qwen3-1.7B-bf16. Linux is covered by the CI ROCm job; the Windows CUDA build is unverified (no NVIDIA host).

Refs #2601
@inureyes inureyes added status:review Under review type:enhancement New features, capabilities, or significant additions priority:medium Medium priority area:core mlxcel-core: MLX FFI, primitives, KV cache, layers platform:windows Windows (native) specific labels Oct 25, 2026
With device memory not host-visible on Windows, every buffer read on the CPU gets a pinned host shadow, and raw_ptr now marks it dirty. Three allocator paths did not account for that. A scalar-pool slot released by the buffer cache kept its shadow, and SmallSizePool::malloc reset the pointer when handing the slot out again, leaking one pinned allocation per recycled scalar. A cache hit handed the next owner the previous owner's dirty shadow, so the next gpu_ptr spent a full-buffer copy writing stale bytes to the device before the new owner's kernel overwrote them. free_async freed the shadow but left host_dirty set, so a cache hit that retags a buffer would skip the device-to-host copy into a fresh shadow.

free_rocm_buffer now releases a pool slot's shadow, free_async clears host_dirty with the shadow, and a cache hit in malloc_async clears host_dirty. On Linux none of these paths has a shadow with host_dirty set except the discrete path this PR already changes, so Linux behavior is otherwise unchanged. LOCAL_FIXES entry 55 records it.

Verified with cargo clippy -p mlxcel-core --release --features rocm (the overlay allocator recompiled cleanly) and rocm_overlay.py drift and records.

Refs #2601
@inureyes

Copy link
Copy Markdown
Member Author

Security and performance review

No CRITICAL or HIGH findings, so nothing was changed on the branch. Linux paths: device_memory_host_visible() is a constexpr true there, so the new conditions fold away; the only Linux-visible additions are plain host_dirty stores (cache hit, free) and host_dirty = true in the device >= 0 branch of raw_ptr, which Linux reaches only with MLX_ROCM_USE_ASYNC_POOL=1.

Remaining items (not fixed, for follow-up):

  • MEDIUM, jit_module.cpp:150-176,376: rocm_home() accepts an empty ROCM_HOME/ROCM_PATH/HIP_PATH, producing -I/include. On Windows that is the root of the current drive (C:\include, creatable by any local user, or the CWD's drive), and the slicing JIT source includes <hip/hip_runtime.h> through it. Treat empty values as unset, as windows_rocm::resolve_rocm_root already does.
  • MEDIUM, allocator.cpp:927-952: host shadows are pinned memory outside active_memory_ and the memory limit, and stay attached while a buffer sits in the cache. A large CPU read (for example a prompt-cache save of the KV cache) can exhaust the roughly 28 GiB of pinnable RAM and surface as a hipHostMalloc (host shadow) failed exception.
  • LOW, jit_module.cpp:67-100: on Windows _dup2 onto fd 2 closes the original STD_ERROR_HANDLE and installs a new value on restore. The suppressor itself restores on every path; the risk is only for a component that cached the old handle value.
  • LOW, naive_gemm.hip:2229-2240: the MoE workspace shell deleter does not free a host_shadow (pre-existing, both platforms).
  • LOW, build.rs:775: no rerun-if-changed for libhipblaslt.dll.a, so an SDK update in place keeps a stale hipblaslt.lib copy.
  • Out of scope (feat: self-contained Windows ROCm runtime and server validation #2602): the executables import the ROCm DLLs by name, and the loader searches the CWD before PATH, so a DLL planted in the working directory wins.

Checked and fine: build-time tools run by absolute path, the __builtin_clz shim is only reached with a nonzero mantissa (hipblaslt_cast_from_uf8 is called only with we=5), GlobalMemoryStatusEx sizing, and the hipBLASLt copy panics with the path on failure.

Verification: cargo test -p mlxcel-mlx-pin --lib (44 passed), clippy on mlxcel-core --features rocm clean, and generate on Qwen3-0.6B-4bit gives correct text at 116 tok/s.

rocm_home() in the overlay's jit_module.cpp treated an empty variable as set, because getenv returns "" rather than null. That produced the hiprtc include flag -I/include, which on Windows is the root of the current drive, where any local user can plant hip/hip_runtime.h for the slicing JIT to include.

Empty values are now skipped in favor of the next candidate, matching windows_rocm::resolve_rocm_root in the build script. The LOCAL_FIXES.md entry 54 records it, and the environment variable reference notes that an empty value counts as unset.

Validation: cargo clippy -p mlxcel-core --release --features rocm -- -D warnings passes; rocm_overlay.py drift and records pass; cargo fmt --all --check and cargo test -p mlxcel-mlx-pin --lib pass.

Refs #2601
The Windows ROCm prerequisites listed amdgpu-arch.exe as living in bin or lib\llvm\bin without saying which SDK uses which. The ROCm 10.1 TheRock tarball keeps it in lib\llvm\bin, other SDKs in bin; the build script already falls back between the two.

No Korean counterpart exists for docs/installation.md or docs/environment-variables.md (docs/ko and docs/*.ko.md are absent), so nothing to mirror.

Refs #2601
@inureyes inureyes added status:done Completed and removed status:review Under review labels Oct 25, 2026
@inureyes
inureyes merged commit 65b7ade into main Oct 25, 2026
1 check passed
@inureyes
inureyes deleted the feature/issue-2601-windows-rocm branch October 25, 2026 18:40
@inureyes inureyes added this to the 0.8 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core mlxcel-core: MLX FFI, primitives, KV cache, layers platform:windows Windows (native) specific priority:medium Medium priority status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: build and run mlxcel natively on Windows with ROCm (gfx1151)

1 participant