Skip to content

[WebGPU] Add DynamicSparseAttention - #32529

Merged
kunal-vaishnavi merged 48 commits into
mainfrom
copilot/copilotadd-cuda-dynamicsparseattention
Oct 3, 2026
Merged

kunal-vaishnavi merged 48 commits into
mainfrom
copilot/copilotadd-cuda-dynamicsparseattention

Conversation

Copilot AI commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Description

Adds WebGPU execution for com.microsoft.DynamicSparseAttention, covering Qwen4-Exp selected-token attention and DeepSeek V4 local-plus-selected attention without host readback.

  • Attention
    • Selected main-cache and local-plus-auxiliary modes
    • Joint FP32 stable softmax with optional sink
    • Tiled QK reductions and guarded int32 indices
  • QKV and cache
    • Packed and unpacked QKV
    • GQA head mapping
    • Alias-safe contiguous cache updates
    • Shared auxiliary K/V storage
  • Position processing
    • QK RMSNorm
    • Partial and interleaved RoPE
    • Rotary offsets and explicit position IDs
  • Coverage
    • FP32 and FP16
    • Cache append, sparse/empty selections, padding, invalid-index guards, hybrid attention, sinks, QK norm, and RoPE

Motivation and Context

Enables model-neutral sparse-attention execution on WebGPU while keeping selection metadata processing and attention computation device-resident.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@kunal-vaishnavi
kunal-vaishnavi marked this pull request as ready for review September 10, 2026 07:20
Copilot AI balanced review requested due to automatic review settings September 10, 2026 07:20
@kunal-vaishnavi
kunal-vaishnavi changed the base branch from main to copilot/add-cuda-dynamicsparseattention September 10, 2026 07:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟑 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_selected full QK reductions even when selected_count is much smaller; candidate_valid is 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.

Copilot AI and others added 8 commits September 10, 2026 00:41
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>
@kunal-vaishnavi
kunal-vaishnavi force-pushed the copilot/copilotadd-cuda-dynamicsparseattention branch from 71884ef to 2662765 Compare September 10, 2026 07:41
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 kunal-vaishnavi changed the title Add WebGPU DynamicSparseAttention [WebGPU] Add DynamicSparseAttention Sep 10, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Medium severity Β· 2 Low severity

Open (4)
Resolved since last review (3)

Copilot AI and others added 2 commits October 2, 2026 18:32
…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>
Copilot AI and others added 2 commits October 2, 2026 21:11
…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>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
@kunal-vaishnavi
kunal-vaishnavi merged commit 0aecb6c into main Oct 3, 2026
@kunal-vaishnavi
kunal-vaishnavi deleted the copilot/copilotadd-cuda-dynamicsparseattention branch October 3, 2026 02:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants