Skip to content

shape_inference: Validate expected size of scalar tensor in ParseData#7675

Merged
justinchuby merged 8 commits intoonnx:mainfrom
skottmckay:CheckExpectedSizeOfScalarInParseData
Feb 19, 2026
Merged

shape_inference: Validate expected size of scalar tensor in ParseData#7675
justinchuby merged 8 commits intoonnx:mainfrom
skottmckay:CheckExpectedSizeOfScalarInParseData

Conversation

@skottmckay
Copy link
Copy Markdown
Contributor

Motivation and Context

Fix segfault for a model which has a scalar input that is missing data. The model is invalid, but the current code does not catch that due to validation of the expected size for a scalar being skipped.

Fix a couple of places where a tensor was created but the shape wasn't set correctly.

Fixes #

@skottmckay skottmckay requested review from a team as code owners February 16, 2026 05:13
@github-project-automation github-project-automation Bot moved this to In progress in PR Tracker Feb 16, 2026
… This allows an invalid model with missing data to not fail until reading the data is attempted (e.g. in compute_output_dim_for_range if the `delta` input lacks int64 data).

Signed-off-by: Scott McKay <skottmckay@gmail.com>
…t correctly set.

Signed-off-by: Scott McKay <skottmckay@gmail.com>
Signed-off-by: Scott McKay <skottmckay@gmail.com>
@skottmckay skottmckay force-pushed the CheckExpectedSizeOfScalarInParseData branch from da159bf to cba251d Compare February 16, 2026 05:15
@skottmckay skottmckay changed the title Validated expected size of scalar tensor in ParseData shape_inference: Validate expected size of scalar tensor in ParseData Feb 16, 2026
@skottmckay skottmckay added the module: shape inference Issues related to shape inference label Feb 16, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 55.29%. Comparing base (4b5716a) to head (b190d5d).
⚠️ Report is 2 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7675      +/-   ##
==========================================
+ Coverage   55.28%   55.29%   +0.01%     
==========================================
  Files         517      517              
  Lines       32621    32633      +12     
  Branches     2904     2904              
==========================================
+ Hits        18034    18046      +12     
  Misses      13794    13794              
  Partials      793      793              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@andife andife added auto update doc Generate md/proto files automatically using the CI pipeline and removed auto update doc Generate md/proto files automatically using the CI pipeline labels Feb 16, 2026
@justinchuby justinchuby requested a review from Copilot February 16, 2026 06:06
@justinchuby justinchuby added this to the 1.21 milestone Feb 16, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 tightens tensor validation in shape inference utilities to prevent invalid scalar tensors (missing data) from slipping through and causing crashes, and it corrects tensor shape metadata when constructing constants during version conversion.

Changes:

  • Ensure ToTensor(std::vector<...>) populates dims so created tensors have correct 1-D shape metadata.
  • Always validate expected element count vs. provided repeated-field data in ParseData (including scalar tensors).
  • Fix Reshape 4→5 adapter to set the shape of the created Constant tensor to match the number of shape attribute entries.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
onnx/version_converter/adapters/reshape_4_5.h Sets the created Constant tensor’s 1-D shape length to match the shape attribute entry count.
onnx/defs/tensor_proto_util.cc Sets dims for list-based ToTensor, and strengthens ParseData size validation (including scalars).
Comments suppressed due to low confidence (1)

onnx/version_converter/adapters/reshape_4_5.h:35

  • This change fixes the shape on the created Constant tensor, but the existing 4->5 Reshape conversion test only asserts node types and opset version. Please extend/add a regression test to assert the generated Constant’s value tensor has the expected 1-D shape (length == number of shape attribute entries), so this doesn’t regress again.
    // Create tensor for value attribute
    Tensor t;
    t.elem_type() = TensorProto_DataType_INT64;
    t.sizes() = std::vector<int64_t>{static_cast<int64_t>(node->is(kshape).size())};
    auto& data = t.int64s();
    // Turn shapes attribute into tensor
    for (int64_t shape : node->is(kshape)) {
      data.emplace_back(shape);
    }
    // Add value as input to node
    Node* constant = graph->create(kConstant);
    constant->insertBefore(node);
    constant->t_(kvalue, t);
    node->addInput(constant->output());

Comment thread onnx/defs/tensor_proto_util.cc
Comment thread onnx/defs/tensor_proto_util.cc Outdated
Comment thread onnx/version_converter/adapters/reshape_4_5.h
Comment thread onnx/defs/tensor_proto_util.cc Outdated
@github-project-automation github-project-automation Bot moved this from In progress to Reviewer approved in PR Tracker Feb 17, 2026
@justinchuby
Copy link
Copy Markdown
Member

@skottmckay could you sign off the commits and fix lint? Thanks

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

Comment thread onnx/defs/tensor_proto_util.cc
Comment thread onnx/defs/tensor_proto_util.cc Outdated
Move change from ToTensor to AddTemporaryConstant. There are existing usages of ToTensor (e.g. math/old.cc) that add dims later which would be broken if its implementation is changed.
Add tests for ParseData changes.

Signed-off-by: Scott McKay <skottmckay@gmail.com>
Signed-off-by: Scott McKay <skottmckay@gmail.com>
@skottmckay skottmckay force-pushed the CheckExpectedSizeOfScalarInParseData branch from 674859a to afa7cae Compare February 19, 2026 07:08
@justinchuby justinchuby merged commit cc5dcf9 into onnx:main Feb 19, 2026
45 of 46 checks passed
@github-project-automation github-project-automation Bot moved this from Reviewer approved to Done in PR Tracker Feb 19, 2026
titaiwangms added a commit to microsoft/onnxruntime that referenced this pull request Mar 11, 2026
ONNX 1.21.0 (onnx/onnx#7675) added stricter raw_data size validation
in ParseData<T>. The test had shape {4} but only 3 values {2, 64, 32},
which old ONNX silently ignored. Fix shape to {3}.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module: shape inference Issues related to shape inference topic: bug fix

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants