Repository navigation
metal : commit the graph in chunks with one command buffer thread - #32947
Merged
Merged
Conversation
Assisted-by: Claude Opus 5.5
Contributor
Author
|
@ggerganov I was optimizing the inkling for metal but I'm not sure if this is super useful in general. PTAL if it makes a diff on your setup. |
Member
|
Ok, running it now on my machines. |
Contributor
Author
|
BTW @ggerganov fusing elementwise ops should be a simple change in metal since you compile the shader on the fly. Fusing stuff here seems to be quite beneficial for out-of-the-box good performance. Maybe worth a try with a LLM? |
Member
|
Yes, I think we are fusing most of the simple stuff already. |
Member
|
Seems to be beneficial (PP is noise):
|
Member
|
Huh the gain from this change is even bigger with Qwen3.8-Flash-Next on the M5:
|
Member
|
@ggml-org/ggml-metal Can you run some more evals on your end too? |
ggerganov
approved these changes
Nov 25, 2026
4 of 11 tasks
This was referenced Nov 25, 2026
metal : commit the graph in chunks with one command buffer thread (#32947)
PrismML-Eng/llama.cpp#468
Draft
1 task
Member
|
@ggerganov What's the best way to run an eval for this? Can test on M4 Pro. |
bri-prism
added a commit
to PrismML-Eng/llama.cpp
that referenced
this pull request
Nov 27, 2026
From upstream master, unchanged: - ggml-org#32947 (52f6e02): commit the graph in chunks with one command buffer thread - ggml-org#33034 (7135e2f): spread the short mat-vec lanes over src1 rows The original commit messages and authorship are on the upstream PRs. Apple M5 Pro, prism 76ef9d2 vs this, llama-bench -fa 1, interleaved, 2 x 3 reps: - small model: tg128 +3.2%, pp512 flat - large model: tg128, pp512 and pp2/4/8 flat test-backend-ops -b MTL0 -o MUL_MAT 3201/3201; greedy output identical.
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
With the default
n_cb = 1,ggml_metal_graph_computeencodes the first 10% of the nodes and commits them, then encodes the other 90% into a second command buffer and commits it at the end. The GPU runs the first 10% and then waits until the encoding of the rest is done. For a decode graph of thousands of small ops this wait is several ms per token.With
n_cb == 1, the calling thread now encodes the graph in chunks of 256 nodes and commits each chunk as soon as it is encoded, so the GPU runs a chunk while the next one is encoded. A chunk ends at the end of a fusable group (ggml_metal_fusion_max, as for the existing command buffer boundaries), so the same nodes fuse. The abort callback, the capture andn_cb != 1(iOS) keep the old path.Additional information
M3 Ultra, master 4c92c5b vs this PR,
llama-bench -ngl 99 -fa 1, 2 interleaved rounds of-r 3:-r 5)An Inkling 2 Mini decode graph has about 7k nodes, most of them small ops, so the encoding of the second command buffer takes several ms per token. Prefill graphs keep the GPU busy while the next chunk is encoded, so they do not change.
The output does not change:
llama-perplexity --kl-divergenceagainst master over 4 chunks of 512 gives mean KLD 0 on Inkling 2 Mini and Qwen3.8-27B, and the same maximum KLD and same top p as master against itself (Metal run-to-run differences).Requirements