Bound sequence_token_count in CUDA RemovePadding - #31994
Merged
Akshay Sonawane (apsonawane) merged 2 commits intoAug 13, 2026
Merged
Akshay Sonawane (apsonawane) merged 2 commits into
Akshay Sonawane (apsonawane) merged 2 commits into
Conversation
The getTokenOffset kernel used each sequence_token_count element directly as a loop bound while writing into token_offset, which holds exactly batch_size * sequence_length entries. A value above sequence_length, or a negative one in the padding loop, walked past the end of that buffer and also produced an inconsistent total token count used to size the output. Clamp each per-row count to [0, sequence_length] in the kernel and verify the sequence_token_count shape is (batch_size) before launching. Adds tests for out-of-range and negative counts. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Akshay Sonawane (apsonawane)
enabled auto-merge (squash)
August 11, 2026 22:37
Copilot started reviewing on behalf of
Akshay Sonawane (apsonawane)
August 11, 2026 22:38
View session
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens the CUDA RemovePadding contrib op against malformed sequence_token_count values by clamping per-sequence token counts inside the getTokenOffset kernel and validating the sequence_token_count input shape before kernel launch, preventing out-of-bounds writes and inconsistent output sizing.
Changes:
- Clamp each
sequence_token_count[i]to[0, sequence_length]inside the CUDAgetTokenOffsetkernel to keep writes within thetoken_offsetbuffer. - Add runtime validation that
sequence_token_counthas shape(batch_size)in the CUDA kernel wrapper before launching work. - Add unit tests covering out-of-range (too large) and negative
sequence_token_countvalues.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| onnxruntime/test/contrib_ops/remove_padding_op_test.cc | Adds test coverage for oversized and negative sequence_token_count inputs to ensure safe, clamped behavior. |
| onnxruntime/contrib_ops/cuda/bert/remove_padding.cc | Validates sequence_token_count rank/shape matches (batch_size) prior to launching CUDA kernels. |
| onnxruntime/contrib_ops/cuda/bert/bert_padding.cu | Clamps per-row token counts in getTokenOffset to prevent OOB writes and keep derived token counts consistent. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Ti-Tai Wang (titaiwangms)
approved these changes
Aug 13, 2026
Akshay Sonawane (apsonawane)
deleted the
fix/cuda-remove-padding-token-count-bounds
branch
August 13, 2026 18:49
This was referenced Sep 10, 2026
Open
This was referenced Sep 14, 2026
Open
Closed
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 getTokenOffset kernel used each sequence_token_count element directly as a loop bound while writing into token_offset, which holds exactly batch_size * sequence_length entries. A value above sequence_length, or a negative one in the padding loop, walked past the end of that buffer and also produced an inconsistent total token count used to size the output.
Clamp each per-row count to [0, sequence_length] in the kernel and verify the sequence_token_count shape is (batch_size) before launching. Adds tests for out-of-range and negative counts.