From 7a42530ab687575907ea35226044cba4d51e2ecd Mon Sep 17 00:00:00 2001 From: AndrewZhaoLuo Date: Sat, 15 Jan 2022 12:56:33 -0800 Subject: [PATCH] [ONNX] Fix onnx convtranspose error (#9938) * fix mix up of channels with conv2d-transpose * add grouped convtranspose tests * turn off groups for non-llvm test --- python/tvm/relay/frontend/onnx.py | 18 +++++++++--------- tests/python/frontend/onnx/test_forward.py | 9 +++++++++ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/python/tvm/relay/frontend/onnx.py b/python/tvm/relay/frontend/onnx.py index 57d7568a72ef9..60319d682f0c4 100644 --- a/python/tvm/relay/frontend/onnx.py +++ b/python/tvm/relay/frontend/onnx.py @@ -38,9 +38,9 @@ from .. import ty as _ty from .. import vision as _vision from .common import ( - autopad, AttrCvt, Renamer, + autopad, ensure_scalar_shape, fold_constant, get_name, @@ -557,13 +557,13 @@ class ConvTranspose(OnnxOpConverter): def _impl_v1(cls, inputs, attr, params): # get number of channels out_type = infer_type(inputs[1]) - out_shapes = [get_const_tuple(out_type.checked_type.shape)] - channels = out_shapes[0][1] - attr["channels"] = channels + kernel_shape = [get_const_tuple(out_type.checked_type.shape)] + out_channels = kernel_shape[0][1] * attr.get("group", 1) + attr["channels"] = out_channels groups = attr.get("group", 1) if "kernel_shape" not in attr: - attr["kernel_shape"] = out_shapes[0][2:] + attr["kernel_shape"] = kernel_shape[0][2:] attr["groups"] = groups # infer pads for auto_pad @@ -612,13 +612,13 @@ def _impl_v1(cls, inputs, attr, params): def _impl_v11(cls, inputs, attr, params): # get number of channels out_type = infer_type(inputs[1]) - out_shapes = [get_const_tuple(out_type.checked_type.shape)] - channels = out_shapes[0][1] - attr["channels"] = channels + kernel_shape = [get_const_tuple(out_type.checked_type.shape)] + out_channels = kernel_shape[0][1] * attr.get("group", 1) + attr["channels"] = out_channels groups = attr.get("group", 1) if "kernel_shape" not in attr: - attr["kernel_shape"] = out_shapes[0][2:] + attr["kernel_shape"] = kernel_shape[0][2:] attr["groups"] = groups # infer pads for auto_pad diff --git a/tests/python/frontend/onnx/test_forward.py b/tests/python/frontend/onnx/test_forward.py index 287fbe41bd77c..2bdc5f77cc7bd 100644 --- a/tests/python/frontend/onnx/test_forward.py +++ b/tests/python/frontend/onnx/test_forward.py @@ -189,6 +189,7 @@ def verify_with_ort_with_inputs( opt_level=opt_level, convert_config=convert_config, ) + if not isinstance(tvm_out, list): tvm_out = [tvm_out] if not isinstance(ort_out, list): @@ -2892,6 +2893,14 @@ def verify_convtranspose(x_shape, w_shape, y_shape, p, group=1): # Test undefined groups. verify_convtranspose((1, 1, 3, 3), (1, 2, 3, 3), (1, 2, 7, 3), [1, 2, 1, 2], group=None) + if "llvm" in target: + # GPU does not support groups != 1 for convtranspose, so only test llvm + # Test depthwise-convolution + verify_convtranspose((1, 10, 3, 3), (10, 1, 3, 3), (1, 10, 7, 3), [1, 2, 1, 2], group=10) + + # Test grouped-convolution + verify_convtranspose((1, 10, 3, 3), (10, 1, 3, 3), (1, 5, 7, 3), [1, 2, 1, 2], group=5) + def repeat(N, D): return tuple([N for _ in range(D)])