Repository navigation
fix(rocm): fix and enable the expert-batched gather_qmm prefill kernel - #2112
Merged
Merged
Conversation
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
force-pushed
the
fix/issue-2066-gather-qmm-expert-batched
branch
from
October 5, 2026 12:09
ab77f04 to
c587178
Compare
This was referenced Oct 5, 2026
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
LOCAL_FIXES.mditem 9:gather_qmv_expert_batched_kernelreadlhs_indices[b]andrhs_indices[b]as flat[B]arrays, but MLX broadcasts the index arrays against the activation's batch shape without copying. Sortedx[T, 1, K]withrhs_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.qmm.hipcompile time is unchanged (about 38 s).MLX_ROCM_GATHER_QMV_EXPERT_BATCHED=0turns it off.gather_qmv_kernel) is filed as perf(rocm): add a batched gather_qmm path for mxfp4 MoE prefill #2106.LOCAL_FIXES.mditem 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.mdupdated. 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)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.compare_logit_traces.py --decided 2.0against 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 atw8andw256all have 0 decided-position disagreements.make verify-rocm(withMLXCEL_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-overlayandcargo test --test dead_doc_pointerspass.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 insrc/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