【PaddlePaddle Hackathon 4】add paddle index_select op (#15851)

* 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 <xiuchuan.zhai@intel.com>
This commit is contained in:
PuQing 2023-05-19 16:57:11 +08:00 committed by GitHub
parent c72a950701
commit d52efb96ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 95 additions and 2 deletions

View File

@ -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

View File

@ -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

View File

@ -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<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},

View File

@ -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"),

View File

@ -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()