From d52efb96ea9879ca7cc2091a9d0f4286d24c657e Mon Sep 17 00:00:00 2001 From: PuQing Date: Fri, 19 May 2023 16:57:11 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90PaddlePaddle=20Hackathon=204=E3=80=91a?= =?UTF-8?q?dd=20paddle=20index=5Fselect=20op=20(#15851)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add index_select * fix func error * fix index size error * add axis native test * set default dim 0 * fix supported md --------- Co-authored-by: Xiuchuan Zhai --- .../Supported_Frameworks_Layers.md | 6 +- src/frontends/paddle/src/op/index_select.cpp | 25 ++++++++ src/frontends/paddle/src/op_table.cpp | 2 + src/frontends/paddle/tests/op_fuzzy.cpp | 4 ++ .../gen_scripts/generate_index_select.py | 60 +++++++++++++++++++ 5 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 src/frontends/paddle/src/op/index_select.cpp create mode 100644 src/frontends/paddle/tests/test_models/gen_scripts/generate_index_select.py diff --git a/docs/MO_DG/prepare_model/Supported_Frameworks_Layers.md b/docs/MO_DG/prepare_model/Supported_Frameworks_Layers.md index 28ee56bf219..e3311fb23d2 100644 --- a/docs/MO_DG/prepare_model/Supported_Frameworks_Layers.md +++ b/docs/MO_DG/prepare_model/Supported_Frameworks_Layers.md @@ -752,7 +752,8 @@ paddlepaddle >= 2.1 greater_than group_norm hard_sigmoid - hard_swish + hard_swish + index_select layer_norm leaky_relu less_than @@ -792,7 +793,8 @@ paddlepaddle >= 2.1 sigmoid slice softmax - softplus + softplus + softshrink split sqrt squeeze diff --git a/src/frontends/paddle/src/op/index_select.cpp b/src/frontends/paddle/src/op/index_select.cpp new file mode 100644 index 00000000000..7a85619f5a4 --- /dev/null +++ b/src/frontends/paddle/src/op/index_select.cpp @@ -0,0 +1,25 @@ +// 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 index_select(const NodeContext& node) { + auto data = node.get_input("X"); + auto index = node.get_input("Index"); + Output axis_node; + const int axis_value = node.get_attribute("dim", 0); + axis_node = default_opset::Constant::create(element::i32, Shape{}, {axis_value}); + return node.default_single_output_mapping({std::make_shared(data, index, axis_node)}, + {"Out"}); +} + +} // namespace op +} // namespace paddle +} // namespace frontend +} // namespace ov \ No newline at end of file diff --git a/src/frontends/paddle/src/op_table.cpp b/src/frontends/paddle/src/op_table.cpp index 22c907fb800..c23bc138106 100644 --- a/src/frontends/paddle/src/op_table.cpp +++ b/src/frontends/paddle/src/op_table.cpp @@ -54,6 +54,7 @@ OP_CONVERTER(grid_sampler); OP_CONVERTER(group_norm); OP_CONVERTER(hard_sigmoid); OP_CONVERTER(hard_swish); +OP_CONVERTER(index_select); OP_CONVERTER(layer_norm); OP_CONVERTER(leaky_relu); OP_CONVERTER(less_than); @@ -170,6 +171,7 @@ std::map get_supported_ops() { {"group_norm", op::group_norm}, {"hard_sigmoid", op::hard_sigmoid}, {"hard_swish", op::hard_swish}, + {"index_select", op::index_select}, {"layer_norm", op::layer_norm}, {"leaky_relu", op::leaky_relu}, {"less_than", op::less_than}, diff --git a/src/frontends/paddle/tests/op_fuzzy.cpp b/src/frontends/paddle/tests/op_fuzzy.cpp index f71e0524b17..c6db82b7a6b 100644 --- a/src/frontends/paddle/tests/op_fuzzy.cpp +++ b/src/frontends/paddle/tests/op_fuzzy.cpp @@ -243,6 +243,10 @@ static const std::vector models{ std::string("group_norm_3/group_norm_3.pdmodel"), std::string("hard_sigmoid"), std::string("hard_swish"), + std::string("index_select_axis_0"), + std::string("index_select_axis_1"), + std::string("index_select_axis_native_-1"), + std::string("index_select_axis_native_-2"), std::string("layer_norm/layer_norm.pdmodel"), std::string("layer_norm_noall/layer_norm_noall.pdmodel"), std::string("layer_norm_noscale/layer_norm_noscale.pdmodel"), diff --git a/src/frontends/paddle/tests/test_models/gen_scripts/generate_index_select.py b/src/frontends/paddle/tests/test_models/gen_scripts/generate_index_select.py new file mode 100644 index 00000000000..f1f2bdcd39e --- /dev/null +++ b/src/frontends/paddle/tests/test_models/gen_scripts/generate_index_select.py @@ -0,0 +1,60 @@ +# Copyright (C) 2018-2023 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +# +# index_select paddle model generator +# +import numpy as np +from paddle.fluid import param_attr +from save_model import saveModel +import paddle +import sys + +data_type = "float32" + + +def index_select(name: str, x, index, axis): + paddle.enable_static() + + with paddle.static.program_guard(paddle.static.Program(), paddle.static.Program()): + data = paddle.static.data(name="x", shape=x.shape, dtype=data_type) + tensor_index = paddle.static.data( + name="index", shape=index.shape, dtype="int32" + ) + out = paddle.index_select( + data, + index=tensor_index, + axis=axis, + ) + + cpu = paddle.static.cpu_places(1) + exe = paddle.static.Executor(cpu[0]) + # startup program will call initializer to initialize the parameters. + exe.run(paddle.static.default_startup_program()) + + outs = exe.run(feed={"x": x, "index": index}, fetch_list=[out]) + + saveModel( + name, + exe, + feedkeys=["x", "index"], + fetchlist=[out], + inputs=[x, index], + outputs=[outs[0]], + target_dir=sys.argv[1], + ) + + return outs[0] + + +def main(): + x = np.random.rand(8, 24, 32).astype(data_type) + index = np.random.randint(0, 7, (5)).astype("int32") + index_select("index_select_axis_0", x, index, axis=0) + index_select("index_select_axis_1", x, index, axis=1) + index_select("index_select_axis_native_-1", x, index, axis=-1) + index_select("index_select_axis_native_-2", x, index, axis=-2) + + +if __name__ == "__main__": + main()