From a237ff8e6d37a14a7a51c44c01e95abdc65d2384 Mon Sep 17 00:00:00 2001 From: Allen Guo Date: Mon, 29 Aug 2022 11:53:16 +0800 Subject: [PATCH] [IPU] support depthwise_conv2d ops (#45234) * support depthwise_conv2d ops Co-authored-by: Zhixin Yao Co-authored-by: Zhaorui Chen * fix duplicate name Co-authored-by: Zhixin Yao Co-authored-by: Zhaorui Chen --- .../popart_canonicalization/activation_ops.cc | 4 +++ .../ipu/popart_canonicalization/nn_ops.cc | 11 ++++++ .../ipu/test_conv2d_transpose_op_ipu.py | 34 ++++++++++++++++++- .../tests/unittests/ipu/test_conv_op_ipu.py | 33 ++++++++++++++++++ 4 files changed, 81 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/platform/device/ipu/popart_canonicalization/activation_ops.cc b/paddle/fluid/platform/device/ipu/popart_canonicalization/activation_ops.cc index 082bb17cac8..c34cdfd036d 100644 --- a/paddle/fluid/platform/device/ipu/popart_canonicalization/activation_ops.cc +++ b/paddle/fluid/platform/device/ipu/popart_canonicalization/activation_ops.cc @@ -138,6 +138,10 @@ Node *brelu_handler(Graph *graph, Node *node) { Node *gelu_handler(Graph *graph, Node *node) { auto *op = node->Op(); + // In case of the Op has no `approximate` attr. + if (!op->HasAttr("approximate")) { + return activation_op_handler(graph, node, "popart_gelu_v2"); + } auto approximate_ = PADDLE_GET_CONST(bool, op->GetAttr("approximate")); if (approximate_) { return activation_op_handler(graph, node, "popart_gelu_v2"); diff --git a/paddle/fluid/platform/device/ipu/popart_canonicalization/nn_ops.cc b/paddle/fluid/platform/device/ipu/popart_canonicalization/nn_ops.cc index a016647efc9..34ab79fb87d 100644 --- a/paddle/fluid/platform/device/ipu/popart_canonicalization/nn_ops.cc +++ b/paddle/fluid/platform/device/ipu/popart_canonicalization/nn_ops.cc @@ -824,6 +824,14 @@ Node *pad_handler(Graph *graph, Node *node) { {{"mode", mode}}); } +Node *depthwise_conv2d_handler(Graph *graph, Node *node) { + return conv2d_handler(graph, node); +} + +Node *depthwise_conv2d_transpose_handler(Graph *graph, Node *node) { + return conv2d_transpose_handler(graph, node); +} + } // namespace } // namespace ipu } // namespace platform @@ -846,3 +854,6 @@ REGISTER_HANDLER(linear_interp_v2, linear_interp_v2_handler); REGISTER_HANDLER(trilinear_interp_v2, trilinear_interp_v2_handler); REGISTER_HANDLER(data_norm, data_norm_handler); REGISTER_HANDLER(pad3d, pad_handler); +REGISTER_HANDLER(depthwise_conv2d, depthwise_conv2d_handler); +REGISTER_HANDLER(depthwise_conv2d_transpose, + depthwise_conv2d_transpose_handler); diff --git a/python/paddle/fluid/tests/unittests/ipu/test_conv2d_transpose_op_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_conv2d_transpose_op_ipu.py index 6136bf34ffb..824b6b628de 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_conv2d_transpose_op_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_conv2d_transpose_op_ipu.py @@ -150,11 +150,43 @@ class TestCase10(TestBase): class TestCase11(TestBase): - # Depthwise conv2d transpose def set_op_attrs(self): super().set_op_attrs() self.attrs['groups'] = 3 +# depthwise_conv2d_transpose Op +class TestCase12(TestBase): + + def set_feed(self): + data = np.random.uniform(size=[1, 3, 10, 10]) + weight = np.random.uniform(size=[3, 1, 3, 3]) + self.feed_fp32 = { + 'in_0': data.astype(np.float32), + 'in_1': weight.astype(np.float32) + } + self.feed_fp16 = { + 'in_0': data.astype(np.float16), + 'in_1': weight.astype(np.float16) + } + self.feed_shape = [x.shape for x in self.feed_fp32.values()] + self.feed_list = list(self.feed_fp32.keys()) + + def set_op_attrs(self): + self.attrs = {} + self.attrs['groups'] = 3 + + @IPUOpTest.static_graph + def build_model(self): + x = paddle.static.data(name=self.feed_list[0], + shape=self.feed_shape[0], + dtype='float32') + weight = paddle.static.data(name=self.feed_list[1], + shape=self.feed_shape[1], + dtype='float32') + x = paddle.nn.functional.conv2d_transpose(x, weight, **self.attrs) + self.fetch_list = [x.name] + + if __name__ == "__main__": unittest.main() diff --git a/python/paddle/fluid/tests/unittests/ipu/test_conv_op_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_conv_op_ipu.py index 3fac45bbbd9..076d51a5e67 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_conv_op_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_conv_op_ipu.py @@ -133,5 +133,38 @@ class TestCase8(TestBase): self.attrs['padding'] = [1, 2, 2, 3] +# depthwise_conv2d Op +class TestCase9(TestBase): + + def set_feed(self): + data = np.random.uniform(size=[1, 3, 10, 10]) + weight = np.random.uniform(size=[3, 1, 3, 3]) + self.feed_fp32 = { + 'in_0': data.astype(np.float32), + 'in_1': weight.astype(np.float32) + } + self.feed_fp16 = { + 'in_0': data.astype(np.float16), + 'in_1': weight.astype(np.float16) + } + self.feed_shape = [x.shape for x in self.feed_fp32.values()] + self.feed_list = list(self.feed_fp32.keys()) + + def set_op_attrs(self): + self.attrs = {} + self.attrs['groups'] = 3 + + @IPUOpTest.static_graph + def build_model(self): + x = paddle.static.data(name=self.feed_list[0], + shape=self.feed_shape[0], + dtype='float32') + weight = paddle.static.data(name=self.feed_list[1], + shape=self.feed_shape[1], + dtype='float32') + x = paddle.nn.functional.conv2d(x, weight, **self.attrs) + self.fetch_list = [x.name] + + if __name__ == "__main__": unittest.main() -- GitLab