[WebGPU] Add SparsePagedAttention - #32533
Open
kunal-vaishnavi with Copilot wants to merge 39 commits into
Open
Conversation
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>
…ntion' into copilot/copilotadd-cuda-sparsepagedattention 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>
Copilot created this pull request from a session on behalf of
kunal-vaishnavi
September 10, 2026 07:45
View session
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
kunal-vaishnavi
changed the base branch from
main
to
copilot/add-cuda-sparsepagedattention
September 10, 2026 07:45
kunal-vaishnavi
added this pull request to stack #32534
September 10, 2026 07:45
kunal-vaishnavi
marked this pull request as ready for review
September 10, 2026 07:45
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>
Contributor
There was a problem hiding this comment.
🔵 Needs a closer look
It introduces a new attention operator plus substantial new GPU kernels (WebGPU WGSL pipeline and CUDA implementation) that warrant final human review for long-tail correctness/perf/device-limit edge cases.
Pull request overview
This PR introduces com.microsoft.SparsePagedAttention (schema + docs) and adds Execution Provider implementations, with a staged WebGPU pipeline that reuses parts of the existing WebGPU PagedAttention prologue (packed-QKV split, RoPE, and block-table scatter), plus a functional CUDA implementation and unit tests to validate key behaviors (cache writes, selection semantics, and joint softmax).
Changes:
- Added the
SparsePagedAttentioncontrib op schema (Microsoft opset v1) and registered kernels for WebGPU and CUDA. - Implemented a staged WebGPU kernel (scatter/token-meta/main+aux partials/finalize) with explicit feature guarding and device-limit validation.
- Added deterministic unit tests and updated generated contrib-op/operator-kernel documentation, plus WebGPU/CUDA operator docs.
File summaries
| File | Description |
|---|---|
| onnxruntime/test/contrib_ops/sparse_paged_attention_op_test.cc | New deterministic tests covering WebGPU (and a couple CUDA reference cases) for selection, cache updates, and sanitization. |
| onnxruntime/python/tools/symbolic_shape_infer.py | Routes SparsePagedAttention to the existing PagedAttention symbolic inference logic. |
| onnxruntime/core/graph/contrib_ops/ms_opset.h | Adds SparsePagedAttention schema class to Microsoft opset v1 list. |
| onnxruntime/core/graph/contrib_ops/bert_defs.cc | Defines the SparsePagedAttention v1 schema and reuses PagedAttention type/shape inference. |
| onnxruntime/contrib_ops/webgpu/webgpu_contrib_kernels.cc | Registers the WebGPU SparsePagedAttention kernel and includes its header. |
| onnxruntime/contrib_ops/webgpu/bert/sparse_paged_attention.h | Declares the staged WebGPU programs and SparsePagedAttention kernel class. |
| onnxruntime/contrib_ops/webgpu/bert/sparse_paged_attention.cc | Implements WebGPU kernel dispatch, validation, stage binding-budget checks, and cache alias/copy behavior. |
| onnxruntime/contrib_ops/webgpu/bert/sparse_paged_attention_token_meta.wgsl.template | WGSL stage to compute sanitized per-token metadata on device. |
| onnxruntime/contrib_ops/webgpu/bert/sparse_paged_attention_scatter_kv.wgsl.template | WGSL stage to scatter current K/V into cache using slot_mapping. |
| onnxruntime/contrib_ops/webgpu/bert/sparse_paged_attention_main.wgsl.template | WGSL main-cache partial attention stage producing FP32 softmax state. |
| onnxruntime/contrib_ops/webgpu/bert/sparse_paged_attention_auxiliary.wgsl.template | WGSL auxiliary-cache partial attention stage producing FP32 softmax state. |
| onnxruntime/contrib_ops/webgpu/bert/sparse_paged_attention_finalize.wgsl.template | WGSL finalize stage merging partial softmax states (and optional sink) into output. |
| onnxruntime/contrib_ops/webgpu/bert/paged_attention.h | Exposes prologue dispatch helpers for reuse by SparsePagedAttention. |
| onnxruntime/contrib_ops/webgpu/bert/paged_attention.cc | Makes prologue dispatch helpers externally callable and updates internal call sites. |
| onnxruntime/contrib_ops/cuda/cuda_contrib_kernels.cc | Registers CUDA SparsePagedAttention typed kernels. |
| onnxruntime/contrib_ops/cuda/bert/sparse_paged_attention.h | Declares the CUDA SparsePagedAttention kernel class template. |
| onnxruntime/contrib_ops/cuda/bert/sparse_paged_attention.cc | Implements CUDA kernel plumbing/validation and invokes the CUDA implementation. |
| onnxruntime/contrib_ops/cuda/bert/sparse_paged_attention_impl.h | Declares the CUDA implementation entry point and enums. |
| onnxruntime/contrib_ops/cuda/bert/sparse_paged_attention_impl.cu | Implements the functional CUDA kernel and launch code for sparse attention. |
| onnxruntime/contrib_ops/cuda/bert/paged_attention_impl.h | Adds a reusable PreparePagedAttentionQueryAndCache API used by sparse attention. |
| onnxruntime/contrib_ops/cuda/bert/paged_attention_impl.cu | Implements/instantiates PreparePagedAttentionQueryAndCache. |
| docs/OperatorKernels.md | Updates generated kernel documentation to include SparsePagedAttention. |
| docs/ContribOperators.md | Updates generated contrib-op documentation to include SparsePagedAttention schema details. |
| docs/contrib_ops/webgpu/sparse_paged_attention.md | New WebGPU-specific doc describing staged execution and supported feature matrix/limits. |
| docs/contrib_ops/cuda/sparse_paged_attention.md | New CUDA-specific doc describing modes and capability matrix. |
Review details
- Files reviewed: 25/25 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
…ntion' into copilot/copilotadd-cuda-sparsepagedattention # Conflicts: # onnxruntime/contrib_ops/cuda/bert/sparse_paged_attention.cc # onnxruntime/contrib_ops/cuda/bert/sparse_paged_attention.h # onnxruntime/contrib_ops/cuda/bert/sparse_paged_attention_impl.cu # onnxruntime/test/contrib_ops/sparse_paged_attention_op_test.cc Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
…ntion' into copilot/copilotadd-cuda-sparsepagedattention Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
…ntion' into copilot/copilotadd-cuda-sparsepagedattention 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>
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>
…ntion' into copilot/copilotadd-cuda-sparsepagedattention # Conflicts: # onnxruntime/contrib_ops/cuda/bert/sparse_paged_attention_impl.h # onnxruntime/test/contrib_ops/sparse_paged_attention_op_test.cc Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
…ntion' into copilot/copilotadd-cuda-sparsepagedattention # Conflicts: # onnxruntime/contrib_ops/cuda/bert/sparse_paged_attention_impl.h # onnxruntime/test/contrib_ops/sparse_paged_attention_op_test.cc 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 Provider support for
com.microsoft.SparsePagedAttentionusing the shared CPU/CUDA schema.Motivation and Context
SparsePagedAttentionenables QSA-style selected reads and CSA-style local-plus-compressed attention over paged caches. WebGPU support allows these models to preserve the common operator ABI and device-resident scheduling semantics without provider-specific graph rewrites.