Skip to content

Harden contrib CPU int narrowing for attention attrs - #31648

Merged
Akshay Sonawane (apsonawane) merged 11 commits into
mainfrom
msrc/cpu-linearattention-int-wrap-fix
Aug 14, 2026
Merged

Akshay Sonawane (apsonawane) merged 11 commits into
mainfrom
msrc/cpu-linearattention-int-wrap-fix

Conversation

@apsonawane

Copy link
Copy Markdown
Contributor

This pull request strengthens input validation for attention-related operators and adds corresponding unit tests to ensure that large attribute values do not cause integer overflows. The changes affect the LinearAttention, LongformerAttentionBase, and GPT subgraph implementations, improving reliability and error reporting.

Input validation improvements:

Testing enhancements:

Code maintenance:

  • Included <limits> header where necessary to support the new validation checks. [1] [2] [3]

Validate int64 attributes and shape-derived values fit in int before narrowing in LinearAttention, LongformerAttentionBase, and GptSubgraph validation.

Add LinearAttention regressions for oversized q_num_heads and kv_num_heads attributes.

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 hardens contrib CPU attention-related operators against unsafe int64_tint narrowing by validating attribute/tensor-dimension ranges and adds unit tests to ensure overflow-scale values are rejected with clear errors.

Changes:

  • Add INT_MAX-bounded validation for q_num_heads/kv_num_heads (LinearAttention) and num_heads/window (LongformerAttentionBase) before static_cast<int>().
  • Add INT_MAX-bounded validation for GPT subgraph dimension values before narrowing to int.
  • Add LinearAttention unit tests that exercise the new overflow-range validation for head-count attributes.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
onnxruntime/contrib_ops/cpu/bert/linear_attention.cc Validates q_num_heads/kv_num_heads are within [1, INT_MAX] prior to narrowing to int.
onnxruntime/contrib_ops/cpu/bert/longformer_attention_base.h Validates num_heads and window are within [1, INT_MAX] prior to narrowing to int.
onnxruntime/contrib_ops/cpu/transformers/subgraph_gpt.cc Validates key subgraph shape dims are <= INT_MAX before narrowing to int.
onnxruntime/test/contrib_ops/linear_attention_op_test.cc Adds failure-mode tests for out-of-range q_num_heads/kv_num_heads.

Comment thread onnxruntime/contrib_ops/cpu/transformers/subgraph_gpt.cc Outdated
The error message on line 174 incorrectly referenced 'past state dimension 2'
when it was actually validating the logits output dimension 2 (vocabulary size).
This fix clarifies the message to correctly reference 'logits dimension 2' for
better debugging experience when vocabulary size validation fails.

Addresses review comment feedback from PR #31648.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The overflow tests for q_num_heads and kv_num_heads were failing schema
validation before reaching the attribute range checks because the LinearAttention
node requires two outputs. Add dummy output and present_state outputs so the
invalid attribute validation is exercised as intended.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The overflow tests were failing during schema shape inference before reaching
the intended q_num_heads / kv_num_heads constructor validation. Use unknown
output dimensions in the test harness so the failure path exercises the
attribute range checks directly.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

4 participants