Skip to content

Harden contrib CPU int narrowing for attention attrs - #31648

Open
apsonawane wants to merge 1 commit into
mainfrom
msrc/cpu-linearattention-int-wrap-fix
Open

Harden contrib CPU int narrowing for attention attrs#31648
apsonawane wants to merge 1 commit 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.

@@ -172,6 +173,13 @@ Status GptSubgraph::Validate(const std::vector<const NodeArg*>& subgraph_inputs,
ORT_RETURN_IF(!logits_shape->dim(2).has_dim_value() || logits_shape->dim(2).dim_value() <= 0,
"subgraph past state dimension 2 shall have a positive value for vocabulary size");
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.

2 participants