From c7f46cbc8a096a8ff548befd9743dea86df19595 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 26 Sep 2023 13:43:43 +0000 Subject: [PATCH 1/7] add paddle mapping --- src/frontends/paddle/src/op/unstack.cpp | 30 ++++++++++++++++ src/frontends/paddle/src/op_table.cpp | 2 ++ src/frontends/paddle/tests/op_fuzzy.cpp | 1 + .../gen_scripts/generate_unstack.py | 35 +++++++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 src/frontends/paddle/src/op/unstack.cpp create mode 100644 src/frontends/paddle/tests/test_models/gen_scripts/generate_unstack.py diff --git a/src/frontends/paddle/src/op/unstack.cpp b/src/frontends/paddle/src/op/unstack.cpp new file mode 100644 index 00000000000000..9871ba9e48ad6e --- /dev/null +++ b/src/frontends/paddle/src/op/unstack.cpp @@ -0,0 +1,30 @@ +// Copyright (C) 2018-2023 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 unstack(const NodeContext& node) { + auto data = node.get_input("X"); + auto dim = node.get_attribute("axis"); + auto axis = std::make_shared(ov::element::i32, Shape{}, {dim}); + auto shape = data.get_shape() + auto num_or_sections = std::make_shared(ov::element::i32, Shape{}, data.shape[dim]); + auto split_outputs = std::make_shared(data, axis, num_or_sections); + NamedOutputs named_outputs; + auto out_names = node.get_output_names(); + auto it = std::find(out_names.begin(), out_names.end(), "Out"); + for (const auto& split_output : split_outputs) { + named_outputs[*it].push_back(std::make_shared(split_output)); + } + return named_outputs; +} +} // 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 e3e39ebf2e4ec9..f701065569517e 100644 --- a/src/frontends/paddle/src/op_table.cpp +++ b/src/frontends/paddle/src/op_table.cpp @@ -120,6 +120,7 @@ OP_CONVERTER(top_k_v2); OP_CONVERTER(transpose2); OP_CONVERTER(trilinear_interp_v2); OP_CONVERTER(unsqueeze); +OP_CONVERTER(unstack); OP_CONVERTER(where); OP_CONVERTER(while_); OP_CONVERTER(write_to_array); @@ -249,6 +250,7 @@ std::map get_supported_ops() { {"transpose2", op::transpose2}, {"trilinear_interp_v2", op::trilinear_interp_v2}, {"unsqueeze2", op::unsqueeze}, + {"unstack", op::unstack}, {"where", op::where}, {"while", op::while_}, {"write_to_array", op::write_to_array}, diff --git a/src/frontends/paddle/tests/op_fuzzy.cpp b/src/frontends/paddle/tests/op_fuzzy.cpp index 98c8c1af5970c9..0d882c4fc3e4f9 100644 --- a/src/frontends/paddle/tests/op_fuzzy.cpp +++ b/src/frontends/paddle/tests/op_fuzzy.cpp @@ -565,6 +565,7 @@ static const std::vector models{ std::string("trilinear_upsample_scales2/trilinear_upsample_scales2.pdmodel"), std::string("trilinear_upsample_true_0/trilinear_upsample_true_0.pdmodel"), std::string("unsqueeze"), + std::string("unstack_1") std::string("where_1"), std::string("where_2"), std::string("where_3"), diff --git a/src/frontends/paddle/tests/test_models/gen_scripts/generate_unstack.py b/src/frontends/paddle/tests/test_models/gen_scripts/generate_unstack.py new file mode 100644 index 00000000000000..f0d2d523e3c096 --- /dev/null +++ b/src/frontends/paddle/tests/test_models/gen_scripts/generate_unstack.py @@ -0,0 +1,35 @@ +# Copyright (C) 2018-2023 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +# +# unstack paddle model generator +# +import paddle +import numpy as np +from save_model import saveModel +import sys + + +def unstack(name: str, x, axis): + paddle.enable_static() + + with paddle.static.program_guard(paddle.static.Program(), paddle.static.Program()): + x_node = paddle.static.data(name="x", shape=x.shape, dtype=x.dtype) + out = paddle.unstack(x_node, axis) + place = paddle.CPUPlace() + exe = paddle.static.Executor(place) + exe.run(paddle.static.default_startup_program()) + outs = exe.run(feed={"x": x}, fetch_list=[out]) + saveModel(name, exe, feedkeys=['x'], fetchlist=[out], inputs=[x], outputs=[outs], + target_dir=sys.argv[1]) + + return outs + + +def main(): + dtype = np.float32 + x = np.random.randn(2, 3, 4).astype(dtype) + unstack(name='unstack_1', x=x, axis=0) + +if __name__ == "__main__": + main() \ No newline at end of file From 9535a8d3b3112129c47c1a5daa98695c08e5ad91 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 28 Sep 2023 02:43:10 +0000 Subject: [PATCH 2/7] develop test sampes --- src/frontends/paddle/src/op/unstack.cpp | 16 ++++++++++------ src/frontends/paddle/tests/op_fuzzy.cpp | 4 +++- .../test_models/gen_scripts/generate_unstack.py | 13 ++++++++++--- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/frontends/paddle/src/op/unstack.cpp b/src/frontends/paddle/src/op/unstack.cpp index 9871ba9e48ad6e..1030c02f03d5d3 100644 --- a/src/frontends/paddle/src/op/unstack.cpp +++ b/src/frontends/paddle/src/op/unstack.cpp @@ -12,15 +12,19 @@ namespace op { NamedOutputs unstack(const NodeContext& node) { auto data = node.get_input("X"); auto dim = node.get_attribute("axis"); - auto axis = std::make_shared(ov::element::i32, Shape{}, {dim}); - auto shape = data.get_shape() - auto num_or_sections = std::make_shared(ov::element::i32, Shape{}, data.shape[dim]); - auto split_outputs = std::make_shared(data, axis, num_or_sections); + if (dim < 0) { + dim = dim + data.get_partial_shape().rank().get_length(); + } + auto axis = default_opset::Constant::create(element::i32, {}, {dim}); + auto shape = data.get_shape(); + auto splits = std::make_shared(data, axis, shape[dim]); + auto split_outputs = splits->outputs(); NamedOutputs named_outputs; auto out_names = node.get_output_names(); - auto it = std::find(out_names.begin(), out_names.end(), "Out"); + auto it = std::find(out_names.begin(), out_names.end(), "Y"); + PADDLE_OP_CHECK(node, it != out_names.end(), "Expected output not found"); for (const auto& split_output : split_outputs) { - named_outputs[*it].push_back(std::make_shared(split_output)); + named_outputs[*it].push_back(std::make_shared(split_output)); } return named_outputs; } diff --git a/src/frontends/paddle/tests/op_fuzzy.cpp b/src/frontends/paddle/tests/op_fuzzy.cpp index 0d882c4fc3e4f9..db2e2972fcf798 100644 --- a/src/frontends/paddle/tests/op_fuzzy.cpp +++ b/src/frontends/paddle/tests/op_fuzzy.cpp @@ -565,7 +565,9 @@ static const std::vector models{ std::string("trilinear_upsample_scales2/trilinear_upsample_scales2.pdmodel"), std::string("trilinear_upsample_true_0/trilinear_upsample_true_0.pdmodel"), std::string("unsqueeze"), - std::string("unstack_1") + std::string("unstack_1"), + std::string("unstack_2"), + std::string("unstack_3"), std::string("where_1"), std::string("where_2"), std::string("where_3"), diff --git a/src/frontends/paddle/tests/test_models/gen_scripts/generate_unstack.py b/src/frontends/paddle/tests/test_models/gen_scripts/generate_unstack.py index f0d2d523e3c096..87fbc0bee74b3b 100644 --- a/src/frontends/paddle/tests/test_models/gen_scripts/generate_unstack.py +++ b/src/frontends/paddle/tests/test_models/gen_scripts/generate_unstack.py @@ -20,8 +20,7 @@ def unstack(name: str, x, axis): exe = paddle.static.Executor(place) exe.run(paddle.static.default_startup_program()) outs = exe.run(feed={"x": x}, fetch_list=[out]) - saveModel(name, exe, feedkeys=['x'], fetchlist=[out], inputs=[x], outputs=[outs], - target_dir=sys.argv[1]) + saveModel(name, exe, feedkeys=['x'], fetchlist=out, inputs=[x], outputs=outs, target_dir=sys.argv[1]) return outs @@ -31,5 +30,13 @@ def main(): x = np.random.randn(2, 3, 4).astype(dtype) unstack(name='unstack_1', x=x, axis=0) + dtype = np.int32 + x = np.random.randn(2, 3, 4).astype(dtype) + unstack(name='unstack_2', x=x, axis=1) + + dtype = np.int64 + x = np.random.randn(3, 4).astype(dtype) + unstack(name='unstack_3', x=x, axis=-1) + if __name__ == "__main__": - main() \ No newline at end of file + main() From 5eab5161235e60dc5a284f7690f8b04fb960b517 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 20 Oct 2023 12:48:05 +0000 Subject: [PATCH 3/7] remove redundant code --- src/frontends/paddle/src/op/unstack.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/frontends/paddle/src/op/unstack.cpp b/src/frontends/paddle/src/op/unstack.cpp index 1030c02f03d5d3..ecb85b2ff50492 100644 --- a/src/frontends/paddle/src/op/unstack.cpp +++ b/src/frontends/paddle/src/op/unstack.cpp @@ -12,9 +12,6 @@ namespace op { NamedOutputs unstack(const NodeContext& node) { auto data = node.get_input("X"); auto dim = node.get_attribute("axis"); - if (dim < 0) { - dim = dim + data.get_partial_shape().rank().get_length(); - } auto axis = default_opset::Constant::create(element::i32, {}, {dim}); auto shape = data.get_shape(); auto splits = std::make_shared(data, axis, shape[dim]); From c27e7e27eeb7cb19f272be1b0ee04a5e7ac9fe9a Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 7 Nov 2023 03:40:28 +0000 Subject: [PATCH 4/7] revoke code change --- src/frontends/paddle/src/op/unstack.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/frontends/paddle/src/op/unstack.cpp b/src/frontends/paddle/src/op/unstack.cpp index ecb85b2ff50492..0e34511e1062f7 100644 --- a/src/frontends/paddle/src/op/unstack.cpp +++ b/src/frontends/paddle/src/op/unstack.cpp @@ -12,9 +12,12 @@ namespace op { NamedOutputs unstack(const NodeContext& node) { auto data = node.get_input("X"); auto dim = node.get_attribute("axis"); + if (dim < 0) { + dim = dim + data.get_partial_shape().rank().get_length(); + } auto axis = default_opset::Constant::create(element::i32, {}, {dim}); auto shape = data.get_shape(); - auto splits = std::make_shared(data, axis, shape[dim]); + auto splits = std::make_shared(data, axis, shape.at(dim)); auto split_outputs = splits->outputs(); NamedOutputs named_outputs; auto out_names = node.get_output_names(); From 518a8e882bea1e68cfb7542755ff8df6e553386e Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 8 Nov 2023 03:29:56 +0000 Subject: [PATCH 5/7] add type mapping --- src/frontends/paddle/src/op/unstack.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/frontends/paddle/src/op/unstack.cpp b/src/frontends/paddle/src/op/unstack.cpp index 0e34511e1062f7..9ea44a5a83eeb3 100644 --- a/src/frontends/paddle/src/op/unstack.cpp +++ b/src/frontends/paddle/src/op/unstack.cpp @@ -13,7 +13,7 @@ NamedOutputs unstack(const NodeContext& node) { auto data = node.get_input("X"); auto dim = node.get_attribute("axis"); if (dim < 0) { - dim = dim + data.get_partial_shape().rank().get_length(); + dim = dim + static_cast(data.get_partial_shape().rank().get_length()); } auto axis = default_opset::Constant::create(element::i32, {}, {dim}); auto shape = data.get_shape(); From bb64a50c9921057da5c2807474a56b408c546dff Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 8 Nov 2023 09:21:39 +0000 Subject: [PATCH 6/7] add test sample --- src/frontends/paddle/src/op/unstack.cpp | 12 ++++++++---- src/frontends/paddle/tests/op_fuzzy.cpp | 2 ++ .../test_models/gen_scripts/generate_unstack.py | 6 +++++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/frontends/paddle/src/op/unstack.cpp b/src/frontends/paddle/src/op/unstack.cpp index 9ea44a5a83eeb3..0577bdc0f4e531 100644 --- a/src/frontends/paddle/src/op/unstack.cpp +++ b/src/frontends/paddle/src/op/unstack.cpp @@ -11,12 +11,16 @@ namespace paddle { namespace op { NamedOutputs unstack(const NodeContext& node) { auto data = node.get_input("X"); - auto dim = node.get_attribute("axis"); + auto input_shape = data.get_partial_shape(); + PADDLE_OP_CHECK(node, + input_shape.rank().is_static(), + "rank of input data should be static"); + auto dim = node.get_attribute("axis", 0); if (dim < 0) { - dim = dim + static_cast(data.get_partial_shape().rank().get_length()); + dim = dim + static_cast(input_shape.rank().get_length()); } auto axis = default_opset::Constant::create(element::i32, {}, {dim}); - auto shape = data.get_shape(); + auto shape = input_shape.get_shape(); auto splits = std::make_shared(data, axis, shape.at(dim)); auto split_outputs = splits->outputs(); NamedOutputs named_outputs; @@ -24,7 +28,7 @@ NamedOutputs unstack(const NodeContext& node) { auto it = std::find(out_names.begin(), out_names.end(), "Y"); PADDLE_OP_CHECK(node, it != out_names.end(), "Expected output not found"); for (const auto& split_output : split_outputs) { - named_outputs[*it].push_back(std::make_shared(split_output)); + named_outputs[*it].push_back(std::make_shared(split_output, axis)); } return named_outputs; } diff --git a/src/frontends/paddle/tests/op_fuzzy.cpp b/src/frontends/paddle/tests/op_fuzzy.cpp index db2e2972fcf798..9294f3b72c75b4 100644 --- a/src/frontends/paddle/tests/op_fuzzy.cpp +++ b/src/frontends/paddle/tests/op_fuzzy.cpp @@ -568,6 +568,8 @@ static const std::vector models{ std::string("unstack_1"), std::string("unstack_2"), std::string("unstack_3"), + std::string("unstack_4"), + std::string("unstack_5"), std::string("where_1"), std::string("where_2"), std::string("where_3"), diff --git a/src/frontends/paddle/tests/test_models/gen_scripts/generate_unstack.py b/src/frontends/paddle/tests/test_models/gen_scripts/generate_unstack.py index 87fbc0bee74b3b..68eae25608205a 100644 --- a/src/frontends/paddle/tests/test_models/gen_scripts/generate_unstack.py +++ b/src/frontends/paddle/tests/test_models/gen_scripts/generate_unstack.py @@ -15,7 +15,7 @@ def unstack(name: str, x, axis): with paddle.static.program_guard(paddle.static.Program(), paddle.static.Program()): x_node = paddle.static.data(name="x", shape=x.shape, dtype=x.dtype) - out = paddle.unstack(x_node, axis) + out = paddle.unstack(x_node, axis) if axis is not None else paddle.unstack(x_node) place = paddle.CPUPlace() exe = paddle.static.Executor(place) exe.run(paddle.static.default_startup_program()) @@ -37,6 +37,10 @@ def main(): dtype = np.int64 x = np.random.randn(3, 4).astype(dtype) unstack(name='unstack_3', x=x, axis=-1) + unstack(name='unstack_4', x=x, axis=None) + + x = np.random.randn(2, 1, 4).astype(dtype) + unstack(name='unstack_5', x=x, axis=0) if __name__ == "__main__": main() From e9f0651aa687e63ec16476ad559ab18678d4387b Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 15 Nov 2023 01:52:13 +0000 Subject: [PATCH 7/7] format code --- src/frontends/paddle/src/op/unstack.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/frontends/paddle/src/op/unstack.cpp b/src/frontends/paddle/src/op/unstack.cpp index 0577bdc0f4e531..56b18e723449da 100644 --- a/src/frontends/paddle/src/op/unstack.cpp +++ b/src/frontends/paddle/src/op/unstack.cpp @@ -12,9 +12,7 @@ namespace op { NamedOutputs unstack(const NodeContext& node) { auto data = node.get_input("X"); auto input_shape = data.get_partial_shape(); - PADDLE_OP_CHECK(node, - input_shape.rank().is_static(), - "rank of input data should be static"); + PADDLE_OP_CHECK(node, input_shape.rank().is_static(), "rank of input data should be static"); auto dim = node.get_attribute("axis", 0); if (dim < 0) { dim = dim + static_cast(input_shape.rank().get_length());