From 8ee191aa3116dfc0d58b67f8d26b72835d20f32b Mon Sep 17 00:00:00 2001 From: Slawomir Siwek Date: Thu, 18 Nov 2021 12:35:28 +0100 Subject: [PATCH 1/5] Add new unittests --- .../mkldnn/test_conv2d_transpose_mkldnn_op.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/python/paddle/fluid/tests/unittests/mkldnn/test_conv2d_transpose_mkldnn_op.py b/python/paddle/fluid/tests/unittests/mkldnn/test_conv2d_transpose_mkldnn_op.py index 86609f015a260..a66fd8f379404 100644 --- a/python/paddle/fluid/tests/unittests/mkldnn/test_conv2d_transpose_mkldnn_op.py +++ b/python/paddle/fluid/tests/unittests/mkldnn/test_conv2d_transpose_mkldnn_op.py @@ -154,6 +154,27 @@ def init_test_case(self): self.padding_algorithm = "EXPLICIT" +class TestMKLDNNWithGroups(TestConv2DTransposeMKLDNNOp): + def init_test_case(self): + TestConv2DTransposeMKLDNNOp.init_test_case(self) + self.pad = [1, 1] + self.groups = 2 + self.input_size = [2, 4, 5, 5] # NCHW + f_c = self.input_size[1] + self.filter_size = [f_c, 3, 3, 3] + + +class TestMKLDNNWithGroups_NHWC(TestConv2DTransposeMKLDNNOp): + def init_test_case(self): + TestConv2DTransposeMKLDNNOp.init_test_case(self) + self.pad = [1, 1] + self.groups = 2 + self.input_size = [2, 5, 5, 4] # NHWC + f_c = self.input_size[-1] + self.filter_size = [f_c, 3, 3, 3] + self.data_format = 'NHWC' + + if __name__ == '__main__': enable_static() unittest.main() From 89a14b531b233c08062dbe62c4d2376e93ec8c0d Mon Sep 17 00:00:00 2001 From: Slawomir Siwek Date: Fri, 26 Nov 2021 14:37:48 +0100 Subject: [PATCH 2/5] Replace I with O channel for filter groups --- paddle/fluid/platform/mkldnn_helper.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/paddle/fluid/platform/mkldnn_helper.h b/paddle/fluid/platform/mkldnn_helper.h index 123ea5f1ef3a9..0ad15ba46c4a5 100644 --- a/paddle/fluid/platform/mkldnn_helper.h +++ b/paddle/fluid/platform/mkldnn_helper.h @@ -548,12 +548,12 @@ inline std::vector> ToMkldnnPadding( inline void GetGroupConvWeightsTz(std::vector& weights_tz, // NOLINT const int groups) { if (groups > 1) { - // if (is_conv3d) [o, i, d, h, w]->[g, o/g, i, d, h, w] - // else [o, i, h, w] -> [g, o/g, i, h, w] + // if (is_conv3d) [o, i, d, h, w]->[g, o, i/g, d, h, w] + // else [o, i, h, w] -> [g, o, i/g, h, w] weights_tz.push_back(0); std::rotate(weights_tz.begin(), weights_tz.end() - 1, weights_tz.end()); weights_tz[0] = groups; - weights_tz[1] = weights_tz[1] / groups; + weights_tz[2] = weights_tz[2] / groups; } } From faa9462d3ce0f34ede24dbd7d7d1fd77d5e35b74 Mon Sep 17 00:00:00 2001 From: Slawomir Siwek Date: Fri, 26 Nov 2021 21:25:10 +0100 Subject: [PATCH 3/5] Undo changes affecting other operators --- .../operators/mkldnn/conv_transpose_mkldnn_op.cc | 12 +++++------- paddle/fluid/platform/mkldnn_helper.h | 6 +++--- .../mkldnn/test_conv2d_transpose_mkldnn_op.py | 4 ++-- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/paddle/fluid/operators/mkldnn/conv_transpose_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/conv_transpose_mkldnn_op.cc index 35e35eb4bcb55..51cb0e60a483b 100644 --- a/paddle/fluid/operators/mkldnn/conv_transpose_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/conv_transpose_mkldnn_op.cc @@ -25,15 +25,13 @@ namespace operators { using Tensor = framework::Tensor; using framework::DataLayout; -inline dnnl::memory::dims GetWeightsTz(const Tensor* filter, const int groups) { - auto iohw_weights_tz = framework::vectorize(filter->dims()); - auto weights_tz = iohw_weights_tz; - - // IOHW -> OIHW - weights_tz[0] = iohw_weights_tz[1]; - weights_tz[1] = iohw_weights_tz[0]; +inline mkldnn::memory::dims GetWeightsTz(const Tensor* filter, const int groups) { + auto weights_tz = framework::vectorize(filter->dims()); int g = std::max(groups, 1); + int g_dim = (g > 1) ? 1 : 0; platform::GetGroupConvWeightsTz(weights_tz, g); + // gIOHW -> gOIHW || IOHW -> OIHW + std::swap(weights_tz[g_dim + 0], weights_tz[g_dim + 1]); return weights_tz; } diff --git a/paddle/fluid/platform/mkldnn_helper.h b/paddle/fluid/platform/mkldnn_helper.h index e7d293f02e60a..b98ca33285a39 100644 --- a/paddle/fluid/platform/mkldnn_helper.h +++ b/paddle/fluid/platform/mkldnn_helper.h @@ -548,12 +548,12 @@ inline std::vector> ToMkldnnPadding( inline void GetGroupConvWeightsTz(std::vector& weights_tz, // NOLINT const int groups) { if (groups > 1) { - // if (is_conv3d) [o, i, d, h, w]->[g, o, i/g, d, h, w] - // else [o, i, h, w] -> [g, o, i/g, h, w] + // if (is_conv3d) [o, i, d, h, w]->[g, o/g, i, d, h, w] + // else [o, i, h, w] -> [g, o/g, i, h, w] weights_tz.push_back(0); std::rotate(weights_tz.begin(), weights_tz.end() - 1, weights_tz.end()); weights_tz[0] = groups; - weights_tz[2] = weights_tz[2] / groups; + weights_tz[1] = weights_tz[1] / groups; } } diff --git a/python/paddle/fluid/tests/unittests/mkldnn/test_conv2d_transpose_mkldnn_op.py b/python/paddle/fluid/tests/unittests/mkldnn/test_conv2d_transpose_mkldnn_op.py index a66fd8f379404..a36fc28013bb4 100644 --- a/python/paddle/fluid/tests/unittests/mkldnn/test_conv2d_transpose_mkldnn_op.py +++ b/python/paddle/fluid/tests/unittests/mkldnn/test_conv2d_transpose_mkldnn_op.py @@ -155,14 +155,14 @@ def init_test_case(self): class TestMKLDNNWithGroups(TestConv2DTransposeMKLDNNOp): - def init_test_case(self): + def init_test_case(self): TestConv2DTransposeMKLDNNOp.init_test_case(self) self.pad = [1, 1] self.groups = 2 self.input_size = [2, 4, 5, 5] # NCHW f_c = self.input_size[1] self.filter_size = [f_c, 3, 3, 3] - + class TestMKLDNNWithGroups_NHWC(TestConv2DTransposeMKLDNNOp): def init_test_case(self): From bbc5cc78b47f0832f5271fb59d425514cc7c12fe Mon Sep 17 00:00:00 2001 From: Slawomir Siwek Date: Fri, 26 Nov 2021 21:45:28 +0100 Subject: [PATCH 4/5] Fix oneDNN namespace typo --- paddle/fluid/operators/mkldnn/conv_transpose_mkldnn_op.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paddle/fluid/operators/mkldnn/conv_transpose_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/conv_transpose_mkldnn_op.cc index 51cb0e60a483b..8d82c37aa29dd 100644 --- a/paddle/fluid/operators/mkldnn/conv_transpose_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/conv_transpose_mkldnn_op.cc @@ -25,7 +25,7 @@ namespace operators { using Tensor = framework::Tensor; using framework::DataLayout; -inline mkldnn::memory::dims GetWeightsTz(const Tensor* filter, const int groups) { +inline dnnl::memory::dims GetWeightsTz(const Tensor* filter, const int groups) { auto weights_tz = framework::vectorize(filter->dims()); int g = std::max(groups, 1); int g_dim = (g > 1) ? 1 : 0; From 6e31dd25b623ee879abcfeba0be4829c6f307425 Mon Sep 17 00:00:00 2001 From: Slawomir Siwek Date: Sat, 27 Nov 2021 19:17:38 +0100 Subject: [PATCH 5/5] Fix code format error --- paddle/fluid/operators/mkldnn/conv_transpose_mkldnn_op.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paddle/fluid/operators/mkldnn/conv_transpose_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/conv_transpose_mkldnn_op.cc index 8d82c37aa29dd..4a3d1f455bd26 100644 --- a/paddle/fluid/operators/mkldnn/conv_transpose_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/conv_transpose_mkldnn_op.cc @@ -28,7 +28,7 @@ using framework::DataLayout; inline dnnl::memory::dims GetWeightsTz(const Tensor* filter, const int groups) { auto weights_tz = framework::vectorize(filter->dims()); int g = std::max(groups, 1); - int g_dim = (g > 1) ? 1 : 0; + int g_dim = (g > 1) ? 1 : 0; platform::GetGroupConvWeightsTz(weights_tz, g); // gIOHW -> gOIHW || IOHW -> OIHW std::swap(weights_tz[g_dim + 0], weights_tz[g_dim + 1]);