Skip to content

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

Done

Description

@Jaeyeong-CHOI

Summary

A sorted quantized GatherQMM switches to the #629 grouped-GEMM fast path at B >= MLXCEL_GATHER_QMM_GROUPED_MIN_ROWS * E (default 8 * E). On a 128-expert top-8 model that threshold is a 128-token prompt, where the fast path's fixed per-call dequantization makes time to first token about twice that of a 127-token prompt.

Background

The fast path dequantizes the whole [E, N, K] expert stack to the activation dtype on every call, a fixed cost independent of the pair count B, then runs one grouped GEMM. The legacy per-pair path reads one expert matrix per pair. 8 * E was chosen in #629 and validated at 2048-token prompts (64 or more pairs per expert), well above the measured crossover of about 20 pairs per expert. Between 8 and 20 pairs the fast path is slower than the path it replaces, by up to 2.1x on gemma-4-26b-a4b-it-4bit and 2.6x on Qwen3-30B-A3B-3bit at 128 tokens; above about 20 pairs it is faster, up to 3 to 4.5x at 2048 to 4096 tokens as #629 measured. The first generated token is identical in every arm and at every length.

Proposed Solution

Raise the grouped threshold, or make it adaptive to shape, so the fast path engages only where it is cheaper than the legacy path (around 20 pairs per expert on these models, not 8). This overlaps with #3757, which is about the dispatch order against gather_qmm_rhs_sm80 for block-float modes.

Implementation Notes

  • src/lib/mlxcelverse/overlays/cuda/mlx/backend/cuda/quantized/quantized.cpp: GatherQMM::eval_gpu, the use_grouped_gemm lambda and its B >= min_rows * E comparison, env MLXCEL_GATHER_QMM_GROUPED_MIN_ROWS.
  • Measured on mlx-community/gemma-4-26b-a4b-it-4bit, Qwen3-30B-A3B-3bit and Qwen3-30B-A3B-4bit (all E 128, top 8). The per-call traffic model puts the traffic-only crossover at 9 pairs per expert; measured crossover is about 20.

Acceptance Criteria

  • No step in TTFT at 128 tokens on 128-expert top-8 models.
  • The fast path engages only where it is measured to be cheaper than the legacy per-pair path.
  • The first generated token is unchanged across both dispatch arms at every prompt length.

Original Suggestion

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

Problem

On CUDA, a sorted quantized GatherQMM (the SwitchGLU prefill contract, M == 1, one row per (token, expert) pair) switches to the #629 fast path once the pair count B reaches MLXCEL_GATHER_QMM_GROUPED_MIN_ROWS * E (default 8 * E). The fast path dequantizes the whole expert stack [E, N, K] to the activation dtype on every call and then runs one CUTLASS grouped GEMM. That dequantization is a fixed cost per call: it does not shrink with the number of tokens. At 8 * E pairs it costs far more than the legacy per-pair path it replaces.

On a 128-expert, top-8 model the threshold is B = 1024 pairs, which is a 128-token prompt. Time to first token there is about twice that of a 127-token prompt.

Repro

mlxcel-server -m <model> --no-prompt-cache, raw /v1/completions with a prompt of exactly L tokens (checked with usage.prompt_tokens), max_tokens: 1, temperature: 0. Wall time per request, median of 5 after one discarded request, two server instances per arm with the arms rotated. Compare against the same binary with MLXCEL_GATHER_QMM_GROUPED=0 (legacy path).

Actual (main 9402cf8)

mlx-community/gemma-4-26b-a4b-it-4bit (rev 0d77464e; E = 128, top 8, 30 layers), median of the two server instances, ms:

prompt tokens pairs per expert default GROUPED=0
96 6 249 251
127 7.9 318 317
128 8 678 319
136 8.5 695 341
160 10 705 395
192 12 713 467
256 16 742 612
384 24 801 911
512 32 825 1198
1024 64 1064 2539
4096 256 3498 10624

mlx-community/Qwen3-30B-A3B-3bit (rev 6add5f3b; E = 128, top 8, 48 layers):

prompt tokens pairs per expert default GROUPED=0
127 7.9 434 443
128 8 1144 448
192 12 1073 664
256 16 1089 886
320 20 1088 1101
384 24 1119 1309
512 32 1170 1716
2048 128 1566 7157

mlx-community/Qwen3-30B-A3B-4bit (rev d388dead), default build only (four server instances): 127 tokens 312 ms, 128 tokens 757 ms, 256 tokens 817 ms, 352 tokens 843 ms.

The first generated token is the same in every arm and at every length.

On both models the two paths cross at about 20 pairs per expert (around 320 tokens). Between 8 and 20 pairs per expert the fast path is slower than the path it replaces, by up to 2.1x (Gemma) and 2.6x (Qwen3) at 128 tokens. Above about 20 pairs per expert it is faster, up to 3-4.5x at 2048-4096 tokens, as #629 measured.

Expected

No step in TTFT at 128 tokens: the fast path should engage only where it is cheaper than the legacy path.

Cause

8 * E was chosen in #629 and validated at 2048-token prompts (64 or more pairs per expert on these models), well above the crossover. The fast path's cost per call is roughly E * N * K * (bits / 8 + 2 * sizeof(activation)) bytes of traffic (read the packed stack, write the dequantized stack, read it again in the GEMM), independent of B. The legacy path reads one expert matrix per pair, B * N * K * bits / 8. With bf16 activations and 4-bit weights the traffic alone would put the crossover at 9 pairs per expert; measured, it is about 20.

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

  1. changed the title [-]perf(cuda): MoE prefill TTFT doubles at the #629 grouped-GEMM threshold (128 tokens on 128-expert top-8 models)[/-] [+]perf(cuda): MoE prefill TTFT roughly doubles at the grouped-GEMM threshold (128 tokens on 128-expert top-8 models)[/+] on Dec 3, 2026
  2. added
    priority:high
    status:ready
    type:performance
    on Dec 3, 2026
  3. added
    status:done
    and removed
    status:ready
    on Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority:highstatus:donetype:performance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions