Skip to content

Relax IF's shape inference rule#2345

Merged
wschin merged 15 commits intoonnx:masterfrom
wschin:relax-if-shape-rule
Sep 24, 2019
Merged

Relax IF's shape inference rule#2345
wschin merged 15 commits intoonnx:masterfrom
wschin:relax-if-shape-rule

Conversation

@wschin
Copy link
Copy Markdown
Collaborator

@wschin wschin commented Sep 23, 2019

This PR allows If to produce different shapes in then and else branches. It makes the uses of If easier. It will also fix a shape inference problem in SSD objection detection model.

@wschin wschin added the topic: operator Issues related to ONNX operators label Sep 23, 2019
@wschin wschin added this to the 1.6 milestone Sep 23, 2019
@wschin wschin requested review from a team as code owners September 23, 2019 18:38
Comment thread onnx/defs/controlflow/defs.cc Outdated
Comment thread onnx/defs/controlflow/defs.cc Outdated
Comment thread onnx/defs/controlflow/defs.cc Outdated
Comment thread docs/Changelog.md Outdated
Comment thread docs/Changelog.md Outdated
Comment thread docs/Operators.md Outdated
Comment thread onnx/defs/controlflow/defs.cc Outdated

# Create a simple If node where the 'then' subgraph adds to the current value, and the 'else' subgraph
# subtracts.
# can't use self._make_graph for the subgraphs as that add more inputs for the Reshape operations it inserts.
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.

Wording is awkward here, perhaps avoid calling self._make_graph here for subgraph generation as extra reshape nodes will be created

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I feel the current description is more precise. From this description, adding Reshape itself is not a problem, but adding input required by Reshape causes the problem.

Comment thread onnx/defs/controlflow/defs.cc Outdated
Comment thread onnx/defs/controlflow/defs.cc Outdated
Comment thread onnx/defs/controlflow/old.cc
auto* if_output = ctx.getOutputType(i);
*if_output = *then_output;

if (then_output->has_tensor_type()) {
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.

Suggest to avoide has_type() function. Simply type() would return a default instance which would have value_case() NOT_SET

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I feel it's a bigger problem in the whole codebase. If you agree, I'd like to open another issue for addressing it globally.

Comment thread onnx/defs/shape_inference.h Outdated
inline bool checkDimensionCompatibility(
const TensorShapeProto_Dimension& source_dim,
const TensorShapeProto_Dimension& target_dim) {
if (source_dim.has_dim_value() && target_dim.has_dim_value()) {
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.

Use switch statement instead of has_*() functions which are absent in proto3. You want this to run in ML.NET.

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.

I believe it is not a problem with "one of" fields … the has_* functions are generated even in proto3 for "one of", if I am not mistaken

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.

See https://developers.google.com/protocol-buffers/docs/reference/cpp-generated#oneof-numeric-fields and https://developers.google.com/protocol-buffers/docs/reference/cpp-generated#oneof-embedded-message-fields … both proto2 and proto3 support "has_*" for one of fields. It is only for primitive-type fields that are not "one of" we have the proto2/proto3 issue (IIUC).

Comment thread onnx/defs/controlflow/defs.cc Outdated
Comment thread onnx/defs/controlflow/defs.cc Outdated
Comment thread onnx/defs/controlflow/defs.cc Outdated
Comment thread docs/Operators.md Outdated
@wschin wschin closed this Sep 24, 2019
@wschin wschin reopened this Sep 24, 2019
@wschin wschin merged commit ab6b942 into onnx:master Sep 24, 2019
wschin added a commit to wschin/onnx that referenced this pull request Sep 24, 2019
* Relax If's shape inference rule

* Make shape inference tests ok and move code to the right place

* Add document changes

* Update onnx/defs/controlflow/defs.cc

* Update onnx/defs/controlflow/defs.cc

* Address comments

* Address comments

* Address comments

* Fix shape inference test

* Disable a type check

* Address a comment

* Update defs.cc

* Update Changelog.md

* Update Operators.md
kevinch-nv pushed a commit that referenced this pull request Sep 25, 2019
* Relax IF's shape inference rule (#2345)

* Relax If's shape inference rule

* Make shape inference tests ok and move code to the right place

* Add document changes

* Update onnx/defs/controlflow/defs.cc

* Update onnx/defs/controlflow/defs.cc

* Address comments

* Address comments

* Address comments

* Fix shape inference test

* Disable a type check

* Address a comment

* Update defs.cc

* Update Changelog.md

* Update Operators.md

* Bump NMS version for avoiding regression in existing models (#2348)

* Bump NMS version for avoiding regression in existing models

* Bring old logic back

* Fix spec and shape inference for Unsqueeze op (#2347)

* Fix spec for Unsqueeze

* Update Changelog.md

* Refine doc

* Refine

* PR comments

* Update Changelog.md

* Update shape inference test file
jcwchen pushed a commit to jcwchen/onnx that referenced this pull request Sep 23, 2020
* Relax If's shape inference rule

* Make shape inference tests ok and move code to the right place

* Add document changes

* Update onnx/defs/controlflow/defs.cc

* Update onnx/defs/controlflow/defs.cc

* Address comments

* Address comments

* Address comments

* Fix shape inference test

* Disable a type check

* Address a comment

* Update defs.cc

* Update Changelog.md

* Update Operators.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic: operator Issues related to ONNX operators

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants