From c35f3c909517f56f60619420c5b5c54d89c4d73b Mon Sep 17 00:00:00 2001 From: kice Date: Mon, 20 Jan 2020 11:09:48 -0500 Subject: [PATCH 1/2] Fix onnx import bugs Fix onnx attributes of string type incorrect handling Merge symmetric padding of Conv to symmetric form --- python/tvm/relay/frontend/onnx.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/python/tvm/relay/frontend/onnx.py b/python/tvm/relay/frontend/onnx.py index 1bd8673cd3aa..78d22acc6ea7 100644 --- a/python/tvm/relay/frontend/onnx.py +++ b/python/tvm/relay/frontend/onnx.py @@ -292,6 +292,14 @@ def _impl_v1(cls, inputs, attr, params): msg = 'Value {} in attribute "auto_pad" of operator Conv is invalid.' raise tvm.error.OpAttributeInvalid(msg.format(attr['auto_pad'])) attr.pop('auto_pad') + else: + sym_pad = True + padding = attr['pads'] + for i in range(0, len(padding), 2): + sym_pad = sym_pad and padding[i] == padding[i + 1] + + if sym_pad: + attr['pads'] = padding[0::2] out = AttrCvt( op_name=dimension_picker('conv'), @@ -455,7 +463,7 @@ def _impl_v1(cls, inputs, attr, params): for i in range(dims): pad_width.append((pads[i], pads[i+dims])) attr['pad_width'] = pad_width - pad_mode = attr.get('mode', 'constant').decode('utf-8') + pad_mode = attr.get('mode', b'constant').decode('utf-8') if pad_mode in ['constant', 'edge', 'reflect']: attr['pad_mode'] = pad_mode attr.pop('mode', None) @@ -478,7 +486,7 @@ def _impl_v2(cls, inputs, attr, params): for i in range(dims): pad_width.append((pads[i], pads[i+dims])) attr['pad_width'] = pad_width - pad_mode = attr.get('mode', 'constant').decode('utf-8') + pad_mode = attr.get('mode', b'constant').decode('utf-8') if pad_mode in ['constant', 'edge', 'reflect']: attr['pad_mode'] = pad_mode attr.pop('mode', None) @@ -570,7 +578,7 @@ class DepthToSpace(OnnxOpConverter): def _impl_v11(cls, inputs, attr, params): block_size = int(attr['blocksize']) - mode = attr.get("mode", "DCR") + mode = attr.get('mode', b'DCR').decode('utf-8') return _op.nn.depth_to_space(inputs[0], block_size, mode=mode) From fbf74f6285758d14f3353dd36a06a77924182560 Mon Sep 17 00:00:00 2001 From: kice Date: Tue, 21 Jan 2020 19:59:59 -0500 Subject: [PATCH 2/2] Only merge symmetric padding for conv2d --- python/tvm/relay/frontend/onnx.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/tvm/relay/frontend/onnx.py b/python/tvm/relay/frontend/onnx.py index 78d22acc6ea7..35a5c4eb1c0e 100644 --- a/python/tvm/relay/frontend/onnx.py +++ b/python/tvm/relay/frontend/onnx.py @@ -292,7 +292,7 @@ def _impl_v1(cls, inputs, attr, params): msg = 'Value {} in attribute "auto_pad" of operator Conv is invalid.' raise tvm.error.OpAttributeInvalid(msg.format(attr['auto_pad'])) attr.pop('auto_pad') - else: + elif len(attr['kernel_shape']) == 2: sym_pad = True padding = attr['pads'] for i in range(0, len(padding), 2):