Skip to content

[Metal] Enable affine qmv_mma on M3 Ultra - #4827

Merged
zcbenz merged 2 commits into
ml-explore:mainfrom
padwowen:small-m-qmv-ultra
Nov 23, 2026
Merged

zcbenz merged 2 commits into
ml-explore:mainfrom
padwowen:small-m-qmv-ultra

Conversation

@padwowen

@padwowen padwowen commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Enable the existing affine qmv_mma kernel for small-batch quantized matmul on M3 Ultra and newer Ultra chips, and lower the 4-bit qmv_nax threshold on M5-class hardware.

Both kernels already exist. This change only adjusts dispatch conditions; no new kernels are introduced.

Motivation

PR #4787 introduced the faster qmv_mma path for small batches but excluded Ultra chips, which were not included in its benchmarks.

On M3 Ultra, the existing vector kernel scales poorly at 4–16 rows for large quantized layers. For Llama-3.3-70B, matmul latency at 8 and 12 rows reaches 3.21x and 5.42x single-row latency, respectively.

These batch sizes are particularly relevant to batched decoding and speculative decoding verification.

Changes

  • M3 Ultra and newer: Enable qmv_mma at 4–16 rows when both N and K are at least 4096, following the large-layer threshold used by the floating-point Ultra path in [Metal] Add qmv_mma/qmv_nax kernels for 6-16 rows with large N/K #4712. This covers 2-, 3-, 4-, 5-, 6-, and 8-bit quantization.
  • M5 (NAX): Lower the 4-bit qmv_nax dispatch threshold from 6 rows to 4. Other bit widths are unchanged.
  • Tests: Add test_affine_qmm_few_rows_large, covering 2–16 rows, 2-, 3-, 4-, 5-, 6-, and 8-bit quantization, group sizes 32–128, bf16/fp16/fp32, eligible and ineligible shapes, and an odd-offset view.

Smaller layers, 1–3 rows, and 17+ rows retain existing dispatch behaviour (M1/M2 Ultra dispatch is covered by #4890).

Performance

M3 Ultra: Llama-3.3-70B, 4-bit

Workload MLX main This change Speedup
1 user 16.9 tok/s 16.9 tok/s Unchanged
4 users 38.0 tok/s 42.0 tok/s 1.11x
8 users 37.0 tok/s 80.2 tok/s 2.17x
16 users 45.1 tok/s 83.6 tok/s 1.85x
Speculative decode (k=6) 25.4 tok/s 43.9 tok/s 1.73x

An 8-token forward pass through the full 70B model decreases from 211 ms to 95 ms.

For 8-bit quantization on an 8B model, throughput improves by 1.69x at both 8 and 16 users.

Two benchmark rounds, run in reverse build order, agreed within 0.3%. Prefill performance and peak memory usage are unchanged, with no measured regressions.

M5 Pro: 4-bit NAX

At 4–5 rows, matmul latency decreases from 1.17–1.48x single-row latency to 1.03–1.13x.

On an 8B model:

  • 4-user batched decode: 175 → 202 tok/s (+15%)
  • Speculative decode (k=4): 102 → 112 tok/s (+10%)

Validation

pytest python/tests/test_quantized.py passes on both M3 Ultra and M5 Pro:

  • 65 passed, 1 skipped (CUDA-only)
  • 8,272 subtests exercised

The benchmark results were reproduced across two runs.

Limitations

Only M3 Ultra and M5 Pro were benchmarked. The NAX dispatch change applies to M5-class chips generally.

Small layers on M3 Ultra, including K/V projections, retain the existing vector path. Since #4890, M1/M2 Ultra use qmv_mma for mid-size layers. Optimising small layers is outside the scope of this change.

As with other kernel dispatch changes, small numerical differences may occur near bf16 rounding boundaries (see #4819).

AI usage

This change was developed with AI assistance. I have reviewed the implementation, understand the changes, and validated the results through hardware benchmarks and the test suite.


The Ultra and M5 dispatch changes are independent. Happy to split them into separate PRs if preferred.

  • ☑️ I understand it is strictly prohibited to use AI to write PR description
  • AI usage disclosure: Code investigation, implementation, comment cleanup, and test/benchmark runs were performed by Claude Code (Claude Opus 5.5) on local hardware. I reviewed the change and all results, and wrote the commit message and PR description.

@zcbenz zcbenz added the await verification This pull request is non-trivial and requires a human expert to verify its correctness. label Nov 20, 2026

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please rebase this PR to resolve the conflicts from #4890 and #4860?

@zcbenz zcbenz added await response This pull request is waiting for response from the author. and removed await verification This pull request is non-trivial and requires a human expert to verify its correctness. labels Nov 23, 2026
@zcbenz zcbenz removed the await response This pull request is waiting for response from the author. label Nov 23, 2026
@padwowen

Copy link
Copy Markdown
Contributor Author

Rebased onto #4890 and #4860

M3 Ultra now also covers 2,3,5 and 6 bits.

@padwowen
padwowen requested a review from zcbenz November 23, 2026 02:04
Also lower the 4-bit qmv_nax dispatch threshold from 6 to 4 rows on M5-class chips.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@zcbenz
zcbenz merged commit 8f578df into ml-explore:main Nov 23, 2026
24 checks passed
rcfa pushed a commit to cubiculum-ai/mlx that referenced this pull request Nov 26, 2026
osaurus-ai/mlx takes the non-NAX part only. The 4-bit affine_qmv_nax rows need ml-explore#4781.
(cherry picked from commit 8f578df)
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