-
Notifications
You must be signed in to change notification settings - Fork 6k
Enable the convolution/relu6(bounded_relu) fusion for FP32 on Intel platform. #17130
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
luotao1
merged 8 commits into
PaddlePaddle:develop
from
guomingz:conv_relu6_fusion_fp32
May 22, 2019
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a3bbda7
Relu6 is the bottleneck op for Mobilenet-v2. As the mkldnn supports t…
guomingz 937271e
Fix the format issue
guomingz 625c8ce
Add the missing nolint comments.
guomingz 69d6a9f
Fix the typos.
guomingz d6f305b
Register the conv_brelu_mkldnn_fuse_pass for the MKLDNN engine.
guomingz 2ef8f88
Adjust the indentation.
guomingz a312f0b
Add the test_conv_brelu_mkldnn_fuse_pass case.
guomingz e4d6b1b
Slightly update the code per Baidu comments.
guomingz 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
71 changes: 71 additions & 0 deletions
71
paddle/fluid/framework/ir/mkldnn/conv_brelu_mkldnn_fuse_pass.cc
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,71 @@ | ||
| // 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 "paddle/fluid/framework/ir/mkldnn/conv_brelu_mkldnn_fuse_pass.h" | ||
| #include <string> | ||
| #include <vector> | ||
| #include "paddle/fluid/platform/enforce.h" | ||
|
|
||
| namespace paddle { | ||
| namespace framework { | ||
| namespace ir { | ||
|
|
||
| void ConvBReLUFusePass::ApplyImpl(ir::Graph* graph) const { | ||
| PADDLE_ENFORCE(graph); | ||
| FusePassBase::Init("conv_bounded_relu_mkldnn_fuse", graph); | ||
|
|
||
| GraphPatternDetector gpd; | ||
| auto* conv_input = gpd.mutable_pattern() | ||
| ->NewNode("conv_bounded_relu_mkldnn_fuse/conv_input") | ||
| ->AsInput() | ||
| ->assert_is_op_input("conv2d", "Input"); | ||
| patterns::ConvBReLU conv_brelu_pattern(gpd.mutable_pattern(), | ||
| "conv_bounded_relu_mkldnn_fuse"); | ||
| conv_brelu_pattern(conv_input); | ||
|
|
||
| int found_conv_brelu_count = 0; | ||
| auto handler = [&](const GraphPatternDetector::subgraph_t& subgraph, | ||
| Graph* g) { | ||
| VLOG(4) << "handle ConvBoundedReLUFusePass fuse"; | ||
| GET_IR_NODE_FROM_SUBGRAPH(conv_weight, conv_weight, | ||
| conv_brelu_pattern); // Filter | ||
| GET_IR_NODE_FROM_SUBGRAPH(conv_out, conv_out, conv_brelu_pattern); // tmp | ||
| GET_IR_NODE_FROM_SUBGRAPH(conv, conv, conv_brelu_pattern); // CONV op | ||
| GET_IR_NODE_FROM_SUBGRAPH(brelu_out, brelu_out, conv_brelu_pattern); // Out | ||
| GET_IR_NODE_FROM_SUBGRAPH(brelu, brelu, conv_brelu_pattern); // ReLU op | ||
|
|
||
| // Transform Conv node into ConvBReLU node. | ||
| OpDesc* desc = conv->Op(); | ||
| desc->SetOutput("Output", std::vector<std::string>({brelu_out->Name()})); | ||
| desc->SetAttr("fuse_brelu", true); | ||
| desc->SetAttr("fuse_brelu_threshold", brelu->Op()->GetAttr("threshold")); | ||
|
|
||
| GraphSafeRemoveNodes(graph, {brelu, conv_out}); | ||
|
|
||
| PADDLE_ENFORCE(subgraph.count(conv_input)); | ||
| IR_NODE_LINK_TO(conv, brelu_out); | ||
| found_conv_brelu_count++; | ||
| }; | ||
|
|
||
| gpd(graph, handler); | ||
|
|
||
| AddStatis(found_conv_brelu_count); | ||
| } | ||
|
|
||
| } // namespace ir | ||
| } // namespace framework | ||
| } // namespace paddle | ||
|
|
||
| REGISTER_PASS(conv_brelu_mkldnn_fuse_pass, | ||
| paddle::framework::ir::ConvBReLUFusePass); |
39 changes: 39 additions & 0 deletions
39
paddle/fluid/framework/ir/mkldnn/conv_brelu_mkldnn_fuse_pass.h
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,39 @@ | ||
| // 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 "paddle/fluid/framework/ir/fuse_pass_base.h" | ||
| #include "paddle/fluid/framework/ir/graph.h" | ||
| #include "paddle/fluid/framework/ir/graph_pattern_detector.h" | ||
| #include "paddle/fluid/framework/ir/pass.h" | ||
|
|
||
| namespace paddle { | ||
| namespace framework { | ||
| namespace ir { | ||
|
|
||
| /* | ||
| * Fuse the CONV and ReLU6 to a ConvReLU6Op. | ||
| */ | ||
| class ConvBReLUFusePass : public FusePassBase { | ||
| public: | ||
| virtual ~ConvBReLUFusePass() {} | ||
|
|
||
| protected: | ||
| void ApplyImpl(ir::Graph* graph) const override; | ||
| }; | ||
|
|
||
| } // namespace ir | ||
| } // namespace framework | ||
| } // namespace paddle |
135 changes: 135 additions & 0 deletions
135
paddle/fluid/framework/ir/mkldnn/conv_brelu_mkldnn_fuse_pass_tester.cc
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,135 @@ | ||
| // 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 "paddle/fluid/framework/ir/mkldnn/conv_brelu_mkldnn_fuse_pass.h" | ||
|
|
||
| #include <gtest/gtest.h> | ||
| #include "paddle/fluid/framework/op_proto_maker.h" | ||
|
|
||
| namespace paddle { | ||
| namespace framework { | ||
| namespace ir { | ||
|
|
||
| void SetOp(ProgramDesc* prog, const std::string& type, const std::string& name, | ||
| const std::vector<std::string>& inputs, | ||
| const std::vector<std::string>& outputs, bool use_mkldnn = false) { | ||
| auto* op = prog->MutableBlock(0)->AppendOp(); | ||
| op->SetType(type); | ||
| if (type == "conv2d") { | ||
| op->SetAttr("use_mkldnn", use_mkldnn); | ||
| op->SetAttr("name", name); | ||
| op->SetInput("Input", {inputs[0]}); | ||
| op->SetInput("Filter", {inputs[1]}); | ||
| op->SetInput("Bias", {inputs[2]}); | ||
| } else if (type == "relu6") { | ||
| op->SetAttr("use_mkldnn", use_mkldnn); | ||
| if (use_mkldnn) { | ||
| op->SetAttr("threshold", 6.0f); | ||
| } | ||
| op->SetInput("X", inputs); | ||
| } | ||
| op->SetOutput("Out", outputs); | ||
| op->SetAttr(OpProtoAndCheckerMaker::OpRoleAttrName(), | ||
| static_cast<int>(OpRole::kForward)); | ||
| } | ||
|
|
||
| // a->OP0->b | ||
| // b->OP1->c | ||
| // (c, weights, bias)->conv->f | ||
| // (f)->brelu->g | ||
| ProgramDesc BuildProgramDesc() { | ||
| ProgramDesc prog; | ||
| for (auto& v : | ||
| std::vector<std::string>({"a", "b", "c", "weights", "bias", "f", "g", | ||
| "h", "weights2", "bias2", "k", "l"})) { | ||
| auto* var = prog.MutableBlock(0)->Var(v); | ||
| var->SetType(proto::VarType::SELECTED_ROWS); | ||
| if (v == "weights" || v == "bias") { | ||
| var->SetPersistable(true); | ||
| } | ||
| } | ||
|
|
||
| SetOp(&prog, "OP0", "op0", std::vector<std::string>({"a"}), | ||
| std::vector<std::string>({"b"})); | ||
| SetOp(&prog, "OP1", "op1", std::vector<std::string>({"b"}), | ||
| std::vector<std::string>({"c"})); | ||
| // conv+brelu, both with MKL-DNN | ||
| SetOp(&prog, "conv2d", "conv1", | ||
| std::vector<std::string>({"c", "weights", "bias"}), | ||
| std::vector<std::string>({"f"}), true); | ||
| SetOp(&prog, "relu6", "relu1", std::vector<std::string>({"f"}), | ||
| std::vector<std::string>({"g"}), true); | ||
| SetOp(&prog, "OP3", "op3", std::vector<std::string>({"g"}), | ||
| std::vector<std::string>({"h"})); | ||
| // conv+brelu, only one with MKL-DNN | ||
| SetOp(&prog, "conv2d", "conv2", | ||
| std::vector<std::string>({"h", "weights2", "bias2"}), | ||
| std::vector<std::string>({"k"}), true); | ||
| SetOp(&prog, "relu6", "relu2", std::vector<std::string>({"k"}), | ||
| std::vector<std::string>({"l"})); | ||
|
|
||
| return prog; | ||
| } | ||
|
|
||
| TEST(ConvBReLUFusePass, basic) { | ||
| auto prog = BuildProgramDesc(); | ||
|
|
||
| std::unique_ptr<ir::Graph> graph(new ir::Graph(prog)); | ||
|
|
||
| auto pass = PassRegistry::Instance().Get("conv_brelu_mkldnn_fuse_pass"); | ||
|
|
||
| int original_nodes_num = graph->Nodes().size(); | ||
|
|
||
| graph.reset(pass->Apply(graph.release())); | ||
|
|
||
| int current_nodes_num = graph->Nodes().size(); | ||
|
|
||
| // Remove 3 Nodes: CONV, BRELU, conv_out | ||
| // Add 1 Node: ConvBReLU | ||
| EXPECT_EQ(original_nodes_num - 2, current_nodes_num); | ||
|
|
||
| // Assert conv_brelu op in newly generated graph | ||
| int conv_brelu_count = 0; | ||
|
|
||
| for (auto* node : graph->Nodes()) { | ||
| if (node->IsOp() && node->Op()->Type() == "conv2d") { | ||
| auto* op = node->Op(); | ||
| ASSERT_TRUE(op->HasAttr("use_mkldnn")); | ||
| EXPECT_TRUE(boost::get<bool>(op->GetAttr("use_mkldnn"))); | ||
| // check if only "conv1" convolution is fused | ||
| auto op_name = boost::get<std::string>(op->GetAttr("name")); | ||
| if (op_name == "conv1") { | ||
| ASSERT_TRUE(op->HasAttr("fuse_brelu")); | ||
| ASSERT_TRUE(op->HasAttr("fuse_brelu_threshold")); | ||
|
|
||
| bool fuse_brelu = boost::get<bool>(op->GetAttr("fuse_brelu")); | ||
| if (fuse_brelu) { | ||
| ++conv_brelu_count; | ||
| float fuse_brelu_threshold = | ||
| boost::get<float>(op->GetAttr("fuse_brelu_threshold")); | ||
| EXPECT_EQ(fuse_brelu_threshold, 6.0f); | ||
| } | ||
| } else if (op_name == "conv2") { | ||
| ASSERT_FALSE(op->HasAttr("fuse_brelu")); | ||
| } | ||
| } | ||
| } | ||
| EXPECT_EQ(conv_brelu_count, 1); | ||
| } | ||
|
|
||
| } // namespace ir | ||
| } // namespace framework | ||
| } // namespace paddle | ||
|
|
||
| USE_PASS(conv_brelu_mkldnn_fuse_pass); |
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
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.
Why need
fuse_brelu_threshold?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's a parameter of bounded ReLU. 6 is the typical value but it may vary.
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.
@luotao1 ,
relu6is a case ofbounded_reluop in which 6 is the default value of the threshold parameter.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, when will this PR be merged ? :)
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.
@qingqing01 How do you see this PR add two attributes
fuse_breluandfuse_brelu_threshold?