Skip to content

Compute SparseAttention CUDA buffer sizes and offsets in size_t - #31996

Merged
Akshay Sonawane (apsonawane) merged 4 commits into
mainfrom
fix/sparse-attention-size-overflow
Aug 13, 2026
Merged

Compute SparseAttention CUDA buffer sizes and offsets in size_t#31996
Akshay Sonawane (apsonawane) merged 4 commits into
mainfrom
fix/sparse-attention-size-overflow

Conversation

@apsonawane

Copy link
Copy Markdown
Contributor

The scratch allocation sizes in SparseAttention::ComputeInternal and the Q/K/V and rotary offsets in QkvToContext were evaluated as products of int shape fields and only widened afterwards, so a large batch_size or sequence_length could wrap the product before it reached GetScratchBuffer or the pointer arithmetic. Use SafeInt<size_t> so the products are computed at full width and overflow throws instead of wrapping.

The Triton kernel parameter structs take 32-bit strides, so also bound the corresponding element counts in CheckInputs to keep those strides representable.

The scratch allocation sizes in SparseAttention::ComputeInternal and the
Q/K/V and rotary offsets in QkvToContext were evaluated as products of int
shape fields and only widened afterwards, so a large batch_size or
sequence_length could wrap the product before it reached GetScratchBuffer
or the pointer arithmetic. Use SafeInt<size_t> so the products are computed
at full width and overflow throws instead of wrapping.

The Triton kernel parameter structs take 32-bit strides, so also bound the
corresponding element counts in CheckInputs to keep those strides
representable.

Co-authored-by: Copilot <223556219+Copilot@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.

Pull request overview

This PR hardens SparseAttention’s CUDA path against integer overflow by computing scratch-buffer sizes and Q/K/V/rotary offsets in size_t (via SafeInt<size_t>), and adds input validation to ensure Triton kernel stride-related products remain representable in 32-bit integers.

Changes:

  • Use SafeInt<size_t> for scratch allocation size computations in SparseAttention::ComputeInternal to avoid pre-widening overflow.
  • Use SafeInt<size_t> for Q/K/V element-count offsets and rotary buffer offset math in QkvToContext.
  • Add input-shape product bounds in CheckInputs to keep Triton stride-related quantities within int32_t limits.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
onnxruntime/contrib_ops/cuda/sparse/sparse_attention.cc Uses SafeInt<size_t> to compute scratch buffer sizes without intermediate overflow.
onnxruntime/contrib_ops/cuda/sparse/sparse_attention_impl.cu Uses SafeInt<size_t> for Q/K/V and rotary offset computations used in pointer arithmetic.
onnxruntime/contrib_ops/cpu/sparse/sparse_attention_helper.h Adds shape-product bounds checks intended to keep Triton stride-related quantities within int32_t.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread onnxruntime/contrib_ops/cpu/sparse/sparse_attention_helper.h
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.

3 participants