-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
【Hackathon 5th No.95】add paddle unique op #20077
Merged
ceciliapeng2011
merged 7 commits into
openvinotoolkit:master
from
AndPuQing:feature-unique
Dec 15, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4f92324
add unique
AndPuQing 1fdbcd6
Merge branch 'master' into feature-unique
AndPuQing c5b1a34
fix args
AndPuQing 079483b
Merge branch 'master' of https://github.com/openvinotoolkit/openvino …
AndPuQing d6ad32a
Refactor unique() function to handle dtype
AndPuQing de6fd82
Merge branch 'master' into feature-unique
AndPuQing befd777
Merge branch 'master' into feature-unique
xczhai 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 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,37 @@ | ||
// Copyright (C) 2018-2023 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include "default_opset.hpp" | ||
#include "openvino/frontend/paddle/node_context.hpp" | ||
#include "openvino/opsets/opset10.hpp" | ||
|
||
namespace ov { | ||
namespace frontend { | ||
namespace paddle { | ||
namespace op { | ||
NamedOutputs unique(const NodeContext& node) { | ||
auto x = node.get_input("X"); | ||
|
||
std::vector<Output<Node>> outputs; | ||
|
||
auto axis = node.get_attribute<std::vector<int32_t>>("axis"); | ||
auto dtype = node.get_attribute<ov::element::Type>("dtype"); | ||
|
||
if (axis.size() != 0) { | ||
auto axis_node = std::make_shared<default_opset::Constant>(dtype, Shape{}, axis); | ||
outputs = std::make_shared<ov::opset10::Unique>(x, axis_node, true, dtype, dtype)->outputs(); | ||
} else { | ||
outputs = std::make_shared<ov::opset10::Unique>(x, true, dtype, dtype)->outputs(); | ||
} | ||
|
||
return NamedOutputs{{"Out", {outputs[0]}}, | ||
{"Indices", {outputs[1]}}, | ||
{"Index", {outputs[2]}}, | ||
{"Counts", {outputs[3]}}}; | ||
} | ||
|
||
} // 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
78 changes: 78 additions & 0 deletions
78
src/frontends/paddle/tests/test_models/gen_scripts/generate_unique.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,78 @@ | ||
# Copyright (C) 2018-2023 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# | ||
# unique paddle model generator | ||
# | ||
import numpy as np | ||
from save_model import saveModel | ||
import paddle | ||
import sys | ||
|
||
data_type = "float32" | ||
|
||
|
||
def unique(name: str, x, **op_args): | ||
paddle.enable_static() | ||
|
||
with paddle.static.program_guard(paddle.static.Program(), paddle.static.Program()): | ||
node_x = paddle.static.data(name="x", shape=x.shape, dtype=data_type) | ||
|
||
unique_outs = paddle.unique(node_x, **op_args) | ||
if isinstance(unique_outs, tuple): | ||
outputs = [] | ||
for i, out in enumerate(unique_outs): | ||
if i == 0: | ||
outputs.append(out) | ||
continue | ||
if out is not None: | ||
if out.dtype == paddle.int64: | ||
out = paddle.cast(out, "int32") | ||
outputs.append(out) | ||
else: | ||
outputs = [unique_outs] | ||
|
||
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()) | ||
|
||
fetch_vars = [x for x in outputs if x is not None] | ||
|
||
outs = exe.run(feed={"x": x}, fetch_list=fetch_vars) | ||
|
||
saveModel( | ||
name, | ||
exe, | ||
feedkeys=["x"], | ||
fetchlist=fetch_vars, | ||
inputs=[x], | ||
outputs=outs, | ||
target_dir=sys.argv[1], | ||
) | ||
|
||
|
||
def main(): | ||
data = np.array([2, 3, 3, 1, 5, 3]).astype(data_type) | ||
unique("unique", data) | ||
unique("unique_ret_index", data, return_index=True) | ||
unique("unique_ret_inverse", data, return_inverse=True) | ||
unique("unique_ret_counts", data, return_counts=True) | ||
unique("unique_ret_index_inverse", data, return_index=True, return_inverse=True) | ||
unique("unique_ret_index_counts", data, return_index=True, return_counts=True) | ||
unique("unique_ret_inverse_counts", data, return_inverse=True, return_counts=True) | ||
unique( | ||
"unique_ret_index_inverse_counts", | ||
data, | ||
return_index=True, | ||
return_inverse=True, | ||
return_counts=True, | ||
) | ||
|
||
data = np.array([[2, 1, 3], [3, 0, 1], [2, 1, 3]]).astype(data_type) | ||
unique("unique_ret_index_axis", data, return_index=True, axis=0) | ||
unique("unique_ret_index_i32", data, return_index=True, dtype="int32") | ||
|
||
|
||
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.
Can we use default_opset other than opset10 ?
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.
To the best of my knowledge, the unique operator is defined in opset10
But the default_opset is 9
openvino/src/frontends/paddle/src/default_opset.hpp
Line 11 in 47cdbb9
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.
It is okay here. I will upgrate the
default_opset
later.