From be22b7d3c6fa51df9308ff14c2b9ec2f8162e847 Mon Sep 17 00:00:00 2001 From: NetPunk <69072522+Patrick-Star125@users.noreply.github.com> Date: Wed, 22 Nov 2023 16:02:31 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90Hackathon=205th=20No.96=E3=80=91add=20?= =?UTF-8?q?paddle=20unstack=20op=20(#20080)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add paddle mapping * develop test sampes * remove redundant code * revoke code change * add type mapping * add test sample * format code --------- Co-authored-by: Your Name --- src/frontends/paddle/src/op/unstack.cpp | 36 +++++++++++++++ src/frontends/paddle/src/op_table.cpp | 2 + src/frontends/paddle/tests/op_fuzzy.cpp | 5 ++ .../gen_scripts/generate_unstack.py | 46 +++++++++++++++++++ 4 files changed, 89 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..56b18e723449da --- /dev/null +++ b/src/frontends/paddle/src/op/unstack.cpp @@ -0,0 +1,36 @@ +// 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 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(input_shape.rank().get_length()); + } + auto axis = default_opset::Constant::create(element::i32, {}, {dim}); + auto shape = input_shape.get_shape(); + 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(); + 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, axis)); + } + 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 d31a54ff71dfbe..8487bed8827af3 100644 --- a/src/frontends/paddle/tests/op_fuzzy.cpp +++ b/src/frontends/paddle/tests/op_fuzzy.cpp @@ -561,6 +561,11 @@ 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_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 new file mode 100644 index 00000000000000..68eae25608205a --- /dev/null +++ b/src/frontends/paddle/tests/test_models/gen_scripts/generate_unstack.py @@ -0,0 +1,46 @@ +# 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) 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()) + 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) + + 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) + 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()