Repository navigation
Commit aaa31ad
authored
[Vulkan] Fuse SwiGLU elementwise operations (pytorch#22979)
### Summary
SwiGLU currently runs a sigmoid and two multiplies as separate Vulkan
dispatches. Fuse `gate * sigmoid(gate) * up` into one binary kernel. For
Qwen3-0.6B, this removes 56 dispatches per token (737 → 681). Models
need to be re-exported to use the fusion.
The matcher handles either multiply operand order and skips shared
sigmoid/SiLU intermediates. The fused op reuses the existing binary-op
broadcasting, packing, and dynamic-resize logic for FP16/FP32 buffers
and 3D textures. FP16 inputs are evaluated in FP32 with one output
conversion, so results can differ slightly from the unfused intermediate
rounding.
On an Apple M1 Pro with MoltenVK, Qwen3-0.6B W4 (group size 32, FP16
storage, context limit 1024):
| Prompt tokens | Median decode ms/token, before → after | Median paired
decode TPS gain | Median prefill ms, before → after |
| --- | --- | --- | --- |
| 41 | 20.47 → 19.52 | +4.1% | 106.5 → 106.0 |
| 768 | 21.87 → 20.59 | +6.5% | 662.5 → 656.5 |
Four alternating pairs per prompt, full warmup, and 64 measured decode
tokens per run using the same unprofiled Release runner. All eight pairs
favored fusion, with identical generated text within each prompt.
Prefill showed no regression in these runs. Both benchmark arms include
the W4 specialization from pytorch#22973 and the merged view removal from
pytorch#22898; this PR contains only SwiGLU fusion. Both runners used the same
255-token prefill chunk cap to respect the export's shape bound. Adreno
performance has not been measured.
### Test plan
All 21 tests in `backends/vulkan/test/test_vulkan_passes.py` passed. The
five new SwiGLU tests cover operand order, broadcasting, dynamic
projection ordering, shared intermediates, and mixed precision; they
were rerun after test cleanup.
All 56 cases selected by `vulkan_compute_api_test
--gtest_filter='*VulkanSwiGLUTest*'` passed on MoltenVK. Each exercises
row counts 1 → 7 → 63 → 1, covering FP16/FP32, buffers/textures, packed
layouts, odd widths, broadcasting, and the 3072-wide LLM case. Results
are checked against an independent float reference and the unfused GPU
path with half-precision rounding bounds. Full model runs also exercised
prefill chunks up to 255 followed by decode.
The new shaders and C++ dispatch code built in Release. Python/C++ lint
and formatting checks passed. GPU traces confirmed that the only
dispatch-count changes were 28 fewer sigmoid kernels, 56 fewer multiply
kernels, and 28 new SwiGLU kernels.
Authored with assistance from OpenAI Codex.
cc @SS-JIA @manuelcandales @digantdesai @cbilgin1 parent 9cced90 commit aaa31ad
12 files changed
Lines changed: 358 additions & 0 deletions
File tree
- backends/vulkan
- patterns
- runtime/graph/ops
- glsl
- impl
- test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1131 | 1131 | | |
1132 | 1132 | | |
1133 | 1133 | | |
| 1134 | + | |
| 1135 | + | |
| 1136 | + | |
| 1137 | + | |
| 1138 | + | |
| 1139 | + | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
| 1143 | + | |
| 1144 | + | |
| 1145 | + | |
1134 | 1146 | | |
1135 | 1147 | | |
1136 | 1148 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
272 | 272 | | |
273 | 273 | | |
274 | 274 | | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
275 | 285 | | |
276 | 286 | | |
277 | 287 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
| 32 | + | |
31 | 33 | | |
32 | 34 | | |
33 | 35 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
65 | 70 | | |
66 | 71 | | |
67 | 72 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
25 | 31 | | |
26 | 32 | | |
27 | 33 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
65 | 70 | | |
66 | 71 | | |
67 | 72 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
25 | 31 | | |
26 | 32 | | |
27 | 33 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
134 | 134 | | |
135 | 135 | | |
136 | 136 | | |
| 137 | + | |
137 | 138 | | |
138 | 139 | | |
139 | 140 | | |
| |||
149 | 150 | | |
150 | 151 | | |
151 | 152 | | |
| 153 | + | |
152 | 154 | | |
153 | 155 | | |
154 | 156 | | |
| |||
0 commit comments