Skip to content

metal : use mul_mm for 32 <= K < 64 - #30982

Merged
ggerganov merged 3 commits into
ggml-org:masterfrom
vieduy:metal-mul-mm-small-k
Nov 5, 2026
Merged

ggerganov merged 3 commits into
ggml-org:masterfrom
vieduy:metal-mul-mm-small-k

Conversation

@vieduy

@vieduy vieduy commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Overview

On Metal, matmuls with batch > 8 use mul_mm only when K >= 64. Below that, mul_mv is used: _short for K < 32, and the generic kernel for K >= 32. The generic kernel is built for long rows, so at K = 32–63 its per-row reduction and dispatch overhead dominate. At n = 512, K = 32 runs 32x slower than K = 64, despite half the math.

LoRA matrices have K = rank, so rank 32–63 adapters hit this. On master, an r32 or r48 adapter roughly halves prefill speed; r16 and r64 don't (about 16% of popular LoRA adapters on Hugging Face are r32, also my research team choose r32 for good result enough).

This change sends 32 <= K < 64 to mul_mm, which already supports it: it steps K by 32 and zero-pads the tail.

  • K < 32 is unchanged; mul_mm is slower there at small batch.

Additional information

All numbers are from an Apple M4 (10-core GPU), macOS 27.0.1.

Correctness: test-backend-ops on Metal passes (MUL_MAT 2475/2475, MUL_MAT_ID 867/867, full suite 17861/17861). Model output is unchanged: llama-perplexity KLD vs master on Qwen3.5 0.8B with r32 and r48 adapters is 2e-6, the same as rerunning master itself.

Op level (master vs PR, 30 shapes per K: F16/F32, m 1024–6144, n 16–2048):

K speedup (geomean, min–max)
32 30.9x (5.8–54.6x)
48 20.5x (4.0–35.6x)
all other K (8–24, 64–128) 0.98–1.03x

e.g. F16 2048x512, K = 32: 1772 us → 36 us.

End to end: prefill tok/s, llama-batched-bench -npp 512 -ntg 64 -npl 1 -ngl 99, median of 3.

model / pack master PR
Qwen3.5 0.8B, no pack 2282 2239
Qwen3.5 0.8B, r16 1706 1733
Qwen3.5 0.8B, r32 799 1817
Qwen3.5 0.8B, r48 793 1792
Qwen3.5 0.8B, r64 1804 1772
Ternary Bonsai 2 2B, no pack 983 992
Ternary Bonsai 2 2B, my fine-tune r32 pack 489 859
Ternary Bonsai 2 2B, same pack zero-padded to r64 862 856

Reproduce:

test-backend-ops perf -b MTL0 -o MUL_MAT -p "type_a=f16,type_b=f32,m=2048,n=512,k=(16|32|48|64),bs"
llama-batched-bench -m <model> --lora <rank-32 adapter> -c 4096 -npp 512 -ntg 64 -npl 1 -ngl 99

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES. Claude Code was used to help develop and test the fix. I reviewed every line and take full responsibility for the changes.

@vieduy
vieduy requested review from a team and ggerganov as code owners October 22, 2026 14:10
@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 22, 2026
@vieduy
vieduy force-pushed the metal-mul-mm-small-k branch from 016c6c2 to 2b627c6 Compare October 26, 2026 15:37
@vieduy
vieduy force-pushed the metal-mul-mm-small-k branch from 2b627c6 to 4697e99 Compare October 26, 2026 15:41
@vieduy

vieduy commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

Hi @ggerganov ,a short ping on this PR. I just merged the latest master. Is there anything you'd like me to add or change? Thanks for your time.

@ggerganov ggerganov self-assigned this Nov 5, 2026

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

I think we can simply add a BF16 x BF16 kernel as a stopgap. I'll be reworking the Metal backend to add more generic support for src1 BF16.

@vieduy

vieduy commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

@ggerganov Thanks for your suggestion about adding a BF16 x BF16 kernel.

@ggerganov
ggerganov merged commit f7b41cc into ggml-org:master Nov 5, 2026
25 checks passed
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 testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants