-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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.94】add paddle partial_op (#20103)
* add partial_concat and partial_sum * add static_cast * merge into partial_ops * update * add shape check * support for paddle2.4 --------- Co-authored-by: cecilia peng <[email protected]> Co-authored-by: Yu Xu <[email protected]>
- Loading branch information
1 parent
e2c9f24
commit 93727d1
Showing
6 changed files
with
210 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,23 @@ | ||
// Copyright (C) 2018-2023 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include "partial_ops.hpp" | ||
|
||
namespace ov { | ||
namespace frontend { | ||
namespace paddle { | ||
namespace op { | ||
|
||
NamedOutputs partial_sum(const NodeContext& node) { | ||
return partial_ops(node, "sum"); | ||
} | ||
|
||
NamedOutputs partial_concat(const NodeContext& node) { | ||
return partial_ops(node, "concat"); | ||
} | ||
|
||
} // 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// 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 partial_ops(const NodeContext& node, const std::string type) { | ||
auto x = node.get_ng_inputs("X"); | ||
const auto start_index = node.get_attribute<int>("start_index"); | ||
const auto length = node.get_attribute<int>("length"); | ||
PADDLE_OP_CHECK(node, x[0].get_partial_shape().rank().get_length() == 2, "partial ops only support 2-D Tensor"); | ||
|
||
int end_index; | ||
if (length < 0) { | ||
// Negative values for all elements after start_index on second dim. | ||
end_index = static_cast<int>(x[0].get_shape()[1]); | ||
} else { | ||
end_index = start_index + length; | ||
} | ||
|
||
const auto start_node = std::make_shared<default_opset::Constant>(element::i32, Shape{1}, start_index); | ||
const auto end_node = std::make_shared<default_opset::Constant>(element::i32, Shape{1}, end_index); | ||
const auto one_node = std::make_shared<default_opset::Constant>(element::i32, Shape{1}, 1); | ||
|
||
auto left = std::make_shared<default_opset::Slice>(x[0], start_node, end_node, one_node, one_node); | ||
auto right = std::make_shared<default_opset::Slice>(x[1], start_node, end_node, one_node, one_node); | ||
|
||
if (type == "sum") | ||
return node.default_single_output_mapping({std::make_shared<default_opset::Add>(left, right)}, {"Out"}); | ||
return node.default_single_output_mapping({std::make_shared<default_opset::Concat>(NodeVector{left, right}, 1)}, | ||
{"Out"}); | ||
} | ||
|
||
} // 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
66 changes: 66 additions & 0 deletions
66
src/frontends/paddle/tests/test_models/gen_scripts/generate_partial_concat.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,66 @@ | ||
# Copyright (C) 2018-2023 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# | ||
# partial_concat paddle model generator | ||
# | ||
import numpy as np | ||
from save_model import saveModel | ||
import paddle | ||
import sys | ||
|
||
def partial_concat(name: str, x, y, start_index=0, length=-1): | ||
paddle.enable_static() | ||
|
||
with paddle.static.program_guard(paddle.static.Program(), paddle.static.Program()): | ||
x_data = paddle.static.data(name="x", shape=x.shape, dtype=x.dtype) | ||
y_data = paddle.static.data(name="y", shape=x.shape, dtype=y.dtype) | ||
|
||
if paddle.__version__ >= '2.5.1': | ||
out = paddle.incubate.layers.nn.partial_concat( | ||
[x_data, y_data], start_index=start_index, length=length | ||
) | ||
else: | ||
out = paddle.fluid.contrib.layers.partial_concat( | ||
[x_data, y_data], start_index=start_index, length=length | ||
) | ||
|
||
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, "y": y}, fetch_list=[out]) | ||
|
||
saveModel( | ||
name, | ||
exe, | ||
feedkeys=["x", "y"], | ||
fetchlist=[out], | ||
inputs=[x, y], | ||
outputs=[outs[0]], | ||
target_dir=sys.argv[1], | ||
) | ||
|
||
return outs[0] | ||
|
||
|
||
def main(): | ||
dtype = 'float32' | ||
x = np.random.randn(6, 4).astype(dtype) | ||
y = np.random.randn(6, 4).astype(dtype) | ||
partial_concat("partial_concat_1", x, y, start_index=2, length=2) | ||
|
||
|
||
dtype = 'int32' | ||
x = np.random.randint(-10, 10, [5, 3]).astype(dtype) | ||
y = np.random.randint(-10, 10, [5, 3]).astype(dtype) | ||
partial_concat("partial_concat_2", x, y, start_index=1, length=-1) | ||
|
||
dtype = 'int64' | ||
x = np.random.randint(-10, 10, [8, 10]).astype(dtype) | ||
y = np.random.randint(-10, 10, [8, 10]).astype(dtype) | ||
partial_concat("partial_concat_3", x, y, start_index=1, length=5) | ||
|
||
if __name__ == "__main__": | ||
main() |
68 changes: 68 additions & 0 deletions
68
src/frontends/paddle/tests/test_models/gen_scripts/generate_partial_sum.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,68 @@ | ||
# Copyright (C) 2018-2023 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# | ||
# partial_sum paddle model generator | ||
# | ||
import numpy as np | ||
from save_model import saveModel | ||
import paddle | ||
import sys | ||
|
||
|
||
def partial_sum(name: str, x, y, start_index=0, length=-1): | ||
paddle.enable_static() | ||
|
||
with paddle.static.program_guard(paddle.static.Program(), paddle.static.Program()): | ||
x_data = paddle.static.data(name="x", shape=x.shape, dtype=x.dtype) | ||
y_data = paddle.static.data(name="y", shape=x.shape, dtype=y.dtype) | ||
|
||
if paddle.__version__ >= '2.5.1': | ||
out = paddle.incubate.layers.nn.partial_sum( | ||
[x_data, y_data], start_index=start_index, length=length | ||
) | ||
else: | ||
out = paddle.fluid.contrib.layers.partial_sum( | ||
[x_data, y_data], start_index=start_index, length=length | ||
) | ||
|
||
|
||
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, "y": y}, fetch_list=[out]) | ||
|
||
saveModel( | ||
name, | ||
exe, | ||
feedkeys=["x", "y"], | ||
fetchlist=[out], | ||
inputs=[x, y], | ||
outputs=[outs[0]], | ||
target_dir=sys.argv[1], | ||
) | ||
|
||
return outs[0] | ||
|
||
|
||
def main(): | ||
dtype = 'float32' | ||
x = np.random.randn(6, 4).astype(dtype) | ||
y = np.random.randn(6, 4).astype(dtype) | ||
partial_sum("partial_sum_1", x, y, start_index=2, length=2) | ||
|
||
|
||
dtype = 'int32' | ||
x = np.random.randint(-10, 10, [5, 3]).astype(dtype) | ||
y = np.random.randint(-10, 10, [5, 3]).astype(dtype) | ||
partial_sum("partial_sum_2", x, y, start_index=1, length=-1) | ||
|
||
dtype = 'int64' | ||
x = np.random.randint(-10, 10, [8, 10]).astype(dtype) | ||
y = np.random.randint(-10, 10, [8, 10]).astype(dtype) | ||
partial_sum("partial_sum_3", x, y, start_index=1, length=5) | ||
|
||
if __name__ == "__main__": | ||
main() |