From 74b6610d803f122f956d697f35149a55573cdead Mon Sep 17 00:00:00 2001 From: Trevor Morris Date: Thu, 11 Feb 2021 17:20:38 +0000 Subject: [PATCH 1/3] Ignore some TF2.0 attributes --- python/tvm/relay/frontend/tensorflow.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python/tvm/relay/frontend/tensorflow.py b/python/tvm/relay/frontend/tensorflow.py index b34e6c723645..d362806635ae 100644 --- a/python/tvm/relay/frontend/tensorflow.py +++ b/python/tvm/relay/frontend/tensorflow.py @@ -278,7 +278,7 @@ def _impl(inputs, attr, params, mod): out = AttrCvt( op_name=_dimension_picker(name), transforms={"kernel_shape": "pool_size", "data_format": "layout"}, - ignores=["ksize"], + ignores=["ksize", "explicit_paddings"], extras={"ceil_mode": False}, custom_check=_dimension_constraint(), )(inputs, attr) @@ -1416,9 +1416,9 @@ def _squeeze(): def _impl(inputs, attr, params, mod): if len(attr["squeeze_dims"]) == 0: attr["squeeze_dims"] = None - return AttrCvt(op_name="squeeze", transforms={"squeeze_dims": "axis"}, ignores=["T"])( - inputs, attr - ) + return AttrCvt( + op_name="squeeze", transforms={"squeeze_dims": "axis"}, ignores=["T", "_cloned"] + )(inputs, attr) return _impl From 73c4eaf5e9fcd089f8cc9a13afe7fa288e43c8a4 Mon Sep 17 00:00:00 2001 From: Trevor Morris Date: Fri, 19 Feb 2021 20:33:54 +0000 Subject: [PATCH 2/3] Support explicit padding for conv2d, max_pool, conv3d --- python/tvm/relay/frontend/tensorflow.py | 36 ++++++++++++- .../frontend/tensorflow/test_forward.py | 51 +++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/python/tvm/relay/frontend/tensorflow.py b/python/tvm/relay/frontend/tensorflow.py index d362806635ae..b656e738eadc 100644 --- a/python/tvm/relay/frontend/tensorflow.py +++ b/python/tvm/relay/frontend/tensorflow.py @@ -268,6 +268,13 @@ def _impl(inputs, attr, params, mod): pad_h = _get_pad_pair(in_w, kernel_w, stride_w) attr["padding"] = [pad_v[0], pad_h[0], pad_v[1], pad_h[1]] + elif attr["padding"] == "EXPLICIT": + paddings = attr["explicit_paddings"] + assert len(paddings) == 8 + if flip_layout or attr["data_format"] == "NHWC": + attr["padding"] = [paddings[2], paddings[4], paddings[3], paddings[5]] + else: + attr["padding"] = [paddings[4], paddings[6], paddings[5], paddings[7]] else: msg = 'Value {} in attribute "padding" of operator Pooling is ' "not valid." raise tvm.error.OpAttributeInvalid(msg.format(attr["padding"])) @@ -418,6 +425,13 @@ def _impl(inputs, attr, params, mod): pad_h = _get_pad_pair(in_w, dilated_kernel_w, stride_w) attr["padding"] = [pad_v[0], pad_h[0], pad_v[1], pad_h[1]] + elif attr["padding"] == "EXPLICIT": + paddings = attr["explicit_paddings"] + assert len(paddings) == 8 + if flip_layout or attr["data_format"] == "NHWC": + attr["padding"] = [paddings[2], paddings[4], paddings[3], paddings[5]] + else: + attr["padding"] = [paddings[4], paddings[6], paddings[5], paddings[7]] else: msg = 'Value {} in attribute "padding" of operator Conv is not ' "valid." raise tvm.error.OpAttributeInvalid(msg.format(attr["padding"])) @@ -626,7 +640,27 @@ def _impl(inputs, attr, params, mod): pad_h = _get_pad_pair(in_w, dilated_kernel_w, stride_w) attr["padding"] = [pad_d[0], pad_v[0], pad_h[0], pad_d[1], pad_v[1], pad_h[1]] - + elif attr["padding"] == "EXPLICIT": + paddings = attr["explicit_paddings"] + assert len(paddings) == 10 + if flip_layout or attr["data_format"] == "NDHWC": + attr["padding"] = [ + paddings[2], + paddings[4], + paddings[6], + paddings[3], + paddings[5], + paddings[7], + ] + else: + attr["padding"] = [ + paddings[4], + paddings[6], + paddings[8], + paddings[5], + paddings[7], + paddings[9], + ] else: msg = 'Value {} in attribute "padding" of operator Conv is not ' "valid." raise tvm.error.OpAttributeInvalid(msg.format(attr["padding"])) diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index 34ee0f3528ae..969dd5d7df15 100644 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -414,6 +414,16 @@ def test_forward_pooling(): pooling_type=pool_type, dilation_rate=[2], ) + # Explicit padding + if package_version.parse(tf.VERSION) >= package_version.parse("2.4.1"): + _test_pooling( + input_shape=[2, 9, 10, 2], + window_shape=[4, 4], + padding=[[0, 0], [0, 1], [2, 3], [0, 0]], + pooling_type="MAX", + dilation_rate=[1, 1], + strides=[1, 1], + ) ####################################################################### @@ -830,6 +840,36 @@ def test_forward_convolution(): [4, 8, 8, 176], add_shapes_to_graph_def=False, ) + # Explicit padding + if package_version.parse(tf.VERSION) >= package_version.parse("2.4.1"): + _test_convolution( + "conv", + [4, 8, 8, 16], + [1, 1, 16, 32], + [1, 1], + [1, 1], + [[0, 0], [2, 3], [0, 1], [0, 0]], + "NHWC", + ) + _test_convolution( + "depthwise", + [4, 8, 8, 16], + [1, 1, 16, 1], + [1, 1], + [1, 1], + [[0, 0], [2, 3], [0, 1], [0, 0]], + "NHWC", + ) + _test_convolution( + "conv_transpose", + [4, 8, 8, 32], + [3, 3, 176, 32], + [1, 1], + [2, 2], + [[0, 0], [1, 0], [1, 0], [0, 0]], + "NHWC", + [4, 16, 16, 176], + ) ####################################################################### @@ -924,6 +964,17 @@ def test_forward_convolution3d(): "NDHWC", add_shapes_to_graph_def=False, ) + # Explicit padding + if package_version.parse(tf.VERSION) >= package_version.parse("2.4.1"): + _test_convolution3d( + "conv", + [4, 8, 8, 8, 16], + [1, 1, 1, 16, 32], + [1, 1, 1], + [1, 1, 1], + [[0, 0], [1, 0], [0, 1], [2, 3]], + "NDHWC", + ) ####################################################################### From efdd57bf73d58c2d1ac83e77991a8d5653e40ebe Mon Sep 17 00:00:00 2001 From: Trevor Morris Date: Fri, 19 Feb 2021 20:38:14 +0000 Subject: [PATCH 3/3] Remove conv3d explicit padding test since TF API doesn't allow it --- tests/python/frontend/tensorflow/test_forward.py | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index 969dd5d7df15..1bd286040934 100644 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -964,17 +964,6 @@ def test_forward_convolution3d(): "NDHWC", add_shapes_to_graph_def=False, ) - # Explicit padding - if package_version.parse(tf.VERSION) >= package_version.parse("2.4.1"): - _test_convolution3d( - "conv", - [4, 8, 8, 8, 16], - [1, 1, 1, 16, 32], - [1, 1, 1], - [1, 1, 1], - [[0, 0], [1, 0], [0, 1], [2, 3]], - "NDHWC", - ) #######################################################################