Skip to content

[Metal] Add int8 NAX path for affine quantized_matmul - #4802

Merged
zcbenz merged 3 commits into
ml-explore:mainfrom
XXXXRT666:metal-nax-affine-int-mma
Nov 27, 2026
Merged

zcbenz merged 3 commits into
ml-explore:mainfrom
XXXXRT666:metal-nax-affine-int-mma

Conversation

@XXXXRT666

@XXXXRT666 XXXXRT666 commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor
  • ☑️ I understand it is strictly prohibited to use AI to write PR description
  • AI usage disclosure: Benchmarking, Testing, Profiling, Heuristic Searching

mx.quantized_matmul gets a keyword argument quantize_input (default False). With quantize_input=True, x is quantized to signed 8 bits with one scale per group_size elements before the multiplication.

Accuracy

M = 1024, K = N = 4096, x and w from normal distribution, bf16. RMS error relative to the RMS of x @ w in float32:

w quantize_input=False quantize_input=True
affine 4-bit, gs 64 9.10% 9.12%
affine 4-bit, gs 128 10.0% 10.0%
affine 6-bit, gs 64 2.23% 2.30%
affine 8-bit, gs 64 0.76% 0.95%
affine 8-bit, gs 128 0.81% 1.03%

Performance

Measured on M5 Pro (16 cores), M5 Max (40 cores) and M6 (12 cores), bf16, group size 64. The numbers are from the previous QQMM version of this PR, the kernels and the routes for M >= 64 are the same.

Shapes (K x N): 4096x4096, 4096x14336, 14336x4096, 8192x2048.

Speedup of quantize_input=True over quantize_input=False, geomean over the 4 shapes (>1 is faster):

M5 Pro

M 4-bit 6-bit 8-bit
64 1.28x 1.12x 1.00x
256 1.20x 1.12x 1.17x
1024 1.21x 1.11x 1.19x
4096 1.21x 1.12x 1.19x

M5 Max

M 4-bit 6-bit 8-bit
64 1.08x 1.00x 0.94x
256 1.21x 1.11x 1.18x
1024 1.23x 1.06x 1.20x
4096 1.18x 1.09x 1.18x

M6

M 4-bit 6-bit 8-bit
64 1.19x 1.04x 1.04x
256 1.31x 1.27x 1.26x
1024 1.37x 1.31x 1.34x
4096 1.60x 1.38x 1.52x

For M < 32 the int8 MMAs are not used, and quantize_input=True only adds the cost of quantizing x.

Per shape at M = 1024, microseconds (quantize_input=False / quantize_input=True):

4-bit:

K x N M5 Pro M5 Max M6
4096x4096 1324 / 1066 557 / 448 1669 / 1274
4096x14336 4638 / 3533 1865 / 1444 5884 / 4147
14336x4096 4654 / 3823 1936 / 1614 7914 / 5114
8192x2048 1299 / 1183 597 / 498 1731 / 1431

8-bit:

K x N M5 Pro M5 Max M6
4096x4096 1313 / 1097 548 / 455 1670 / 1317
4096x14336 4618 / 3616 1855 / 1477 5875 / 4295
14336x4096 4674 / 3896 1936 / 1633 8107 / 5743
8192x2048 1287 / 1194 591 / 505 1915 / 1468

@zcbenz

zcbenz commented Nov 1, 2026

Copy link
Copy Markdown
Member

It will take a while before I can look into this, but is this the same approach taken in #4722/#4627?

@XXXXRT666

XXXXRT666 commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor Author

It will take a while before I can look into this, but is this the same approach taken in #4722/#4627?

Almost, I think it's better to be implemented in qqmm

4722 converts the affine weights to symmetric int8 with one scale per row, so they are rounded a second time. Here the MMA uses the original codes, and the per-group scales and biases are applied in the kernel.

@XXXXRT666
XXXXRT666 force-pushed the metal-nax-affine-int-mma branch from 239bfa7 to 21f5e03 Compare November 3, 2026 00:54
@zcbenz zcbenz added the await discussion This pull request makes some major changes that requires the team to have a discussion. label Nov 7, 2026
@XXXXRT666
XXXXRT666 force-pushed the metal-nax-affine-int-mma branch from 21f5e03 to 1321d43 Compare November 9, 2026 18:36

@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.

Sorry for the late response, after discussion we decided not to add affine qqmm: there is no common standard and we think it is against the direction of industry. But we do think that using int8 NAX to accelerate quantized_matmul is still a reasonable optimization, so can you please refactor this PR to only accelerate quantized_matmul without changing the interface of qqmm, and benchmark against #4722?

@zcbenz zcbenz removed the await discussion This pull request makes some major changes that requires the team to have a discussion. label Nov 22, 2026
@XXXXRT666
XXXXRT666 force-pushed the metal-nax-affine-int-mma branch from 1321d43 to cc830b1 Compare November 23, 2026 00:24
@XXXXRT666 XXXXRT666 changed the title [Metal] Add affine qqmm with int8 NAX MMAs [Metal] Add int8 NAX path for affine quantized_matmul Nov 23, 2026
@XXXXRT666

XXXXRT666 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Sorry for the late response, after discussion we decided not to add affine qqmm: there is no common standard and we think it is against the direction of industry. But we do think that using int8 NAX to accelerate quantized_matmul is still a reasonable optimization, so can you please refactor this PR to only accelerate quantized_matmul without changing the interface of qqmm, and benchmark against #4722?

Thanks, I have refactored the PR

quantized_matmul gets a keyword argument quantize_input=False, x is quantized to int8 with one scale per group, and the int8 NAX kernel is used when the shape fits. Otherwise the other routes run on the dequantized x. Only affine on Metal for now.

Comparison with 4722. The numbers are from PR descriptions. M6 is the only chip in both. 4-bit, group size 64, bf16, speedup over quantized_matmul:

K x N (this PR / #4722) this PR (M = 1024) #4722 mode 1 / 4 / 2 (M = 2048)
4096x4096 1.31x 1.33x / 1.49x / 1.60x
4096x14336 / 4096x12288 1.42x 1.31x / 1.52x / 1.63x
14336x4096 / 12288x4096 1.55x 1.49x / 1.73x / 1.85x

Precision: 4722 rounds w again to int8 with a per-row scale (x per row, refined per 64 / 256 elements in mode 1 / 4). This PR keeps the codes of w and uses a per-group scale for x: 4-bit gs 64 error goes from 9.10% to 9.12%.

The test machines I used are not mine, so I cannot run both PRs on the same shapes and machines for now.

Comment thread mlx/ops.h Resolved
@XXXXRT666
XXXXRT666 force-pushed the metal-nax-affine-int-mma branch from cc830b1 to 8d496df Compare November 23, 2026 21:28
@zcbenz
zcbenz force-pushed the metal-nax-affine-int-mma branch from 8d496df to 718b9e5 Compare November 27, 2026 02:18
@zcbenz
zcbenz merged commit c910808 into ml-explore:main Nov 27, 2026
24 checks passed
jeberly pushed a commit to botdevhq/mlx-serve that referenced this pull request Nov 28, 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