From 0c43fa056b29c84e36366494a807583a3e702868 Mon Sep 17 00:00:00 2001 From: Matthew Brookhart Date: Sat, 9 May 2020 16:54:39 -0700 Subject: [PATCH] Apparently, ONNX Conv with no 'pads' defaults to zero padding (#5548) --- python/tvm/relay/frontend/onnx.py | 6 ++++-- tests/python/frontend/onnx/test_forward.py | 23 ++++++++++++++++++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/python/tvm/relay/frontend/onnx.py b/python/tvm/relay/frontend/onnx.py index f4bcb6bf947a..1a4aee0a0d6c 100644 --- a/python/tvm/relay/frontend/onnx.py +++ b/python/tvm/relay/frontend/onnx.py @@ -325,7 +325,6 @@ class Conv(OnnxOpConverter): def _impl_v1(cls, inputs, attr, params): # Use shape of input to determine convolution type. input_shape = infer_shape(inputs[0]) - if 'auto_pad' in attr: attr['auto_pad'] = attr['auto_pad'].decode('utf-8') if attr['auto_pad'] in ('SAME_UPPER', 'SAME_LOWER'): @@ -350,7 +349,10 @@ def _impl_v1(cls, inputs, attr, params): attr.pop('auto_pad') elif len(attr['kernel_shape']) == 2: sym_pad = True - padding = attr['pads'] + if 'pads' in attr: + padding = attr['pads'] + else: + padding = [0, 0, 0, 0] for i in range(0, len(padding), 2): sym_pad = sym_pad and padding[i] == padding[i + 1] diff --git a/tests/python/frontend/onnx/test_forward.py b/tests/python/frontend/onnx/test_forward.py index 8bc9f458153f..a26c6137b32b 100644 --- a/tests/python/frontend/onnx/test_forward.py +++ b/tests/python/frontend/onnx/test_forward.py @@ -2028,8 +2028,18 @@ def test_or(): verify_or(indata=[x, y], dtype=bool) -def verify_conv(x_shape, w_shape, y_shape, padding, kernel_shape, strides, dilations, auto_pad="NOTSET"): - if padding is None: +def verify_conv(x_shape, w_shape, y_shape, padding, kernel_shape, strides, dilations, auto_pad="NOTSET", unset_pad=False): + if unset_pad: + node = helper.make_node('Conv', + inputs=['x', 'W'], + outputs=['y'], + kernel_shape=kernel_shape, + # Default values for other attributes: + strides=strides, + dilations=dilations, + # groups=1 + ) + elif padding is None: node = helper.make_node('Conv', inputs=['x', 'W'], outputs=['y'], @@ -2095,6 +2105,15 @@ def repeat(N, D): repeat(1, D), repeat(1, D), auto_pad="SAME_UPPER") + # Convolution with unset padding + verify_conv((1, 1) + repeat(5, D), + (1, 1) + repeat(3, D), + (1, 1) + repeat(3, D), + 2 * repeat(0, D), + repeat(3, D), + repeat(1, D), + repeat(1, D), + True) # Convolution with non uniform stride verify_conv((1, 1) + repeat(5, D), (1, 1) + repeat(3, D),