Skip to content

metal : few-row MMA mat-mul for the remaining src0 types - #30065

Merged
ggerganov merged 1 commit into
ggml-org:masterfrom
pratiknarola-t:metal-mma-types
Oct 7, 2026
Merged

ggerganov merged 1 commit into
ggml-org:masterfrom
pratiknarola-t:metal-mma-types

Conversation

@pratiknarola-t

Copy link
Copy Markdown
Contributor

Overview

Split out of #30047, as asked there: the MUL_MAT part only.

The few-row MMA mat-mul from #29869 now takes every src0 type with a 16-weight dequantizer, adding BF16, Q1_0, Q2_0, MXFP4, Q2_K, Q3_K, TQ2_0 and the IQ types. They run on the generic MMA kernel, so the change is new instantiations plus the dispatch. Each type starts at the row count where it beats the current kernels on an M3 Ultra: 5 for TQ2_0, 4 for BF16, 3 for MXFP4, Q2_0, Q2_K and IQ4_NL, and 2 for the rest.

Additional information

test-backend-ops perf -o MUL_MAT, m=4096, k=14336, Apple M3 Ultra (60-core GPU), macOS 15.7.9. Speedups are the time on master divided by the time with this PR, from two interleaved runs of each build. A dash marks a row count below the type's threshold, where the kernel is the same as on master:

type n=2 n=3 n=4 n=5 n=8 n=16
BF16 - - 1.34x 1.60x 2.83x 3.63x
Q1_0 1.27x 1.83x 2.33x 2.84x 4.41x 3.38x
Q2_0 - 1.50x 1.93x 2.40x 3.74x 3.57x
MXFP4 - 1.19x 1.76x 1.77x 3.32x 3.66x
Q2_K - 1.20x 2.13x 2.73x 4.30x 3.64x
Q3_K 1.78x 2.83x 1.95x 2.30x 3.96x 3.64x
TQ2_0 - - - 1.15x 1.71x 4.03x
IQ1_S 1.08x 1.54x 1.99x 2.45x 3.68x 3.60x
IQ1_M 1.15x 1.64x 2.12x 2.64x 3.94x 3.54x
IQ2_XXS 1.02x 1.42x 1.83x 2.29x 3.34x 3.19x
IQ2_XS 1.07x 1.49x 1.91x 2.34x 3.48x 3.67x
IQ2_S 1.08x 1.52x 1.96x 2.42x 3.63x 3.63x
IQ3_XXS 1.10x 1.56x 2.00x 2.47x 3.74x 3.66x
IQ3_S 1.15x 1.66x 2.14x 2.63x 3.90x 3.65x
IQ4_NL - 1.16x 1.76x 1.89x 3.34x 3.58x
IQ4_XS 1.07x 1.55x 1.96x 2.42x 3.70x 3.54x

At 9 to 16 rows the new types run 3.06x to 4.14x faster. The types that already had MMA kernels (F32, F16, Q4_0, Q4_1, Q5_0, Q5_1, Q8_0, Q4_K, Q5_K, Q6_K) run the same kernel code as on master and measure 0.97x to 1.03x at 1 to 16 rows, and every type is within 1% at 512 rows. Where the speedup does not grow with n, as for Q3_K at n=3, it follows the time of the kernel on master, which does not grow monotonically with n.

Some cells move between runs on this machine. On master, the MXFP4 and IQ4_NL kernels at 3 to 8 rows differed by up to 22% between two sessions (MXFP4 at n=4: 175 us and 202 us), and with this PR the BF16 kernel at n=4 took between 152 us and 174 us (1.17x to 1.34x).

The mul_mv_mma library takes longer to compile: xcrun metal -c on an Apple M5 takes 0.33 s on master and 0.67 s with this PR.

Tests:

  • test-backend-ops -o MUL_MAT,MUL_MAT_ID -b MTL0: 3314/3314 on the M3 Ultra. On an Apple M5 with the tensor API on and off (GGML_METAL_TENSOR_DISABLE=1): MUL_MAT and MUL_MAT_ADD 2587/2587.
  • The new MUL_MAT cases also pass on CUDA (RTX 5090, GB10) and Vulkan (RTX 5090, GB10, Radeon 8060S).

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES - the kernels, the tests, the benchmarks and this description were written with Claude Code (Anthropic).

The generic few-row MMA kernel works for any type with a 16-weight
dequantizer, so it now also takes BF16, Q1_0, Q2_0, MXFP4, Q2_K, Q3_K,
TQ2_0 and the IQ types. Each type starts at the row count where it beats
the current kernels on an M3 Ultra: 5 rows for TQ2_0, 4 for BF16, 3
for MXFP4, Q2_0, Q2_K and IQ4_NL, and 2 for the others.

test-backend-ops perf -o MUL_MAT, m=4096, k=14336, M3 Ultra, time of this
change over master (mean of two interleaved runs each): 0.23 to 0.98 from
the threshold to 8 rows, 0.24 to 0.33 at 9 to 16 rows, and 0.99 to 1.01 at
1 and 512 rows.
@github-actions github-actions Bot added testing Everything test related ggml changes relating to the ggml tensor library for machine learning Apple Metal https://en.wikipedia.org/wiki/Metal_(API) labels Oct 6, 2026
@ggerganov ggerganov self-assigned this Oct 6, 2026
@ggerganov
ggerganov marked this pull request as ready for review October 7, 2026 07:41
@ggerganov
ggerganov requested review from a team and ggerganov as code owners October 7, 2026 07:41
@ggerganov ggerganov added the merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. label Oct 7, 2026
@ggerganov
ggerganov merged commit 9881906 into ggml-org:master Oct 7, 2026
27 of 28 checks passed
thelittlefireman pushed a commit to thelittlefireman/llama.cpp that referenced this pull request Oct 12, 2026
)

The generic few-row MMA kernel works for any type with a 16-weight
dequantizer, so it now also takes BF16, Q1_0, Q2_0, MXFP4, Q2_K, Q3_K,
TQ2_0 and the IQ types. Each type starts at the row count where it beats
the current kernels on an M3 Ultra: 5 rows for TQ2_0, 4 for BF16, 3
for MXFP4, Q2_0, Q2_K and IQ4_NL, and 2 for the others.

test-backend-ops perf -o MUL_MAT, m=4096, k=14336, M3 Ultra, time of this
change over master (mean of two interleaved runs each): 0.23 to 0.98 from
the threshold to 8 rows, 0.24 to 0.33 at 9 to 16 rows, and 0.99 to 1.01 at
1 and 512 rows.
Achilles1089 added a commit to Achilles1089/llama.cpp that referenced this pull request Oct 15, 2026
…gml-org#29869 + ggml-org#30065, without their MUL_MAT+ADD fusion); stop accepting draft tokens at EOG (ggml-org#29638); graph capture flag fix (ggml-org#29390)

The 2..16-row mat-muls that MTP verify produces ran on mat-vec kernels whose time grows with every row. Measured on
the M1 Ultra with Raven's Qwen3.8-27B Q8_0 shapes: 8 rows now cost 1.6-1.9x one row (was 3.3-3.8x), matching
upstream within 1-3%. End to end (temp 1.0, 512 tokens): at --spec-draft-n-max 6 --spec-draft-n-min 1
--spec-draft-p-min 0.6, code echo 34.9 -> 54.7 tok/s and tool JSON 35.2 -> 54.9, prose level (25.6 -> 25.9).
Greedy output is identical to the previous build, with and without drafting. test-backend-ops MUL_MAT: 1163/1163.
The capture fix: the -1 capture counter was passed as a bool, so every node of every graph got debug-group markers.
@BrewTestBot BrewTestBot mentioned this pull request Oct 15, 2026
1 task done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Apple Metal https://en.wikipedia.org/wiki/Metal_(API) ggml changes relating to the ggml tensor library for machine learning merge ready A maintainer can use this label to indicate that they consider the changes final and ready to merge. testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants