Repository navigation
fix(rocm): key custom-kernel JIT modules by their generated source - #2181
Merged
Merged
Conversation
The ROCm overlay cached a compiled custom kernel under device index plus the kernel name, and the name covers only template_args. build_kernel also writes input and output dtypes and each input's 0-d-ness into the source, so a second call differing only there reused the first module: an f16 input was read as f32, and a 0-d input after a 1-d one launched with a missing argument. CustomKernel::eval_gpu now passes name_ + "_" + hex(hash(source_)) as the module name, as upstream CUDA does at pin 81ba1c6a; the kernel symbol is unchanged. Adds the rocm_jit_key_probe bridge fixture and tests/rocm_custom_kernel_jit_key.rs (f32/f16/bf16, and 1-d then 0-d in a child process). Both fail with the name-only key restored and pass with the fix on gfx1151. Records LOCAL_FIXES item 33 (#2179 took 32), and corrects the CUDA keying description in check_kernel_dtype_keys.py and docs/code-guidelines.md; the checker and every explicit dtype key stay as defense in depth. Closes #2149
Review follow-up for #2149. The probe gains an f16_output flag and tests/rocm_custom_kernel_jit_key.rs adds an f32-then-f16 output case at one input dtype; with the name-only key restored it fails (the f16 buffer is written through float*), and it passes with the source-hash key on gfx1151. The keying comments in turbo/paged_attention.cpp and paged_attention_v2.cpp described CUDA as name-keyed in the present tense; they now say that was true when #1053 was found and that CUDA and ROCm key on the source hash, with the dtype args kept as defense in depth. LOCAL_FIXES item 33 lists the independent-input-dtype limit of the template-args check, and the checker docstring is reflowed.
7 tasks done
inureyes
added a commit
that referenced
this pull request
Oct 6, 2026
…2186) ## Summary The fused paged-attention kernels computed the per-token pool base in 32 bits, so a layer slab past 2^32 elements per side (131,072 blocks of `[32, 8, 128]` for Llama-3.1-8B) silently read another row of the same buffer. The base is now 64-bit in all six bodies, and the 32-bit merge kernel is guarded by size checks instead. ## What changed Kernel bodies: only the pool-base declaration changes, plus a one-line comment above it. The variable type and the casts change; template args, grid, JIT names and dtype keys are unchanged (`make verify-kernel-dtype-keys`: 9 in scope). | Backend | v1 decode | v2 partial | |---|---|---| | Metal (`ulong`) | `paged_attention.cpp:149` | `paged_attention_v2.cpp:176` | | CUDA (`uint64_t`) | `paged_attention.cpp:303` | `paged_attention_v2.cpp:372` | | HIP (`uint64_t`) | `paged_attention_hip.h:140` | `paged_attention_hip.h:302` | Each line becomes `T base = ((T)row * page + slot) * (T)stride_kv + kv_head * dim;`, with `row` widened before the multiply. The `k_pool[base + d]` / `v_pool[base + d]` reads use the 64-bit value unchanged. The same arithmetic in `fused_norm.cpp` and `fused_rope_append.cpp` already uses `ulong` (Metal) and `uint64_t` (CUDA). Sparse decode runs on v2 partial, so it gets the same fix. - Merge: `paged_attention_merge_states` throws `std::invalid_argument` (an `Err` in Rust) when `v_in` or the merged output exceeds `UINT32_MAX` elements. `PagedDecodePlan::validate` rejects such plans before launch. The batched decode panics on a launch `Err`, so with this check those batches go to gather instead. - Docs: the `MLXCEL_PAGED_SLAB_BLOCKS` row in `docs/environment-variables.md`. - Tests are in a new `cache/paged_pool_offset_tests.rs`, not `paged_batch_decode_tests.rs`, which is already 599 lines. ## Verification On gfx1151, rebased on `9c0ae2e9` (after #2181). Review follow-up `c3fef346` changes comments and docs only: - `paged_pool_past_u32_elements_matches_gather` (ignored, about 17 GiB): f16 pool of 131,073 blocks, K/V only in the last block, v1 and v2 checked against a host reference. With main's bodies it fails with max error 0.456 for both kernels. With this PR it passes (guarded runs). It also confirms the last block reads back and row 0 (where a 32-bit base lands) is zero. - Source pin test: with only the HIP v2 body reverted it fails, naming that line. - Merge refusal test (lazy broadcasts) and plan-bound test pass. - `cargo test -p mlxcel-core --features rocm --lib -- paged`: 264 passed. Clippy on lib and tests is clean, and the script gates plus `verify-rocm-overlay` pass. - Decode: Llama-3.1-8B-Instruct-4bit, server `--parallel 4 --ctx-size 131072`, 4 clients at ~16K prompt, 128 tokens, fused v2 on both arms (the log shows `fused v2 launch`). A build with main's kernel bodies (this branch with the three kernel files reverted) and this PR were run as a pair inside one `rocm_gpu_guard.sh` window, three times, and all attempts were clean. Per-request decode tok/s was 14.0/13.9/14.0 before and 14.1/14.0/14.0 after. Both medians are 14.0, so there is no regression. `examples/paged_attention_kernel_bench` was not used: it keeps the 32-block default slab, so at batch 4 / 16K it measures gather. - `make verify-rocm` on `c3fef346` (with `MLXCEL_ROCM_SMOKE_MODEL=models/mlx/Qwen3-0.6B-4bit`): script gates, workspace clippy and smoke pass. Tests: 152 suites, 11984 passed, 5 failed, 383 ignored. The 5 failures are the Gemma 3 lookahead tests in `server::batch::scheduler` (from #2182). They fail the same way with every file of this PR reverted to `origin/main` `9c0ae2e9`, pass when run alone, and are filed as #2187. Found while writing the test: on ROCm, strided MLX copies of 2^32 or more elements fail to launch. That is filed as #2184, and the test avoids it. Not verified: Metal and CUDA are not available on this host. Their v1 and v2 partial pool-base lines in the table above changed and were only reviewed, not compiled or run. Closes #2153
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The ROCm overlay cached compiled
fast::hip_kernelmodules under device index plus kernel name, and the name covers onlytemplate_args.build_kernelalso writes each input's and output's dtype and each input's 0-d-ness into the source, so a later call that differed only there reused the first module and returned wrong values (or, for a 0-d input after a 1-d one, launched with an argument list the module does not declare).CustomKernel::eval_gpunow passesname_ + "_" + hex(std::hash<std::string>{}(source_))as the module name, copied from upstream CUDA at the pin (mlx/backend/cuda/custom_kernel.cpparound line 313, upstream ml-explore/mlx#4273). The kernel symbol andget_jit_module's device-index prefix are unchanged, sorocprofv3traces keep theircustom_kernel_*names.What changed
patches-rocm/mlx/backend/rocm/custom_kernel.cpp, recorded as LOCAL_FIXES item 33 (fix(rocm): refuse wave32-only kernel ports on wave64 devices #2179 took 32), kept in mlxcelverse under the fork policy.rocm_jit_key_probe(input, f16_output)(wrappermlxcel_core::rocm_faults::jit_key_probe_array): one kernel name, no template args, an f32 or f16 output ofinp_shape[0](or a -1 sentinel for a 0-d input) followed by the input's values.tests/rocm_custom_kernel_jit_key.rs: f32 then f16 then bf16 inputs, f32 then f16 outputs at one input dtype, and 1-d then 0-d input in a child process astests/rocm_gpu_faults.rsdoes.scripts/ci/check_kernel_dtype_keys.pyanddocs/code-guidelines.md: the CUDA paragraph now says CUDA keys by source hash since upstream 9f5f7931 (present at pin 81ba1c6a), ROCm does too after this change, and the check stays as defense in depth against a re-vendored fork, pin bump or new backend bringing a name-only key back. No launch drops its explicit dtype keys: the keys cost nothing, several are used as type aliases in kernel bodies, and the rest are the insurance the check exists for. The checker's rule, scope and pinned count (9) are unchanged. The same stale CUDA description in the keying comments ofturbo/paged_attention.cppandturbo/paged_attention_v2.cppis corrected (comment-only).Verification
On gfx1151 (ROCm, this host):
cargo test --profile test-fast --features rocm --test rocm_custom_kernel_jit_key -- --test-threads=1: 3 passed. With the name-only key restored in the overlay, all 3 fail: the f16-input call returns[8, -0.0313, 131336, 3.5e13, 8, 0, ...]for input[0.5, -1.25, 3, 8, -0.75, ...], the f16-output call returns[0, 2.5, 0, 1.75, ...], and the 0-d child dies with SIGSEGV after its 1-d call.cargo clippy --features rocm --test rocm_custom_kernel_jit_key -- -D warningsandcargo clippy -p mlxcel-core --features rocm --lib -- -D warnings: clean.make verify-versions verify-kernel-dtype-keys verify-kernel-port-dispatch verify-llama-compat verify-fmt verify-rocm-overlayafter rebasing onto b7116d1: pass (checker negative-case suite included).make verify-rocmon the final head b11b4cc: OK, 152 test suites, 11972 passed, 0 failed, 382 ignored (the new file: 3 passed, 1 ignored child); clippy, smoke, overlay and script gates pass.Not verified: Metal and CUDA are not available on this host. The change touches only the ROCm overlay, a bridge function compiled to a throwing stub off ROCm, a ROCm-only test file (
#![cfg(feature = "rocm")]), and comments and docs; no Metal or CUDA code path changes.Closes #2149