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

infer old squeeze on known input shape and empty axes #5283

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 24 additions & 10 deletions onnx/defs/tensor/old.cc
Expand Up @@ -2064,18 +2064,25 @@ ONNX_OPERATOR_SET_SCHEMA(
return;
}

std::vector<int64_t> axes;
if (!getRepeatedAttribute(ctx, "axes", axes)) {
return;
}

if (!ctx.getInputType(0)->tensor_type().has_shape()) {
return;
}

ctx.getOutputType(0)->mutable_tensor_type()->mutable_shape();
const auto& input_shape = ctx.getInputType(0)->tensor_type().shape();
const auto input_ndim = input_shape.dim_size();
std::vector<int64_t> axes;
if (!getRepeatedAttribute(ctx, "axes", axes)) {
for (int i = 0; i < input_ndim; ++i) {
if (!input_shape.dim(i).has_dim_value()) {
return;
}
if (input_shape.dim(i).dim_value() == 1) {
axes.push_back(i);
}
}
}

std::transform(axes.begin(), axes.end(), axes.begin(), [&](int64_t axis) -> int64_t {
return axis < 0 ? axis + input_ndim : axis;
});
Expand Down Expand Up @@ -4128,17 +4135,24 @@ ONNX_OPERATOR_SET_SCHEMA(
return;
}

std::vector<int64_t> axes;
if (!getRepeatedAttribute(ctx, "axes", axes)) {
return;
}

if (!ctx.getInputType(0)->tensor_type().has_shape()) {
return;
}

ctx.getOutputType(0)->mutable_tensor_type()->mutable_shape();
const auto& input_shape = ctx.getInputType(0)->tensor_type().shape();
const auto input_ndim = input_shape.dim_size();
Copy link
Contributor

Choose a reason for hiding this comment

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

That seems to be the same code than above, could it be moved in one separate function?

std::vector<int64_t> axes;
if (!getRepeatedAttribute(ctx, "axes", axes)) {
for (int i = 0; i < input_ndim; ++i) {
if (!input_shape.dim(i).has_dim_value()) {
return;
}
if (input_shape.dim(i).dim_value() == 1) {
axes.push_back(i);
}
}
}

for (int i = 0, j = 0; i < input_shape.dim_size(); ++i) {
if (static_cast<size_t>(j) < axes.size() && axes[j] == i) {
Expand Down
15 changes: 15 additions & 0 deletions onnx/test/shape_inference_test.py
Expand Up @@ -1183,6 +1183,21 @@ def test_squeeze(self) -> None:
graph, [make_tensor_value_info("y", TensorProto.FLOAT, (3, 2))]
)

def test_squeeze_no_axes_opset11(self) -> None:
graph = self._make_graph(
[
("x", TensorProto.FLOAT, (1, 3, 1, 1, 2, 1)),
],
[make_node("Squeeze", ["x"], "y")],
[],
)
operatorsetid = OperatorSetIdProto()
operatorsetid.domain = ""
operatorsetid.version = 11
self._assert_inferred(
graph, [make_tensor_value_info("y", TensorProto.FLOAT, (3, 2))]
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess that the opset needs to be specified, eg., something like opset_imports=[helper.make_opsetid(ONNX_DOMAIN, 11)]

)

def test_unsqueeze_regular(self) -> None:
graph = self._make_graph(
[("x", TensorProto.FLOAT, (3, 2)), ("axes", TensorProto.INT64, (4,))],
Expand Down