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 flip op #15828

Merged
merged 18 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/frontends/paddle/src/op/flip.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include "default_opset.hpp"
#include "openvino/frontend/paddle/node_context.hpp"

#define out(a) std::cout << a << std::endl;
Asthestarsfalll marked this conversation as resolved.
Show resolved Hide resolved

namespace ov {
namespace frontend {
namespace paddle {
namespace op {
NamedOutputs flip(const NodeContext& node) {
const auto data_node = node.get_input("X");
const auto axes = node.get_attribute<std::vector<int32_t>>("axis");
auto axes_length = axes.size();
const auto starts =
default_opset::Constant::create(element::i32,
{axes_length},
std::vector<int32_t>(axes_length, std::numeric_limits<int32_t>::max()));
const auto stops =
default_opset::Constant::create(element::i32,
{axes_length},
std::vector<int32_t>(axes_length, std::numeric_limits<int32_t>::min()));
const auto steps =
default_opset::Constant::create(element::i32, {axes_length}, std::vector<int32_t>(axes_length, -1));
const auto axes_node = default_opset::Constant::create(element::i32, {axes_length}, axes);

return node.default_single_output_mapping(
{std::make_shared<default_opset::Slice>(data_node, starts, stops, steps, axes_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 @@ -41,6 +41,7 @@ OP_CONVERTER(elementwise_sub);
OP_CONVERTER(embedding);
OP_CONVERTER(exp);
OP_CONVERTER(expand_v2);
OP_CONVERTER(flip);
OP_CONVERTER(fill_any_like);
OP_CONVERTER(fill_constant_batch_size_like);
OP_CONVERTER(fill_constant);
Expand Down Expand Up @@ -155,6 +156,7 @@ std::map<std::string, CreatorFunction> get_supported_ops() {
{"fill_constant_batch_size_like", op::fill_constant_batch_size_like},
{"fill_constant", op::fill_constant},
{"flatten_contiguous_range", op::flatten_contiguous_range},
{"flip", op::flip},
{"floor", op::floor},
{"gather", op::gather},
{"gather_nd", op::gather_nd},
Expand Down
7 changes: 7 additions & 0 deletions src/frontends/paddle/tests/op_fuzzy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ static const std::vector<std::string> models{
std::string("expand_v2_tensor_list"),
std::string("expand_v2_tensor_list2"),
std::string("exp_test_float32"),
std::string("flip_1"),
std::string("flip_2"),
std::string("flip_3"),
std::string("flip_4"),
std::string("flip_5"),
std::string("flip_dynamic_1"),
std::string("flip_dynamic_2"),
std::string("fill_any_like"),
std::string("fill_any_like_f16"),
std::string("fill_any_like_f32"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#
# flip paddle model generator
#
import numpy as np
from save_model import saveModel
import paddle
import sys


def flip(name: str, x, axis, is_dynamic=False):
paddle.enable_static()

with paddle.static.program_guard(paddle.static.Program(), paddle.static.Program()):
if is_dynamic:
data = paddle.static.data(name='x', shape=(-1, ) * len(x.shape), dtype=x.dtype)
else:
data = paddle.static.data(name='x', shape=x.shape, dtype=x.dtype)
out = paddle.flip(data, 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},
fetch_list=[out])

saveModel(name, exe, feedkeys=['x'], fetchlist=[out], inputs=[
x], outputs=[outs[0]], target_dir=sys.argv[1])

return outs[0]


def main():
data_type = 'int32'
axis = [2, 3]
x = np.random.randint(0, 5, (2, 3, 4, 5)).astype(data_type)
flip("flip_1", x, axis)

data_type = 'float32'
axis = [-1, -3]
# axis = [3, 1]
x = np.random.randn(3, 2, 1, 5).astype(data_type)
flip("flip_2", x, axis)

data_type = 'float32'
axis = [0, 1]
x = np.random.randn(1, 1, 1, 1).astype(data_type)
flip("flip_3", x, axis)

data_type = 'int64'
axis = 1
x = np.random.randint(-1, 3, (5, 3, 1, 1)).astype(data_type)
flip("flip_4", x, axis)

data_type = 'float32'
axis = -1
x = np.random.randn(1).astype(data_type)
flip("flip_5", x, axis)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can have "bool" type tested?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested bool type before, but it occurs error that openvino test do not support bool output

data_type = 'int64'
axis = 3
x = np.random.randint(-5, 5, (1, 1, 4, 1)).astype(data_type)
flip("flip_dynamic_1", x, axis, True)

data_type = 'float32'
axis = [-1, -2]
x = np.random.randn(1, 4, 1).astype(data_type)
flip("flip_dynamic_2", x, axis, True)

if __name__ == "__main__":
main()