From 8f4593f418c4f7ea85406898b88837b6d06fee52 Mon Sep 17 00:00:00 2001 From: Andrew Luo Date: Fri, 14 Jan 2022 14:36:23 -0800 Subject: [PATCH 1/3] fix mix up of channels with conv2d-transpose --- python/tvm/relay/frontend/onnx.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/python/tvm/relay/frontend/onnx.py b/python/tvm/relay/frontend/onnx.py index 57d7568a72ef..60319d682f0c 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 From 487f03540151b2e15533ef5f2edb7c46ec37c26f Mon Sep 17 00:00:00 2001 From: Andrew Luo Date: Fri, 14 Jan 2022 14:58:20 -0800 Subject: [PATCH 2/3] add grouped convtranspose tests --- tests/python/frontend/onnx/test_forward.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/python/frontend/onnx/test_forward.py b/tests/python/frontend/onnx/test_forward.py index 287fbe41bd77..df061867aadc 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): @@ -233,7 +234,8 @@ def verify_with_ort( def quantize_and_verify_with_ort(onnx_model, input_names, input_shapes, target, dev): - from onnxruntime.quantization import CalibrationDataReader, QuantType, quantize_static + from onnxruntime.quantization import (CalibrationDataReader, QuantType, + quantize_static) input_arrays = [np.random.random(shape).astype("float32") for shape in input_shapes] @@ -2892,6 +2894,12 @@ 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) + # 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)]) From 884b1720698cbb528b96fddbc26cdbc4224125b0 Mon Sep 17 00:00:00 2001 From: Andrew Luo Date: Fri, 14 Jan 2022 15:00:38 -0800 Subject: [PATCH 3/3] turn off groups for non-llvm test --- tests/python/frontend/onnx/test_forward.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/python/frontend/onnx/test_forward.py b/tests/python/frontend/onnx/test_forward.py index df061867aadc..2bdc5f77cc7b 100644 --- a/tests/python/frontend/onnx/test_forward.py +++ b/tests/python/frontend/onnx/test_forward.py @@ -234,8 +234,7 @@ def verify_with_ort( def quantize_and_verify_with_ort(onnx_model, input_names, input_shapes, target, dev): - from onnxruntime.quantization import (CalibrationDataReader, QuantType, - quantize_static) + from onnxruntime.quantization import CalibrationDataReader, QuantType, quantize_static input_arrays = [np.random.random(shape).astype("float32") for shape in input_shapes] @@ -2894,11 +2893,13 @@ 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) - # Test depthwise-convolution - verify_convtranspose((1, 10, 3, 3), (10, 1, 3, 3), (1, 10, 7, 3), [1, 2, 1, 2], group=10) + 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) + # 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)])