-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
【PaddlePaddle Hackathon 4】add paddle softshrink op #15845
Merged
ceciliapeng2011
merged 14 commits into
openvinotoolkit:master
from
AndPuQing:patch-softshrink
May 4, 2023
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
55f9450
add softshrink
AndPuQing d05d0b0
Merge branch 'master' into patch-softshrink
AndPuQing 96b19ff
Modify the implementation
AndPuQing a72e2c2
Merge branch 'patch-softshrink' of github.com:AndPuQing/openvino into…
AndPuQing 886e9a9
fix input unsigned
AndPuQing 9cc29e4
Merge branch 'master' into patch-softshrink
AndPuQing 24c06ee
fix name typo
AndPuQing 2c16539
Merge branch 'patch-softshrink' of github.com:AndPuQing/openvino into…
AndPuQing c2bf69a
fix unsigned
AndPuQing 42c3a75
Merge branch 'master' into patch-softshrink
AndPuQing d3598a7
fix selct
AndPuQing f3bd84a
Merge branch 'master' into patch-softshrink
liubo-intel 1da3a86
add input dtype check
AndPuQing 3c98cec
Merge branch 'master' into patch-softshrink
AndPuQing 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,54 @@ | ||
// 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 softshrink(const NodeContext& node) { | ||
auto data = node.get_input("X"); | ||
const float lambda = node.get_attribute<float>("lambda", 0.5f); | ||
|
||
const auto input_element_type = data.get_element_type(); | ||
std::shared_ptr<ngraph::Node> output; | ||
const auto positive_lambda = default_opset::Constant::create(input_element_type, Shape{}, {lambda}); | ||
std::shared_ptr<ngraph::Node> negative_node = std::make_shared<default_opset::Subtract>(data, positive_lambda); | ||
std::shared_ptr<ngraph::Node> zero_node = default_opset::Constant::create(input_element_type, Shape{}, {0}); | ||
if (input_element_type.is_signed()) { | ||
std::shared_ptr<default_opset::Constant> negative_lambda = | ||
default_opset::Constant::create(input_element_type, Shape{}, {-lambda}); | ||
|
||
// Create masks for values below negative lambda and above positive lambda | ||
std::shared_ptr<ngraph::Node> values_below_neg_lambda = | ||
std::make_shared<default_opset::Less>(data, negative_lambda); | ||
std::shared_ptr<ngraph::Node> values_above_pos_lambda = | ||
std::make_shared<default_opset::Greater>(data, positive_lambda); | ||
|
||
std::shared_ptr<ngraph::Node> positive_node = std::make_shared<default_opset::Add>(data, positive_lambda); | ||
|
||
output = std::make_shared<default_opset::Select>(values_above_pos_lambda, negative_node, data); | ||
output = std::make_shared<default_opset::Select>(values_below_neg_lambda, positive_node, output); | ||
|
||
std::shared_ptr<ngraph::Node> zero_mask = | ||
std::make_shared<default_opset::LogicalOr>(values_below_neg_lambda, values_above_pos_lambda); | ||
|
||
output = std::make_shared<default_opset::Select>(zero_mask, output, zero_node); | ||
} else { | ||
// Passing -lambd to unsigned type constant will cause an overflow. | ||
// For unsigned types the lowest possible value is 0. So we just need to compare with lambda. | ||
std::shared_ptr<ngraph::Node> values_above_pos_lambda = | ||
std::make_shared<default_opset::Greater>(data, positive_lambda); | ||
|
||
output = std::make_shared<default_opset::Select>(values_above_pos_lambda, negative_node, zero_node); | ||
} | ||
|
||
return node.default_single_output_mapping({output}, {"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
56 changes: 56 additions & 0 deletions
56
src/frontends/paddle/tests/test_models/gen_scripts/generate_softshrink.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,56 @@ | ||
# Copyright (C) 2018-2023 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# | ||
# softshrink paddle model generator | ||
# | ||
import numpy as np | ||
import sys | ||
from save_model import saveModel | ||
|
||
|
||
def softshrink(name: str, x, threshold): | ||
import paddle | ||
|
||
paddle.enable_static() | ||
|
||
node_x = paddle.static.data(name="x", shape=x.shape, dtype="float32") | ||
if threshold == None: | ||
out = paddle.nn.functional.softshrink(node_x) | ||
else: | ||
out = paddle.nn.functional.softshrink(node_x, threshold) | ||
|
||
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(): | ||
data = np.array( | ||
[ | ||
[[2.0, 3.0, 4.0, 5.0], [0.0, 4.0, -5.0, 6.0], [7.0, -8.0, 8.0, 9.0]], | ||
[[-1.0, 2.0, 3.0, 4.0], [-5.0, 6.0, 7.0, 8.0], [6.0, 7.0, 8.0, 9.0]], | ||
] | ||
).astype(np.float32) | ||
|
||
softshrink("softshrink_default_params", data, threshold=None) | ||
softshrink("softshrink_threshold_0.6", data, threshold=0.6) | ||
|
||
|
||
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.
Refer to Paddle spec, input x could be float32 and float64 only. How can a test case trigger this else branch?
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.
@ceciliapeng2011 : the other Paddle 'softshrink' api defined here : https://www.paddlepaddle.org.cn/documentation/docs/zh/api/paddle/nn/Softshrink_cn.html#softshrink
looks like there is no such limitation for input x type. To cover this api, I think we may still need the branch to handle '-lambd to unsigned type' cases.
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, @AndPuQing : about @ceciliapeng2011 's concern, could you please have a try to add such unsigned type test case by using
paddle.nn.Softshrink()
api?if such test case is difficult to generate from paddle side, let's disable such unsigned input support(delete the else branch) this time, and add an input type check
PADDLE_OP_CHECK(node, input_element_type.is_signed(), "softshrink only supports singed input data type");
for this op.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.
I test the unsighed tensor input on the paddlepaddle delelop version, there is the result:
So, I think just add dtype check currently.