Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix shape inference for Squeeze without axes #3465

Merged
merged 9 commits into from
May 14, 2021

Conversation

jcwchen
Copy link
Member

@jcwchen jcwchen commented May 3, 2021

Description
Add shape inference for Squeeze without axes: keep dimension if it is not equal to 0.

Motivation
#3456 Currently there is no shape inference for Squeeze without axes.

Signed-off-by: Chun-Wei Chen <jacky82226@gmail.com>
@jcwchen jcwchen requested a review from a team as a code owner May 3, 2021 23:21
Signed-off-by: Chun-Wei Chen <jacky82226@gmail.com>
Signed-off-by: Chun-Wei Chen <jacky82226@gmail.com>
@@ -1655,8 +1646,12 @@ ONNX_OPERATOR_SET_SCHEMA(
return axis < 0 ? axis + input_ndim : axis;
});

for (int i = 0, j = 0; i < input_ndim; ++i) {
if (std::find(axes.begin(), axes.end(), i) != axes.end()) {
for (int i = 0; i < input_ndim; ++i) {
Copy link
Contributor

Choose a reason for hiding this comment

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

if-then-else may need to be refactored. If axes is not specified, and input_shape.dim(i) has a symbolic value, it will fall into the else branch, which seems wrong.

Copy link
Contributor

@gramalingam gramalingam May 4, 2021

Choose a reason for hiding this comment

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

The (not_specify_axes) case is a bit more involved. If there is even one unknown input-dimension, we can't really set any of the other dimensions, since the output rank itself becomes unknown. I suggest creating a temporary shape, which is updated: if we see any unknown-dimension, bail out as we can't even set rank

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch. Since using temporary shape still needs 2-pass for loop (copying from temporary shape needs one), I decided to create another for loop before the original for loop to avoid creating more variables. It will stop early if it encounters a symbolic value from input. Please review it again. Thank you!

@gramalingam gramalingam added the shape inference Issues related to shape inference label May 10, 2021
Signed-off-by: Chun-Wei Chen <jacky82226@gmail.com>
@jcwchen jcwchen linked an issue May 11, 2021 that may be closed by this pull request
onnx/defs/tensor/defs.cc Outdated Show resolved Hide resolved
for (int i = 0, j = 0; i < input_ndim; ++i) {
if (std::find(axes.begin(), axes.end(), i) != axes.end()) {
for (int i = 0; i < input_ndim; ++i) {
if (axes_not_specified && input_shape.dim(i).has_dim_value() &&
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: input_shape.dim(i).has_dim_value() can be dropped since it is already checked in previous loop.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. I have removed two of them. Thanks

Signed-off-by: Chun-Wei Chen <jacky82226@gmail.com>
Signed-off-by: Chun-Wei Chen <jacky82226@gmail.com>
Signed-off-by: Chun-Wei Chen <jacky82226@gmail.com>
@gramalingam gramalingam merged commit 53f4371 into onnx:master May 14, 2021
@jcwchen jcwchen deleted the fix-squeeze branch September 1, 2021 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
shape inference Issues related to shape inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Shape inference for 'Squeeze' not working if no axes are provided
3 participants