[WebGPU] Add gating logic for subgroup shuffle primitives#18823
[WebGPU] Add gating logic for subgroup shuffle primitives#18823ksgr5566 wants to merge 16 commits intoapache:mainfrom
Conversation
Summary of ChangesHello @ksgr5566, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances WebGPU support by introducing a flexible mechanism for utilizing subgroup shuffle primitives. Recognizing that not all WebGPU devices support subgroups, the changes ensure that shared memory reductions are used as a default, universally compatible approach. For devices that do support subgroups, a new target parameter ( Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces support for subgroup shuffle primitives in WebGPU, gated by a supports_subgroups target flag. The changes are well-structured and follow the logic described. The implementation correctly enables subgroup operations when the feature is available and requested, and falls back to shared memory reductions otherwise.
I've added a couple of suggestions to improve the code. One is to refactor the target checks in lower_thread_allreduce.cc for better maintainability. The other is to make the thread_warp_size setting more flexible in target_kind.cc to allow user overrides. Overall, this is a great addition for WebGPU performance.
| if ((target_->kind->name != "cuda") && (target_->kind->name != "rocm") && | ||
| (target_->kind->name != "metal")) { | ||
| (target_->kind->name != "metal") && (target_->kind->name != "webgpu")) { | ||
| return false; | ||
| } | ||
|
|
||
| need_warp_shuffle_mask_ = target_->kind->name != "metal"; | ||
| need_warp_shuffle_mask_ = target_->kind->name != "metal" && target_->kind->name != "webgpu"; |
There was a problem hiding this comment.
To improve maintainability, consider using std::unordered_set for checking the target kind. This makes it easier to add or remove supported targets in the future.
| if ((target_->kind->name != "cuda") && (target_->kind->name != "rocm") && | |
| (target_->kind->name != "metal")) { | |
| (target_->kind->name != "metal") && (target_->kind->name != "webgpu")) { | |
| return false; | |
| } | |
| need_warp_shuffle_mask_ = target_->kind->name != "metal"; | |
| need_warp_shuffle_mask_ = target_->kind->name != "metal" && target_->kind->name != "webgpu"; | |
| const std::unordered_set<std::string> supported_targets = {"cuda", "rocm", "metal", "webgpu"}; | |
| if (!supported_targets.count(target_->kind->name)) { | |
| return false; | |
| } | |
| const std::unordered_set<std::string> no_mask_targets = {"metal", "webgpu"}; | |
| need_warp_shuffle_mask_ = !no_mask_targets.count(target_->kind->name); |
There was a problem hiding this comment.
Pull request overview
Adds compile-time gating for WebGPU subgroup shuffle generation by introducing a supports_subgroups target attribute and wiring it through TIR lowering, intrinsic lowering, and WGSL codegen so subgroup ops are only emitted when explicitly enabled.
Changes:
- Add WebGPU target parsing/attrs to default
thread_warp_size=1and setthread_warp_size=32whensupports_subgroups=true. - Extend
LowerThreadAllreduceand WebGPU intrinsic rules to allow warp shuffle-based reductions on WebGPU when enabled. - Emit
enable subgroups;in WGSL whensupports_subgroupsis enabled; request thesubgroupsdevice feature in the web runtime when available.
Reviewed changes
Copilot reviewed 6 out of 7 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| web/src/webgpu.ts | Requests subgroups feature when the adapter advertises support. |
| web/package-lock.json | Updates locked JS dependencies. |
| src/tir/transforms/lower_thread_allreduce.cc | Enables warp reduction path for WebGPU and adjusts shuffle lowering behavior. |
| src/target/target_kind.cc | Adds supports_subgroups + WebGPU target parser to set thread_warp_size. |
| src/target/source/intrin_rule_webgpu.cc | Lowers tvm_warp_shuffle* to WGSL subgroupShuffle* intrinsics. |
| src/target/source/codegen_webgpu.h | Adds enable_subgroups_ state to the WebGPU codegen. |
| src/target/source/codegen_webgpu.cc | Emits enable subgroups; directive based on target attrs. |
Files not reviewed (1)
- web/package-lock.json: Language not supported
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
ea41337 to
2cba8b3
Compare
2cba8b3 to
f119bbd
Compare
|
Also there are failing tests. Please take a look and resolve them. |
|
@tvm-bot rerun |
|
Failed to re-run CI in https://github.com/apache/tvm/actions/runs/22498616486 Detailswith response |
…st for all shuffle ops
|
cc @tqchen for review |
|
@MasterJH5574 would appreciate it if you could take a look. Thanks! |
|
BTW just a quick note. @ksgr5566 you can resolve all addressed comments, which helps make the PR page cleaner. |
Subgroup support is decided by the user’s compile flag. Automatic device detection is not part of this compile path. It is only possible during WebGPU runtime in the browser, and WebGPU can then select the relevant compiled WASM. |
Summary
This adds gating logic on top of #17699 to support optional subgroup shuffle
primitives based on a compile-time flag.
Problem
The PR #17699 always generates subgroup shuffle ops when targeting WebGPU.
However, not all WebGPU devices support subgroups. We need a way to:
Solution
Implement gating via TVM target parameter:
thread_warp_size=1disables warp reductions (uses shared memory + barriers)UpdateWebGPUAttrs()that setsthread_warp_size=32whensupports_subgroups=true--enable-subgroupsCLI flag in mlc-llm to surface the option to usersThe gating happens at the reduction path selection level (
IsWarpReduction()inlower_thread_allreduce.cc), ensuring subgroup ops are never generated unless explicitly enabled.Changes
Testing
Tested with Llama-3.2-1B-q4f16_1. Baseline (no flag) uses shared memory reductions;
with flag, generates subgroupShuffle* ops.
Both the generated WGSLs here: https://gist.github.com/ksgr5566/301664a5dda3e46f44092be4d09b2d4f
Benchmarking: https://gist.github.com/ksgr5566/c9bd5bc5aadba999ec2f2c38eb0c49b3