Repository navigation
[WebGPU] Add SparsePagedAttention - #32533
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>
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
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>
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>
…da-sparsepagedattention # Conflicts: # docs/ContribOperators.md # docs/OperatorKernels.md # onnxruntime/contrib_ops/cuda/bert/sparse_paged_attention_impl.h # onnxruntime/core/graph/contrib_ops/ms_opset.h # onnxruntime/test/contrib_ops/sparse_paged_attention_op_test.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: 1
Open (3)
In malformedcumulative_sequence_lengthcases (e.g., the token index falls beyondcum_hi),… · New Avoidusing namespace ...directives in headers because it pollutes the enclosing namespace for… · New This resolvesbatch_idxwith an O(batch_size) linear scan per token. For largebatch_sizeand… · New
…da-sparsepagedattention # Conflicts: # onnxruntime/contrib_ops/webgpu/bert/paged_attention.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>
…da-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>
### Description Optimize the WebGPU `SparsePagedAttention` execution pipeline: - write directly to the activation output for single-source attention without a head sink, avoiding FP32 partial-state allocation and the finalize dispatch - compute online-softmax scalar state once per workgroup and share it across lanes - run finalization with one workgroup per `(token, head)` and share merge coefficients across output dimensions - keep staged joint-softmax finalization for main-plus-auxiliary attention and head-sink configurations - account for the updated workgroup-memory requirements and preserve shader-cache specialization - add focused auxiliary direct-output coverage and update the WebGPU operator documentation This follows #32533, which is now merged into `main`. ### Motivation and Context The original WebGPU implementation always materialized FP32 partial softmax state and launched a separate finalize pass. It also repeated per-head scalar calculations across output lanes. These changes reduce scratch-buffer traffic and dispatch overhead for the common single-source path while reducing redundant work in staged configurations without changing attention semantics. Validation: - WGSL template tests: 89 passed - `clang-format --dry-run --Werror` on the changed C++ files - `git diff --check` - focused code review found no correctness, synchronization, bounds, or numerical issues --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com> Co-authored-by: Kunal Vaishnavi <kvaishnavi@microsoft.com>


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.