This is an automated email from the ASF dual-hosted git repository.
guan404ming pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm.git
The following commit(s) were added to refs/heads/main by this push:
new 3d89c4991c [Relax][ONNX] Support constant output_shape input in
MaxUnpool (#21000)
3d89c4991c is described below
commit 3d89c4991c5a37492f470800231a5ea8f57a95c6
Author: Neo Chien <[email protected]>
AuthorDate: Sun Nov 22 16:52:25 2026 +0800
[Relax][ONNX] Support constant output_shape input in MaxUnpool (#21000)
Hi Committers,
This PR fixes issue https://github.com/apache/tvm/issues/20888. Any
suggestions would be greatly appreciated.
### Root Cause
The ONNX `MaxUnpool` converter builds `total_output_shape` as a NumPy
array when the optional third input `output_shape` is absent. When
`output_shape` is provided, the converter assigned the Ralex expression
directly: total_output_shape = output_shape That value is a
`GenericConst` (or a `relax.Var` when `keep_params_in_input=True`), not
a NumPy array. The following
`relax.ShapeExpr(total_output_shape.tolist())` then fails with:
AttributeError: 'GenericConst' object has no attribute 'tolist'
### Solution
In the `output_shape` branch of `MaxUnpool._impl_v9`:
- Resolve the input with `get_constant(output_shape, params)`, the same
helper other converters (Reshape, Tile, Resize, ...) use to read
constant inputs.
- Convert the resulting constant to an `int64` NumPy array, so both
paths produce the same type and `.tolist()` / `ShapeExpr` work
unchanged.
- If `output_shape` is not a constant (e.g., a graph input), raise an
explicit `ValueError` instead of an opaque `AttributeError`, since
`ShapeExpr` needs static values.
---------
Co-authored-by: cchung100m <[email protected]>
---
python/tvm/relax/frontend/onnx/onnx_frontend.py | 8 ++++-
tests/python/relax/test_frontend_onnx.py | 39 +++++++++++++++++++++++++
2 files changed, 46 insertions(+), 1 deletion(-)
diff --git a/python/tvm/relax/frontend/onnx/onnx_frontend.py
b/python/tvm/relax/frontend/onnx/onnx_frontend.py
index 742145d86a..33a4392d25 100644
--- a/python/tvm/relax/frontend/onnx/onnx_frontend.py
+++ b/python/tvm/relax/frontend/onnx/onnx_frontend.py
@@ -4802,7 +4802,13 @@ class MaxUnpool(OnnxOpConverter):
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
diff --git a/tests/python/relax/test_frontend_onnx.py
b/tests/python/relax/test_frontend_onnx.py
index e23c4dfe75..d9a4037baa 100644
--- a/tests/python/relax/test_frontend_onnx.py
+++ b/tests/python/relax/test_frontend_onnx.py
@@ -11160,6 +11160,45 @@ def test_maxunpool():
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)