Skip to content

[CUDA] Add SparsePagedAttention - #32524

Merged
kunal-vaishnavi merged 20 commits into
mainfrom
copilot/add-cuda-sparsepagedattention
Oct 2, 2026
Merged

kunal-vaishnavi merged 20 commits into
mainfrom
copilot/add-cuda-sparsepagedattention

Conversation

Copilot AI commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Description

Adds com.microsoft.SparsePagedAttention, a model-neutral CUDA executor for externally selected attention indices over ORT paged KV caches.

  • Attention modes
    • Selected main-cache attention for Qwen4-Exp QSA.
    • Joint local-main and selected auxiliary attention for DeepSeek V4 CSA.
    • Single online softmax across local, selected, and sink logits.
  • Cache integration
    • Reuses PagedAttention writes, block tables, slot mapping, cache aliasing, QK-Norm, RoPE, and INT8 quantization.
    • Supports contiguous FP16/BF16 auxiliary caches and shared auxiliary K=V.
  • CUDA execution
    • Resolves request-local indices directly on device.
    • Uses fixed-capacity launches and device-resident counts for CUDA Graph compatibility.
    • Guards invalid, unmapped, out-of-range, and non-causal indices.
  • Integration
    • Adds schema and symbolic shape inference.
    • Adds CUDA correctness tests, operator documentation, and support-matrix entries.
QSA: attention_mode="selected_only", selected_kv_source="main"
CSA: attention_mode="local_plus_selected", selected_kv_source="auxiliary"

Motivation and Context

Qwen4-Exp QSA and DeepSeek V4 CSA require sparse attention over different KV lifetimes and logical index spaces. This operator separates externally produced selection metadata from paged-cache writes and attention execution without introducing model-specific policy modes.

@azure-pipelines

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

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

A validation failure can occur after mutating the KV cache, and central sparse, quantized, and joint-softmax behaviors lack effective coverage.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Adds CUDA SparsePagedAttention for externally selected indices over paged or auxiliary KV caches.

Changes:

  • Defines and registers the new contrib operator and symbolic shape inference.
  • Implements CUDA cache preparation, sparse attention, quantization, and auxiliary-cache support.
  • Adds CUDA tests and operator documentation.
File summaries
File Description
onnxruntime/test/contrib_ops/sparse_paged_attention_op_test.cc Adds basic CUDA correctness tests.
onnxruntime/python/tools/symbolic_shape_infer.py Registers symbolic inference.
onnxruntime/core/graph/contrib_ops/ms_opset.h Registers the schema.
onnxruntime/core/graph/contrib_ops/bert_defs.cc Defines the operator contract.
onnxruntime/contrib_ops/cuda/cuda_contrib_kernels.cc Registers CUDA kernels.
onnxruntime/contrib_ops/cuda/bert/sparse_paged_attention.h Declares the CUDA kernel class.
onnxruntime/contrib_ops/cuda/bert/sparse_paged_attention.cc Validates inputs and dispatches execution.
onnxruntime/contrib_ops/cuda/bert/sparse_paged_attention_impl.h Declares sparse implementation APIs.
onnxruntime/contrib_ops/cuda/bert/sparse_paged_attention_impl.cu Implements sparse attention.
onnxruntime/contrib_ops/cuda/bert/paged_attention_impl.h Exposes the shared cache prologue.
onnxruntime/contrib_ops/cuda/bert/paged_attention_impl.cu Implements and instantiates that prologue.
docs/OperatorKernels.md Documents CUDA kernel support.
docs/ContribOperators.md Documents the generated schema.
docs/contrib_ops/cuda/sparse_paged_attention.md Adds usage and support details.
Review details
  • Files reviewed: 14/14 changed files
  • Comments generated: 5
  • Review effort level: Balanced

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Copilot AI and others added 9 commits September 10, 2026 13:52
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>
@azure-pipelines

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

…sepagedattention

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.

Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
…sepagedattention

Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
…sepagedattention

# Conflicts:
#	docs/ContribOperators.md
#	docs/OperatorKernels.md
#	onnxruntime/core/graph/contrib_ops/ms_opset.h

Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
@kunal-vaishnavi
kunal-vaishnavi merged commit d32356b into main Oct 2, 2026
@kunal-vaishnavi
kunal-vaishnavi deleted the copilot/add-cuda-sparsepagedattention branch October 2, 2026 02:02
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.

5 participants

⌂ 换网址