Repository navigation
vulkan: skip unneeded MoE work in mul_mm coopmat1 path - #25483
Conversation
|
I ran this locally on RTX 5090 and it's actually a bit slower in some models: This is not necessarily a blocker since NVIDIA devices should be using the coopmat2 path. I didn't debug why it's slower, but my first guess is that the branch prevents the A and B loads from happening at the same time. I got a lot of mileage out of this kind of optimization in the coopmat2 path, but there it also changed how the loads and calculations are spread across warps. It might be beneficial to do something like that here, too, but GLSL is not the ideal language for it since it doesn't support writing templated functions. |
Thanks. I ran this on an Intel discrete GPU (B70) and saw the perf gain as well. It seems that on Intel GPUs, the branching cost is lower than the benefit it provides.
build: bec4772 (9913) After:
build: f63220399 (9914) |
|
I ran this locally on a Radeon RX 7900 XT and saw some gain, though not as much as on the Intel GPU. Before:
After:
|
0cc4m
left a comment
There was a problem hiding this comment.
I can measure neutral performance on Nvidia, and performance improvements on AMD. Looks good, thank you!
|
It seems this needs another approval from a maintainer. @jeffbolznv, could you please check the PR again? |
|
Is this still needed after #26686? If you rebase it is there still any perf gain? |
4ca667f to
0d910cd
Compare
Nevermind, I thought part of this change was in the load_row_ids path. Reviewing now... |
It does not, I marked it as merge ready, it's in the queue. |
|
I'm not completely happy with the state of this one, let me see if I can find a reasonable improvement to suggest. |
| load_a_to_shmem(pos_a, loadr_a, loadc_a + l, ir * BM + loadc_a + l, block, end_k); | ||
| } | ||
| #ifdef MUL_MAT_ID | ||
| if (gl_LocalInvocationID.x < required_work_items) { |
There was a problem hiding this comment.
It seems weird to have this outside the loop, and then if the loop does multiple iterations the iterations after the first are unnecessary. But putting it inside the loop would mean the compiler can't batch the loads, so that's not great either. Also, this prevents batching with the A loads (the issue I had previously mentioned). All together this makes me feel like this is not the right way to go about this change.
My best suggestion is still to do something similar to what the coopmat2 shader does - check if the number of rows is <= 1/2 or <= 1/4 of BN and have a specialized code path for each of those, where the B load is fully spread across all invocations, and the A load is in the same branch so it can be batched with the B loads.
There was a problem hiding this comment.
Thanks for your suggestion, @jeffbolznv.
I rebased the code and used Claude to make code changes similar to what the coopmat2 shader does. The changes are available here: fish-jiang@8f7945f
Could you please help check any implementation issue?
Below are my performance test results on the Intel B70 and AMD Radeon RX 7900 XT:
- Intel B70: Noticeable performance drop (~11% for Qwen3.6_35B_A3B) for some models compared to this PR.
- AMD RX 7900 XT: Flat performance data.
Intel B70
base build: 7a46b4b72 (10831)
llama-bench.exe -p 512 -n 0 -r 5 -fa 0,1 --delay 10 -ngl 99 -m C:\kernel\model\gpt-oss-20b-Q4_K_M.gguf,C:\Users\gta\Downloads\model\gemma-4-26B-A4B-it-UD-Q4_K_M.gguf,C:\kernel\model\Qwen3.6-35B-A3B-UD-Q4_K_M.gguf
| model | size | params | backend | ngl | fa | test | t/s |
|---|---|---|---|---|---|---|---|
| gpt-oss 20B Q4_K - Medium | 10.81 GiB | 20.91 B | Vulkan | 99 | 0 | pp512 | 3040.26 ± 50.97 |
| gpt-oss 20B Q4_K - Medium | 10.81 GiB | 20.91 B | Vulkan | 99 | 1 | pp512 | 3066.65 ± 30.87 |
| gemma4 26B.A4B Q4_K - Medium | 15.77 GiB | 25.23 B | Vulkan | 99 | 0 | pp512 | 2700.69 ± 40.67 |
| gemma4 26B.A4B Q4_K - Medium | 15.77 GiB | 25.23 B | Vulkan | 99 | 1 | pp512 | 2374.27 ± 25.83 |
| qwen35moe 35B.A3B Q4_K - Medium | 20.60 GiB | 34.66 B | Vulkan | 99 | 0 | pp512 | 2157.81 ± 20.31 |
| qwen35moe 35B.A3B Q4_K - Medium | 20.60 GiB | 34.66 B | Vulkan | 99 | 1 | pp512 | 2121.66 ± 43.77 |
This PR:
| model | size | params | backend | ngl | fa | test | t/s |
|---|---|---|---|---|---|---|---|
| gpt-oss 20B Q4_K - Medium | 10.81 GiB | 20.91 B | Vulkan | 99 | 0 | pp512 | 3241.69 ± 72.20 |
| gpt-oss 20B Q4_K - Medium | 10.81 GiB | 20.91 B | Vulkan | 99 | 1 | pp512 | 3306.12 ± 19.81 |
| gemma4 26B.A4B Q4_K - Medium | 15.77 GiB | 25.23 B | Vulkan | 99 | 0 | pp512 | 3133.23 ± 84.52 |
| gemma4 26B.A4B Q4_K - Medium | 15.77 GiB | 25.23 B | Vulkan | 99 | 1 | pp512 | 2708.86 ± 9.08 |
| qwen35moe 35B.A3B Q4_K - Medium | 20.60 GiB | 34.66 B | Vulkan | 99 | 0 | pp512 | 2668.74 ± 30.60 |
| qwen35moe 35B.A3B Q4_K - Medium | 20.60 GiB | 34.66 B | Vulkan | 99 | 1 | pp512 | 2626.17 ± 21.98 |
commit: fish-jiang@8f7945f:
| model | size | params | backend | ngl | fa | test | t/s |
|---|---|---|---|---|---|---|---|
| gpt-oss 20B Q4_K - Medium | 10.81 GiB | 20.91 B | Vulkan | 99 | 0 | pp512 | 3314.72 ± 64.80 |
| gpt-oss 20B Q4_K - Medium | 10.81 GiB | 20.91 B | Vulkan | 99 | 1 | pp512 | 3363.04 ± 34.97 |
| gemma4 26B.A4B Q4_K - Medium | 15.77 GiB | 25.23 B | Vulkan | 99 | 0 | pp512 | 2960.98 ± 54.47 |
| gemma4 26B.A4B Q4_K - Medium | 15.77 GiB | 25.23 B | Vulkan | 99 | 1 | pp512 | 2565.53 ± 23.82 |
| qwen35moe 35B.A3B Q4_K - Medium | 20.60 GiB | 34.66 B | Vulkan | 99 | 0 | pp512 | 2357.85 ± 24.48 |
| qwen35moe 35B.A3B Q4_K - Medium | 20.60 GiB | 34.66 B | Vulkan | 99 | 1 | pp512 | 2325.59 ± 26.90 |
AMD RX 7900 XT
Base
| model | size | params | backend | ngl | fa | test | t/s |
|---|---|---|---|---|---|---|---|
| gemma4 26B.A4B Q3_K - Medium | 11.84 GiB | 25.23 B | Vulkan | 99 | 0 | pp512 | 2999.94 ± 50.29 |
| gemma4 26B.A4B Q3_K - Medium | 11.84 GiB | 25.23 B | Vulkan | 99 | 1 | pp512 | 3020.03 ± 72.54 |
| gpt-oss 20B Q4_K - Medium | 10.81 GiB | 20.91 B | Vulkan | 99 | 0 | pp512 | 3653.29 ± 84.19 |
| gpt-oss 20B Q4_K - Medium | 10.81 GiB | 20.91 B | Vulkan | 99 | 1 | pp512 | 3755.54 ± 31.25 |
| qwen35moe 35B.A3B Q2_K - Medium | 11.70 GiB | 35.51 B | Vulkan | 99 | 0 | pp512 | 2798.03 ± 43.30 |
| qwen35moe 35B.A3B Q2_K - Medium | 11.70 GiB | 35.51 B | Vulkan | 99 | 1 | pp512 | 2817.48 ± 60.68 |
This PR:
| model | size | params | backend | ngl | fa | test | t/s |
|---|---|---|---|---|---|---|---|
| gemma4 26B.A4B Q3_K - Medium | 11.84 GiB | 25.23 B | Vulkan | 99 | 0 | pp512 | 3162.40 ± 59.93 |
| gemma4 26B.A4B Q3_K - Medium | 11.84 GiB | 25.23 B | Vulkan | 99 | 1 | pp512 | 3184.39 ± 63.63 |
| gpt-oss 20B Q4_K - Medium | 10.81 GiB | 20.91 B | Vulkan | 99 | 0 | pp512 | 3714.91 ± 63.39 |
| gpt-oss 20B Q4_K - Medium | 10.81 GiB | 20.91 B | Vulkan | 99 | 1 | pp512 | 3814.46 ± 24.63 |
| qwen35moe 35B.A3B Q2_K - Medium | 11.70 GiB | 35.51 B | Vulkan | 99 | 0 | pp512 | 2961.78 ± 56.02 |
| qwen35moe 35B.A3B Q2_K - Medium | 11.70 GiB | 35.51 B | Vulkan | 99 | 1 | pp512 | 2986.64 ± 74.84 |
commit: fish-jiang@8f7945f:
| model | size | params | backend | ngl | fa | test | t/s |
|---|---|---|---|---|---|---|---|
| gemma4 26B.A4B Q3_K - Medium | 11.84 GiB | 25.23 B | Vulkan | 99 | 0 | pp512 | 3167.27 ± 50.98 |
| gemma4 26B.A4B Q3_K - Medium | 11.84 GiB | 25.23 B | Vulkan | 99 | 1 | pp512 | 3202.26 ± 68.92 |
| gpt-oss 20B Q4_K - Medium | 10.81 GiB | 20.91 B | Vulkan | 99 | 0 | pp512 | 3705.70 ± 142.58 |
| gpt-oss 20B Q4_K - Medium | 10.81 GiB | 20.91 B | Vulkan | 99 | 1 | pp512 | 3800.70 ± 70.85 |
| qwen35moe 35B.A3B Q2_K - Medium | 11.70 GiB | 35.51 B | Vulkan | 99 | 0 | pp512 | 2941.44 ± 54.04 |
| qwen35moe 35B.A3B Q2_K - Medium | 11.70 GiB | 35.51 B | Vulkan | 99 | 1 | pp512 | 2919.02 ± 73.72 |
There was a problem hiding this comment.
As I mentioned in the other comment, I didn't see much difference from this, so maybe the load batching is not a big deal, and you can stick with a simpler version. I think it would make sense for the if (gl_LocalInvocationID.x < required_work_items) { path to not do the loop if not needed.
There was a problem hiding this comment.
As I mentioned in the other comment, I didn't see much difference from this, so maybe the load batching is not a big deal, and you can stick with a simpler version. I think it would make sense for the
if (gl_LocalInvocationID.x < required_work_items) {path to not do the loop if not needed.
I tried moving the check inside the loop. Based on limited test data, the perf is roughly on par with the current state of this PR. And it would mean the compiler can't batch the loads — so maybe the current version is already a reasonable balance, and it keeps the code simple.
| } | ||
| } | ||
| #ifdef MUL_MAT_ID | ||
| } |
There was a problem hiding this comment.
| } | |
| } else { | |
| // zero sums on unused threads, to help register allocations | |
| [[unroll]] for (uint i = 0; i < cms_per_row * cms_per_col; i++) { | |
| sums[i] = coopmat<ACC_TYPE, gl_ScopeSubgroup, TM, TN, gl_MatrixUseAccumulator>(0.0f); | |
| } | |
| } |
I spent some time looking at the different load_b changes, and they didn't make much of a difference on my system, and I eventually realized it's the if (warp_c < required_warp_c) { that's slowing things down. I didn't dig into the assembly, but I suspect what happens is the branch requires the compiler to keep the old sums alive across the branch and phi them together after, and it's better to just clear them to zero to indicate to the compiler that they aren't needed (might also be possible to set them to an uninitialized value, I didn't try it).
There was a problem hiding this comment.
Thanks for the code suggestion. I applied the change and got the perf data on Intel B70 and AMD RX 7900 XT. Looks like there is some slight perf regression.
Intel B70:
| model | fa | before (This PR) t/s | after (+ sums-zero suggestion) t/s | delta |
|---|---|---|---|---|
| gpt-oss 20B Q4_K - Medium | 0 | 3241.69 ± 72.20 | 3231.51 ± 54.04 | -0.31% |
| gpt-oss 20B Q4_K - Medium | 1 | 3306.12 ± 19.81 | 3278.87 ± 31.30 | -0.82% |
| gemma4 26B.A4B Q4_K - Medium | 0 | 3133.23 ± 84.52 | 3146.39 ± 36.20 | +0.42% |
| gemma4 26B.A4B Q4_K - Medium | 1 | 2708.86 ± 9.08 | 2718.60 ± 33.23 | +0.36% |
| qwen35moe 35B.A3B Q4_K - Medium | 0 | 2668.74 ± 30.60 | 2663.57 ± 12.53 | -0.19% |
| qwen35moe 35B.A3B Q4_K - Medium | 1 | 2626.17 ± 21.98 | 2575.35 ± 26.12 | -1.94% |
AMD RX 7900 XT:
| model | fa | before (This PR) t/s | after (+sums-zero suggestion) t/s | delta |
|---|---|---|---|---|
| gemma4 26B.A4B Q3_K - Medium | 0 | 3162.40 ± 59.93 | 3107.71 ± 61.89 | -1.73% |
| gemma4 26B.A4B Q3_K - Medium | 1 | 3184.39 ± 63.63 | 3105.13 ± 61.12 | -2.49% |
| gpt-oss 20B Q4_K - Medium | 0 | 3714.91 ± 63.39 | 3572.79 ± 66.95 | -3.83% |
| gpt-oss 20B Q4_K - Medium | 1 | 3814.46 ± 24.63 | 3667.72 ± 35.85 | -3.85% |
| qwen35moe 35B.A3B Q2_K - Medium | 0 | 2961.78 ± 56.02 | 2898.88 ± 56.39 | -2.12% |
| qwen35moe 35B.A3B Q2_K - Medium | 1 | 2986.64 ± 74.84 | 2914.54 ± 66.72 | -2.41% |
| load_a_to_shmem(pos_a, loadr_a, loadc_a + l, ir * BM + loadc_a + l, block, end_k); | ||
| } | ||
| #ifdef MUL_MAT_ID | ||
| if (gl_LocalInvocationID.x < required_work_items) { |
There was a problem hiding this comment.
As I mentioned in the other comment, I didn't see much difference from this, so maybe the load batching is not a big deal, and you can stick with a simpler version. I think it would make sense for the if (gl_LocalInvocationID.x < required_work_items) { path to not do the loop if not needed.
|
At this point, since the perf regression on NVIDIA is understood, I withdraw my objection. I have a slight preference for applying my suggestion of zeroing the accumulators, but I'll leave it to you and @0cc4m to decide what to do. |
Thanks @jeffbolznv. Hi @0cc4m, what is your opinion? |
|
Hi @0cc4m , could you please check if this is good to move forward, or let me know if there's anything else you'd like looked at first? |
|
I think we should just follow the AMD/Intel benchmark in this case, since those are the primary consumers of the shader. That would mean keeping it as is. |
|
Thanks @0cc4m, then, is it ok to mark this PR as merge ready now? Or do I need to rebase it first? |
Overview
For
MUL_MAT_IDon the coopmat1 path, each workgroup's real row count (_ne1 - ic*BN) is frequently less than the fullBN/WNtile once expert routing is accounted for. This addsrequired_work_items/required_warp_cbounds computed right after the existing "workgroup has no work" early-return, and uses them to skip the B-matrix memory load and the coopmat compute loop for subgroups/warps beyond what's actually needed for that workgroup.Scope note: this is split out of #24407
Performance (Panther Lake B390 + Windows OS)
BEFORE:
C:\Users\dungeon\Desktop\fish\llama.cpp\build\bin\Release>llama-bench.exe -p 8192 -n 0 -r 2 -fa 0,1 --delay 10 -ngl 99 -m C:\Users\dungeon\Desktop\models\Qwen3.5-35B-A3B-Q4_K_M\Qwen3.5-35B-A3B-Q4_K_M.gguf,C:\Users\dungeon\Desktop\models\gpt-oss-20b-Q4_K_M\gpt-oss-20b-Q4_K_M.gguf,C:\Users\dungeon\Desktop\models\gemma-4-26B-A4B-it-UD-Q4_K_M\gemma-4-26B-A4B-it-UD-Q4_K_M.gguf,C:\Users\dungeon\Desktop\models\Qwen3-Coder-30B-A3B-Instruct-Q4_K_M\Qwen3-Coder-30B-A3B-Instruct-Q4_K_M.gguf
ggml_vulkan: Found 1 Vulkan devices:
ggml_vulkan: 0 = Intel(R) Arc(TM) B390 GPU (Intel Corporation) | uma: 1 | fp16: 1 | bf16: 0 | warp size: 32 | shared memory: 49152 | int dot: 1 | matrix cores: KHR_coopmat
build: bec4772 (9913)
AFTER:
C:\Users\dungeon\Desktop\fish\llama.cpp\build_moe\bin\Release>llama-bench.exe -p 8192 -n 0 -r 2 -fa 0,1 --delay 10 -ngl 99 -m C:\Users\dungeon\Desktop\models\Qwen3.5-35B-A3B-Q4_K_M\Qwen3.5-35B-A3B-Q4_K_M.gguf,C:\Users\dungeon\Desktop\models\gpt-oss-20b-Q4_K_M\gpt-oss-20b-Q4_K_M.gguf,C:\Users\dungeon\Desktop\models\gemma-4-26B-A4B-it-UD-Q4_K_M\gemma-4-26B-A4B-it-UD-Q4_K_M.gguf,C:\Users\dungeon\Desktop\models\Qwen3-Coder-30B-A3B-Instruct-Q4_K_M\Qwen3-Coder-30B-A3B-Instruct-Q4_K_M.gguf
ggml_vulkan: Found 1 Vulkan devices:
ggml_vulkan: 0 = Intel(R) Arc(TM) B390 GPU (Intel Corporation) | uma: 1 | fp16: 1 | bf16: 0 | warp size: 32 | shared memory: 49152 | int dot: 1 | matrix cores: KHR_coopmat
build: f63220399 (9914)
Requirements
I have read and agree with the contributing guidelines
AI usage disclosure: YES, used claude code, then lots of manual review/tweaking.