Skip to content

[Metal] Use affine qmv_mma on M1 and M2 Ultra from 4 rows - #4890

Merged
zcbenz merged 2 commits into
ml-explore:mainfrom
joshuaswarren:ultra-affine-qmv-mma
Nov 21, 2026
Merged

zcbenz merged 2 commits into
ml-explore:mainfrom
joshuaswarren:ultra-affine-qmv-mma

Conversation

@joshuaswarren

Copy link
Copy Markdown
Contributor

Summary

Use the existing affine qmv_mma kernel 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 in use_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

  • M1 and M2 Ultra: affine_qmv_mma from 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 replaces qmm rather than a vector kernel.
  • 3 rows stays on the vector kernel on Ultra. On the shapes I measured, qmv_mma at 3 rows ranged from 25% faster to 12% slower, so it is not clearly a win there.
  • M3 Ultra and newer: unchanged.
  • Test: test_affine_qmm_few_rows gets 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.

Rows 1 2 3 4 5 6 8 12
bf16 main (ms) 4.41 7.71 11.16 14.54 17.96 21.21 28.05 36.41
bf16 this PR (ms) 4.50 7.88 11.26 9.32 9.18 9.15 9.28 16.96
fp16 main (ms) 4.11 6.88 10.22 13.11 16.30 19.16 25.33 27.89
fp16 this PR (ms) 4.20 7.06 10.14 8.57 8.49 8.59 8.55 14.61

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-format 21.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.

@joshuaswarren

Copy link
Copy Markdown
Contributor Author

GREEN on cb85ea9
Model: zai/glm-5.3

Pure routing change, no kernel code touched, and the two gate edits are coherent with each other:

  • The enablement: the early return that kept ALL Ultras on the vector kernels now excludes only gen 15+ Ultras, so M1/M2 Ultra (g13d/g14d) fall through to the existing non-nax eligibility checks (4/8-bit instantiation, N*K payoff threshold, K%128, dtype/shape gates) instead of being unconditionally denied - the same path non-ultra g13/g14 already take.
  • The min-rows edit: Ultras now take the 4-row floor (matching gen 15+ behavior) instead of the 3-row floor that only applied to non-ultra g13/g14; the comment is updated to state the three-way rule accurately.
  • The ultra detection (architecture string ending in d) is the established convention for the dual-die variants, and the variable is reused for both checks so the two gates cannot drift apart.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants