-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[OPENCL] fuse fc prelu #5468
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
Merged
[OPENCL] fuse fc prelu #5468
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
175957c
fuse fc prelu test=develop
daming5432 4e7cd64
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle-Lite…
daming5432 b7346cc
fix merge conflict test=develop
daming5432 6e0dcb0
fix code according fuse_conv_prelu test=develop
daming5432 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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "lite/core/mir/fusion/fc_prelu_fuse_pass.h" | ||
| #include <memory> | ||
| #include <vector> | ||
| #include "lite/core/mir/fusion/fc_prelu_fuser.h" | ||
| #include "lite/core/mir/pass_registry.h" | ||
|
|
||
| namespace paddle { | ||
| namespace lite { | ||
| namespace mir { | ||
|
|
||
| void FcPreluFusePass::Apply(const std::unique_ptr<SSAGraph>& graph) { | ||
| fusion::FcPreluFuser fuser("prelu"); | ||
| fuser(graph.get()); | ||
| } | ||
|
|
||
| } // namespace mir | ||
| } // namespace lite | ||
| } // namespace paddle | ||
|
|
||
| REGISTER_MIR_PASS(lite_fc_prelu_fuse_pass, paddle::lite::mir::FcPreluFusePass) | ||
| .BindTargets({TARGET(kOpenCL)}) | ||
| .BindKernel("fc"); |
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,32 @@ | ||
| // Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <memory> | ||
| #include <string> | ||
| #include "lite/core/mir/pass.h" | ||
|
|
||
| namespace paddle { | ||
| namespace lite { | ||
| namespace mir { | ||
|
|
||
| class FcPreluFusePass : public ProgramPass { | ||
| public: | ||
| void Apply(const std::unique_ptr<SSAGraph>& graph) override; | ||
| }; | ||
|
|
||
| } // namespace mir | ||
| } // namespace lite | ||
| } // namespace paddle |
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,83 @@ | ||
| // Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "lite/core/mir/fusion/fc_prelu_fuser.h" | ||
| #include <memory> | ||
| #include <vector> | ||
|
|
||
| namespace paddle { | ||
| namespace lite { | ||
| namespace mir { | ||
| namespace fusion { | ||
|
|
||
| void FcPreluFuser::BuildPattern() { | ||
| // create nodes | ||
| // fc | ||
| PMNode* input = | ||
| VarNode("input")->assert_is_op_input("fc", "Input")->AsInput(); | ||
| PMNode* weights = | ||
| VarNode("weights")->assert_is_op_input("fc", "W")->AsInput(); | ||
| PMNode* bias = VarNode("bias")->assert_is_op_input("fc", "Bias")->AsInput(); | ||
| PMNode* fc = OpNode("fc", "fc")->AsIntermediate(); | ||
| PMNode* fc_out = VarNode("fc_out") | ||
| ->assert_is_op_output("fc", "Out") | ||
| ->assert_is_op_input("prelu", "X") | ||
| ->AsIntermediate(); | ||
|
|
||
| // prelu | ||
| PMNode* alpha = | ||
| VarNode("alpha")->assert_is_op_input("prelu", "Alpha")->AsInput(); | ||
| PMNode* prelu = OpNode("prelu", "prelu")->AsIntermediate(); | ||
| PMNode* out = | ||
| VarNode("output")->assert_is_op_output("prelu", "Out")->AsOutput(); | ||
|
|
||
| // create topology. | ||
| std::vector<PMNode*> fc_inputs{bias, weights, input}; | ||
| fc_inputs >> *fc >> *fc_out >> *prelu >> *out; | ||
| *alpha >> *prelu; | ||
| } | ||
|
|
||
| void FcPreluFuser::InsertNewNode(SSAGraph* graph, const key2nodes_t& matched) { | ||
| auto op_desc = GenOpDesc(matched); | ||
| auto fc_op = LiteOpRegistry::Global().Create("fc"); | ||
| auto fc_old = matched.at("fc")->stmt()->op(); | ||
| auto* scope = fc_old->scope(); | ||
| auto& valid_places = fc_old->valid_places(); | ||
| fc_op->Attach(op_desc, scope); | ||
|
|
||
| auto* new_op_node = graph->GraphCreateInstructNode(fc_op, valid_places); | ||
|
|
||
| IR_NODE_LINK_TO(matched.at("input"), new_op_node); | ||
| IR_NODE_LINK_TO(matched.at("weights"), new_op_node); | ||
| IR_NODE_LINK_TO(matched.at("bias"), new_op_node); | ||
| IR_NODE_LINK_TO(matched.at("alpha"), new_op_node); | ||
| IR_NODE_LINK_TO(new_op_node, matched.at("output")); | ||
| } | ||
|
|
||
| cpp::OpDesc FcPreluFuser::GenOpDesc(const key2nodes_t& matched) { | ||
| cpp::OpDesc op_desc = *matched.at("fc")->stmt()->op_info(); | ||
| op_desc.SetInput("Alpha", {matched.at("alpha")->arg()->name}); | ||
| op_desc.SetOutput("Out", {matched.at("output")->arg()->name}); | ||
|
|
||
| cpp::OpDesc prelu_op_desc = *matched.at("prelu")->stmt()->op_info(); | ||
| auto prelu_mode = prelu_op_desc.GetAttr<std::string>("mode"); | ||
| op_desc.SetAttr("prelu_mode", prelu_mode); | ||
| op_desc.SetAttr("activation_type", std::string{"prelu"}); | ||
| return op_desc; | ||
| } | ||
|
|
||
| } // namespace fusion | ||
| } // namespace mir | ||
| } // namespace lite | ||
| } // namespace paddle |
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,40 @@ | ||
| // Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <memory> | ||
| #include <string> | ||
| #include "lite/core/mir/pattern_matcher_high_api.h" | ||
|
|
||
| namespace paddle { | ||
| namespace lite { | ||
| namespace mir { | ||
| namespace fusion { | ||
|
|
||
| class FcPreluFuser : public FuseBase { | ||
| public: | ||
| explicit FcPreluFuser(const std::string& act_type) {} | ||
|
|
||
| void BuildPattern() override; | ||
| void InsertNewNode(SSAGraph* graph, const key2nodes_t& matched) override; | ||
|
|
||
| private: | ||
| cpp::OpDesc GenOpDesc(const key2nodes_t& matched) override; | ||
| }; | ||
|
|
||
| } // namespace fusion | ||
| } // namespace mir | ||
| } // namespace lite | ||
| } // namespace paddle |
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
Oops, something went wrong.
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.
建议使用
CL_DTYPE类型