Skip to content

[Vulkan] Fuse SwiGLU elementwise operations - #22979

Draft
mergennachin wants to merge 1 commit into
mainfrom
mergennachin/vulkan-swiglu
Draft

mergennachin wants to merge 1 commit into
mainfrom
mergennachin/vulkan-swiglu

Conversation

@mergennachin

@mergennachin mergennachin commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

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 #22973 and the merged view removal from #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 @cbilgin

@mergennachin mergennachin added the release notes: vulkan Changes to the Vulkan backend delegate label Sep 21, 2026
@pytorch-bot pytorch-bot Bot added the module: vulkan Issues related to the Vulkan delegate and code under backends/vulkan/ label Sep 21, 2026
@pytorch-bot

pytorch-bot Bot commented Sep 21, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/22979

Note: Links to docs will display an error until the docs builds have been completed.

❌ 2 New Failures

As of commit 5721f54 with merge base dd8297a (image):

NEW FAILURES - The following jobs have failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 21, 2026
@meta-codesync

meta-codesync Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

@mergennachin has imported this pull request. If you are a Meta employee, you can view this in D121198447.

This branch was successfully deployed

1 active deployment
cadence 5721f54c Deployed Sep 21, 2026 by mergennachin via hifi-op-test / hifi4 #28823
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. module: vulkan Issues related to the Vulkan delegate and code under backends/vulkan/ release notes: vulkan Changes to the Vulkan backend delegate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant