Repository navigation
[Metal] Use affine qmv_mma on M1 and M2 Ultra from 4 rows - #4890
Merged
Merged
Conversation
Signed-off-by: Joshua Warren <[email protected]>
Contributor
Author
|
GREEN on cb85ea9 Pure routing change, no kernel code touched, and the two gate edits are coherent with each other:
Tests extend the parametrization with the 4- and 5-row counts and a small-N*K shape (2560x1024) that the M1/M2 Ultra CI runners exercise through the new path; correctness below the floor (rows < 4) keeps the qmv/qmv_wide routes as before. The performance claims on the Ultra parts are your measurements, not rerun here. |
hupe1980
added a commit
to hupe1980/nuvec
that referenced
this pull request
Nov 16, 2026
) Co-Authored-By: Claude Opus 5.5 <[email protected]>
zcbenz
approved these changes
Nov 21, 2026
Open
2 of 4 tasks
1 task
11 tasks
rcfa
pushed a commit
to cubiculum-ai/mlx
that referenced
this pull request
Nov 26, 2026
…#4890) Signed-off-by: Joshua Warren <[email protected]> (cherry picked from commit 5cabdf5)
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.
Summary
Use the existing affine
qmv_mmakernel on M1 Ultra and M2 Ultra from 4 rows. They currently keep the vector kernels, where the cost of a 4 to 11 row quantized matmul grows linearly with the rows. No new kernel: this only changes the dispatch condition inuse_qmv_mma.#4787 added the kernel for gen 13 and 14 and left Ultra out. #4827 enables it on M3 Ultra and newer. This PR covers the Ultras before gen 15 and leaves M3 Ultra and newer to #4827, so the two touch the same lines but not the same chips.
Changes
affine_qmv_mmafrom 4 to 16 rows (non-batched), with the same gate as the other gen 13 and 14 GPUs (K % 128 == 0,N >= 512,K >= 512,N * K >= 2^21,N % 16 == 0, 4 and 8 bits). At 12 to 16 rows on large layers this replacesqmmrather than a vector kernel.qmv_mmaat 3 rows ranged from 25% faster to 12% slower, so it is not clearly a win there.test_affine_qmm_few_rowsgets a 2560 x 1024 shape (a layer size that passes the gate on a 4B model) and 4 and 5 rows.Performance
M1 Ultra, 128 GB. Qwen3-4B-Instruct-2507-4bit, MLX main
fc344c9. Time for one decode step of all 252 quantized projections plus the output head at a given row count, the rows being the concurrent sequences of a batched decode. Minimum of 40 steps, best of 3 rounds, builds alternated.1 to 3 rows are unchanged within noise. From 4 rows the step is 35% to 67% faster.
Single layers,
quantized_matmul, 4-bit and 8-bit, bf16 and fp16, from 2560 x 1024 up to 8192 x 28672, median of 3 runs. For layers with 10M weights or more: 17% to 36% faster at 4 rows, and 30% to 66% faster at 6 and 8 rows. The smallest layer (2560 x 1024) gains less. The 2560 x 151936 output head at 4 rows goes from 1893 us to 1232 us in bf16 and from 1724 us to 1083 us in fp16.Validation
python/tests/test_quantized.py: 61 passed, 1 skipped, 6954 subtests passed on this branch. The new cases check correctness against a dequantized reference. They cannot tell which kernel ran, so they do not fail on the old dispatch.clang-format21.1.8 reports no changes.Not measured: an M2 Ultra (it takes the same gen 14 path as the other gen 14 GPUs, but I did not have one), and end-to-end tokens per second.