Skip to content

Optimize CUDA QSA sparse attention indexing - #32656

Draft
kunal-vaishnavi with Copilot wants to merge 20 commits into
copilot/copilotimplement-packed-sparse-attention-indexerfrom
copilot/copilotcopilotimplement-packed-sparse-attention-in
Draft

kunal-vaishnavi with Copilot wants to merge 20 commits into
copilot/copilotimplement-packed-sparse-attention-indexerfrom
copilot/copilotcopilotimplement-packed-sparse-attention-in

Conversation

Copilot AI commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Description

  • Add deterministic single-pass TopK selection for up to 32 blocks, retaining the existing fallback for larger values.
  • Fuse query rotation into dense and packed QSA scoring kernels.
  • Remove the intermediate FP32 rotated-query workspace and separate rotation launch.
  • Add focused TopK coverage and update performance documentation.
  • Sync with the latest packed sparse attention indexer branch while limiting the source-to-head diff to the intended optimization files.

Motivation and Context

SparseAttentionIndexer(policy_mode="qsa") repeatedly scanned all blocks for each TopK result and materialized rotated queries in FP32 workspace. These changes reduce selection work, workspace traffic, and kernel-launch overhead while preserving deterministic ordering and existing operator contracts.

Copilot AI and others added 2 commits September 16, 2026 20:15
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.

@kunal-vaishnavi
kunal-vaishnavi changed the base branch from main to copilot/copilotimplement-packed-sparse-attention-indexer September 16, 2026 20:21
@kunal-vaishnavi
kunal-vaishnavi added this pull request to stack #32530 September 16, 2026 20:21
@kunal-vaishnavi
kunal-vaishnavi marked this pull request as ready for review September 16, 2026 20:21
Copilot AI balanced review requested due to automatic review settings September 16, 2026 20: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

The dense scoring kernel can exceed CUDA shared-memory limits, and TopK boundary coverage is incomplete.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Optimizes CUDA QSA scoring and deterministic TopK selection while reducing workspace usage.

Changes:

  • Fuses query rotation into dense and packed scoring kernels.
  • Adds single-pass TopK for up to 32 blocks.
  • Updates CUDA tests and performance documentation.
File summaries
File Description
sparse_attention_indexer_op_test.cc Adds focused TopK coverage.
sparse_attention_indexer_impl.cu Optimizes dense CUDA QSA execution.
sparse_attention_indexer_device_math.cuh Adds deterministic block TopK.
packed_sparse_attention_indexer_impl.cu Applies optimizations to packed QSA.
packed_sparse_attention_indexer.md Documents packed CUDA performance.
cuda/sparse_attention_indexer.md Updates optimization limitations.
Review details
  • Files reviewed: 6/6 changed files
  • Comments generated: 2
  • Review effort level: Balanced

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

Comment thread onnxruntime/contrib_ops/cuda/sparse/sparse_attention_indexer_impl.cu Outdated
Comment thread onnxruntime/test/contrib_ops/sparse_attention_indexer_op_test.cc Outdated
Copilot AI and others added 2 commits September 16, 2026 21:33
…sparse-attention-indexer' into copilot/copilotcopilotimplement-packed-sparse-attention-in

Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
…sparse-attention-indexer' into copilot/copilotcopilotimplement-packed-sparse-attention-in

Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Copilot AI and others added 3 commits September 17, 2026 10:08
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>

@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.

Comment on lines 760 to 764
const SparseAttentionIndexerParams&, \
const T*, const T*, const T*, const T*, const T*, \
const bool*, const T*, int32_t*, T*, float*, int32_t*); \
template Status LaunchCsaSparseAttentionIndexer<T>( \
cudaStream_t, const SparseAttentionIndexerParams&, const T*, const T*, const T*, const T*, const T*, \

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.

Suggested change
const SparseAttentionIndexerParams&, \
const T*, const T*, const T*, const T*, const T*, \
const bool*, const T*, int32_t*, T*, float*, int32_t*); \
template Status LaunchCsaSparseAttentionIndexer<T>( \
cudaStream_t, const SparseAttentionIndexerParams&, const T*, const T*, const T*, const T*, const T*, \
const SparseAttentionIndexerParams&, \
const T*, const T*, const T*, const T*, const T*, \
const bool*, const T*, int32_t*, T*, float*, int32_t*); \
template Status LaunchCsaSparseAttentionIndexer<T>( \
cudaStream_t, const SparseAttentionIndexerParams&, const T*, const T*, const T*, const T*, const T*, \

template Status LaunchQsaSparseAttentionIndexer<T>(cudaStream_t, const SparseAttentionIndexerParams&, \
const T*, const T*, const T*, const T*, const T*, \
const bool*, const T*, int32_t*, T*, float*, int32_t*); \
#define INSTANTIATE_SPARSE_ATTENTION_INDEXER(T) \

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.

Suggested change
#define INSTANTIATE_SPARSE_ATTENTION_INDEXER(T) \
#define INSTANTIATE_SPARSE_ATTENTION_INDEXER(T) \

Copilot AI and others added 2 commits September 17, 2026 21:33
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
Co-authored-by: kunal-vaishnavi <115581922+kunal-vaishnavi@users.noreply.github.com>
@@ -0,0 +1,114 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import argparse
import time

import numpy as np
import onnx
import time

import numpy as np
import onnx
import time

import numpy as np
import onnx

@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.

Comment on lines +8 to +9
import onnx
import onnxruntime as ort

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.

Suggested change
import onnx
import onnxruntime as ort
import onnxruntime as ort

# Conflicts:
#	docs/contrib_ops/cuda/sparse_attention_indexer.md
#	onnxruntime/contrib_ops/cuda/sparse/packed_sparse_attention_indexer_impl.cu
#	onnxruntime/contrib_ops/cuda/sparse/sparse_attention_indexer_impl.cu
#	onnxruntime/contrib_ops/webgpu/bert/sparse_attention_indexer.cc
#	onnxruntime/test/contrib_ops/sparse_attention_indexer_op_test.cc
# Conflicts:
#	docs/ContribOperators.md
#	docs/contrib_ops/cuda/sparse_attention_indexer.md
#	docs/contrib_ops/webgpu/sparse_attention_indexer.md
#	onnxruntime/contrib_ops/cuda/sparse/sparse_attention_indexer.cc
#	onnxruntime/contrib_ops/cuda/sparse/sparse_attention_indexer_impl.cu
#	onnxruntime/contrib_ops/cuda/sparse/sparse_attention_indexer_impl.h
#	onnxruntime/contrib_ops/webgpu/bert/sparse_attention_indexer.cc
#	onnxruntime/core/graph/contrib_ops/bert_defs.cc
#	onnxruntime/test/contrib_ops/sparse_attention_indexer_op_test.cc
# Conflicts:
#	onnxruntime/contrib_ops/cuda/sparse/sparse_attention_indexer_impl.cu
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.

4 participants