-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
【PaddlePaddle Hackathon 4】add paddle flip op (#15828)
* add flip op * fix * fix signedness error * update flip * use Slice * remove redundant codes * unify flip and reverse * fix bug --------- Co-authored-by: cecilia peng <cecilia.peng@intel.com>
- Loading branch information
1 parent
d52efb9
commit 36dbe95
Showing
6 changed files
with
139 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (C) 2018-2023 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include "reverse_op.hpp" | ||
|
||
namespace ov { | ||
namespace frontend { | ||
namespace paddle { | ||
namespace op { | ||
NamedOutputs flip(const NodeContext& node) { | ||
return reverse_op(node); | ||
} | ||
} // namespace op | ||
} // namespace paddle | ||
} // namespace frontend | ||
} // namespace ov |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (C) 2018-2023 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
#pragma once | ||
|
||
#include "default_opset.hpp" | ||
#include "openvino/frontend/paddle/node_context.hpp" | ||
|
||
namespace ov { | ||
namespace frontend { | ||
namespace paddle { | ||
namespace op { | ||
namespace { | ||
NamedOutputs reverse_op(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 | ||
} // namespace op | ||
} // namespace paddle | ||
} // namespace frontend | ||
} // namespace ov |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/frontends/paddle/tests/test_models/gen_scripts/generate_flip.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
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() |