-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
【PaddlePaddle Hackathon 3】Add Paddle where_index operator (#12437)
* add paddle where_index op * add more test cases * add more test cases * add more test cases and support boolen * fix clang format * use paddle.nonzero instead of LayerHelper * remove opset6 Co-authored-by: Ilya Churaev <ilyachur@gmail.com> Co-authored-by: cecilia peng <cecilia.peng@intel.com> Co-authored-by: Ilya Churaev <ilyachur@gmail.com>
- Loading branch information
1 parent
8ad3d0f
commit 5e977fc
Showing
4 changed files
with
100 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
70 changes: 70 additions & 0 deletions
70
src/core/tests/frontend/paddle/test_models/gen_scripts/generate_where_index.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,70 @@ | ||
# Copyright (C) 2018-2022 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# | ||
# where paddle model generator | ||
# | ||
import numpy as np | ||
from save_model import saveModel | ||
import sys | ||
import paddle | ||
|
||
paddle.enable_static() | ||
|
||
|
||
def where_index(name: str, x, force_boolean=False): | ||
with paddle.static.program_guard(paddle.static.Program(), paddle.static.Program()): | ||
node_x = paddle.static.data(name='x', shape=x.shape, dtype=x.dtype) | ||
if force_boolean: | ||
node_x_bl = paddle.fluid.layers.cast(node_x, "bool") | ||
out = paddle.nonzero(node_x_bl) | ||
else: | ||
out = paddle.nonzero(node_x) | ||
|
||
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(): | ||
# case of int32 | ||
datatype = "int32" | ||
condition = np.random.randint(0, 5, size=[5, 8, 2], dtype=datatype) | ||
paddle_out = where_index("where_index_1", condition) | ||
|
||
# case of float32 | ||
datatype = "float32" | ||
condition = (np.random.randint( | ||
0, 5, size=[8, 3, 2]) * 1.1).astype(datatype) | ||
paddle_out = where_index("where_index_2", condition) | ||
|
||
# case of dimension 4 | ||
condition = (np.random.randint( | ||
0, 5, size=[8, 3, 2, 6]) * 1.1).astype(datatype) | ||
paddle_out = where_index("where_index_3", condition) | ||
|
||
# case of dimension 5 | ||
condition = (np.random.randint( | ||
0, 5, size=[4, 6, 8, 2, 5]) * 1.1).astype(datatype) | ||
paddle_out = where_index("where_index_4", condition) | ||
|
||
# case of rank 1 | ||
condition = np.ones(10).astype(datatype) | ||
paddle_out = where_index("where_index_5", condition, force_boolean=True) | ||
|
||
# case of rank 1 and boolean zeros | ||
condition = np.array([1, 0, 1]).astype(datatype) | ||
paddle_out = where_index("where_index_6", condition, force_boolean=True) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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,22 @@ | ||
// Copyright (C) 2018-2022 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 where_index(const NodeContext& node) { | ||
const auto condition = node.get_input("Condition"); | ||
const auto perm = default_opset::Constant::create(element::i64, Shape{2}, {1, 0}); | ||
const auto out = std::make_shared<default_opset::NonZero>(condition, element::i64); | ||
return node.default_single_output_mapping({std::make_shared<default_opset::Transpose>(out, perm)}, {"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