Skip to content

Widen CUDA RotaryEmbedding offset arithmetic to 64-bit - #31995

Merged
Akshay Sonawane (apsonawane) merged 3 commits into
mainfrom
fix/cuda-rotary-embedding-offset-overflow
Aug 13, 2026
Merged

Akshay Sonawane (apsonawane) merged 3 commits into
mainfrom
fix/cuda-rotary-embedding-offset-overflow

Conversation

@apsonawane

Copy link
Copy Markdown
Contributor

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.

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>

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 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_t inside the CUDA kernels.
  • Add host-side guards to ensure packed stride products fit in int before populating int4 stride 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.

Comment thread onnxruntime/core/providers/cuda/llm/rotary_embedding_impl.cu Outdated
Comment thread onnxruntime/contrib_ops/cuda/bert/rotary_embedding_impl.cu Outdated
@apsonawane
Akshay Sonawane (apsonawane) merged commit c245d92 into main Aug 13, 2026
91 checks passed
@apsonawane
Akshay Sonawane (apsonawane) deleted the fix/cuda-rotary-embedding-offset-overflow branch August 13, 2026 17:53
This was referenced Sep 10, 2026
This was referenced Sep 14, 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.

3 participants