Repository navigation
metal : use mul_mm for 32 <= K < 64 - #30982
Merged
Merged
Conversation
vieduy
force-pushed
the
metal-mul-mm-small-k
branch
from
October 26, 2026 15:37
016c6c2 to
2b627c6
Compare
vieduy
force-pushed
the
metal-mul-mm-small-k
branch
from
October 26, 2026 15:41
2b627c6 to
4697e99
Compare
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
approved these changes
Nov 5, 2026
ggerganov
left a comment
Member
There was a problem hiding this comment.
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.
Contributor
Author
|
@ggerganov Thanks for your suggestion about adding a BF16 x BF16 kernel. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
On Metal, matmuls with batch > 8 use
mul_mmonly when K >= 64. Below that,mul_mvis used:_shortfor 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.mul_mmis slower there at small batch.Additional information
All numbers are from an Apple M4 (10-core GPU), macOS 27.0.1.
Correctness:
test-backend-opson Metal passes (MUL_MAT 2475/2475, MUL_MAT_ID 867/867, full suite 17861/17861). Model output is unchanged:llama-perplexityKLD 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):
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.Reproduce:
Requirements