Skip to content

vulkan: roll bk loop in matmul for asahi linux - #24663

Merged
0cc4m merged 5 commits into
ggml-org:masterfrom
xingjianll:toggle-unroll-mulmm
Jun 30, 2026
Merged

vulkan: roll bk loop in matmul for asahi linux#24663
0cc4m merged 5 commits into
ggml-org:masterfrom
xingjianll:toggle-unroll-mulmm

Conversation

@xingjianll

@xingjianll xingjianll commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Overview

Removing the unroll tag on the fma loop in mul_mm.comp gives a big performance boost on Asahi Linux, possibly because the unrolled loop body is otherwise too large to fit in Apple's instruction cache (12 KB). This PR adds an UNROLL macro in mul_mm.comp and generates rolled and unrolled variants at build time. It then only selects the rolled variant at pipeline-creation time for Asahi Linux.

llama-2-7B Q4_0 end-to-end, Vulkan (Apple M1 Ultra, 48 GPU core)

metric   before            after             delta
pp512    306.75 ± 0.39     375.56 ± 0.39     +22.4%
tg128     34.91 ± 0.08      34.22 ± 0.25      -2.0% 

related results from test-backend-ops:
matmul_perf_table.txt

see #10982 for full discussion.

Requirements

@xingjianll
xingjianll requested a review from a team as a code owner June 15, 2026 17:29
@github-actions github-actions Bot added Vulkan Issues specific to the Vulkan backend ggml changes relating to the ggml tensor library for machine learning labels Jun 15, 2026
@jeffbolznv

Copy link
Copy Markdown
Contributor

While I'm OK with this change in theory, we have a problem with the number of mul_mm variants.

Before:
dir build\ggml\src\ggml-vulkan /os
...
06/15/2026  12:59 PM       217,346,363 mul_mm.comp.cpp

dir build\bin\RelWithDebInfo\ggml-vulkan.dll
...
06/15/2026  01:00 PM        89,548,288 ggml-vulkan.dll

After:
dir build\ggml\src\ggml-vulkan /os
...
06/15/2026  12:54 PM       384,390,079 mul_mm.comp.cpp

dir build\bin\RelWithDebInfo\ggml-vulkan.dll
...
06/15/2026  12:55 PM       135,829,504 ggml-vulkan.dll

I'd rather fix this by editing the SPIR-V when we create the pipeline, similar to what we do for ggml_vk_strip_decode_vector, for example. Do you want to make this change? If you're not comfortable doing it, I can do it.

@xingjianll

Copy link
Copy Markdown
Contributor Author

While I'm OK with this change in theory, we have a problem with the number of mul_mm variants.

Before:
dir build\ggml\src\ggml-vulkan /os
...
06/15/2026  12:59 PM       217,346,363 mul_mm.comp.cpp

dir build\bin\RelWithDebInfo\ggml-vulkan.dll
...
06/15/2026  01:00 PM        89,548,288 ggml-vulkan.dll

After:
dir build\ggml\src\ggml-vulkan /os
...
06/15/2026  12:54 PM       384,390,079 mul_mm.comp.cpp

dir build\bin\RelWithDebInfo\ggml-vulkan.dll
...
06/15/2026  12:55 PM       135,829,504 ggml-vulkan.dll

I'd rather fix this by editing the SPIR-V when we create the pipeline, similar to what we do for ggml_vk_strip_decode_vector, for example. Do you want to make this change? If you're not comfortable doing it, I can do it.

Yes I can try tonight.

@xingjianll

Copy link
Copy Markdown
Contributor Author

@jeffbolznv Hi, I implemented the change as requested. Currently I tried to limit my change to only editing the spirv during pipeline creation and the performance is identical to the previous version.

@jeffbolznv jeffbolznv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall I think this looks correct, though it may be fragile if we rewrite mul_mm.

}

auto encloses = [](const vk_spv_loop & a, const vk_spv_loop & b) {
return a.header < b.header && b.header < a.end;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes the blocks are laid out in an order that matches the control flow nesting, which is not strictly required by SPIR-V but I think is true for glslang, so this should be fine.

Comment thread ggml/src/ggml-vulkan/ggml-vulkan.cpp Outdated
// hardware like Apple M1/M2.
// Assumes 1. code comes from mul_mm.comp 2. the K-tile loop has no loop
// control hint and 3. the BK loop is the last loop nested directly inside
// the K-tile loop.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is whitespace at the end of some lines, which CI will reject,

@xingjianll
xingjianll requested a review from jeffbolznv June 18, 2026 17:18
@0cc4m
0cc4m merged commit f708a5b into ggml-org:master Jun 30, 2026
26 of 27 checks passed
turbo-tan pushed a commit to turbo-tan/llama.cpp-tq3 that referenced this pull request Jul 1, 2026
* vulkan: roll bk loop in matmul for asahi linux

* vulkan: fix inline comment

* vulkan: revert BK-loop unroll change

* vulkan: edit spirv directly for asahi roll bk loop

* vulkan: remove trailing whitespace at the end of comments
DFveloper pushed a commit to DFveloper/aikar-engine that referenced this pull request Jul 3, 2026
* vulkan: roll bk loop in matmul for asahi linux

* vulkan: fix inline comment

* vulkan: revert BK-loop unroll change

* vulkan: edit spirv directly for asahi roll bk loop

* vulkan: remove trailing whitespace at the end of comments
adrianhoehne pushed a commit to adrianhoehne/llama.cpp that referenced this pull request Jul 5, 2026
* vulkan: roll bk loop in matmul for asahi linux

* vulkan: fix inline comment

* vulkan: revert BK-loop unroll change

* vulkan: edit spirv directly for asahi roll bk loop

* vulkan: remove trailing whitespace at the end of comments
CowboyTim pushed a commit to aardbeiplantje/llama.cpp that referenced this pull request Jul 21, 2026
* vulkan: roll bk loop in matmul for asahi linux

* vulkan: fix inline comment

* vulkan: revert BK-loop unroll change

* vulkan: edit spirv directly for asahi roll bk loop

* vulkan: remove trailing whitespace at the end of comments
zommiommy pushed a commit to zommiommy/llama.cpp that referenced this pull request Aug 18, 2026
* vulkan: roll bk loop in matmul for asahi linux

* vulkan: fix inline comment

* vulkan: revert BK-loop unroll change

* vulkan: edit spirv directly for asahi roll bk loop

* vulkan: remove trailing whitespace at the end of comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ggml changes relating to the ggml tensor library for machine learning Vulkan Issues specific to the Vulkan backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants