Clamp 1D attention mask_index to valid bounds - #29449
Merged
Merged
Conversation
Use std::clamp for end_position and start_position in the 1D mask_index branch of attention_helper.h so out-of-range mask values cannot drive out-of-bounds writes into the mask buffer. Add AttentionMaskIndex1DClampOOB test covering an over-large end_position and a negative start_position.
apsonawane
approved these changes
Jul 2, 2026
tianleiwu
added a commit
that referenced
this pull request
Jul 7, 2026
# PR: Clamp 1D attention mask_index to valid bounds ## Description The CPU attention helper translates a 1D `mask_index` (right-side end position, and optionally a left-side start position) into per-token mask-fill loop bounds. The existing `std::max(0, std::min(...))` clamp was correct but verbose; this PR replaces it with `std::clamp` and adds a regression test that exercises out-of-range mask values. This hardens the kernel against out-of-bounds writes when callers supply mask positions outside `[0, all_sequence_length]`. ## Summary of Changes ### Bounds clamping | File | Change | |------|--------| | `onnxruntime/contrib_ops/cpu/bert/attention_helper.h` | Use `std::clamp(static_cast<int>(mask_index[...]), 0, all_sequence_length)` for both `end_position` and `start_position` in the 1D `mask_index` branch of `PrepareMask`. | ### Test coverage | File | Change | |------|--------| | `onnxruntime/test/contrib_ops/attention_op_test.cc` | Add `AttentionMaskIndex1DClampOOB`, covering an over-large `end_position` (999) and a negative `start_position` (-5). Both clamp cleanly and produce the fully-unmasked output. | ## Testing - Build and run the contrib attention tests: `onnxruntime_test_all --gtest_filter='ContribOpAttentionTest.AttentionMaskIndex1DClampOOB'` - The new test asserts that an out-of-range end position (above `all_sequence_length`) applies no right-side masking, and a negative start position applies no left-side masking — matching a fully-unmasked sequence. - Existing `ContribOpAttentionTest.*` cases continue to pass; behavior for in-range mask values is unchanged. ## Motivation and Context `end_position`/`start_position` drive raw index loops into the mask buffer. An unclamped value past `all_sequence_length` (or a negative start) could otherwise step outside the intended range. `std::clamp` guarantees both stay within `[0, all_sequence_length]`, preventing out-of-bounds writes with no behavior change for valid inputs. ## Checklist - [x] Tests added/updated - [ ] Documentation updated (if applicable) - [x] No breaking changes (or documented in description) - [ ] CI passes
This was referenced Jul 25, 2026
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.
PR: Clamp 1D attention mask_index to valid bounds
Description
The CPU attention helper translates a 1D
mask_index(right-side end position, andoptionally a left-side start position) into per-token mask-fill loop bounds. The
existing
std::max(0, std::min(...))clamp was correct but verbose; this PR replaces itwith
std::clampand adds a regression test that exercises out-of-range mask values.This hardens the kernel against out-of-bounds writes when callers supply mask positions
outside
[0, all_sequence_length].Summary of Changes
Bounds clamping
onnxruntime/contrib_ops/cpu/bert/attention_helper.hstd::clamp(static_cast<int>(mask_index[...]), 0, all_sequence_length)for bothend_positionandstart_positionin the 1Dmask_indexbranch ofPrepareMask.Test coverage
onnxruntime/test/contrib_ops/attention_op_test.ccAttentionMaskIndex1DClampOOB, covering an over-largeend_position(999) and a negativestart_position(-5). Both clamp cleanly and produce the fully-unmasked output.Testing
onnxruntime_test_all --gtest_filter='ContribOpAttentionTest.AttentionMaskIndex1DClampOOB'all_sequence_length)applies no right-side masking, and a negative start position applies no left-side
masking — matching a fully-unmasked sequence.
ContribOpAttentionTest.*cases continue to pass; behavior for in-rangemask values is unchanged.
Motivation and Context
end_position/start_positiondrive raw index loops into the mask buffer. Anunclamped value past
all_sequence_length(or a negative start) could otherwise stepoutside the intended range.
std::clampguarantees both stay within[0, all_sequence_length], preventing out-of-bounds writes with no behavior change forvalid inputs.
Checklist