Skip to content

Optimize QSA sparse attention indexing for long-context decode - #32656

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

kunal-vaishnavi with Copilot wants to merge 24 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

  • Extend SparseAttentionIndexer to support query RMS normalization, optional packed QK input, INT64 padding masks with internal causal filtering, and shared rank-2 or batched rank-3 rotary caches across CUDA and WebGPU. Regenerate the operator documentation for the updated contract.
  • Optimize dense CUDA QSA decode by preparing normalized/rotated queries once per query head, avoiding visible-index materialization for causal-prefix masks, skipping redundant past-key copies when state aliases in place, and replacing full-context sorting/repeated scans with deterministic bounded TopK selection for the common block_topk <= 512 case.
  • Optimize packed CUDA QSA by hoisting query preparation out of block scoring and using deterministic single-read block TopK selection for up to 32 selected blocks, with the correctness fallback retained for larger values.
  • Preserve score-descending/index-ascending tie ordering, exact handling of padding-mask holes, cache state semantics, and CUDA graph compatibility.
  • Add CUDA and WebGPU schema/numerical coverage, shared-rotary and packed-QK tests, long-context parity at 8K/32K/64K/128K/256K for compress_ratio=4 and token_budget=2048, CUDA graph kernel-topology and latency regression checks, and a Python decode benchmark sweep.

Motivation and Context

A 65K-context CUDA profile showed QSA index selection dominating decode time, with additional context-scaled work in visibility compaction, query preparation, cache copying, and temporary-buffer traffic. This change removes repeated TopK scans and full-row sorting, bounds selection storage and writes by the configured token budget, avoids redundant work on the common causal-prefix/in-place decode path, and keeps exact QSA output ordering and fallback behavior.

Exact query-specific scoring and padding-mask inspection still scale with the visible context because each query can change, but the selection algorithm and its intermediate traffic no longer scale with the entire candidate row. The schema updates also match exporter/model layouts without requiring expanded per-batch rotary tables or materialized per-query causal masks.

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
# Conflicts:
#	onnxruntime/contrib_ops/cuda/sparse/sparse_attention_indexer_impl.h
#	onnxruntime/test/contrib_ops/sparse_attention_indexer_op_test.cc

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

@@ -54,6 +54,7 @@ class SparseAttentionIndexerQsaSelectProgram final
{"query_row_stride", ProgramUniformVariableDataType::Uint32},

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
{"query_row_stride", ProgramUniformVariableDataType::Uint32},
{"query_row_stride", ProgramUniformVariableDataType::Uint32},

@kunal-vaishnavi kunal-vaishnavi changed the title Optimize CUDA QSA sparse attention indexing Optimize QSA sparse attention indexing for long-context decode Sep 20, 2026
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