-
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 one_hot_v2 op (#15859)
* update op linspace * rewrite function name * add one_hot op mapping * change status * add depth_tensor * adjust op test * Update generate_one_hot_v2.py * adjust cpp * adjust cpp * remove default value * Update Supported_Frameworks_Layers.md * support N-dims * remove restriction --------- Co-authored-by: Yu Xu <yu.xu@intel.com> Co-authored-by: cecilia peng <cecilia.peng@intel.com> Co-authored-by: Xiuchuan Zhai <xiuchuan.zhai@intel.com>
- Loading branch information
1 parent
5b6bab2
commit 0963c23
Showing
5 changed files
with
84 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
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,31 @@ | ||
// 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 one_hot_v2(const NodeContext& node) { | ||
auto data = node.get_input("X"); | ||
Output<Node> depth; | ||
if (node.has_input("depth_tensor")) { | ||
auto depth_value = node.get_input("depth_tensor"); | ||
depth = std::make_shared<default_opset::Squeeze>(depth_value); | ||
} else { | ||
const auto depth_value = node.get_attribute<int>("depth"); | ||
depth = default_opset::Constant::create(element::i32, Shape{}, {depth_value}); | ||
} | ||
auto on_value = default_opset::Constant::create(element::f32, Shape{}, {1}); | ||
auto off_value = default_opset::Constant::create(element::f32, Shape{}, {0}); | ||
const auto indices_axis = -1; | ||
auto result = std::make_shared<default_opset::OneHot>(data, depth, on_value, off_value, indices_axis); | ||
return node.default_single_output_mapping({result}, {"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
47 changes: 47 additions & 0 deletions
47
src/frontends/paddle/tests/test_models/gen_scripts/generate_one_hot_v2.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,47 @@ | ||
# Copyright (C) 2018-2023 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# | ||
# one_hot_v2 paddle model generator | ||
# | ||
import paddle | ||
import numpy as np | ||
from save_model import saveModel | ||
import sys | ||
|
||
|
||
def one_hot_v2(name: str, x, num_classes, is_tensor): | ||
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) | ||
depth_node = paddle.static.data(name="depth_tensor", shape=num_classes.shape, dtype=num_classes.dtype) if is_tensor else num_classes | ||
out = paddle.nn.functional.one_hot(x_node, num_classes=depth_node) | ||
place = paddle.CPUPlace() | ||
exe = paddle.static.Executor(place) | ||
feed_list = {"x": x, "depth_tensor": num_classes} if is_tensor else {"x": x} | ||
outs = exe.run(feed=feed_list, fetch_list=[out]) | ||
feedkey_list = ["x", "depth_tensor"] if is_tensor else ['x'] | ||
input_list = [x, num_classes] if is_tensor else [x] | ||
saveModel(name, exe, feedkeys=feedkey_list, fetchlist=[out], inputs=input_list, outputs=[outs[0]], target_dir=sys.argv[1]) | ||
|
||
return outs[0] | ||
|
||
|
||
def main(): | ||
# int 32 | ||
data = np.array([1]).astype("int32") | ||
num_classes = 4 | ||
one_hot_v2("one_hot_v2_1", data, num_classes, is_tensor=False) | ||
# rank 1 int64 | ||
data = np.array([4, 1, 3, 3]).astype("int64") | ||
num_classes = np.array([5]).astype("int32") | ||
one_hot_v2("one_hot_v2_2", data, num_classes, is_tensor=True) | ||
# rank 2 int64 | ||
data = np.array([[4, 1, 3, 3], [1, 1, 3, 0]]).astype("int64") | ||
num_classes = np.array([5]).astype("int32") | ||
one_hot_v2("one_hot_v2_3", data, num_classes, is_tensor=True) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |