Repository navigation
perf(cuda): raise the MoE grouped-GEMM prefill gate to 22 rows per expert - #3756
Conversation
…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.
…-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
Hardening reviewReviewed against #3755. The gate change is a one-constant default move with the existing Changes pushed
Reproduction on GB10 with #3732 mergedSame release binary,
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
|
Summary
Closes #3755.
On CUDA, a sorted quantized
GatherQMM(MoE prefill) switches to the #629 dequantize + grouped GEMM path once the pair countBreachesMLXCEL_GATHER_QMM_GROUPED_MIN_ROWS * E. That path dequantizes the whole[E, N, K]expert stack on every call. The cost does not shrink withB, 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 defaultmin_rowsof theuse_grouped_gemmgate 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_ROWSstill overrides the default andMLXCEL_GATHER_QMM_GROUPED=0still turns the path off.docs/environment-variables.md: theMLXCEL_GATHER_QMM_GROUPED_MIN_ROWSrow (new default and the reason).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_mmwith sorted indices, which makes the samecutlass_grouped_gemm_unalignedcall). Each case first checks that the two references differ. Cases: 8, 12 and 21 pairs per expert and22 * E - 1pairs (per-pair path); 22, 23 and 64 pairs per expert (grouped); all pairs on 2 of 8 experts at22 * E - 1and22 * E(the gate countsB, not rows per expert);E = 1with 1, 21 and 22 pairs; 3- and 8-bit at22 * E - 1and22 * E. N = 704 (per-pairqmm_naive) and N = 256 (per-pairqmm_sm80).grouped_gemm_numeric_tests.rsand the f32 tolerance rationale insrc/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_referencesays its width "admits CUDA's sorted RHS GEMM", and now it reaches it. In an nsys trace of that test withMLXCEL_GATHER_QMM_GROUPED_MIN_ROWS=8(main's default), the kernels arefp_dequantize,prepare_grouped_mm_dataandGemmGrouped, and there is noqmm_sm80_rhs_kernel. With the new default there are fourqmm_sm80_rhs_kernellaunches 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/completionswith a prompt of exactly L tokens (checked withusage.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=0vsGROUPED_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.
With 128 experts and top 8, L tokens give
L / 16pairs 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_qmmcall (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 anyB(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.(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):Qwen3-30B-A3B 4-bit (
mlx-community/Qwen3-30B-A3B-4bit@ d388dead):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=22in 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.cppand changed nothing else:modline, applied alone on main 9402cf8:cargo test --profile test-fast -p mlxcel-core --features cuda --lib -- --test-threads=1 gather_qmm_grouped_threshold_tests1 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 sixmodels::switch_layers::mxfp_teststests pass in the full run below.make verify-test-cudawith a private 0700TMPDIR: 207 test binaries, 18,895 passed, 2 failed, 578 ignored. The two failures aremlx_test_guard_coverage::mlx_evaluating_lib_tests_take_mlx_test_guard(nemotron_h_nano_omni_tests.rs:276) andstep3p7_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 currentmainby 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, includingcheck_used_by.py,check_used_by_test.sh,check_env_var_docs.pyand the license-header check. The 2 that fail,check_helm_chart.pyandhelm_chart_package_test.sh, needhelm, which is not installed on this host.git merge-tree --write-tree origin/main HEAD: clean (origin/main5bf7b8628). On the merged tree, the Python tooling tests (python3 -m unittest discover -s tests -p 'test_*.py'), thescripts/cichecks CI runs andcargo fmt --all -- --checkpass.Callers of the gate:
use_grouped_gemminGatherQMM::eval_gpuis the only reader ofMLXCEL_GATHER_QMM_GROUPED_MIN_ROWS, and every MoE model reaches it through the sortedgather_qmmof its SwitchGLU (gather_sortfrom 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 - 1and22 * 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_qmvinstead ofqmm_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-treeof the two branches mergesquantized.cppcleanly.docs/environment-variables.mdconflicts 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
MLXCEL_GATHER_QMM_GROUPED_MIN_ROWSremains the knob.N % 128 == 0have 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.