-
Notifications
You must be signed in to change notification settings - Fork 2.8k
【PaddlePaddle Hackathon 4】add paddle one_hot_v2 op #15859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ceciliapeng2011
merged 21 commits into
openvinotoolkit:master
from
Patrick-Star125:one_hot
May 22, 2023
Merged
Changes from 12 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
ce3cc16
update op linspace
Patrick-Star125 f54ca35
Merge branch 'master' of https://github.com/Patrick-Star125/openvino
Patrick-Star125 284b373
Merge branch 'openvinotoolkit:master' into master
Patrick-Star125 f375c6d
rewrite function name
Patrick-Star125 23598e4
Merge branch 'master' of https://github.com/Patrick-Star125/openvino
Patrick-Star125 e778f3d
add one_hot op mapping
Patrick-Star125 cbb0367
change status
Patrick-Star125 2de0104
add depth_tensor
Patrick-Star125 66ed845
adjust op test
Patrick-Star125 1399b5b
Update generate_one_hot_v2.py
Patrick-Star125 8cebc8e
Merge branch 'master' into one_hot
Patrick-Star125 433437a
adjust cpp
Patrick-Star125 dd0ea89
adjust cpp
Patrick-Star125 59bdfb9
remove default value
Patrick-Star125 81c83d7
Merge branch 'master' into one_hot
yuxu42 1f057fb
Merge branch 'master' into one_hot
ceciliapeng2011 c891111
Update Supported_Frameworks_Layers.md
Patrick-Star125 eb5c6d1
support N-dims
Patrick-Star125 5a9745f
Merge branch 'one_hot' of https://github.com/Patrick-Star125/openvino…
Patrick-Star125 120c203
remove restriction
Patrick-Star125 e830062
Merge branch 'master' into one_hot
azhai219 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 { | ||
| 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 hidden or 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 hidden or 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
52 changes: 52 additions & 0 deletions
52
src/frontends/paddle/tests/test_models/gen_scripts/generate_one_hot_v2.py
This file contains hidden or 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,52 @@ | ||
| # 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_1(name: str, x, num_classes): | ||
|
||
| 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.nn.functional.one_hot(x_node, num_classes=num_classes) | ||
| place = paddle.CPUPlace() | ||
| exe = paddle.static.Executor(place) | ||
| 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 one_hot_v2_2(name: str, x, num_classes): | ||
| 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) | ||
| out = paddle.nn.functional.one_hot(x_node, num_classes=depth_node) | ||
| place = paddle.CPUPlace() | ||
| exe = paddle.static.Executor(place) | ||
| outs = exe.run(feed={"x": x, "depth_tensor": num_classes}, fetch_list=[out]) | ||
| saveModel(name, exe, feedkeys=["x", "depth_tensor"], fetchlist=[out], inputs=[x, num_classes], outputs=[outs[0]], target_dir=sys.argv[1]) | ||
|
|
||
| return outs[0] | ||
|
|
||
| def main(): | ||
| # int32 | ||
| data = np.array([1, 1, 3, 0]).astype("int32") | ||
| num_classes = 4 | ||
| one_hot_v2_1("one_hot_v2_1", data, num_classes) | ||
| # int64 | ||
| data = np.array([4, 1, 3, 3]).astype("int64") | ||
| num_classes = np.array([5]).astype("int32") | ||
| one_hot_v2_2("one_hot_v2_2", data, num_classes) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi can we leave it as
const, and also set a default value for the attributesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1.const had been added
2.input without num_class is not permitted in paddle, so I think it is presumably not needful to set a default value. If it is necessary, the default number of classes could be set as one greater than the largest class value in the input tensor, according to the pytorch implement.