From f735e0817dfac76ee3e414681084e0471e108f4f Mon Sep 17 00:00:00 2001 From: liubo-intel Date: Tue, 10 May 2022 11:46:08 +0800 Subject: [PATCH 1/2] Paddle Frontend Op conversion: ROIAlign9,Sqrt,Swish --- src/core/tests/frontend/paddle/op_fuzzy.cpp | 3 ++ .../frontend/paddle/requirements_dev.txt | 2 + .../gen_scripts/generate_roi_align.py | 29 +++++++----- .../test_models/gen_scripts/generate_sqrt.py | 41 +++++++++++++++++ .../test_models/gen_scripts/generate_swish.py | 45 +++++++++++++++++++ src/frontends/paddle/src/default_opset.hpp | 4 +- src/frontends/paddle/src/op/roi_align.cpp | 15 +++++-- src/frontends/paddle/src/op/sqrt.cpp | 21 +++++++++ src/frontends/paddle/src/op/swish.cpp | 23 ++++++++++ src/frontends/paddle/src/op_table.cpp | 4 ++ 10 files changed, 170 insertions(+), 17 deletions(-) create mode 100644 src/core/tests/frontend/paddle/test_models/gen_scripts/generate_sqrt.py create mode 100644 src/core/tests/frontend/paddle/test_models/gen_scripts/generate_swish.py create mode 100644 src/frontends/paddle/src/op/sqrt.cpp create mode 100644 src/frontends/paddle/src/op/swish.cpp diff --git a/src/core/tests/frontend/paddle/op_fuzzy.cpp b/src/core/tests/frontend/paddle/op_fuzzy.cpp index b40ff481a2fe16..84d04a45f29827 100644 --- a/src/core/tests/frontend/paddle/op_fuzzy.cpp +++ b/src/core/tests/frontend/paddle/op_fuzzy.cpp @@ -297,6 +297,7 @@ static const std::vector models{std::string("argmax"), std::string("split_test_dim_int64"), std::string("split_test_list"), std::string("split_test_list_tensor"), + std::string("sqrt_float32"), std::string("squeeze"), std::string("squeeze_null_axes"), std::string("stack_test_float32"), @@ -312,6 +313,8 @@ static const std::vector models{std::string("argmax"), std::string("strided_slice_input2_3"), std::string("strided_slice_input3_1"), std::string("strided_slice_input3_2"), + std::string("swish_default_params"), + std::string("swish_beta"), std::string("tanh"), std::string("trilinear_downsample_false_0"), std::string("trilinear_downsample_false_1"), diff --git a/src/core/tests/frontend/paddle/requirements_dev.txt b/src/core/tests/frontend/paddle/requirements_dev.txt index 477403b86372c0..75dda63235275d 100644 --- a/src/core/tests/frontend/paddle/requirements_dev.txt +++ b/src/core/tests/frontend/paddle/requirements_dev.txt @@ -1,5 +1,7 @@ # PaddlePaddle - generate test models paddlepaddle==2.1.0 +paddledet==2.1.0 +pycocotools gast==0.3.3 numpy~=1.19.2 six~=1.15.0 diff --git a/src/core/tests/frontend/paddle/test_models/gen_scripts/generate_roi_align.py b/src/core/tests/frontend/paddle/test_models/gen_scripts/generate_roi_align.py index 2475a5ed666ba0..c7c708a6d2de11 100644 --- a/src/core/tests/frontend/paddle/test_models/gen_scripts/generate_roi_align.py +++ b/src/core/tests/frontend/paddle/test_models/gen_scripts/generate_roi_align.py @@ -8,6 +8,12 @@ from save_model import saveModel import paddle import sys +# needed by 'aligned' attribute +import os +import importlib +spec = importlib.util.find_spec("ppdet") +sys.path.insert(0, os.path.join(os.path.dirname(spec.origin), 'modeling')) +import ops def make_rois(batch_size, width, height, pooled_width, pooled_height, spatial_scale, roi_per_batch): @@ -34,7 +40,7 @@ def make_rois(batch_size, width, height, pooled_width, pooled_height, spatial_sc return rois, rois_num -def roi_align(name: str, x_data, rois_data, rois_num_data, pooled_height, pooled_width, spatial_scale, sampling_ratio): +def roi_align(name: str, x_data, rois_data, rois_num_data, pooled_height, pooled_width, spatial_scale, sampling_ratio, aligned): paddle.enable_static() with paddle.static.program_guard(paddle.static.Program(), paddle.static.Program()): @@ -44,14 +50,13 @@ def roi_align(name: str, x_data, rois_data, rois_num_data, pooled_height, pooled name='rois', shape=rois_data.shape, dtype=rois_data.dtype) rois_num = paddle.static.data( name='rois_num', shape=rois_num_data.shape, dtype=rois_num_data.dtype) - # TODO: 'aligned' attribute is not supported by Paddle 2.1 - out = paddle.fluid.layers.roi_align(input=x, - rois=rois, - pooled_height=pooled_height, - pooled_width=pooled_width, - spatial_scale=spatial_scale, - sampling_ratio=sampling_ratio, - rois_num=rois_num) + out = ops.roi_align(input=x, + rois=rois, + output_size=(pooled_height, pooled_width), + spatial_scale=spatial_scale, + sampling_ratio=sampling_ratio, + rois_num=rois_num, + aligned=aligned) cpu = paddle.static.cpu_places(1) exe = paddle.static.Executor(cpu[0]) @@ -81,13 +86,14 @@ def main(): pooled_height = 2 pooled_width = 2 sampling_ratio = -1 + aligned = False roi_per_batch = 1 rois, rois_num = make_rois(batch_size, width, height, pooled_width, pooled_height, spatial_scale, roi_per_batch) roi_align("roi_align_test", x, rois, rois_num, pooled_height, - pooled_width, spatial_scale, sampling_ratio) + pooled_width, spatial_scale, sampling_ratio, aligned) batch_size = 1 channels = 3 @@ -101,13 +107,14 @@ def main(): pooled_height = 2 pooled_width = 2 sampling_ratio = 2 + aligned = True roi_per_batch = 2 rois, rois_num = make_rois(batch_size, width, height, pooled_width, pooled_height, spatial_scale, roi_per_batch) roi_align("roi_align_test2", x, rois, rois_num, pooled_height, - pooled_width, spatial_scale, sampling_ratio) + pooled_width, spatial_scale, sampling_ratio, aligned) if __name__ == "__main__": diff --git a/src/core/tests/frontend/paddle/test_models/gen_scripts/generate_sqrt.py b/src/core/tests/frontend/paddle/test_models/gen_scripts/generate_sqrt.py new file mode 100644 index 00000000000000..a9baa482d849a5 --- /dev/null +++ b/src/core/tests/frontend/paddle/test_models/gen_scripts/generate_sqrt.py @@ -0,0 +1,41 @@ +# +# sqrt paddle model generator +# +import numpy as np +from save_model import saveModel +import paddle as pdpd +import sys + + +def sqrt(name: str, x, data_type): + pdpd.enable_static() + + with pdpd.static.program_guard(pdpd.static.Program(), pdpd.static.Program()): + node_x = pdpd.static.data( + name='input_x', shape=x.shape, dtype=data_type) + out = pdpd.sqrt(x=node_x, name='sqrt') + + cpu = pdpd.static.cpu_places(1) + exe = pdpd.static.Executor(cpu[0]) + # startup program will call initializer to initialize the parameters. + exe.run(pdpd.static.default_startup_program()) + + outs = exe.run( + feed={'input_x': x}, + fetch_list=[out]) + + saveModel(name, exe, feedkeys=['input_x'], fetchlist=[out], + inputs=[x], outputs=[outs[0]], target_dir=sys.argv[1]) + + return outs[0] + + +def main(): + + data_type = 'float32' + x = np.array([0.1, 0.2, 0.3, 0.4]).astype(data_type) + sqrt("sqrt_float32", x, data_type) + + +if __name__ == "__main__": + main() diff --git a/src/core/tests/frontend/paddle/test_models/gen_scripts/generate_swish.py b/src/core/tests/frontend/paddle/test_models/gen_scripts/generate_swish.py new file mode 100644 index 00000000000000..9d7d68ae0ec8ed --- /dev/null +++ b/src/core/tests/frontend/paddle/test_models/gen_scripts/generate_swish.py @@ -0,0 +1,45 @@ +# +# swish paddle model generator +# +import numpy as np +from save_model import saveModel +import paddle as pdpd +import sys + + +def swish(name: str, x, data_type, input_beta): + pdpd.enable_static() + + with pdpd.static.program_guard(pdpd.static.Program(), pdpd.static.Program()): + node_x = pdpd.static.data( + name='input_x', shape=x.shape, dtype=data_type) + out = pdpd.fluid.layers.swish(x=node_x, beta=input_beta, name='swish') + + cpu = pdpd.static.cpu_places(1) + exe = pdpd.static.Executor(cpu[0]) + # startup program will call initializer to initialize the parameters. + exe.run(pdpd.static.default_startup_program()) + + outs = exe.run( + feed={'input_x': x}, + fetch_list=[out]) + + saveModel(name, exe, feedkeys=['input_x'], fetchlist=[out], + inputs=[x], outputs=[outs[0]], target_dir=sys.argv[1]) + + return outs[0] + + +def main(): + data_type = 'float32' + input_beta = 1.0 + x = np.random.randn(2, 3).astype(data_type) + swish("swish_default_params", x, data_type, input_beta) + + input_beta = 2.0 + x = np.random.randn(2, 3).astype(data_type) + swish("swish_beta", x, data_type, input_beta) + + +if __name__ == "__main__": + main() diff --git a/src/frontends/paddle/src/default_opset.hpp b/src/frontends/paddle/src/default_opset.hpp index 8eedfd0aff8fec..645412abc71a25 100644 --- a/src/frontends/paddle/src/default_opset.hpp +++ b/src/frontends/paddle/src/default_opset.hpp @@ -2,13 +2,13 @@ // SPDX-License-Identifier: Apache-2.0 // -#include "openvino/opsets/opset8.hpp" +#include "openvino/opsets/opset9.hpp" namespace ov { namespace frontend { namespace paddle { namespace op { -namespace default_opset = ov::opset8; +namespace default_opset = ov::opset9; } // namespace op } // namespace paddle diff --git a/src/frontends/paddle/src/op/roi_align.cpp b/src/frontends/paddle/src/op/roi_align.cpp index 51347182050c0f..eb3ca3fe423bf8 100644 --- a/src/frontends/paddle/src/op/roi_align.cpp +++ b/src/frontends/paddle/src/op/roi_align.cpp @@ -9,12 +9,19 @@ namespace ov { namespace frontend { namespace paddle { namespace op { +using AlignedMode = default_opset::ROIAlign::AlignedMode; +using PoolingMode = default_opset::ROIAlign::PoolingMode; NamedOutputs roi_align(const NodeContext& node) { const auto data_node = node.get_input("X"); const auto roi_node = node.get_input("ROIs"); - // TODO: support 'aligned' feature #82319 const auto aligned = node.get_attribute("aligned", false); - PADDLE_OP_CHECK(node, !aligned, "OpenVINO not support 'aligned' feature!"); + // Paddle only use 'avg' interpolation mode + const auto pooling_mode = PoolingMode::AVG; + AlignedMode aligned_mode; + if (aligned) + aligned_mode = AlignedMode::HALF_PIXEL_FOR_NN; + else + aligned_mode = AlignedMode::ASYMMETRIC; // TODO: support multiple batches #83232 if (data_node.get_partial_shape().rank().is_static() && data_node.get_partial_shape()[0].is_static()) @@ -35,7 +42,6 @@ NamedOutputs roi_align(const NodeContext& node) { auto sampling_ratio = node.get_attribute("sampling_ratio", -1); sampling_ratio = (sampling_ratio <= 0) ? 0 : sampling_ratio; - // Paddle only use 'avg' interpolation mode return node.default_single_output_mapping({std::make_shared(data_node, roi_node, fake_roisNum_node, @@ -43,7 +49,8 @@ NamedOutputs roi_align(const NodeContext& node) { pooled_w, sampling_ratio, spatial_scale, - "avg")}, + pooling_mode, + aligned_mode)}, {"Out"}); } } // namespace op diff --git a/src/frontends/paddle/src/op/sqrt.cpp b/src/frontends/paddle/src/op/sqrt.cpp new file mode 100644 index 00000000000000..9644549f2cc919 --- /dev/null +++ b/src/frontends/paddle/src/op/sqrt.cpp @@ -0,0 +1,21 @@ +// Copyright (C) 2018-2022 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "default_opset.hpp" +#include "openvino/frontend/paddle/node_context.hpp" + +namespace ov { +namespace frontend { +namespace paddle { +namespace op { +NamedOutputs sqrt(const NodeContext& node) { + const auto x = node.get_input("X"); + + return node.default_single_output_mapping({std::make_shared(x)}, {"Out"}); +} + +} // namespace op +} // namespace paddle +} // namespace frontend +} // namespace ov diff --git a/src/frontends/paddle/src/op/swish.cpp b/src/frontends/paddle/src/op/swish.cpp new file mode 100644 index 00000000000000..ea4f437b11de51 --- /dev/null +++ b/src/frontends/paddle/src/op/swish.cpp @@ -0,0 +1,23 @@ +// Copyright (C) 2018-2022 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 +// + +#include "default_opset.hpp" +#include "openvino/frontend/paddle/node_context.hpp" + +namespace ov { +namespace frontend { +namespace paddle { +namespace op { +NamedOutputs swish(const NodeContext& node) { + const auto x = node.get_input("X"); + const float beta = node.get_attribute("beta", 1.0f); + const auto beta_node = default_opset::Constant::create(element::f32, Shape{}, {beta}); + + return node.default_single_output_mapping({std::make_shared(x, beta_node)}, {"Out"}); +} + +} // namespace op +} // namespace paddle +} // namespace frontend +} // namespace ov diff --git a/src/frontends/paddle/src/op_table.cpp b/src/frontends/paddle/src/op_table.cpp index 4a7ec62e848a88..cb4b28c583de4b 100644 --- a/src/frontends/paddle/src/op_table.cpp +++ b/src/frontends/paddle/src/op_table.cpp @@ -81,9 +81,11 @@ OP_CONVERTER(softmax); OP_CONVERTER(softplus); OP_CONVERTER(sigmoid); OP_CONVERTER(split); +OP_CONVERTER(sqrt); OP_CONVERTER(squeeze); OP_CONVERTER(stack); OP_CONVERTER(strided_slice); +OP_CONVERTER(swish); OP_CONVERTER(tanh); OP_CONVERTER(transpose2); OP_CONVERTER(trilinear_interp_v2); @@ -173,9 +175,11 @@ std::map get_supported_ops() { {"softplus", op::softplus}, {"sigmoid", op::sigmoid}, {"split", op::split}, + {"sqrt", op::sqrt}, {"squeeze2", op::squeeze}, {"stack", op::stack}, {"strided_slice", op::strided_slice}, + {"swish", op::swish}, {"sync_batch_norm", op::batch_norm}, {"tanh", op::tanh}, {"transpose2", op::transpose2}, From fbcf0839baa580e9ad7c72c854e7c47e955d0aad Mon Sep 17 00:00:00 2001 From: liubo-intel Date: Mon, 16 May 2022 22:40:26 +0800 Subject: [PATCH 2/2] modify import ppdet way based on the latest master branch --- src/core/tests/frontend/paddle/requirements_dev.txt | 2 -- .../paddle/test_models/gen_scripts/generate_roi_align.py | 7 +------ 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/src/core/tests/frontend/paddle/requirements_dev.txt b/src/core/tests/frontend/paddle/requirements_dev.txt index 75dda63235275d..477403b86372c0 100644 --- a/src/core/tests/frontend/paddle/requirements_dev.txt +++ b/src/core/tests/frontend/paddle/requirements_dev.txt @@ -1,7 +1,5 @@ # PaddlePaddle - generate test models paddlepaddle==2.1.0 -paddledet==2.1.0 -pycocotools gast==0.3.3 numpy~=1.19.2 six~=1.15.0 diff --git a/src/core/tests/frontend/paddle/test_models/gen_scripts/generate_roi_align.py b/src/core/tests/frontend/paddle/test_models/gen_scripts/generate_roi_align.py index c7c708a6d2de11..0137dcdc54c10f 100644 --- a/src/core/tests/frontend/paddle/test_models/gen_scripts/generate_roi_align.py +++ b/src/core/tests/frontend/paddle/test_models/gen_scripts/generate_roi_align.py @@ -7,13 +7,8 @@ import numpy as np from save_model import saveModel import paddle -import sys -# needed by 'aligned' attribute -import os -import importlib -spec = importlib.util.find_spec("ppdet") -sys.path.insert(0, os.path.join(os.path.dirname(spec.origin), 'modeling')) import ops +import sys def make_rois(batch_size, width, height, pooled_width, pooled_height, spatial_scale, roi_per_batch):