Skip to content

[CUDA] Implement SparseAttentionIndexer operator - #32526

Merged
kunal-vaishnavi merged 27 commits into
mainfrom
copilot/implement-sparse-attention-indexer
Oct 1, 2026
Merged

kunal-vaishnavi merged 27 commits into
mainfrom
copilot/implement-sparse-attention-indexer

Conversation

Copilot AI commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Description

Adds com.microsoft.SparseAttentionIndexer with CUDA implementations for QSA token selection and CSA compressed-block selection.

  • Operator contract
    • Adds schema, shape inference, symbolic shape inference, and CUDA kernel registration.
    • Supports FP32, FP16, and BF16 inputs.
  • CUDA execution
    • Implements rotary embedding, key compression, scoring, deterministic top-k selection, and cache updates.
    • Handles batched inputs, long sequences, and strided channel processing.
  • Validation and coverage
    • Rejects non-positive attention-head counts during graph resolution and execution.
    • Adds reference-based coverage for QSA, CSA, cache-only steps, batch strides, and multi-tile scans.
  • Documentation
    • Documents policy semantics, tensor contracts, cache behavior, and kernel pipelines.

Motivation and Context

Recent sparse-attention decoders require either token-level QSA indexing or stateful CSA compressed-block indexing. This operator provides both policies through one validated CUDA interface while preserving the cache state required by subsequent decoding steps.

@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 06:52
Copilot AI balanced review requested due to automatic review settings September 10, 2026 06:52

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

Attribute narrowing, zero-value semantics, CUDA edge cases, missing symbolic tests, and incomplete generated documentation remain unresolved.

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

Pull request overview

Adds the CUDA-only com.microsoft.SparseAttentionIndexer contrib operator for QSA token and CSA compressed-block selection.

Changes:

  • Defines schema, validation, shape inference, and symbolic inference.
  • Implements CUDA kernels and state-cache handling for FP32, FP16, and BF16.
  • Adds reference-based tests and operator documentation.
File summaries
File Description
onnxruntime/test/contrib_ops/sparse_attention_indexer_op_test.cc Adds shape and CUDA numerical tests.
onnxruntime/python/tools/symbolic_shape_infer.py Adds symbolic output inference.
onnxruntime/core/graph/contrib_ops/ms_opset.h Registers the schema.
onnxruntime/core/graph/contrib_ops/bert_defs.cc Defines the operator contract and shape inference.
onnxruntime/contrib_ops/cuda/sparse/sparse_attention_indexer.h Declares the CUDA kernel.
onnxruntime/contrib_ops/cuda/sparse/sparse_attention_indexer.cc Validates inputs and dispatches policies.
onnxruntime/contrib_ops/cuda/sparse/sparse_attention_indexer_impl.h Defines launch interfaces and parameters.
onnxruntime/contrib_ops/cuda/sparse/sparse_attention_indexer_impl.cu Implements CUDA pipelines.
onnxruntime/contrib_ops/cuda/cuda_contrib_kernels.cc Registers typed CUDA kernels.
onnxruntime/contrib_ops/cpu/sparse/sparse_attention_indexer_common.h Shares policy and window-planning utilities.
docs/OperatorKernels.md Lists kernel availability and types.
docs/contrib_ops/cuda/sparse_attention_indexer.md Documents semantics and implementation.
Review details

Suppressed comments (1)

onnxruntime/contrib_ops/cuda/sparse/sparse_attention_indexer.cc:65

  • token_budget and index_topk also remain unchecked int64 values even though capacity/top-k fields are later narrowed to int. For example, index_topk = 2^32 passes validation but becomes zero, producing an output shape and selection behavior that disagree with the schema-inferred capacity. Add representability checks before accepting these attributes and mirror them in shape inference.
  const bool has_token_budget = info.GetAttr<int64_t>("token_budget", &token_budget_).IsOK();
  const bool has_index_topk = info.GetAttr<int64_t>("index_topk", &index_topk_).IsOK();
  • Files reviewed: 12/12 changed files
  • Comments generated: 6
  • Review effort level: Balanced

πŸ’‘ Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@github-actions github-actions Bot 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.

You can commit the suggested changes from lintrunner.

@kunal-vaishnavi
kunal-vaishnavi force-pushed the copilot/implement-sparse-attention-indexer branch from 1c15246 to f330287 Compare September 10, 2026 07:42
@kunal-vaishnavi kunal-vaishnavi changed the title Implement SparseAttentionIndexer CUDA operator [CUDA] Implement SparseAttentionIndexer operator Sep 10, 2026
Copilot AI and others added 5 commits September 10, 2026 13:54
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>
…_infer.py

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@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 AI and others added 2 commits September 15, 2026 15:16
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>

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.

Copilot review overview

🟑 Changes recommended

CUDA indexing overflow and shared-memory configuration races remain unresolved.

Review effort: Balanced
Findings: 3 High severity

Open (3)
Resolved since last review (15)

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.

Copilot review overview

πŸ”΅ Needs a closer look

Aliased cache updates and top-k scans beyond 128 candidates need regression coverage.

Review effort: Balanced
Findings: None

Resolved since last review (3)
Previously missed (2)

In code that hasn't changed since last review

Medium severity Add aliased shared-cache tests for copy skipping and cache updates

onnxruntime/​contrib_ops/​cuda/​sparse/​sparse_attention_indexer.cc:33

The shared-cache tests check capacity, but never alias past_key and present_key. They supply past_key as a graph input and leave the output unbound; graph inputs are not reused by the allocation planner (allocation_planner.cc:643–659). Consequently, neither policy tests the copy-skipping branch or updates to the caller's cache. Add CUDA-backed QSA and CSA tests that bind both cache tensors to the same device buffer, assert pointer equality, and compare successive decoding steps with the reference, including preservation of untouched cache rows.

Medium severity Test candidate selection beyond 128 entries and tie ordering

onnxruntime/​test/​contrib_ops/​sparse_attention_indexer_op_test.cc:1112

The largest QSA numeric case has only 100 complete visible blocks, and CSA cases have at most four entries. With 128 selection threads, none reaches a second iteration of ScanForNext, so later-candidate selection and tie ordering are untested for both policies. Add reference-based cases with more than 128 visible candidates: make entries beyond index 127 win, and use equal scores with enough selected entries to cross that boundary. The suggested fixtures exercise both scenarios while retaining QSA's strided-channel coverage.

Copilot AI and others added 3 commits October 1, 2026 20:38
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
…rse-attention-indexer

# Conflicts:
#	onnxruntime/test/python/onnxruntime_test_python_symbolic_shape_infer.py

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

@github-actions github-actions Bot 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.

You can commit the suggested changes from lintrunner.

Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
@kunal-vaishnavi
kunal-vaishnavi merged commit 5bbba22 into main Oct 1, 2026
@kunal-vaishnavi
kunal-vaishnavi deleted the copilot/implement-sparse-attention-indexer branch October 1, 2026 23:35
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