Skip to content

perf(cuda): raise the MoE grouped-GEMM prefill gate to 22 rows per expert - #3756

Merged
inureyes merged 4 commits into
lablup:mainfrom
Jaeyeong-CHOI:perf/cuda-moe-prefill-cliff
Dec 4, 2026
Merged

inureyes merged 4 commits into
lablup:mainfrom
Jaeyeong-CHOI:perf/cuda-moe-prefill-cliff

Conversation

@Jaeyeong-CHOI

@Jaeyeong-CHOI Jaeyeong-CHOI commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Closes #3755.

On CUDA, a sorted quantized GatherQMM (MoE prefill) switches to the #629 dequantize + grouped GEMM path once the pair count B reaches MLXCEL_GATHER_QMM_GROUPED_MIN_ROWS * E. That path dequantizes the whole [E, N, K] expert stack on every call. The cost does not shrink with B, and at the old default of 8 pairs per expert the path is slower than the per-pair kernels it replaces. On a 128-expert top-8 model the threshold falls on a 128-token prompt, which took 2.1x (Gemma 4 26B-A4B 4-bit) to 2.6x (Qwen3-30B-A3B 3-bit) as long to prefill as a 127-token one.

The default of 8 came with the sorted grouped GEMM path in #726, chosen on GB10 as near the measured break-even at the time. With the current kernels and the same binary, the two paths now cross at 21-23 pairs per expert over a whole prefill on GB10.

This raises the default to 22 pairs per expert. That is where the two paths cross over a whole prefill on GB10 (21-23 on the three checkpoints below). Prompts that already took the grouped path at 22 or more pairs per expert are unchanged.

What changed

  • src/lib/mlxcelverse/overlays/cuda/mlx/backend/cuda/quantized/quantized.cpp, GatherQMM::eval_gpu: the default min_rows of the use_grouped_gemm gate goes from 8 to 22. A comment states why: the dequantization is a fixed cost per call, and gives the measured crossover. The existing comment stays; its "default 8" now reads "default 22". MLXCEL_GATHER_QMM_GROUPED_MIN_ROWS still overrides the default and MLXCEL_GATHER_QMM_GROUPED=0 still turns the path off.
  • docs/environment-variables.md: the MLXCEL_GATHER_QMM_GROUPED_MIN_ROWS row (new default and the reason).
  • New src/lib/mlxcel-core/src/gather_qmm_grouped_threshold_tests.rs (CUDA only). It identifies the path a sorted gather took from its bits. Below the threshold, a sorted gather must equal the same gather unsorted: right_sorted_ only selects the grouped path, so the unsorted call runs the same per-pair kernel over the same rows. From the threshold, it must equal the float grouped GEMM over the dequantized stack (gather_mm with sorted indices, which makes the same cutlass_grouped_gemm_unaligned call). Each case first checks that the two references differ. Cases: 8, 12 and 21 pairs per expert and 22 * E - 1 pairs (per-pair path); 22, 23 and 64 pairs per expert (grouped); all pairs on 2 of 8 experts at 22 * E - 1 and 22 * E (the gate counts B, not rows per expert); E = 1 with 1, 21 and 22 pairs; 3- and 8-bit at 22 * E - 1 and 22 * E. N = 704 (per-pair qmm_naive) and N = 256 (per-pair qmm_sm80).
  • Comments that quoted the old gate: grouped_gemm_numeric_tests.rs and the f32 tolerance rationale in src/models/switch_layers_mxfp_tests.rs. Under the old gate, that file's sorted-prefill case (16 pairs per expert) took the grouped path. It no longer does; the 2e-3 f32 bound is kept for a batch that crosses the gate. One side effect: mxfp_sorted_prefill_aligned_width_matches_host_reference says its width "admits CUDA's sorted RHS GEMM", and now it reaches it. In an nsys trace of that test with MLXCEL_GATHER_QMM_GROUPED_MIN_ROWS=8 (main's default), the kernels are fp_dequantize, prepare_grouped_mm_data and GemmGrouped, and there is no qmm_sm80_rhs_kernel. With the new default there are four qmm_sm80_rhs_kernel launches and no grouped GEMM.

Measurements

GB10 (sm_121), driver 580.178.04, CUDA 13.0.88. Server: mlxcel-server -m <model> --no-prompt-cache. Requests: raw /v1/completions with a prompt of exactly L tokens (checked with usage.prompt_tokens), max_tokens: 1, temperature: 0. Each value is the median of 5 requests after one discarded request.

Where the paths cross (main, same binary, GROUPED=0 vs GROUPED_MIN_ROWS=1)

Ratio of whole-request time with the grouped path forced to time with the per-pair path forced. Median of two server instances per arm.

pairs per expert 16 18 20 22 24 26 28
Gemma 4 26B-A4B 4-bit 1.23 1.12 1.02 0.95 0.88 0.83 0.77
Qwen3-30B-A3B 3-bit 1.30 1.18 1.02 0.99 0.91 0.85 0.81
Qwen3-30B-A3B 4-bit 1.35 1.21 1.11 1.02 0.95 0.88 0.82

With 128 experts and top 8, L tokens give L / 16 pairs per expert (16 is 256 tokens, 28 is 448). A gate at 20 would leave the Qwen3 4-bit prefill 11% slower on the grouped path at 20 pairs per expert. At 22 the largest loss left is Gemma just below 22 pairs per expert, where the per-pair path is about 5% slower than the grouped one.

A single gather_qmm call (a probe that times one sorted call per path, median of 7 batches, two runs) crosses at 19-28 pairs per expert for the affine Gemma 4 and Qwen3 expert shapes at 3, 4 and 8 bits and for an E = 8, 4096 x 4096 stack, and at about 32 for mxfp4 (2880 x 2880 and 704 x 2816). The grouped call costs about the same at any B (Gemma 4 gate/up, 4-bit: 8.0-8.6 ms from 64 to 2048 tokens, 4.9 ms of it the stack dequantization). At 8 pairs per expert it was 2.0-4.4x slower than the per-pair call on every shape measured.

Before and after (whole request, ms)

Arms are main, this branch and main again, each in a fresh server, rotated over two rounds. "main" is the median of the four main values. "null" is the gap between the two main arms' medians. The first generated token was the same in every arm at every length.

Gemma 4 26B-A4B 4-bit (mlx-community/gemma-4-26b-a4b-it-4bit @ 0d77464e). The first main arm ran while the machine was compiling, which widens the null spread here.

prompt tokens pairs per expert main this branch change null
96 6 251.2 261.6 +4.2% 1.4%
127 7.9 319.7 319.2 -0.2% 0.9%
128 8 701.6 320.2 -54.4% 4.7%
160 10 723.4 394.1 -45.5% 3.8%
192 12 731.5 467.6 -36.1% 6.3%
256 16 760.8 607.6 -20.1% 6.1%
320 20 786.4 757.7 -3.7% 4.3%
352 22 790.2 794.5 +0.5% 4.8%
512 32 821.2 828.0 +0.8% 4.4%
1023 64 1044.9 1052.1 +0.7% 2.8%
4096 256 3375.3 3385.4 +0.3% 2.5%

(96 tokens: one of the two branch values was 271 ms, the other 252; that length does not reach the gate on either build.)

Qwen3-30B-A3B 3-bit (mlx-community/Qwen3-30B-A3B-3bit @ 6add5f3b):

prompt tokens pairs per expert main this branch change null
127 7.9 425.7 425.8 +0.0% 0.0%
128 8 1020.1 428.8 -58.0% 0.6%
160 10 1047.9 530.6 -49.4% 1.0%
192 12 1054.6 629.5 -40.3% 1.0%
256 16 1071.1 830.4 -22.5% 0.8%
318 19.9 1093.4 1024.7 -6.3% 0.4%
352 22 1104.6 1118.7 +1.3% 0.2%
512 32 1155.2 1165.5 +0.9% 0.2%
1024 64 1297.2 1307.5 +0.8% 0.6%
4096 256 3060.3 3080.1 +0.6% 0.0%

Qwen3-30B-A3B 4-bit (mlx-community/Qwen3-30B-A3B-4bit @ d388dead):

prompt tokens pairs per expert main this branch change null
127 7.9 311.7 310.3 -0.4% 0.0%
128 8 757.2 313.5 -58.6% 0.3%
160 10 783.7 389.7 -50.3% 0.5%
192 12 794.5 460.9 -42.0% 0.5%
256 16 816.5 608.8 -25.4% 1.2%
318 19.9 824.2 752.6 -8.7% 1.6%
352 22 842.5 841.5 -0.1% 0.3%
512 32 894.0 895.5 +0.2% 1.8%
1024 64 1015.9 1016.6 +0.1% 1.4%
4096 256 2397.8 2401.7 +0.2% 0.8%

From 22 pairs per expert on, both builds take the same kernels.

On Qwen3 3-bit this branch measured 0.5-1.3% above main from 352 tokens on, where both builds run the same kernels. A second run used the main binary with MLXCEL_GATHER_QMM_GROUPED_MIN_ROWS=22 in two arms against this branch, so all three arms had the same dispatch. This branch was -0.3% to +0.5% from 352 to 2048 tokens. The two main arms alone differed by up to 2% per instance at 352 tokens (1097-1122 ms). At 128 and 255 tokens the env setting on main gave the same times as this branch (128: 428-429 ms for main with the setting, 427.9 and 437.5 ms for this branch; 255: 829 vs 826 ms, one branch value at 862).

Verification

On GB10, on the tree of this commit (checked before and after the run, no local changes), except that a later amend reworded one source comment in quantized.cpp and changed nothing else:

  • Fail before / pass after. The new test file plus its mod line, applied alone on main 9402cf8:
    cargo test --profile test-fast -p mlxcel-core --features cuda --lib -- --test-threads=1 gather_qmm_grouped_threshold_tests
    1 passed, 3 failed, e.g. assertion left == right failed: N=704 K=2816 E=8 B=64 (8 rows per expert) left: Grouped right: PerPair, E=8 B=175 on two experts, E=1 B=21. The test that passes on main is the one for the grouped side, which main also takes. With this change: 4 passed.
  • cargo test --workspace --profile test-fast --features cuda --lib -- --test-threads=1 gather_qmm_grouped_threshold_tests grouped_gemm_numeric_tests qmm_fold_batch_tests fused_moe_parity qmm_naive_tile: 21 passed, 0 failed. The six models::switch_layers::mxfp_tests tests pass in the full run below.
  • make verify-test-cuda with a private 0700 TMPDIR: 207 test binaries, 18,895 passed, 2 failed, 578 ignored. The two failures are mlx_test_guard_coverage::mlx_evaluating_lib_tests_take_mlx_test_guard (nemotron_h_nano_omni_tests.rs:276) and step3p7_parity::text_decode_delegates_sequence_local_native_batching (assertion failed: !wrapper.supports_padded_prefill()). Both are in files this change does not touch, fail with the same messages on main 9402cf8 (the base), and are fixed on current main by feat(speculative): add DeepSeek-V4.1 bundled DSpark drafter (#3262) #3686 and test: restore MLX guard scan and no-GPU catalog test gates #3765.
  • cargo fmt --all -- --check: clean. cargo clippy --locked --workspace --all-targets --features cuda -- -D warnings: clean.
  • scripts/ci: 35 of 37 checks pass, including check_used_by.py, check_used_by_test.sh, check_env_var_docs.py and the license-header check. The 2 that fail, check_helm_chart.py and helm_chart_package_test.sh, need helm, which is not installed on this host.
  • git merge-tree --write-tree origin/main HEAD: clean (origin/main 5bf7b8628). On the merged tree, the Python tooling tests (python3 -m unittest discover -s tests -p 'test_*.py'), the scripts/ci checks CI runs and cargo fmt --all -- --check pass.

Callers of the gate: use_grouped_gemm in GatherQMM::eval_gpu is the only reader of MLXCEL_GATHER_QMM_GROUPED_MIN_ROWS, and every MoE model reaches it through the sorted gather_qmm of its SwitchGLU (gather_sort from 64 pairs). Metal and ROCm have their own dispatch and are not affected. Boundary and extreme cases are in the new tests: 1 pair, E = 1, 22 * E - 1 and 22 * E, all pairs on two experts. The gate is O(1) per call.

Interaction with #3732

#3732 (open) runs one-row sorted gathers below this gate on gather_qmv instead of qmm_naive; it reports 8-127-token prefills 5-10% faster on Gemma 4. It changes a different branch of the same function, below the gate. git merge-tree of the two branches merges quantized.cpp cleanly. docs/environment-variables.md conflicts only because #3732 adds its row directly below the row this change edits; keeping both rows resolves it. A faster per-pair path would move the crossover up; the two changes were not measured together.

Limits

  • Measured on GB10 only. The crossover depends on how fast the per-pair kernels and the dequantization run on a device. On a part with much more memory bandwidth per SM it may sit elsewhere, and MLXCEL_GATHER_QMM_GROUPED_MIN_ROWS remains the knob.
  • End-to-end numbers are from three 128-expert top-8 checkpoints. No E = 8 or E = 32 checkpoint (Mixtral, gpt-oss) was run. For those shapes there is only the single-call probe, which crossed at about 19 (E = 8, affine 4-bit) and 32 (mxfp4).
  • The gate is one number for every mode, bit width and shape. The single-call crossovers spread from 19 to 32 pairs per expert. Shapes whose single-call crossover is above 22 are still a little slower on the grouped path between 22 and their crossover, as they were on main. Shapes whose crossover is below 22 (Gemma 4 gate/up at 4 bits and the E = 8 affine stack, both about 19) are now a little slower than main between their crossover and 21 pairs per expert: up to about 10% per call, and at most about 5% of a whole Gemma 4 request in that prompt range.
  • Block-float stacks with N % 128 == 0 have a third kernel, gather_qmm_rhs_sm80, which the grouped gate pre-empts. In the probe it was faster than the grouped path up to about 512 pairs per expert on five mxfp4/mxfp8 shapes. That is a separate issue and not changed here, beyond the gate moving from 8 to 22.
  • Batched decode reaches this gate only at very high concurrency. On a 128-expert top-8 model, main sent a decode step to the grouped path from 128 concurrent sequences on; with this change that takes 352. Steps in between now stay on the per-pair kernels, which the probe measured as faster at those widths. On an E = 8 top-2 model (Mixtral) the affected range is 32-87 concurrent sequences. No serving benchmark at that concurrency was run.

…pert

The sorted GatherQMM fast path from lablup#629 dequantizes the whole expert
stack on every call, a cost that does not shrink with the pair count.
At the old default of 8 pairs per expert it was slower than the per-pair
path it replaced: on a 128-expert top-8 model a 128-token prompt took
2.1-2.6x as long to prefill as a 127-token one. On GB10 the two paths
cross at 21-23 pairs per expert over a whole prefill, so start at 22.

MLXCEL_GATHER_QMM_GROUPED_MIN_ROWS still overrides the default and
MLXCEL_GATHER_QMM_GROUPED=0 still disables the path.
@inureyes inureyes added status:review Under review type:performance Performance improvements priority:high High priority area:core mlxcel-core: MLX FFI, primitives, KV cache, layers labels Dec 4, 2026
…-cliff

# Conflicts:
#	docs/environment-variables.md
After merging main (lablup#3732), a one-row sorted gather below the grouped gate runs gather_qmv instead of qmm_naive, so the threshold test's per-pair comment now names gather_qmv. The lablup#3732 one-row tests described the old 8 * E grouped floor; they stay below the new 22 * E floor too, and the comment says so.

Also drop the unused randn helper in gather_qmm_one_row_tests.rs, which failed cargo clippy --all-targets --features cuda -D warnings on main.

Refs lablup#3755, lablup#3756
@inureyes

inureyes commented Dec 4, 2026

Copy link
Copy Markdown
Member

Hardening review

Reviewed against #3755. The gate change is a one-constant default move with the existing MLXCEL_GATHER_QMM_GROUPED_MIN_ROWS override and MLXCEL_GATHER_QMM_GROUPED=0 kill switch still in place, and the env docs row is updated. A single number for every shape is a simplification, but the PR measures the spread (19-32 pairs per expert single-call, 21-23 whole prefill across three checkpoints) and keeps the override, so it is not a coherence problem. Numerics: below the gate the sorted gather is bit-identical to the unsorted per-pair path, and from the gate it matches the float grouped GEMM; the new threshold tests pin both sides.

Changes pushed

Reproduction on GB10 with #3732 merged

Same release binary, MLXCEL_GATHER_QMM_GROUPED_MIN_ROWS=8 (main's dispatch) vs the new default, three fresh-server arms rotated over two rounds, median of 5 after one discarded request, ms:

tokens Gemma 4 26B-A4B 4-bit old / new Qwen3-30B-A3B 4-bit old / new
127 314.9 / 314.9 356.6 / 363.3
128 732.1 / 315.5 (-56.9%) 774.9 / 368.0 (-52.5%)
192 756.4 / 461.9 (-38.9%) 811.4 / 536.8 (-33.9%)
256 777.5 / 594.7 (-23.5%) 834.7 / 701.4 (-16.0%)
320 802.3 / 741.4 (-7.6%) 841.1 / 867.5 (+3.1%, null 2.1%)
352+ within 2% (same kernels) within 2.3% (same kernels)

The first generated token was identical in every arm at every length. #3732's faster per-pair path did not move the crossover enough to change the choice of 22.

Validation

  • cargo test --profile test-fast -p mlxcel-core --features cuda --lib for gather_qmm_grouped_threshold_tests, gather_qmm_one_row_tests, grouped_gemm_numeric_tests, qmm_fold_batch_tests: all pass on the merged tree (under gpu-lock).
  • cargo clippy --locked --workspace --all-targets -- -D warnings (default and --features cuda), cargo fmt --all -- --check, scripts/ci/check_env_var_docs.py: clean.

@inureyes
inureyes merged commit f84b7f2 into lablup:main Dec 4, 2026
36 checks passed
@inureyes inureyes added status:done Completed and removed status:review Under review labels Dec 4, 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 priority:high High priority status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(cuda): MoE prefill TTFT roughly doubles at the grouped-GEMM threshold (128 tokens on 128-expert top-8 models)

2 participants