Skip to content

update(cuda): fold batched decode rows in QuantizedMatmul - #2767

Merged
inureyes merged 5 commits into
mainfrom
update/issue-2690-fold-batched-qmm
Oct 31, 2026
Merged

inureyes merged 5 commits into
mainfrom
update/issue-2690-fold-batched-qmm

Conversation

@inureyes

@inureyes inureyes commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

Batched decode hands CUDA QuantizedMatmul an x = [B, 1, K] over a shared 2D weight, and it ran that as B independent problems, re-reading the weight per row. Metal and ROCm already fold this shape into rows; CUDA did not. This folds it. On compute capability 12.1, a folded batch of a weight of at least 2^24 elements also runs as one GEMM from 5 rows, and a smaller weight stays on qmv through 8 rows.

Result: RTX Spark N1X (sm_121), n=3, one binary, median aggregate tok/s

Model 5 clients, shipped / no fold 8 clients, shipped / no fold
qwen3.8-27b 34.9 / 23.3 45.0 / 14.0
llama-3.1-8b 59.9 / 47.0 75.6 / 33.7
gpt-oss-20b 89.6 / 86.0 87.9 / 72.1
gemma-4-26b 35.7 / 37.0 34.1 / 16.9

1 and 4 clients run the same kernels on both arms. Method, per-width sweeps and traces are in docs/benchmark_results/cuda-qmm-fold-batch-n1x-2026-10-31.md.

Behavior

  • Gemma 4 26B loses 2-5% at 5 to 7 clients against qmv. The cause is not established. Moving every weight costs it 5-7%, which is why the threshold depends on size, and GEMM below 5 rows costs it 10-19% at 4 clients.
  • A batched row that takes GEMM is no longer bitwise equal to the same row decoded alone. At 6 rows with every weight on GEMM: Llama 0/256 top-1 disagreements, Qwen 5/256 (gaps at most 0.125), none on decided positions. Gemma 4 26B diverges with or without this change. The shipped size gate was not traced separately.
  • Verify blocks and prefill pieces (M > 1) keep the threshold of 8, because speculative verify relies on multirow qmv's bit identity.
  • MLXCEL_QMM_FOLD_BATCH=0, MLXCEL_QMM_FOLD_MIN_ROWS and MLXCEL_QMM_FOLD_MIN_ELEMENTS control it.

Not covered

  • GB10, H100 and other compute capabilities are not measured. The fold applies to all of them; only 12.1 changes the threshold.
  • GatherQMM is unchanged. Gemma 4 26B stays flat at 34-40 tok/s from 4 to 8 clients, and MoE reads top_k * B expert matrices per operation.

The fold test also passes with the fold off, so it guards correctness, not speed. This also fixes a unix-only symlink that kept the mlxcel-core lib tests from compiling on Windows. fmt, clippy with --features cuda, and the fold test pass locally.

Closes #2690

Batched decode passes every projection x = [B, 1, K] against one shared 2D quantized weight. The CUDA QuantizedMatmul overlay took that as B independent M == 1 problems, so each batch entry re-read the whole weight: qmv over l = B below M * B = 8, and qmm_sm80 or qmm_naive with an M == 1 tile from there. An [8, 1, 5120] x [17408, 5120] 4-bit matmul took 8.7 ms against 0.9 ms for the same rows as [8, 5120] on an RTX Spark N1X (sm_121), and serving at concurrency 8 ran slower than at concurrency 4 on gemma-4-26b-a4b and qwen3.8-27b.

When the weight is 2D and x holds B * M * K elements, the batch is now folded into rows and the kernels get [B * M, K] and [B * M, N] views. B < 8 keeps the same qmv kernel over the same rows; B >= 8 becomes one GEMM that reads the weight once. 3D weights and GatherQMM are unchanged. MLXCEL_QMM_FOLD_BATCH=0 restores the per-batch dispatch.

Also adds examples/qmm_fold_batch_probe.rs, a CUDA test that [B, 1, K] and [B, K] agree bit for bit and match a dequantized reference (including an offset x view), and replaces a unix-only symlink that kept the mlxcel-core lib tests from compiling on Windows.

Validated on the N1X: probe shows the [8, 1, K] cases at the [8, K] time with the fold and the old times with the kill switch; the new test passes.

Refs #2690
The fold left 2 to 7 rows on multirow qmv, whose 8-wide instantiation slows at 5 to 7 rows. Sweeping qmv against GEMM per width on an RTX Spark N1X, GEMM gains 10-99% aggregate at 5 to 7 clients on Qwen 3.8 27B, Llama 3.1 8B and gpt-oss 20B. Gemma 4 26B loses up to 10% there and 10-19% at 4 clients, so the default is 5 rather than lower.

MLXCEL_QMM_FOLD_MIN_ROWS overrides it. Only folded M == 1 batches move: verify blocks and prefill pieces keep 8, because speculative verify relies on multirow qmv being bit-identical to per-row decode. Other compute capabilities keep 8 because they are unmeasured.

Refs #2690
@inureyes inureyes added status:in-progress Currently being worked on type:performance Performance improvements priority:high High priority area:core mlxcel-core: MLX FFI, primitives, KV cache, layers platform:windows Windows (native) specific labels Oct 30, 2026
GEMM at 5 rows for every weight cost Gemma 4 26B 5-7% at 5 to 7 clients. On compute capability 12.1 a weight of at least 2^24 elements now moves at 5 rows and a smaller one stays on qmv through 8 rows. That keeps the 22-104% gain on Qwen 3.8 and Llama, reduces the Gemma 4 loss to 2-5%, and costs gpt-oss 2-4% of its gain. MLXCEL_QMM_FOLD_MIN_ELEMENTS overrides the size.

Add the benchmark record. Validated on an RTX Spark N1X; GB10 and H100 were not measured.

Refs #2690
@inureyes
inureyes marked this pull request as ready for review October 30, 2026 21:22
@inureyes
inureyes merged commit 9f06fef into main Oct 31, 2026
31 checks passed
@inureyes inureyes added this to the 0.8 milestone Nov 30, 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 platform:windows Windows (native) specific priority:high High priority status:in-progress Currently being worked on type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(cuda): fold shared-weight batch dims into rows in QuantizedMatmul

1 participant