Skip to content

feat(rocm): prefer bundle headers over HIP_PATH, log compiled targets - #2648

Merged
inureyes merged 5 commits into
mainfrom
feature/issue-2639-bundle-headers-gpu-targets
Oct 26, 2026
Merged

inureyes merged 5 commits into
mainfrom
feature/issue-2639-bundle-headers-gpu-targets

Conversation

@inureyes

@inureyes inureyes commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

Summary

  • hiprtc header order on Windows is now ROCM_HOME, ROCM_PATH, bundle include (runtime dir source Env or ExeDir with hip\hip_runtime.h), HIP_PATH, runtime include for a Baked source, baked include. A HIP SDK's machine-wide HIP_PATH no longer overrides a bundle. When a variable and a bundle disagree, one line names both: [mlx-rocm] hiprtc headers: <chosen> (<source>); ignoring <other> from <source>. Linux passes no bundle and no HIP_PATH, so its -I string is unchanged.
  • choose_rocm_include_dir() stays pure and awk-extractable; rocm_runtime_dir_source() (delayload.cpp) and the pure rocm_include_dir_notice() live beside it in runtime_dir.h.
  • MLXCEL_ROCM_ARCHITECTURES comes from CMAKE_HIP_ARCHITECTURES in the overlay CMake (no build.rs change: build.rs already passes the resolved list as that variable). device.cpp prints [mlx-rocm] compiled HIP targets: ... once after the bound-device line.
  • The overlay CMake rejects any MLX_ROCM_ARCHITECTURES entry that is not a HIP target ID (gfx1151, gfx11-generic, gfx90a:xnack+ forms) with Not a HIP target ID, because the list is now also compiled into a C string literal (security review, LOW).
  • The bundle script exits 1 naming every --arch without .kpack archives or rocBLAS/hipBLASLt entries.
  • Docs: new order, rewritten HIP_PATH caveat, distributable list gfx1100;gfx1101;gfx1102;gfx1150;gfx1151;gfx1200;gfx1201, LOCAL_FIXES item 66.

Target facts (ROCm 10.1.0)

  • The installed gfx1151 tarball has kernel data and .kpack for gfx1151 only; its hipcc and device libs compile all seven targets.
  • therock-dist-windows-multiarch-10.1.0.tar.gz (4.9 GB) covers all seven in bin\rocblas\library, bin\hipblaslt\library and .kpack (checked by streaming tar -t, nothing extracted). The per-family tarballs cover only their own family.
  • Kernel archive mlx_rocm_kernels.lib: 716.6 MiB (7 targets) vs 190.1 MiB (gfx1151). HIP kernel step, CMake configure to archive, -j 8: 5 min 8 s (cold tree) vs 3 min 6 s (fresh mlxcel-core build dir in the warm tree). Every HIP object holds all seven code objects.
  • Seven-target binaries (CARGO_TARGET_DIR=C:\t\rw7): the -j 8 build compiled every HIP object, then the link failed with link.exe 0xC0000142 under memory pressure on the 32 GB host. Rerunning cargo build -j 2 --bin mlxcel --bin mlxcel-server linked both in 7 min 01 s. mlxcel.exe 745,592,320 bytes vs 193,465,856 (gfx1151 alone); mlxcel-server.exe 735,546,880 vs 183,420,416. On gfx1151 it prints [mlx-rocm] compiled HIP targets: gfx1100,gfx1101,gfx1102,gfx1150,gfx1151,gfx1200,gfx1201 and generated "Paris" at 75 tok/s. The docs tell a ~32 GB host to link at -j 2. Non-gfx1151 targets are build-verified only (compile, no hardware).

Test plan

  • rocm_runtime_dir_test.sh passes; the new test fails on main (compile error, and 2 checks fail when main's order gets the new signature)
  • Packaging tests (22) pass; real SDK with --arch gfx1151 --arch gfx1100 --arch gfx1201 exits 1 naming both, writes nothing
  • gfx1151 build; bundle generate (Qwen3-0.6B-4bit, Qwen3-1.7B-bf16) and server chat/stream/messages correct
  • HIP_PATH set to a decoy whose hip_runtime.h is #error: bundle JIT compiles 3 kernels and prints the line; the build-tree binary (Baked) hits the decoy error, as intended
  • ROCM_PATH at run time still wins and prints the line
  • tests/test_rocm_arch_validation.py runs the real CMake validation block with cmake -P: accepts the seven-target list and target-ID forms, rejects a quote-injection string, native, GFX1151, sm_80; gfx1151 rebuild after the check and a bundle generate ("Paris", 76.8 tok/s)
  • records, drift, env-var docs, cargo test -p mlxcel-mlx-pin --lib, fmt
  • cargo clippy -p mlxcel-core --release --features rocm -j 8 -- -D warnings clean (4 min 26 s, warm C:\t\rw)

Closes #2639

The AMD HIP SDK installer sets HIP_PATH machine-wide, and HIP_PATH outranked a Windows ROCm runtime bundle's include, so on any machine with a HIP SDK a ROCm 10.1 bundle JIT-compiled kernels against another release's headers.

choose_rocm_include_dir() now takes runtime_is_bundle and orders ROCM_HOME, ROCM_PATH, the bundle include (runtime dir source Env or ExeDir, holding hip/hip_runtime.h), HIP_PATH, the runtime include of a Baked source, then the baked include. delayload.cpp exposes rocm_runtime_dir_source(), and the pure rocm_include_dir_notice() in runtime_dir.h prints one line naming both directories when an environment variable and a bundle disagree. Linux passes no bundle and no HIP_PATH, so its -I string is unchanged.

The overlay CMake defines MLXCEL_ROCM_ARCHITECTURES from CMAKE_HIP_ARCHITECTURES and device.cpp prints it once after the bound-device line. The bundle script fails with every --arch that lacks .kpack archives or rocBLAS/hipBLASLt entries named, instead of writing a partial bundle. Docs describe the new order, the distributable target list and where its kernel data comes from (the TheRock multiarch tarball).

Validated: host include-order test (fails on main), packaging tests, bundle generate and server on gfx1151, a decoy HIP_PATH ignored with the notice, ROCM_PATH still winning. The seven-target build compiled every HIP kernel but did not link on the 32 GB host.

Closes #2639
@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 26, 2026
The GPU targets section still said the seven-target link had not completed. It has: rerun with -j 2 after the HIP objects were compiled, the link of mlxcel and mlxcel-server took 7 min 1 s, and the binaries run on gfx1151 and print the compiled target list. The section now gives the binary sizes for seven targets against gfx1151 alone next to the kernel archive and HIP step figures, and tells a ~32 GB host to link at -j 2, since -j 8 failed with link.exe 0xC0000142.

Refs #2639
@inureyes

Copy link
Copy Markdown
Member Author

Implementation Review Summary

Intent

Windows hiprtc headers: a runtime bundle outranks a machine-wide HIP_PATH with one naming line on disagreement, Linux unchanged; log the compiled HIP targets; document the distributable seven-target list; packaging fails naming targets without kernel data.

Findings Addressed

  • docs/installation.md still said the seven-target link had not completed and gave no binary sizes or link-parallelism advice; it now has the measured sizes (seven targets vs gfx1151), the 7 min 1 s -j 2 link, and tells a ~32 GB host to link at -j 2 (HIGH, 272e74e). The PR body's target facts and clippy line are updated to match.

Remaining Items

  • None. The include order, the notice logic (pure, covered for ROCM_PATH-over-bundle, bundle-over-HIP_PATH, same-directory and silent cases), the MLXCEL_ROCM_ARCHITECTURES definition (same quoted-string pattern as the existing MLXCEL_ROCM_BIN_DIR, commas so ; cannot split it) and the packaging failure path read correct.

Verification

  • All stated requirements implemented
  • No placeholder/mock code remaining
  • Integrated into project code flow
  • Project conventions followed
  • Existing modules reused where applicable
  • No unintended structural changes
  • Tests pass: rocm_runtime_dir_test.sh, 22 packaging tests, env-var docs check, and cargo clippy -p mlxcel-core --release --features rocm -- -D warnings clean

CMAKE_HIP_ARCHITECTURES comes from the MLX_ROCM_ARCHITECTURES environment variable verbatim and now also lands in the MLXCEL_ROCM_ARCHITECTURES C string literal printed by device.cpp, so a value with a quote could end the literal and splice code into the build. The backend CMakeLists.txt now fails the configure step on any entry that is not a HIP target ID (gfx1151, gfx11-generic and gfx90a:xnack+ pass; quotes, spaces and upper case do not). Every documented form is a semicolon list of plain targets, so no existing build changes.

Checked the pattern with cmake -P against valid and invalid entries, overlay drift and records pass, and the gfx1151 build was rebuilt with the change.

Refs #2639
@inureyes

Copy link
Copy Markdown
Member Author

Security and performance review of the header precedence, notice line, runtime_dir.h helpers, compiled-target log and packaging check.

Fixed in d6348c9 (LOW, build-time hardening): MLX_ROCM_ARCHITECTURES reaches CMAKE_HIP_ARCHITECTURES verbatim and now also lands in the MLXCEL_ROCM_ARCHITECTURES string literal, so a quote in it could end the literal. The backend CMakeLists.txt now fails configure on any entry that is not a HIP target ID (^gfx[0-9a-f]+(-[0-9a-f]+)?(-generic)?(:[a-z0-9]+[+-])*$). A plain gfx[0-9a-f]+ check was not used because it would reject valid gfx90a:xnack+ and gfx11-generic targets. LOCAL_FIXES item 66 records this.

No CRITICAL, HIGH or MEDIUM findings. Notes, not changed:

  • Trust: the bundle include comes from the executable's directory or MLXCEL_ROCM_RUNTIME_DIR. Anyone who can write there can already replace the binary and the DLLs, so ranking it above HIP_PATH gives nobody new control. ROCM_HOME/ROCM_PATH still win and are named in the line.
  • A bundle without hip/hip_runtime.h falls back to HIP_PATH with no line. That is the earlier behavior, and the packaging script always ships include/.
  • same_include_dir folds only ASCII case, and getenv returns ANSI while the runtime include is UTF-8. A non-ASCII path can therefore print an extra notice line, but it never changes which directory is chosen. The helpers allocate and do nothing else; no exception can escape them other than bad_alloc.
  • The packaging missing-target check runs in build_plan before anything is written. A failed run leaves no stage directory.

Verification: cmake -P checked the pattern against valid and invalid entries. Overlay drift and records pass. The host C++ test and 22 packaging tests pass. The gfx1151 release rebuild (15m33s) succeeded. A bundle staged from it generated "Paris" from a clean environment with a fresh hsaco cache and printed compiled HIP targets: gfx1151. --arch gfx1100 failed with the target named and no stage directory created.

… IDs

Add tests/test_rocm_arch_validation.py, which extracts the validation loop from the ROCm overlay CMakeLists.txt and runs it with cmake -P: gfx IDs, feature suffixes and generic targets pass, while native, uppercase, option strings and quote injection fail with "Not a HIP target ID". It skips when cmake is absent.

Document the accepted target-ID forms where MLX_ROCM_ARCHITECTURES is described in docs/installation.md (both ROCm sections) and docs/environment-variables.md.

Validation:
- python3 -m unittest tests/test_rocm_arch_validation.py tests/test_package_windows_rocm_runtime.py: 24 tests OK
- cargo fmt --all --check, check_env_var_docs.py, overlay drift and records: OK

Refs #2639
@inureyes inureyes added status:done Completed and removed status:review Under review labels Oct 26, 2026
@inureyes
inureyes merged commit 11463e5 into main Oct 26, 2026
29 checks passed
@inureyes
inureyes deleted the feature/issue-2639-bundle-headers-gpu-targets branch October 26, 2026 17:12
@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: Prefer Windows ROCm bundle headers over HIP_PATH and add GPU targets

1 participant