Repository navigation
feat: build and run mlxcel natively on Windows with CUDA - #2599
Conversation
Native Windows MSVC builds of mlxcel and mlxcel-server with the cuda feature now compile, link and run single-GPU inference. Validated on aarch64-pc-windows-msvc (NVIDIA RTX Spark N1X, sm_121, CUDA 13.4.3, cuDNN 9.27, VS 2026); the glue resolves x64 and arm64 toolkit layouts alike. - Build glue (build_support/windows_cuda.rs, shared by both build scripts): toolkit and cuDNN layout under lib\<arch> and bin\<arch>, Ninja generator, forward-slash CMake paths, import libraries, /DELAYLOAD for the CUDA and cuDNN DLLs, MLX's delayload.cpp compiled into the bridge, and the OpenBLAS import library plus DLL copy. - MSVC flags for the C++ bridge (/std:c++20, MLX_STATIC, /bigobj, /fp:fast); MLXCEL_CXX_MARCH maps to /arch. - CUDA overlays: delayload.cpp preloads the DLLs NVRTC and cuDNN open by name and resolves each DLL's imports beside it; jit_module.cpp finds CUDA headers without CUDA_PATH; matmul.cpp gains the missing BlockMaskedMM::eval_gpu that only MSVC's link rejected. - Drop the dead C++ split() and tokenizers' esaxx_fast (static-CRT C++ that cannot link with /MD). - Windows is single-node: distributed flags are refused at startup; Unix-only listener and fd paths are gated. Smoke tests on the N1X with no CUDA env vars and a cold JIT cache: gemma-4-e4b, gemma-4-26b-a4b and a qwen3_5 27B generate correct text, and mlxcel-server serves streaming and non-streaming chat completions. Refs #58
Implementation Review SummaryIntent
Findings AddressedNo CRITICAL or HIGH findings, so no fixes were committed. Remaining Items
Verification
Checked against Linux CUDA, ROCm and macOS by reading the code. On those builds |
- Refuse distributed flags on Windows before the model is resolved, in mlxcel generate, mlxcel serve and mlxcel-server, so a repo id is not downloaded only to be turned away. The shared ServerDistributedFlags helper backs both server CLIs and the start_server check. - Stop compiling MLX's delayload.cpp into the bridge: CMake already builds it into mlx.lib, so the hook was defined twice. /INCLUDE now pulls the single copy from mlx.lib, which is linked ahead of delayimp.lib. - Document CUDA 13.x only on Windows; CUDA 12.x keeps its DLLs in bin\ and the cuDNN installer layout is not searched. - Fix the Unix-socket refusal message and the per-CLI flag list in docs/distributed.md. Verified on the RTX Spark N1X: cold-JIT generate with no CUDA env vars, and the three entry points refuse --tp-size / --pp-auto with a nonexistent repo id in 0.2 s. Refs #58
bind_tcp set SO_REUSEADDR unconditionally. On Unix that only allows rebinding past TIME_WAIT, but on Windows it marks the port as sharable: any other socket that also sets SO_REUSEADDR can bind the same address and port while mlxcel-server is listening and receive its incoming connections, including API keys and prompts. std, mio and tokio leave the option unset on Windows for this reason, and Windows rebinds a listener past TIME_WAIT without it. SO_EXCLUSIVEADDRUSE is not used because it delays restarts while connections sit in TIME_WAIT, and a plain socket already refuses a second SO_REUSEADDR bind (WSAEACCES). The option is now set only under cfg(not(windows)), so Linux and macOS behavior is unchanged. This also makes the existing a_second_bind_without_reuse_port_fails test hold on Windows. New test a_second_socket_with_so_reuseaddr_cannot_take_a_live_port binds the server listener, then asserts that a raw socket2 socket with SO_REUSEADDR cannot bind and listen on the same port. It runs on every platform. Validated on Windows 11 ARM64: cargo fmt --check and cargo clippy --release --features cuda --lib -D warnings pass. Clippy with --tests reports no findings in the listen files; its remaining errors are pre-existing Windows dead-code and unused-import findings in other test modules. A Winsock probe confirmed the four REUSEADDR combinations behave as described. Tests were not run locally. Refs #58
Security and performance reviewScope: what this PR adds or newly exposes by making native Windows a documented platform. The DLL loader was compared against upstream MLX Fixed
Not fixed (MEDIUM/LOW, left for follow-up)
The distributed refusal covers the server CLI, Validation: |
- The delay-load overlay loads CUDA, cuDNN and NVRTC DLLs with LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS instead of LOAD_WITH_ALTERED_SEARCH_PATH, so their imports never come from the current directory or PATH (security review LOW). - Download tempfiles are opened with create_new on Windows too, so an existing entry at the path, a symlink included, fails the open instead of being truncated through. - docs/installation.md: router-mode downloads and cache removal are refused on Windows, not served by a fallback. - Tests: the non-UTF-8 argument test builds an unpaired-surrogate OsString on Windows; symlink and Unix-socket tests and their imports are cfg(unix); a new test checks that a Unix socket target is refused off Unix. The mlxcel lib tests now compile on Windows, and clippy --lib --tests --bins -D warnings is clean there. Verified on the RTX Spark N1X: 17 listen, distributed and short-flag tests pass; gemma-4-e4b (cold JIT) and qwen3_5 27B (cuDNN path) generate with no CUDA env vars and no DLL warnings. Refs #58
The Windows CUDA build reads CUDA_PATH (after CUDA_HOME) and CUDNN_PATH, but environment-variables.md only named CUDA_HOME. Add them next to the existing CUDA note, linking the Windows install section. Refs #58
The previous commit moved the scan_insecure_allowlist_dirs import into one cfg(unix) test, but a second cfg(unix) test in the same file also calls it, so the Linux lib-test build failed (clippy, OpenXLA compile, WebUI installed artifact and 2-host CI jobs). The import is module-level again, gated on cfg(unix). Refs #58
Native Windows MSVC builds of
mlxcelandmlxcel-serverwith--features cudanow compile, link and run single-GPU inference.The only NVIDIA Windows host available is ARM64 (RTX Spark N1X,
sm_121), so the spike ran onaarch64-pc-windows-msvc(decided with the issue owner); the build glue resolvesx64andarm64toolkit layouts alike, but no x86_64 GPU host has run it yet.Phase 1 gate
Upstream MLX at the pin plus mlxcel's CPU and CUDA overlays builds under MSVC 14.51 + nvcc 13.4 with no source patch; only CMake library hints are needed, because
FindCUDAToolkitand MLX'sFindCUDNNsearchlib\x64only.What changed
build_support/windows_cuda.rs(shared by both build scripts): toolkit and cuDNN layout (CUDA 13 redistributable layout,lib\<arch>/bin\<arch>), Ninja generator, forward-slash CMake paths, import libraries, and/DELAYLOADplus/INCLUDEso MLX's delay-load hook inmlx.libis linked. OpenBLAS import library and DLL copy for MLX's Windows CPU backend./std:c++20,MLX_STATIC,/bigobj,/fp:fast);MLXCEL_CXX_MARCHmaps to/arch.delayload.cpp(new) preloads the DLLs NVRTC and cuDNN open by plain name and resolves each DLL's imports from its own directory, never from the current directory orPATH;jit_module.cppfinds CUDA headers withoutCUDA_PATH;matmul.cppgains the missingBlockMaskedMM::eval_gpu, a stale-overlay bug GNU ld hid by discarding the unused vtable.split()andtokenizers'esaxx_fastfeature (training-only, static-CRT C++ that cannot link with/MD).generate,serveandmlxcel-serverrefuse distributed flags before resolving the model (src/distributed/platform.rs,ServerDistributedFlags). Unix-only code iscfg-gated; the lib tests compile on Windows.docs/installation.md, README,docs/distributed.md,docs/environment-variables.md.Changes during review
SO_REUSEADDRon Windows, where it lets another socket bind the live port; download tempfiles use exclusive create on Windows.mlx.lib), not also into the bridge.Validation (RTX Spark N1X, no CUDA env vars, cold JIT cache)
mlxcel-serverserves streaming and non-streaming chat completions.--lib --tests --bins -D warnings) and fmt clean; targeted Windows tests pass (17 lib tests, 7mlxcel-mlx-pin). Linux and macOS are covered by CI.Not done
Release packaging and a Windows CI job; x86_64 GPU validation; CUDA 12.x layout; Qwen 3.5 MTP stays Metal-only by existing design; decode throughput on this host (27B dense about 4.4 tok/s) is not investigated.
Closes #58