[WebGPU] Add DynamicSparseAttention - #32529
Open
kunal-vaishnavi with Copilot wants to merge 7 commits into
Open
Conversation
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Copilot created this pull request from a session on behalf of
kunal-vaishnavi
September 10, 2026 07:11
View session
kunal-vaishnavi
marked this pull request as ready for review
September 10, 2026 07:20
kunal-vaishnavi
changed the base branch from
main
to
copilot/add-cuda-dynamicsparseattention
September 10, 2026 07:21
kunal-vaishnavi
added this pull request to stack #32531
September 10, 2026 07:21
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Dispatch padding can cause out-of-bounds access, and several loops defeat sparse-attention performance.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Adds WebGPU support for com.microsoft.DynamicSparseAttention, keeping sparse-attention processing device-resident.
Changes:
- Implements query preparation, cache updates, RoPE, normalization, and sparse attention shaders.
- Registers the WebGPU kernel.
- Adds FP16/FP32 coverage for key attention modes and features.
File summaries
| File | Description |
|---|---|
dynamic_sparse_attention.cc |
Implements WebGPU execution. |
dynamic_sparse_attention.h |
Defines kernel and shader programs. |
webgpu_contrib_kernels.cc |
Registers the kernel. |
dynamic_sparse_attention_op_test.cc |
Adds WebGPU tests. |
Review details
Suppressed comments (2)
onnxruntime/contrib_ops/webgpu/bert/dynamic_sparse_attention.cc:388
- The loop always executes
max_selectedfull QK reductions even whenselected_countis much smaller;candidate_validis checked only after the reduction. A common row with one selected token and width 4096 consequently does 4096 dot products. Clamp the device-provided count for safety and use it as the loop bound.
<< " for (var i = 0u; i < uniforms.max_selected; i++) {\n"
onnxruntime/contrib_ops/webgpu/bert/dynamic_sparse_attention.cc:240
- As in query preparation, every key-channel invocation repeats the identical full-head RMS reduction, producing O(head_size²) key loads per appended KV head. This is especially costly for token decode with few selected entries. Reduce once per
(batch, sequence, KV head)workgroup and share the inverse RMS across channel writes.
<< " for (var c = 0u; c < uniforms.head_size; c++) {\n"
<< " let kv = f32("
<< (packed_qkv_ ? query->GetByOffset("key_base + c") : key->GetByOffset("key_base + c"))
<< ");\n"
<< " k_sumsq += kv * kv;\n"
- Files reviewed: 4/4 changed files
- Comments generated: 4
- Review effort level: Balanced
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
kunal-vaishnavi
force-pushed
the
copilot/copilotadd-cuda-dynamicsparseattention
branch
from
September 10, 2026 07:41
71884ef to
2662765
Compare
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
…tention' into copilot/copilotadd-cuda-dynamicsparseattention Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
…tention' into copilot/copilotadd-cuda-dynamicsparseattention Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds WebGPU execution for
com.microsoft.DynamicSparseAttention, covering Qwen4-Exp selected-token attention and DeepSeek V4 local-plus-selected attention without host readback.int32indicesMotivation and Context
Enables model-neutral sparse-attention execution on WebGPU while keeping selection metadata processing and attention computation device-resident.