Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion python/tvm/relax/frontend/onnx/onnx_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -4802,7 +4802,13 @@ def _impl_v9(cls, bb, inputs, attr, params):
total_output_shape -= _np.concatenate([[0, 0], list(strides)], axis=0)

if output_shape is not None:
total_output_shape = output_shape
output_shape = get_constant(output_shape, params)
if not isinstance(output_shape, tvm_tirx.ir.GenericConst):
raise ValueError(
"MaxUnpool only supports a constant `output_shape` input, "
f"but got a non-constant expression: {output_shape}"
)
total_output_shape = output_shape.value.numpy().astype("int64")

elif pads is not None:
# Get pads in the proper format
Expand Down
39 changes: 39 additions & 0 deletions tests/python/relax/test_frontend_onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -11160,6 +11160,45 @@ def main(
verify_maxunpool([3, 3], [1, 1, 1, 1], [2, 2], ExpectedMaxUnpool7)


def test_maxunpool_with_output_shape():
data = np.array([[[[5.0, 6.0], [7.0, 8.0]]]], dtype="float32")
indices = np.array([[[[5, 7], [13, 15]]]], dtype="int64")

out_shape = [1, 1, 5, 5]

def make_model(output_shape_as_input):
initializers = [
helper.make_tensor("I", TensorProto.INT64, indices.shape, indices.flatten())
]
node_inputs = ["X", "I"]
graph_inputs = [helper.make_tensor_value_info("X", TensorProto.FLOAT, data.shape)]
if output_shape_as_input == "initializer":
initializers.append(helper.make_tensor("S", TensorProto.INT64, [4], out_shape))
node_inputs.append("S")
elif output_shape_as_input == "graph_input":
graph_inputs.append(helper.make_tensor_value_info("S", TensorProto.INT64, [4]))
node_inputs.append("S")
node = helper.make_node(
"MaxUnpool", node_inputs, ["y"], kernel_shape=[2, 2], strides=[2, 2]
)
graph = helper.make_graph(
[node],
"maxunpool_output_shape_test",
inputs=graph_inputs,
outputs=[helper.make_tensor_value_info("y", TensorProto.FLOAT, out_shape)],
initializer=initializers,
)
return helper.make_model(graph, producer_name="maxunpool_output_shape_test")

check_correctness(make_model("initializer"), inputs={"X": data}, opset=11)

tvm_model = from_onnx(make_model("initializer"))
assert [int(d) for d in tvm_model["main"].ret_ty.shape] == out_shape

with pytest.raises(ValueError, match="constant `output_shape`"):
from_onnx(make_model("graph_input"))


def test_dropout():
def verify_dropout_ir(opset, attrs, expected):
node = helper.make_node("Dropout", ["x"], ["y"], **attrs)
Expand Down
Loading