Compute SparseAttention CUDA buffer sizes and offsets in size_t - #31996
Merged
Akshay Sonawane (apsonawane) merged 4 commits intoAug 13, 2026
Conversation
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>
Akshay Sonawane (apsonawane)
requested review from
Tianlei Wu (tianleiwu)
and
a lite review from Copilot
August 11, 2026 23:00
Copilot started reviewing on behalf of
Akshay Sonawane (apsonawane)
August 11, 2026 23:00
View session
Contributor
There was a problem hiding this comment.
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 inSparseAttention::ComputeInternalto avoid pre-widening overflow. - Use
SafeInt<size_t>for Q/K/V element-count offsets and rotary buffer offset math inQkvToContext. - Add input-shape product bounds in
CheckInputsto keep Triton stride-related quantities withinint32_tlimits.
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.
Akshay Sonawane (apsonawane)
enabled auto-merge (squash)
August 12, 2026 05:05
Ti-Tai Wang (titaiwangms)
approved these changes
Aug 13, 2026
Akshay Sonawane (apsonawane)
deleted the
fix/sparse-attention-size-overflow
branch
August 13, 2026 18:53
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.