Skip to content

Fix default attribute values in shape inference#7602

Merged
justinchuby merged 6 commits intoonnx:mainfrom
matteosal:bugfix/ShapeInferenceDefaults
Jan 26, 2026
Merged

Fix default attribute values in shape inference#7602
justinchuby merged 6 commits intoonnx:mainfrom
matteosal:bugfix/ShapeInferenceDefaults

Conversation

@matteosal
Copy link
Copy Markdown
Contributor

This is an attempt to fix #7573

When using the default value for an attribute, there are two separate scenarios:

  • The attribute is not present
  • The attribute is present but the numerical value for the attribute is not set

The previous logic was using the schema default in both cases, but in the second case one should use the protobuf default for the relevant data type instead.

This change fixes the example reported in the issue.

@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 23, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 55.27%. Comparing base (ee910d0) to head (6cd62ae).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7602   +/-   ##
=======================================
  Coverage   55.26%   55.27%           
=======================================
  Files         515      515           
  Lines       32420    32425    +5     
  Branches     2898     2898           
=======================================
+ Hits        17918    17923    +5     
  Misses      13713    13713           
  Partials      789      789           

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

@justinchuby justinchuby added module: shape inference Issues related to shape inference topic: bug fix labels Jan 23, 2026
Copy link
Copy Markdown
Member

@justinchuby justinchuby left a comment

Choose a reason for hiding this comment

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

Thanks for fixing the bug! Could you signoff the commits? Thanks: https://github.com/onnx/onnx/pull/7602/checks?check_run_id=61303166766

@github-project-automation github-project-automation Bot moved this from In progress to Reviewer approved in PR Tracker Jan 23, 2026
@justinchuby
Copy link
Copy Markdown
Member

@justinchuby justinchuby added this to the 1.21 milestone Jan 23, 2026
Matteo Salvarezza added 2 commits January 26, 2026 17:17
Signed-off-by: Matteo Salvarezza <matteos@wolfram.com>
Signed-off-by: Matteo Salvarezza <matteos@wolfram.com>
@matteosal matteosal force-pushed the bugfix/ShapeInferenceDefaults branch from 1bfe456 to bea63a8 Compare January 26, 2026 16:20
@matteosal matteosal requested a review from a team as a code owner January 26, 2026 16:20
@matteosal
Copy link
Copy Markdown
Contributor Author

matteosal commented Jan 26, 2026

Added signoff and test. I had to add import base64 to the test file because the test needs to start from a serialized model that doesn't explicitly contain the value of 0 for the attribute (badModel.onnx, see original issue), and such serialized model cannot apparently be created by the helper (using an equivalent model created by the helper was passing the test even with the fix removed, which means it's not sensitive to the issue).

Comment thread onnx/test/shape_inference_test.py Fixed
Comment thread onnx/test/shape_inference_test.py Fixed
Comment thread onnx/test/shape_inference_test.py Fixed
Comment thread onnx/test/shape_inference_test.py Fixed
Comment thread onnx/test/shape_inference_test.py Fixed
Comment thread onnx/test/shape_inference_test.py Fixed
Comment thread onnx/test/shape_inference_test.py Fixed
Comment thread onnx/test/shape_inference_test.py Fixed
Comment thread onnx/test/shape_inference_test.py Fixed
Comment thread onnx/test/shape_inference_test.py Fixed
andife and others added 2 commits January 26, 2026 17:39
Signed-off-by: Matteo Salvarezza <matteos@wolfram.com>
@justinchuby justinchuby self-assigned this Jan 26, 2026
Comment thread onnx/test/shape_inference_test.py Outdated
Replaced base64 model loading with text format parsing for clarity and maintainability.
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

Fixes ONNX shape-inference handling of attribute defaults when an attribute is present in the protobuf but its scalar value field is unset (e.g., axis declared but i not set), aligning behavior with protobuf scalar defaults rather than schema defaults.

Changes:

  • Update getAttribute helpers to distinguish “attribute absent” vs “attribute present but scalar unset”, using protobuf defaults in the latter case.
  • Add a regression test covering the Flatten(axis) scenario from issue #7573 using textproto parsing.

Reviewed changes

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

File Description
onnx/defs/shape_inference.h Adjusts attribute lookup defaults to treat present-but-unset scalar fields as protobuf defaults.
onnx/test/shape_inference_test.py Adds a regression test reproducing the protobuf-default vs schema-default mismatch.

Comment on lines 170 to +177
inline int64_t getAttribute(const DataPropagationContext& ctx, const std::string& attributeName, int64_t defaultValue) {
const auto* attr_proto = ctx.getAttribute(attributeName);
if ((nullptr != attr_proto) && attr_proto->has_i())
return attr_proto->i();
return defaultValue;
else if (nullptr != attr_proto)
return 0; // protobuf default for integers
else
return defaultValue;
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

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

Same issue as the InferenceContext overload: returning 0 whenever attr_proto exists but has_i() is false ignores the attribute's declared type (or missing type), and can mask malformed attributes. It would be safer to only use the protobuf scalar default when attr_proto->type() is INT, and otherwise fall back to defaultValue or report an inference/type error.

Copilot uses AI. Check for mistakes.
Comment on lines 181 to +188
getAttribute(const InferenceContext& ctx, const std::string& attributeName, const std::string& defaultValue) {
const auto* attr_proto = ctx.getAttribute(attributeName);
if ((nullptr != attr_proto) && attr_proto->has_s())
return attr_proto->s();
return defaultValue;
else if (nullptr != attr_proto)
return ""; // protobuf default for strings
else
return defaultValue;
Copy link

Copilot AI Jan 26, 2026

Choose a reason for hiding this comment

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

Returning an empty string whenever the attribute exists but has_s() is false has the same caveat as the integer overload: it applies even if the attribute's type is unset/UNDEFINED or not STRING, which can silently override the schema default. Consider checking attr_proto->has_type() && attr_proto->type() == AttributeProto_AttributeType_STRING before using the protobuf default (otherwise keep defaultValue or fail).

Copilot uses AI. Check for mistakes.
@justinchuby justinchuby merged commit 78925bf into onnx:main Jan 26, 2026
52 checks passed
@github-project-automation github-project-automation Bot moved this from Reviewer approved to Done in PR Tracker Jan 26, 2026
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.

Some operator attribute defaults conflict with protobuf defaults

5 participants