Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【PaddlePaddle Hackathon 4】add paddle index_select op #15851

Merged
merged 12 commits into from
May 19, 2023
6 changes: 4 additions & 2 deletions docs/MO_DG/prepare_model/Supported_Frameworks_Layers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -792,7 +793,8 @@ paddlepaddle >= 2.1
sigmoid
slice
softmax
softplus
softplus
softshrink
split
sqrt
squeeze
Expand Down
25 changes: 25 additions & 0 deletions src/frontends/paddle/src/op/index_select.cpp
Original file line number Diff line number Diff line change
@@ -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<Node> axis_node;
const int axis_value = node.get_attribute<int>("dim", 0);
axis_node = default_opset::Constant::create(element::i32, Shape{}, {axis_value});
return node.default_single_output_mapping({std::make_shared<default_opset::Gather>(data, index, axis_node)},
{"Out"});
}

} // namespace op
} // namespace paddle
} // namespace frontend
} // namespace ov
2 changes: 2 additions & 0 deletions src/frontends/paddle/src/op_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -169,6 +170,7 @@ std::map<std::string, CreatorFunction> 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},
Expand Down
4 changes: 4 additions & 0 deletions src/frontends/paddle/tests/op_fuzzy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ static const std::vector<std::string> 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"),
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
AndPuQing marked this conversation as resolved.
Show resolved Hide resolved
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()