Repository navigation
[WebGPU] Add DynamicSparseAttention - #32529
Conversation
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
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>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
71884ef to
2662765
Compare
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
There was a problem hiding this comment.
Copilot review overview
π‘ Changes recommended
WebGPU shader-validation failures, incorrect cache shape inference, and an aborting no-exception test block approval.
Review effort: Balanced
Findings: 2
Open (3)
Resolved since last review (4)
Oversized 1-D dispatches are normalized into padded 2-D/3-D grids, but this shader treats everyβ¦ This iterates the configured maximum window and performs a complete QK reduction even after `indexβ¦ Each channel invocation recomputes the same full-head Q RMS sum, making normalization O(head_sizeΒ²)β¦ The new alias-dependent path is not explicitly exercised by the WebGPU tests: the generic OpTesterβ¦
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
β¦da-dynamicsparseattention # Conflicts: # onnxruntime/contrib_ops/webgpu/webgpu_contrib_kernels.cc Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Copilot review overview
Review effort: Lite
Findings: 2
Open (4)
Whenhead_sink != nullptr, the generated shader path is identical regardless of⦠· New Whenhead_sink != nullptr, the generated shader path is identical regardless of⦠· NewkAttentionValuesPerInvocationassumeskMaxHeadSizeis an exact multiple of⦠· New The same#if defined(USE_CUDA) || defined(USE_WEBGPU)guard is repeated for two adjacent include⦠· New
Resolved since last review (3)
β¦da-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>
β¦da-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>
e0c23b4
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>



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.