forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
【Hackathon 5th No.96】add paddle unstack op (openvinotoolkit#20080)
* add paddle mapping * develop test sampes * remove redundant code * revoke code change * add type mapping * add test sample * format code --------- Co-authored-by: Your Name <you@example.com>
- Loading branch information
Showing
4 changed files
with
89 additions
and
0 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,36 @@ | ||
// 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 unstack(const NodeContext& node) { | ||
auto data = node.get_input("X"); | ||
auto input_shape = data.get_partial_shape(); | ||
PADDLE_OP_CHECK(node, input_shape.rank().is_static(), "rank of input data should be static"); | ||
auto dim = node.get_attribute<int32_t>("axis", 0); | ||
if (dim < 0) { | ||
dim = dim + static_cast<int32_t>(input_shape.rank().get_length()); | ||
} | ||
auto axis = default_opset::Constant::create(element::i32, {}, {dim}); | ||
auto shape = input_shape.get_shape(); | ||
auto splits = std::make_shared<default_opset::Split>(data, axis, shape.at(dim)); | ||
auto split_outputs = splits->outputs(); | ||
NamedOutputs named_outputs; | ||
auto out_names = node.get_output_names(); | ||
auto it = std::find(out_names.begin(), out_names.end(), "Y"); | ||
PADDLE_OP_CHECK(node, it != out_names.end(), "Expected output not found"); | ||
for (const auto& split_output : split_outputs) { | ||
named_outputs[*it].push_back(std::make_shared<default_opset::Squeeze>(split_output, axis)); | ||
} | ||
return named_outputs; | ||
} | ||
} // 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
46 changes: 46 additions & 0 deletions
46
src/frontends/paddle/tests/test_models/gen_scripts/generate_unstack.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,46 @@ | ||
# Copyright (C) 2018-2023 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# | ||
# unstack paddle model generator | ||
# | ||
import paddle | ||
import numpy as np | ||
from save_model import saveModel | ||
import sys | ||
|
||
|
||
def unstack(name: str, x, axis): | ||
paddle.enable_static() | ||
|
||
with paddle.static.program_guard(paddle.static.Program(), paddle.static.Program()): | ||
x_node = paddle.static.data(name="x", shape=x.shape, dtype=x.dtype) | ||
out = paddle.unstack(x_node, axis) if axis is not None else paddle.unstack(x_node) | ||
place = paddle.CPUPlace() | ||
exe = paddle.static.Executor(place) | ||
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, target_dir=sys.argv[1]) | ||
|
||
return outs | ||
|
||
|
||
def main(): | ||
dtype = np.float32 | ||
x = np.random.randn(2, 3, 4).astype(dtype) | ||
unstack(name='unstack_1', x=x, axis=0) | ||
|
||
dtype = np.int32 | ||
x = np.random.randn(2, 3, 4).astype(dtype) | ||
unstack(name='unstack_2', x=x, axis=1) | ||
|
||
dtype = np.int64 | ||
x = np.random.randn(3, 4).astype(dtype) | ||
unstack(name='unstack_3', x=x, axis=-1) | ||
unstack(name='unstack_4', x=x, axis=None) | ||
|
||
x = np.random.randn(2, 1, 4).astype(dtype) | ||
unstack(name='unstack_5', x=x, axis=0) | ||
|
||
if __name__ == "__main__": | ||
main() |