shape_inference: Validate expected size of scalar tensor in ParseData#7675
Conversation
… 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>
da159bf to
cba251d
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 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. |
There was a problem hiding this comment.
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<...>)populatesdimsso created tensors have correct 1-D shape metadata. - Always validate expected element count vs. provided repeated-field data in
ParseData(including scalar tensors). - Fix
Reshape4→5 adapter to set the shape of the created Constant tensor to match the number ofshapeattribute 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
valuetensor has the expected 1-D shape (length == number ofshapeattribute 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());
|
@skottmckay could you sign off the commits and fix lint? Thanks |
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>
674859a to
afa7cae
Compare
….com/skottmckay/onnx into CheckExpectedSizeOfScalarInParseData
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>
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 #