Repository navigation
Description
Summary
In GatherQMM::eval_gpu the #629 grouped-GEMM fast path is tested before the sorted tiled kernel gather_qmm_rhs_sm80. Once a sorted block-float prefill reaches the grouped threshold it never reaches the tiled kernel, which on GB10 is the faster of the two up to about 500 rows per expert.
Background
use_grouped_gemm() is evaluated and taken before can_use_gather_qmm_rhs_sm80 is computed. The grouped path dates from #629 (July); gather_qmm_rhs_sm80 arrived with the MLX pin advance in #2304 (October) and handles non-affine modes (mxfp4, mxfp8) with N % 128 == 0, B >= 16 and B / E >= 4. A probe across several shapes (mxfp4/mxfp8, E 8 and 128) shows the grouped path slower than the tiled kernel until roughly 512 pairs per expert, where they reach parity (ratio 0.94 to 1.01). An nsys trace of models::switch_layers::mxfp_tests::mxfp_sorted_prefill_aligned_width_matches_host_reference shows the pre-emption directly. The N % 128 != 0 case (legacy per-pair qmm_naive) crosses near 32 pairs per expert and is not affected.
Proposed Solution
In use_grouped_gemm, return false when can_use_gather_qmm_rhs_sm80 holds, or give that case its own much higher threshold if a size is found where the grouped path wins. This needs an end-to-end check on a block-float MoE checkpoint with N % 128 == 0 experts; none was measured in the report.
Implementation Notes
src/lib/mlxcelverse/overlays/cuda/mlx/backend/cuda/quantized/quantized.cpp:GatherQMM::eval_gpu, theuse_grouped_gemmlambda and the grouped branch taken from it,can_use_gather_qmm_rhs_sm80and thegather_qmm_rhs_sm80branch that currently sits after it, envMLXCEL_GATHER_QMM_GROUPED_MIN_ROWS.- Related: perf(cuda): MoE prefill TTFT roughly doubles at the grouped-GEMM threshold (128 tokens on 128-expert top-8 models) #3755 (the fixed per-call dequantization cost that sets the grouped threshold too low in the first place).
Acceptance Criteria
- Sorted block-float prefill uses
gather_qmm_rhs_sm80where it supports the shape, at least up to the measured crossover. - No regression on affine modes or on
N % 128 != 0shapes. - An end-to-end block-float MoE check (a checkpoint with
N % 128 == 0experts) confirms the dispatch choice.
Original Suggestion
Title: perf(cuda): the #629 grouped-GEMM gate runs before gather_qmm_rhs_sm80 and takes block-float MoE prefill away from it
Problem
GatherQMM::eval_gpu in the CUDA overlay (src/lib/mlxcelverse/overlays/cuda/mlx/backend/cuda/quantized/quantized.cpp) checks the #629 fast path (dequantize the expert stack + CUTLASS grouped GEMM) before anything else. It checks upstream's sorted tiled kernel gather_qmm_rhs_sm80 only after that. The fast path dates from #629 (July). gather_qmm_rhs_sm80 came with the MLX pin advance in #2304 (October) and takes non-affine modes (mxfp4, mxfp8) with N % 128 == 0, B >= 16 and B / E >= 4. Once a sorted block-float prefill reaches the grouped gate (B >= MLXCEL_GATHER_QMM_GROUPED_MIN_ROWS * E), it never reaches gather_qmm_rhs_sm80. On GB10 the grouped path is the slower of the two up to about 500 rows per expert; at 512 rows the two are on par (0.94-1.01).
Measurement
A probe timed one sorted gather_qmm call (one row per (token, expert) pair, sorted indices, bf16 activations). It ran each call with MLXCEL_GATHER_QMM_GROUPED=0 (legacy dispatch, gather_qmm_rhs_sm80 here) and with MLXCEL_GATHER_QMM_GROUPED_MIN_ROWS=1 (grouped), and took the median of 7 batches. GB10 (sm_121), CUDA 13.0.88, main 9402cf8. Ratio is grouped time over legacy time; two runs:
mxfp8, E = 128, top 8, N = 768, K = 2048 (Qwen3-30B-A3B gate/up shape):
| tokens | pairs per expert | grouped / legacy |
|---|---|---|
| 128 | 8 | 4.11 / 4.39 |
| 512 | 32 | 2.80 / 2.82 |
| 1024 | 64 | 2.29 / 2.05 |
| 2048 | 128 | 1.59 / 1.68 |
| 4096 | 256 | 1.37 / 1.37 |
| 8192 | 512 | 1.10 / 1.09 |
More shapes, same probe (ratio at 8 / 32 / 128 / 512 pairs per expert, two runs each):
| mode | E | N x K | top k | 8 | 32 | 128 | 512 |
|---|---|---|---|---|---|---|---|
| mxfp4 | 128 | 768 x 2048 | 8 | 4.68 / 4.09 | 2.77 / 2.90 | 1.69 / 1.67 | 1.11 / 1.08 |
| mxfp4 | 128 | 2048 x 768 | 8 | 3.80 / 4.03 | 3.56 / 3.21 | 1.86 / 1.87 | 1.00 / 1.00 |
| mxfp8 | 128 | 2048 x 768 | 8 | 3.46 / 3.93 | 3.07 / 3.61 | 1.88 / 1.78 | 1.07 / 1.07 |
| mxfp4 | 128 | 2816 x 704 | 8 | 3.93 / 3.92 | 4.03 / 3.16 | 1.75 / 1.83 | 0.94 / 1.01 |
| mxfp4 | 8 | 4096 x 4096 | 2 | 3.06 / 3.44 | 2.82 / 2.81 | 2.60 / 2.47 | 1.05 / 1.17 |
512 pairs per expert is an 8192-token prompt on the 128-expert shapes and a 2048-token prompt on the 8-expert one. The grouped path catches up only around there.
With mxfp4 N % 128 != 0 (704 x 2816, 2880 x 2880), where the legacy path is per-pair qmm_naive, the same probe crossed at about 32 pairs per expert. That case is not affected by this issue.
An nsys trace of models::switch_layers::mxfp_tests::mxfp_sorted_prefill_aligned_width_matches_host_reference (mxfp4 and mxfp8, N = 384, 128 sorted rows over 8 experts) shows the pre-emption directly. With MLXCEL_GATHER_QMM_GROUPED_MIN_ROWS=8 (main's default) the trace has fp_dequantize + prepare_grouped_mm_data + GemmGrouped and no qmm_sm80_rhs_kernel. With 22 it has four qmm_sm80_rhs_kernel launches and no grouped GEMM.
Expected
Sorted block-float prefill uses gather_qmm_rhs_sm80 when it supports the shape, at least up to the size where the grouped path is measured to be faster.
Possible fix
In use_grouped_gemm, return false when can_use_gather_qmm_rhs_sm80 holds; or give that case its own, much higher threshold if a size is found where grouped wins. This needs an end-to-end check on a block-float MoE checkpoint with N % 128 == 0 experts. None was measured here.
Environment
NVIDIA GB10 (sm_121), driver 580.178.04, CUDA 13.0.88, mlxcel main 9402cf8, release build with --features cuda.
Generated by Claude Code
Activity
[-]perf(cuda): the #629 grouped-GEMM gate runs before gather_qmm_rhs_sm80 and takes block-float MoE prefill away from it[/-][+]perf(cuda): GatherQMM checks the grouped-GEMM gate before gather_qmm_rhs_sm80, losing the tiled kernel for block-float MoE prefill[/+]