Skip to content

update(rocm): port ssm_update_kernel to HIP and read the port table - #2099

Merged
inureyes merged 7 commits into
mainfrom
update/issue-2067-rocm-ssm-update-port
Oct 4, 2026
Merged

inureyes merged 7 commits into
mainfrom
update/issue-2067-rocm-ssm-update-port

Conversation

@inureyes

@inureyes inureyes commented Oct 4, 2026 •

Copy link
Copy Markdown
Member

Summary

On ROCm every single-token Mamba2 decode step of the hybrid SSM models ran the ~55-op SSD graph: ssm_ports() had no .rocm entry and ssm_kernel_available() answered with cu::is_available() off Apple, so a filled slot would have stayed unreachable. This fills the slot and makes the predicate read the port table, so the existing seq_len == 1 && ssm_kernel_available() gates in granitemoehybrid, falcon_h1, plamo2 and nemotron_h reach the HIP kernel with no model change. On Nemotron-H that gate selects fused_mamba2_forward, the whole single-token mixer in C++.

  • SSM_HIP_SOURCE / get_ssm_kernel_hip(): the CUDA body with __shfl_down(acc, o, 32), same inputs, outputs, grid and template arguments, plus the wave32 #error guard the perf(rocm): allocator footprint and ROCm ports of mlxcel fused kernels #1814 ports share. That guard is inert with ROCm 10's AMD clang 23 (neither macro is defined for gfx1151, gfx942 or gfx90a); the fold does not depend on it, since the width-32 shuffle stays inside one row on wave64 too.
  • ssm_kernel_available() returns has_kernel_port(ssm_ports()) on every platform, and false when the default device is the CPU (as mamba1_scan_kernel_accepts does). MLXCEL_SSM_KERNEL=0 forces the graph on every backend; MLXCEL_SSM_CUDA_KERNEL=0 stays as an alias, and so now also affects Metal and ROCm.
  • The launch names the A_log, B and C dtypes in its template arguments on every backend: Nemotron-H stores A_log in f32 beside bf16 activations, and CUDA and HIP key the JIT module on name plus template arguments only. The kernels never read the new names. A ROCm-only branch was rejected by verify-kernel-port-dispatch.
  • ssm_update_kernel refuses on the host, as an error, shapes the kernels cannot index (state width not a multiple of 32, heads not divisible by groups, more than one token, mismatched batch or element counts). This was shared by every port; the HIP port made it reachable on ROCm.
  • ssm_update_parity_tests: kernel against an f32 MLX-op reference of the one-token SSD step at the granite-4.0-h-tiny and Nemotron-H shapes plus a padded-rows shape, f32 and bf16 (tolerances from the issue); the A_log dtype key; the shape refusals; both kill switches. It fails with the lane fold starting at 8 (normalized RMS 0.65 to 0.89) or without the dtype keys (0.80).

Results on gfx1151

Decode tok/s, medians of three bench_decode.sh runs per arm at 844bd94c, same binary, MLXCEL_SSM_KERNEL=0 as "before":

Model Before After
granite-4.0-h-tiny-4bit 60.39 88.43 1.46x
NVIDIA-Nemotron-3-Nano-30B-A3B-4bit 51.35 74.37 1.45x

Spread under 1% (granite) and 3% (Nemotron-H) per arm. Every run went through rocm_gpu_guard.sh: the first (granite before, r1) through the copy on main, the rest through the copy fixed in #2098 (c3eab7b2, run from the #2065 worktree, not committed here).

Logits, w1ctx512 (every traced step has a 512-token SSM state): ROCm kernel against Metal's kernel (metal_m5_3c9edea0), 0 / 60 (granite) and 0 / 71 (Nemotron-H) decided-position mismatches at --decided 2.0; against the ROCm graph, 0 in every pairing. Granite w1 against rocm_gfx1151_c5fe9a16: 0 / 116 decided, every position agrees (a w1 step has no state, so it runs the graph either way). Nemotron-H w1: 0 / 128 top-1 disagreements. Greedy 128 tokens with and without MLXCEL_SSM_KERNEL=0 are coherent but not byte-identical; the traces put every flip at an undecided position. Details: docs/benchmark_results/rocm-ssm-update-kernel-gfx1151-2026-10-04.md, traces in benchmarks/logit_traces/rocm_gfx1151_96cbce84/.

Verification

On gfx1151 (Radeon 8060S, ROCm 10.0.0):

  • make verify-rocm at 38071038: OK; 11856 passed, 0 failed, 378 ignored across 146 test binaries; ROCm smoke OK.
  • cargo test --release --features rocm -p mlxcel-core --lib ssm_update_parity_tests -- --test-threads=1 at 2f17f286 (test and doc changes only since the gate): 5 passed. cargo clippy -p mlxcel-core --lib --tests --features rocm -- -D warnings clean; make verify-fmt verify-kernel-dtype-keys verify-kernel-port-dispatch pass with EXPECTED_IN_SCOPE and its count unchanged.
  • Greedy output for both models identical before and after the CPU-device and shape checks.

Not verified: Metal and CUDA (not available on this host). They are touched by the predicate (has_kernel_port, true on both as before; plus the CPU-device and kill-switch rules), the extra TA/TB/TC template arguments (module names only), and the host shape checks. The parity test is written to run on both. MLXCEL_DEVICE=cpu with a hybrid SSM model was not run (no test covers it).

Closes #2067

On ROCm every single-token Mamba2 decode step of the hybrid SSM models (granite-4.0-h, falcon-h1, plamo-2, Nemotron-H) ran the ~55-op SSD graph, because ssm_ports() had no .rocm entry and ssm_kernel_available() answered with cu::is_available() off Apple, so a filled slot would have stayed unreachable.

- ssm_ports().rocm: SSM_HIP_SOURCE, the CUDA body with __shfl_down(acc, o, 32) and the wave32 preprocessor guard, launched through fast::hip_kernel with the same inputs, outputs, grid and template arguments.
- ssm_kernel_available() returns has_kernel_port(ssm_ports()) on every platform. MLXCEL_SSM_KERNEL=0 forces the graph path on every backend; MLXCEL_SSM_CUDA_KERNEL=0 stays as an alias.
- On ROCm the launch also keys the hipRTC module on the A_log, B and C dtypes. Nemotron-H stores A_log in f32 next to bf16 activations, granite in bf16; Metal and CUDA kernel names are unchanged.
- ssm_update_parity_tests compares the kernel with an f32 MLX-op reference of the single-token SSD step at the granite-4.0-h-tiny and Nemotron-H shapes, f32 and bf16, plus the A_log dtype key and both kill switches. Each test was checked to fail with the fold starting at 8 or the dtype keys removed.

Refs #2067
The first commit appended the three dtype keys only when gpu_kernel_backend() was ROCm, which make verify-kernel-port-dispatch rejects as port selection by backend kind. Name them on every backend instead. The kernels never reference TA, TB or TC, so the arithmetic and the launch are unchanged; only the JIT module names differ. CUDA had the same exposure as HIP (its cache key is the name plus the template arguments), so a process that ran a bf16-A_log model and then an f32-A_log one at the same shapes could reuse the wrong module there too.

Also adds the ROCm w1ctx512 and w1 logit traces for granite-4.0-h-tiny and Nemotron-H with the HIP kernel, plus MLXCEL_SSM_KERNEL=0 graph traces from the same binary.

Refs #2067
- docs/benchmark_results/rocm-ssm-update-kernel-gfx1151-2026-10-04.md: decode before and after on gfx1151 (granite-4.0-h-tiny-4bit 60.39 -> 88.43 tok/s, Nemotron-3-Nano-30B-A3B-4bit 51.35 -> 74.37 tok/s, medians of three guarded runs), the parity test, the w1ctx512 kernel-against-kernel traces against Metal and against the ROCm graph, and the greedy comparison.
- environment-variables.md: MLXCEL_SSM_KERNEL as the cross-backend switch, MLXCEL_SSM_CUDA_KERNEL as its alias.
- installation.md ROCm status table and the open item in the 2026-09-30 correctness report point at the new results.
- benchmarks/: the raw bench_decode rows for both arms.

Refs #2067
@inureyes inureyes added type:performance Performance improvements priority:low Low priority area:core mlxcel-core: MLX FFI, primitives, KV cache, layers platform:linux Linux (CUDA / packaging) specific status:review Under review labels Oct 4, 2026
…otes

- ssm_kernel_available() answers false when the default device is not the GPU, as mamba1_scan_kernel_accepts already does. With MLXCEL_DEVICE=cpu on a ROCm build the gate used to fall to the graph only because the predicate was false there; with the port it would have launched a custom kernel on the CPU stream and thrown.
- The HIP wave32 guard comments now say what holds: ROCm 10's AMD clang 23 defines neither __AMDGCN_WAVEFRONT_SIZE spelling for gfx1151, gfx942 or gfx90a, so the guard is inert there, and the width-32 shuffle keeps the fold inside one row on wave64 anyway.
- ssm_update_parity_tests: granite runs with its real time_step_limit (0.001, 100.0), the dt-window and reference comments are corrected, a head_dim-60 shape covers the padded rows of the last threadgroup, and the tests skip when the default device is the CPU.
- Results doc: a reproduce loop that keeps every run's rows, a note that Nemotron-H's gate selects the whole fused mixer, and the CPU-device rule; installation.md says falcon-h1 and plamo-2 were not run on ROCm.

Validated with ssm_update_parity_tests on gfx1151 (4 passed).

Refs #2067
ssm_update_kernel passes its shapes to the Metal, CUDA and HIP kernels as template constants and indexes raw buffers with them. A state width that is not a multiple of 32 left state columns unwritten (and with one under 32, the whole state came from uninitialized memory), heads that do not divide into groups read past B and C, and a mismatched state, A_log, D or dt read out of bounds. The graph path rejects such configs inside reshape or repeat; the kernel path did not. The host now checks rank, a single token, matching batch, the group divisibility, the state width and every input's element count, and throws std::invalid_argument, which the bridge returns to Rust as an error. This was shared by every port; the HIP port made it reachable on ROCm.

ssm_update_kernel_refuses_unsupported_shapes covers a 48-wide state and 4 heads in 3 groups. The env mutations in the kill-switch test now carry a SAFETY note.

Validated on gfx1151: ssm_update_parity_tests 5 passed; greedy 128-token output for granite-4.0-h-tiny-4bit and Nemotron-3-Nano-30B-A3B-4bit identical to the run before this change.

Refs #2067
ssm_update_kernel_refuses_unsupported_shapes now also checks a 16-wide state (under 32, where the kernel wrote no state at all) and a D one element longer than the head count, beside the 48-wide state and the 4-heads-in-3-groups cases.

The results doc names the commit its rows were measured at and what changed after it, and lists the padded-rows shape, the refusal test, the kill-switch test and the untested CPU-device rule. environment-variables.md says the predicate is also false when the default device is the CPU.

Validated on gfx1151: ssm_update_parity_tests 5 passed; clippy -p mlxcel-core --lib --tests --features rocm clean.

Refs #2067
@inureyes inureyes added status:done Completed and removed status:review Under review labels Oct 4, 2026
@inureyes
inureyes merged commit 57d8ed2 into main Oct 4, 2026
25 checks passed
@inureyes
inureyes deleted the update/issue-2067-rocm-ssm-update-port branch October 4, 2026 13:32
inureyes added a commit that referenced this pull request Oct 4, 2026
Rebased onto #2099, which ported ssm_update_kernel to HIP; the README sentence this branch rewrote named only BitLinear and the decode-MoE pair.

Refs #2065
@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:linux Linux (CUDA / packaging) specific priority:low Low priority status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(rocm): port ssm_update_kernel to HIP and fix ssm_kernel_available

1 participant