From 8c7deb1aa9ee5e74257f63ac096f2697f074df65 Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Mon, 10 Apr 2023 00:20:20 +0800 Subject: [PATCH 1/7] [Tensorflow] Fix conv2d_transpose for NHWC layout If "data_format" == "NHWC", the kernel_layout should be "HWOI" rather than "HWIO". --- python/tvm/relay/frontend/tensorflow_ops.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/tvm/relay/frontend/tensorflow_ops.py b/python/tvm/relay/frontend/tensorflow_ops.py index 6b3f144619dd..7e6cfd77e7eb 100644 --- a/python/tvm/relay/frontend/tensorflow_ops.py +++ b/python/tvm/relay/frontend/tensorflow_ops.py @@ -464,8 +464,8 @@ def _impl(inputs, attr, params, mod): if opname == "conv": attr["kernel_layout"] = "HWIO" if attr["data_format"] == "NHWC" else "OIHW" elif opname == "conv_transpose": - # conv_transpose in TVM has weights be IOHW for NCHW - attr["kernel_layout"] = "HWIO" if attr["data_format"] == "NHWC" else "IOHW" + # conv_transpose in TVM has weights be HWOI for NHWC and IOHW for NCHW + attr["kernel_layout"] = "HWOI" if attr["data_format"] == "NHWC" else "IOHW" else: attr["kernel_layout"] = "HWOI" if attr["data_format"] == "NHWC" else "OIHW" From 778749629c420121fbb9e28e9b356871e95554e8 Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Tue, 11 Apr 2023 12:48:13 +0800 Subject: [PATCH 2/7] remove deed code --- python/tvm/relay/frontend/tensorflow_ops.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/tvm/relay/frontend/tensorflow_ops.py b/python/tvm/relay/frontend/tensorflow_ops.py index 7e6cfd77e7eb..21ac453fddd0 100644 --- a/python/tvm/relay/frontend/tensorflow_ops.py +++ b/python/tvm/relay/frontend/tensorflow_ops.py @@ -464,8 +464,8 @@ def _impl(inputs, attr, params, mod): if opname == "conv": attr["kernel_layout"] = "HWIO" if attr["data_format"] == "NHWC" else "OIHW" elif opname == "conv_transpose": - # conv_transpose in TVM has weights be HWOI for NHWC and IOHW for NCHW - attr["kernel_layout"] = "HWOI" if attr["data_format"] == "NHWC" else "IOHW" + # conv_transpose in TVM has weights be IOHW, because the attr["data_format"] always be NCHW when opname='conv_transpose'. + attr["kernel_layout"] = "IOHW" else: attr["kernel_layout"] = "HWOI" if attr["data_format"] == "NHWC" else "OIHW" From 91a92ec9b2d3b649421d1dc16a39a27c106f2bec Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Wed, 12 Apr 2023 16:03:57 +0800 Subject: [PATCH 3/7] add test cases --- .../frontend/tensorflow/test_forward.py | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index 703df799423f..1e0fa0adcbe1 100644 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -742,7 +742,17 @@ def test_forward_convolution(): "NCHW", [1, 1, 8, 8], ) - + _test_convolution( + "conv_transpose", + [4, 19, 8, 8], + [2, 2, 66, 19], + [1, 1], + [2, 2], + "VALID", + "NCHW", + [4, 66, 16, 16], + ) + _test_convolution("conv", [4, 8, 8, 176], [1, 1, 176, 32], [1, 1], [1, 1], "SAME", "NHWC") _test_convolution("conv", [4, 17, 17, 19], [3, 3, 19, 19], [1, 1], [2, 2], "VALID", "NHWC") _test_convolution("conv", [4, 17, 17, 124], [1, 1, 124, 19], [1, 1], [1, 1], "SAME", "NHWC") @@ -917,6 +927,17 @@ def test_forward_convolution(): [4, 8, 8, 176], add_shapes_to_graph_def=False, ) + _test_convolution( + "conv_transpose", + [4, 8, 8, 19], + [2, 2, 66, 19], + [1, 1], + [2, 2], + "VALID", + "NHWC", + [4, 16, 16, 66], + ) + # Explicit padding if package_version.parse(tf.VERSION) >= package_version.parse("2.4.1"): _test_convolution( From 7f67acc131cc48ad426bfdd73001fb7bd522fabf Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Thu, 13 Apr 2023 22:34:40 +0800 Subject: [PATCH 4/7] Update test_forward.py --- tests/python/frontend/tensorflow/test_forward.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index 1e0fa0adcbe1..6b370979981b 100644 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -751,8 +751,7 @@ def test_forward_convolution(): "VALID", "NCHW", [4, 66, 16, 16], - ) - + ) _test_convolution("conv", [4, 8, 8, 176], [1, 1, 176, 32], [1, 1], [1, 1], "SAME", "NHWC") _test_convolution("conv", [4, 17, 17, 19], [3, 3, 19, 19], [1, 1], [2, 2], "VALID", "NHWC") _test_convolution("conv", [4, 17, 17, 124], [1, 1, 124, 19], [1, 1], [1, 1], "SAME", "NHWC") @@ -937,7 +936,6 @@ def test_forward_convolution(): "NHWC", [4, 16, 16, 66], ) - # Explicit padding if package_version.parse(tf.VERSION) >= package_version.parse("2.4.1"): _test_convolution( From 32e64bf78124e9d3dc09b589ee5af9cd19de242b Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Thu, 13 Apr 2023 22:44:17 +0800 Subject: [PATCH 5/7] Update test_forward.py --- tests/python/frontend/tensorflow/test_forward.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/frontend/tensorflow/test_forward.py b/tests/python/frontend/tensorflow/test_forward.py index 6b370979981b..bd966fa71ccc 100644 --- a/tests/python/frontend/tensorflow/test_forward.py +++ b/tests/python/frontend/tensorflow/test_forward.py @@ -751,7 +751,7 @@ def test_forward_convolution(): "VALID", "NCHW", [4, 66, 16, 16], - ) + ) _test_convolution("conv", [4, 8, 8, 176], [1, 1, 176, 32], [1, 1], [1, 1], "SAME", "NHWC") _test_convolution("conv", [4, 17, 17, 19], [3, 3, 19, 19], [1, 1], [2, 2], "VALID", "NHWC") _test_convolution("conv", [4, 17, 17, 124], [1, 1, 124, 19], [1, 1], [1, 1], "SAME", "NHWC") From d98c7d69797bc59ddad4cc2a7f5b46d1b292f99a Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Thu, 13 Apr 2023 23:08:06 +0800 Subject: [PATCH 6/7] Update tensorflow_ops.py --- python/tvm/relay/frontend/tensorflow_ops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/tvm/relay/frontend/tensorflow_ops.py b/python/tvm/relay/frontend/tensorflow_ops.py index 21ac453fddd0..62ae7b1c7b1e 100644 --- a/python/tvm/relay/frontend/tensorflow_ops.py +++ b/python/tvm/relay/frontend/tensorflow_ops.py @@ -464,7 +464,7 @@ def _impl(inputs, attr, params, mod): if opname == "conv": attr["kernel_layout"] = "HWIO" if attr["data_format"] == "NHWC" else "OIHW" elif opname == "conv_transpose": - # conv_transpose in TVM has weights be IOHW, because the attr["data_format"] always be NCHW when opname='conv_transpose'. + # conv_transpose in TVM has weights be IOHW, because the attr["data_format"] always be NCHW. attr["kernel_layout"] = "IOHW" else: attr["kernel_layout"] = "HWOI" if attr["data_format"] == "NHWC" else "OIHW" From 8b2df3541447636063a52770a97874f5526806d7 Mon Sep 17 00:00:00 2001 From: Qingchao Shen Date: Thu, 13 Apr 2023 23:31:25 +0800 Subject: [PATCH 7/7] Update tensorflow_ops.py --- python/tvm/relay/frontend/tensorflow_ops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/tvm/relay/frontend/tensorflow_ops.py b/python/tvm/relay/frontend/tensorflow_ops.py index 62ae7b1c7b1e..27374fad1a94 100644 --- a/python/tvm/relay/frontend/tensorflow_ops.py +++ b/python/tvm/relay/frontend/tensorflow_ops.py @@ -464,7 +464,7 @@ def _impl(inputs, attr, params, mod): if opname == "conv": attr["kernel_layout"] = "HWIO" if attr["data_format"] == "NHWC" else "OIHW" elif opname == "conv_transpose": - # conv_transpose in TVM has weights be IOHW, because the attr["data_format"] always be NCHW. + # conv_transpose has weights be IOHW, because the attr["data_format"] always be NCHW attr["kernel_layout"] = "IOHW" else: attr["kernel_layout"] = "HWOI" if attr["data_format"] == "NHWC" else "OIHW"