Skip to content

metal : commit the graph in chunks with one command buffer thread - #32947

Merged
ggerganov merged 1 commit into
ggml-org:masterfrom
am17an:metal-stream-cb
Nov 25, 2026
Merged

ggerganov merged 1 commit into
ggml-org:masterfrom
am17an:metal-stream-cb

Conversation

@am17an

@am17an am17an commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Overview

With the default n_cb = 1, ggml_metal_graph_compute encodes 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 and n_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:

model test master t/s PR t/s
Inkling 2 Mini IQ4_NL tg128 31.1, 31.0 31.4, 33.8
Inkling 2 Mini IQ4_NL pp512 716, 716 712, 716
Qwen3.8-27B Q4_0 tg128 (3 rounds of -r 5) 34.84, 34.93, 34.96 34.92, 35.03, 34.97
Qwen3.8-27B Q4_0 pp512 331, 331 331, 331
Qwen3.5-0.8B Q4_K_M tg128 263.0, 263.5 278.8, 275.6
Qwen3.5-0.8B Q4_K_M pp512 9156, 9143 9278, 9177

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-divergence against 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

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES. Claude Code found this while profiling Inkling 2 Mini decode on Metal, wrote the change, ran the benchmarks above and wrote this description.

@am17an
am17an requested a review from a team as a code owner November 24, 2026 12:49
@am17an am17an closed this Nov 24, 2026
@am17an

am17an commented Nov 24, 2026

Copy link
Copy Markdown
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.

@ggerganov

Copy link
Copy Markdown
Member

Ok, running it now on my machines.

@am17an

am17an commented Nov 24, 2026

Copy link
Copy Markdown
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?

@ggerganov

Copy link
Copy Markdown
Member

Yes, I think we are fusing most of the simple stuff already.

@ggerganov

Copy link
Copy Markdown
Member

Seems to be beneficial (PP is noise):

Machine pp512 (base → PR) tg128 (base → PR)
M5 Ultra (local) 21889 → 21842 (−0.2 %) 305.9 → 357.6 (+16.9 %)
M2 Ultra (m2) 11195 → 11118 (−0.7 %) 230.0 → 256.1 (+11.4 %)

@am17an am17an reopened this Nov 24, 2026
@github-actions github-actions Bot added ggml changes relating to the ggml tensor library for machine learning Apple Metal https://en.wikipedia.org/wiki/Metal_(API) labels Nov 24, 2026
@ggerganov

Copy link
Copy Markdown
Member

Huh the gain from this change is even bigger with Qwen3.8-Flash-Next on the M5:

Machine Qwen3.5-0.8B Q8_0 Qwen3.8-Flash-Next IQ4_NL
M5 Ultra 305.9 → 357.6 (+16.9 %) 48.8 → 66.1 (+35.5 %)
M2 Ultra 230.0 → 256.1 (+11.4 %) 39.5 → 44.1 (+11.6 %)

@ggerganov

Copy link
Copy Markdown
Member

@ggml-org/ggml-metal Can you run some more evals on your end too?

@ggerganov ggerganov self-assigned this Nov 24, 2026
@nikwen

nikwen commented Nov 25, 2026

Copy link
Copy Markdown
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.
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants