Skip to content

Harden shape inference for contrib ops - #32607

Open
shiyi (shiyi9801) wants to merge 3 commits into
microsoft:mainfrom
shiyi9801:bert_defs_96
Open

shiyi (shiyi9801) wants to merge 3 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
Comment on lines +2903 to +2907
if (ndim < 1 || ndim > 3) {
fail_shape_inference("CausalConvWithState: ndim must be 1, 2, or 3, got ", ndim);
}
if (weight_shape.dim_size() < 2) {
fail_shape_inference("CausalConvWithState: weight must have rank >= 2");
// weight is always channels-first: (channels, 1, k_1, ..., k_ndim), rank == ndim + 2.
if (weight_shape.dim_size() != ndim + 2) {
Comment on lines +3580 to +3583
constexpr int64_t max_dimension = std::numeric_limits<int64_t>::max();
if (num_heads_k > max_dimension / head_size_qk ||
num_heads_v > max_dimension / head_size_v) {
fail_shape_inference("GatedDeltaNet: state_update width overflows int64");
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