Skip to content

Harden shape inference for contrib ops - #32607

Merged
Akshay Sonawane (apsonawane) merged 6 commits into
microsoft:mainfrom
shiyi9801:bert_defs_96
Sep 21, 2026
Merged

Akshay Sonawane (apsonawane) merged 6 commits into
microsoft:mainfrom
shiyi9801:bert_defs_96

Conversation

@shiyi9801

Copy link
Copy Markdown
Contributor

Summary

Strengthens attribute/input validation in three contrib-op shape-inference functions in bert_defs.cc so malformed or malicious models are rejected at Graph::Resolve instead of triggering out-of-bounds reads or signed-integer overflow during shape inference.

Changes

CausalConvWithState: validate the ndim attribute is in [1, 3] and cross-check tensor ranks against it (weight == ndim+2, channels-first input == ndim+2, channels-last input >= 3) before the spatial-dim loop indexes input.dim(2+i). Previously only a rank >= 2 guard existed, so ndim=2/3 with a low-rank input read past the shape's dimensions.
GatedDeltaNet: require the head counts/sizes to be positive and add step-by-step overflow guards before computing the state_update capsule width, preventing signed int64 overflow (UB) in state_update_capacity * (num_heads_v + num_heads_khead_size_qk + num_heads_vhead_size_v).
GroupQueryAttention / SparseAttention: check the parsed total_sequence_length initializer is non-empty before indexing data[0].

Copilot AI balanced review requested due to automatic review settings September 15, 2026 08:55
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@shiyi9801

Copy link
Copy Markdown
Contributor Author

Dwayne Robinson (@fdwr) PTAL, thanks!

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.

🟡 Changes recommended

Invalid ndim values remain accepted when input shape metadata is absent, and the new guards lack regression tests.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Hardens contrib-op shape inference against malformed model metadata.

Changes:

  • Validates CausalConvWithState ranks and ndim.
  • Guards GatedDeltaNet width arithmetic against overflow.
  • Rejects empty attention sequence-length initializers.
File summaries
File Description
onnxruntime/core/graph/contrib_ops/bert_defs.cc Adds shape, attribute, initializer, and overflow validation.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 4
  • Review effort level: Balanced

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread onnxruntime/core/graph/contrib_ops/bert_defs.cc Outdated
Comment thread onnxruntime/core/graph/contrib_ops/bert_defs.cc Outdated
Comment thread onnxruntime/core/graph/contrib_ops/bert_defs.cc Outdated
Comment thread onnxruntime/core/graph/contrib_ops/bert_defs.cc

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.

🟡 Changes recommended

SparseAttention validates the wrong input, GatedDeltaNet validation is output-dependent, and regression tests are missing.

Get a fresh assessment by requesting another Copilot review.

Review details

Suppressed comments (3)

Previously missed (2) — in code that hasn't changed since the last review.

onnxruntime/core/graph/contrib_ops/bert_defs.cc:309

  • ctx.getInputData(6) is the total_sequence_length input for GroupQueryAttention, but input 6 is block_col_indices for SparseAttention; SparseAttention's scalar is input 7 (bert_defs.cc:1861-1868). This therefore validates the wrong initializer for SparseAttention. Parameterize this input index and pass 6 for GroupQueryAttention and 7 for SparseAttention.

This issue also appears on line 307 of the same file.
onnxruntime/core/graph/contrib_ops/bert_defs.cc:3576

  • The positivity check is nested under getNumOutputs() > 2, so a GatedDeltaNet node requesting only output (or output plus final_state) can still resolve with a statically zero head count/size. Move validation of known head dimensions before the optional state_update branch; only the width-overflow calculation should depend on output 2 being present.

onnxruntime/core/graph/contrib_ops/bert_defs.cc:309

  • This condition does not enforce the diagnostic's “single element” contract: a multi-element initializer is accepted and only data[0] is used, while the runtime requires a scalar or one-element vector. Reject every size other than one.
      if (data.empty()) {
        fail_shape_inference("total_sequence_length input must contain a single element");
      }
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Balanced

Comment thread onnxruntime/core/graph/contrib_ops/bert_defs.cc
Comment thread onnxruntime/core/graph/contrib_ops/bert_defs.cc
Comment thread onnxruntime/core/graph/contrib_ops/bert_defs.cc
Comment thread onnxruntime/core/graph/contrib_ops/bert_defs.cc

@fdwr Dwayne Robinson (fdwr) 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.

Thanks for hardening ORT Shiyi. I skimmed it, but I don't have anything to add beyond Akshay and Copilot's comments. Will defer to Akshay for this one, restarting any failed CI's for ya.

@shiyi9801

Copy link
Copy Markdown
Contributor Author

Akshay Sonawane (@apsonawane) Addressed your comments and added some regression tests, please take antoher look, thanks!

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.

🟡 Changes recommended

The new GatedDeltaNet test reads dangling initializer-list storage, and validation remains conditional on an optional output.

Get a fresh assessment by requesting another Copilot review.

Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 6
  • Review effort level: Balanced

Comment thread onnxruntime/core/graph/contrib_ops/bert_defs.cc
Comment thread onnxruntime/test/contrib_ops/causal_conv_with_state_op_test.cc
Comment thread onnxruntime/test/contrib_ops/gated_delta_net_op_test.cc
Comment thread onnxruntime/test/contrib_ops/group_query_attention_op_test.cc
Comment thread onnxruntime/test/contrib_ops/gated_delta_net_op_test.cc Outdated
Comment thread onnxruntime/core/graph/contrib_ops/bert_defs.cc
@apsonawane

Copy link
Copy Markdown
Contributor

shiyi (@shiyi9801) can you review the copilot comments

@shiyi9801

Copy link
Copy Markdown
Contributor Author

Akshay Sonawane (@apsonawane) Addressed the copilot comments, please take another look.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

@apsonawane
Akshay Sonawane (apsonawane) merged commit bca9d15 into microsoft:main Sep 21, 2026
90 checks passed
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