Repository navigation
update(cuda): fold batched decode rows in QuantizedMatmul - #2767
Merged
Merged
Conversation
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
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
marked this pull request as ready for review
October 30, 2026 21:22
This was referenced Oct 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Batched decode hands CUDA
QuantizedMatmulanx = [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
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
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_ROWSandMLXCEL_QMM_FOLD_MIN_ELEMENTScontrol it.Not covered
GatherQMMis unchanged. Gemma 4 26B stays flat at 34-40 tok/s from 4 to 8 clients, and MoE readstop_k * Bexpert 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-corelib tests from compiling on Windows. fmt, clippy with--features cuda, and the fold test pass locally.Closes #2690