Skip to content

fix(rocm): fix and enable the expert-batched gather_qmm prefill kernel - #2112

Merged
inureyes merged 4 commits into
mainfrom
fix/issue-2066-gather-qmm-expert-batched
Oct 5, 2026
Merged

inureyes merged 4 commits into
mainfrom
fix/issue-2066-gather-qmm-expert-batched

Conversation

@inureyes

@inureyes inureyes commented Oct 5, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Root cause of LOCAL_FIXES.md item 9: gather_qmv_expert_batched_kernel read lhs_indices[b] and rhs_indices[b] as flat [B] arrays, but MLX broadcasts the index arrays against the activation's batch shape without copying. Sorted x [T, 1, K] with rhs_indices [T, 1] (the call that found item 9) read both arrays past their end; the bf16-only symptom came from the bf16-only gate. Both arrays are now read through the batch shape and strides (gather_index_loc), as the other gather kernels do; the unlaunched idot variant gets the same fix. SwitchGLU's own sorted path passes flat indices and was never affected.
  • With the fix alone the kernel was 2.3x slower than the per-row wide kernel on granite's prefill, so the inner loop is rewritten: one packed-word load serves four rows, one reduction per row, all 16 lanes work, and block z is the expert (two binary searches instead of a serial walk over earlier runs).
  • f16 shares the kernel (issue step 3: Mixtral spent 99.1% of prefill in the per-row warp-shared f16 kernel). That is the only new instantiation pair; qmm.hip compile time is unchanged (about 38 s).
  • Default: on, per the issue's rule. MLX_ROCM_GATHER_QMV_EXPERT_BATCHED=0 turns it off.
  • mxfp4 (gpt-oss, 99.8% of prefill in the non-affine gather_qmv_kernel) is filed as perf(rocm): add a batched gather_qmm path for mxfp4 MoE prefill #2106.
  • LOCAL_FIXES.md item 9 is rewritten and marked an upstreaming candidate for chore(rocm): mlxcelverse ROCm fork sync script, MLX pin-bump procedure, and upstreaming local fixes #1813 (the fork runs this kernel by default with the flat reads); docs/mlxcelverse/upstream/README.md updated. Profile, measurements and decision: docs/benchmark_results/rocm-moe-prefill-gfx1151-2026-10-05.md.

Results (gfx1151, scripts/rocm_gpu_guard.sh --idle-secs 45, medians of three, kernel off/on alternated on one binary)

Model Prefill tok/s before after Decode tok/s before / after
granite-4.0-h-tiny-4bit (bf16, 64 experts) 535.7 918.6 (1.71x) 88.76 / 88.65
Mixtral-8x7B-Instruct-v0.1-4bit (f16, 8 experts) 25.95 125.6 (4.84x) 9.40 / 10.43 (noise; decode never reaches the kernel)

Short prompts at the gate's edges (granite T 44 to 160, Mixtral T 32 and 64) are also faster with the kernel on; table in the benchmark page.

Verification

All on gfx1151 (Radeon 8060S, HIP 7.15):

  • cargo test --release --features rocm --test rocm_gather_qmm_expert_batched -- --test-threads=1: 48 cases, each holding the expert-batched error to the dtype rounding bound and to 1.25x the unsorted path's (bf16/f16, 4/8 bits, Mixtral-like and granite gate/down shapes, flat, shared and two broadcast index layouts) pass; the expert-batched error is within 0.1% of the unsorted path's against a dequantized f32 reference. It failed on the old kernel (relative error 1.415, BroadcastRows), and fails again with the strided reads reverted in the new kernel.
  • Logit traces with the kernel on, compare_logit_traces.py --decided 2.0 against the test(rocm): correctness matrix against a Metal baseline and a verify-test-rocm gate #1809 Metal references: qwen3-30b-a3b, mixtral-8x7b-instruct and granite-4.0-h-tiny at w8 and w256 all have 0 decided-position disagreements.
  • make verify-rocm (with MLXCEL_ROCM_SMOKE_MODEL=models/mlx/Qwen3-0.6B-4bit): OK, 11870 passed, 0 failed, 378 ignored. cargo clippy --release --features rocm --test rocm_gather_qmm_expert_batched -- -D warnings, make verify-rocm-overlay and cargo test --test dead_doc_pointers pass.

Not verified: Metal and CUDA (not available on this host). The change is confined to patches-rocm/, which only ROCm builds copy, plus a doc comment in src/models/switch_layers_mxfp_tests.rs; Metal and CUDA code paths are untouched.

Changes during review

Implementation and security reviews found no CRITICAL or HIGH issues. Follow-ups: the test also bounds the expert-batched error absolutely, doc wording no longer claims every gather kernel reads strided indices, and the short-prompt sweep was added. Known LOW, unchanged: rows whose rhs index is >= E are left unwritten (the wide kernel writes 0); MLX leaves out-of-range gather indices undefined.

Closes #2066

@inureyes inureyes added status:review Under review type:performance Performance improvements priority:medium Medium priority area:core mlxcel-core: MLX FFI, primitives, KV cache, layers platform:linux Linux (CUDA / packaging) specific status:done Completed and removed status:review Under review labels Oct 5, 2026
The overlay's expert-batched gather qmv read lhs_indices[b] and rhs_indices[b] as flat [B] arrays, but MLX broadcasts the index arrays against the activation's batch shape without copying. A sorted call with x [T, 1, K] and rhs_indices [T, 1] read both arrays past their end (relative error 1.4 against the unsorted path), which is why LOCAL_FIXES item 9 made the kernel opt-in. The kernel now reads both arrays through the batch shape and strides, as the other gather kernels do.

With the fix alone the kernel was 2.3x slower than the per-row wide kernel on granite's prefill, so its inner loop is rewritten: one packed-word load serves four rows, lanes reduce once per row, all 16 lanes work, and block z is the expert found by two binary searches. f16 now shares the kernel (Mixtral spent 99% of prefill in the per-row warp-shared kernel).

On gfx1151, guarded medians of three: granite-4.0-h-tiny prefill 535.7 to 918.6 tok/s, Mixtral-8x7B 25.95 to 125.6 tok/s, decode unchanged. Every case of the new test matches the unsorted path within its own error, and the #1809 MoE traces keep 0 decided mismatches, so the kernel is on by default per the issue's rule (MLX_ROCM_GATHER_QMV_EXPERT_BATCHED=0 turns it off). mxfp4 (gpt-oss) is #2106.

Refs #2066
Review follow-up for #2066: the test now also holds the expert-batched error to the dtype rounding bound and quotes the measured errors (2.3e-3 bf16, 2.8e-4 f16). The kernel comment, LOCAL_FIXES item 9 and the benchmark page no longer say every gather kernel reads strided indices (the unlaunched prefill kernel and the opt-in grouped WMMA path do not), item 9 states the measured 0.1% match, and the page lists short prompts and other MoE families as not measured.
Guarded short-prompt runs (granite T 44 to 160, Mixtral T 32 and 64) show the kernel faster at both gate limits (B >= 64, B / E >= 4), so the default holds below 512 tokens too.

Refs #2066
@inureyes
inureyes force-pushed the fix/issue-2066-gather-qmm-expert-batched branch from ab77f04 to c587178 Compare October 5, 2026 12:09
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:medium Medium priority status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(rocm): fix expert-batched gather_qmm for bf16 MoE prefill

1 participant