Widen CUDA RotaryEmbedding offset arithmetic to 64-bit - #31995
Merged
Akshay Sonawane (apsonawane) merged 3 commits intoAug 13, 2026
Merged
Akshay Sonawane (apsonawane) merged 3 commits into
Akshay Sonawane (apsonawane) merged 3 commits into
Conversation
The kernel built tensor offsets as b * in_strides.x + s * in_strides.z + n * in_strides.y with every operand a 32-bit int. Tensors may hold more than INT32_MAX elements, so these products could wrap for large batch sizes even though each individual stride fits in an int, yielding negative offsets into the input, output, and cos/sin cache pointers. Compute the offsets, b_s_index, and cache_offset in 64-bit, and verify the packed stride products fit in int32 before launching. Applies the same change to the contrib variant, which shares the pattern. 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 22:52
Copilot started reviewing on behalf of
Akshay Sonawane (apsonawane)
August 11, 2026 22:53
View session
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the CUDA RotaryEmbedding kernels to avoid 32-bit overflow when computing tensor element offsets, by widening offset arithmetic (and related indices) to 64-bit and adding pre-launch stride validation. The same pattern is applied to both the core CUDA LLM implementation and the contrib BERT variant.
Changes:
- Compute input/output base offsets, cache offsets, and related indices in
int64_tinside the CUDA kernels. - Add host-side guards to ensure packed stride products fit in
intbefore populatingint4stride structs and launching the kernel. - Apply equivalent fixes to the contrib rotary embedding implementation.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| onnxruntime/core/providers/cuda/llm/rotary_embedding_impl.cu | Widen kernel offset/index arithmetic to 64-bit and add stride overflow guards before launch. |
| onnxruntime/contrib_ops/cuda/bert/rotary_embedding_impl.cu | Mirror the 64-bit offset/index changes and stride guards for the contrib CUDA rotary embedding kernel. |
💡 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:12
Ti-Tai Wang (titaiwangms)
approved these changes
Aug 13, 2026
Akshay Sonawane (apsonawane)
deleted the
fix/cuda-rotary-embedding-offset-overflow
branch
August 13, 2026 17:53
This was referenced Sep 10, 2026
Open
This was referenced Sep 14, 2026
Open
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 kernel built tensor offsets as b * in_strides.x + s * in_strides.z + n * in_strides.y with every operand a 32-bit int. Tensors may hold more than INT32_MAX elements, so these products could wrap for large batch sizes even though each individual stride fits in an int, yielding negative offsets into the input, output, and cos/sin cache pointers.
Compute the offsets, b_s_index, and cache_offset in 64-bit, and verify the packed stride products fit in int32 before launching. Applies the same change to the contrib variant, which shares the pattern.