|
| 1 | +// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "paddle/fluid/framework/ir/mkldnn/conv_brelu_mkldnn_fuse_pass.h" |
| 16 | + |
| 17 | +#include <gtest/gtest.h> |
| 18 | +#include "paddle/fluid/framework/op_proto_maker.h" |
| 19 | + |
| 20 | +namespace paddle { |
| 21 | +namespace framework { |
| 22 | +namespace ir { |
| 23 | + |
| 24 | +void SetOp(ProgramDesc* prog, const std::string& type, const std::string& name, |
| 25 | + const std::vector<std::string>& inputs, |
| 26 | + const std::vector<std::string>& outputs, bool use_mkldnn = false) { |
| 27 | + auto* op = prog->MutableBlock(0)->AppendOp(); |
| 28 | + op->SetType(type); |
| 29 | + if (type == "conv2d") { |
| 30 | + op->SetAttr("use_mkldnn", use_mkldnn); |
| 31 | + op->SetAttr("name", name); |
| 32 | + op->SetInput("Input", {inputs[0]}); |
| 33 | + op->SetInput("Filter", {inputs[1]}); |
| 34 | + op->SetInput("Bias", {inputs[2]}); |
| 35 | + } else if (type == "relu6") { |
| 36 | + op->SetAttr("use_mkldnn", use_mkldnn); |
| 37 | + if (use_mkldnn) { |
| 38 | + op->SetAttr("threshold", 6.0f); |
| 39 | + } |
| 40 | + op->SetInput("X", inputs); |
| 41 | + } |
| 42 | + op->SetOutput("Out", outputs); |
| 43 | + op->SetAttr(OpProtoAndCheckerMaker::OpRoleAttrName(), |
| 44 | + static_cast<int>(OpRole::kForward)); |
| 45 | +} |
| 46 | + |
| 47 | +// a->OP0->b |
| 48 | +// b->OP1->c |
| 49 | +// (c, weights, bias)->conv->f |
| 50 | +// (f)->brelu->g |
| 51 | +ProgramDesc BuildProgramDesc() { |
| 52 | + ProgramDesc prog; |
| 53 | + for (auto& v : |
| 54 | + std::vector<std::string>({"a", "b", "c", "weights", "bias", "f", "g", |
| 55 | + "h", "weights2", "bias2", "k", "l"})) { |
| 56 | + auto* var = prog.MutableBlock(0)->Var(v); |
| 57 | + var->SetType(proto::VarType::SELECTED_ROWS); |
| 58 | + if (v == "weights" || v == "bias") { |
| 59 | + var->SetPersistable(true); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + SetOp(&prog, "OP0", "op0", std::vector<std::string>({"a"}), |
| 64 | + std::vector<std::string>({"b"})); |
| 65 | + SetOp(&prog, "OP1", "op1", std::vector<std::string>({"b"}), |
| 66 | + std::vector<std::string>({"c"})); |
| 67 | + // conv+brelu, both with MKL-DNN |
| 68 | + SetOp(&prog, "conv2d", "conv1", |
| 69 | + std::vector<std::string>({"c", "weights", "bias"}), |
| 70 | + std::vector<std::string>({"f"}), true); |
| 71 | + SetOp(&prog, "relu6", "relu1", std::vector<std::string>({"f"}), |
| 72 | + std::vector<std::string>({"g"}), true); |
| 73 | + SetOp(&prog, "OP3", "op3", std::vector<std::string>({"g"}), |
| 74 | + std::vector<std::string>({"h"})); |
| 75 | + // conv+brelu, only one with MKL-DNN |
| 76 | + SetOp(&prog, "conv2d", "conv2", |
| 77 | + std::vector<std::string>({"h", "weights2", "bias2"}), |
| 78 | + std::vector<std::string>({"k"}), true); |
| 79 | + SetOp(&prog, "relu6", "relu2", std::vector<std::string>({"k"}), |
| 80 | + std::vector<std::string>({"l"})); |
| 81 | + |
| 82 | + return prog; |
| 83 | +} |
| 84 | + |
| 85 | +TEST(ConvBReLUFusePass, basic) { |
| 86 | + auto prog = BuildProgramDesc(); |
| 87 | + |
| 88 | + std::unique_ptr<ir::Graph> graph(new ir::Graph(prog)); |
| 89 | + |
| 90 | + auto pass = PassRegistry::Instance().Get("conv_brelu_mkldnn_fuse_pass"); |
| 91 | + |
| 92 | + int original_nodes_num = graph->Nodes().size(); |
| 93 | + |
| 94 | + graph.reset(pass->Apply(graph.release())); |
| 95 | + |
| 96 | + int current_nodes_num = graph->Nodes().size(); |
| 97 | + |
| 98 | + // Remove 3 Nodes: CONV, BRELU, conv_out |
| 99 | + // Add 1 Node: ConvBReLU |
| 100 | + EXPECT_EQ(original_nodes_num - 2, current_nodes_num); |
| 101 | + |
| 102 | + // Assert conv_brelu op in newly generated graph |
| 103 | + int conv_brelu_count = 0; |
| 104 | + |
| 105 | + for (auto* node : graph->Nodes()) { |
| 106 | + if (node->IsOp() && node->Op()->Type() == "conv2d") { |
| 107 | + auto* op = node->Op(); |
| 108 | + ASSERT_TRUE(op->HasAttr("use_mkldnn")); |
| 109 | + EXPECT_TRUE(boost::get<bool>(op->GetAttr("use_mkldnn"))); |
| 110 | + // check if only "conv1" convolution is fused |
| 111 | + auto op_name = boost::get<std::string>(op->GetAttr("name")); |
| 112 | + if (op_name == "conv1") { |
| 113 | + ASSERT_TRUE(op->HasAttr("fuse_brelu")); |
| 114 | + ASSERT_TRUE(op->HasAttr("fuse_brelu_threshold")); |
| 115 | + |
| 116 | + bool fuse_brelu = boost::get<bool>(op->GetAttr("fuse_brelu")); |
| 117 | + if (fuse_brelu) { |
| 118 | + ++conv_brelu_count; |
| 119 | + float fuse_brelu_threshold = |
| 120 | + boost::get<float>(op->GetAttr("fuse_brelu_threshold")); |
| 121 | + EXPECT_EQ(fuse_brelu_threshold, 6.0f); |
| 122 | + } |
| 123 | + } else if (op_name == "conv2") { |
| 124 | + ASSERT_FALSE(op->HasAttr("fuse_brelu")); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + EXPECT_EQ(conv_brelu_count, 1); |
| 129 | +} |
| 130 | + |
| 131 | +} // namespace ir |
| 132 | +} // namespace framework |
| 133 | +} // namespace paddle |
| 134 | + |
| 135 | +USE_PASS(conv_brelu_mkldnn_fuse_pass); |
0 commit comments