|
| 1 | +// Copyright (c) 2018 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/depthwise_conv_mkldnn_pass.h" |
| 16 | + |
| 17 | +#include <gtest/gtest.h> |
| 18 | + |
| 19 | +namespace paddle { |
| 20 | +namespace framework { |
| 21 | +namespace ir { |
| 22 | + |
| 23 | +void SetOp(ProgramDesc* prog, const std::string& type, const std::string& name, |
| 24 | + const std::vector<std::string>& inputs, |
| 25 | + const std::vector<std::string>& outputs, bool use_mkldnn = false) { |
| 26 | + auto* op = prog->MutableBlock(0)->AppendOp(); |
| 27 | + op->SetType(type); |
| 28 | + op->SetAttr("use_mkldnn", use_mkldnn); |
| 29 | + op->SetAttr("name", name); |
| 30 | + op->SetInput("Input", {inputs[0]}); |
| 31 | + op->SetInput("Filter", {inputs[1]}); |
| 32 | + op->SetInput("Bias", {inputs[2]}); |
| 33 | + op->SetOutput("Out", outputs); |
| 34 | +} |
| 35 | + |
| 36 | +// (a, weights, bias)->depthwise conv mkldnn->b |
| 37 | +// (b, weights2, bias2)->depthwise conv no mkldnn->c |
| 38 | +// (c, weights3, bias3)->conv mkldnn->d |
| 39 | +// (d, weights3, bias3)->conv no mkldnn->e |
| 40 | +ProgramDesc BuildProgramDesc() { |
| 41 | + ProgramDesc prog; |
| 42 | + for (auto& v : std::vector<std::string>( |
| 43 | + {"a", "b", "c", "d", "e", "weights", "bias", "weights2", "bias2", |
| 44 | + "weights3", "bias3", "weights4", "bias4"})) { |
| 45 | + auto* var = prog.MutableBlock(0)->Var(v); |
| 46 | + var->SetType(proto::VarType::SELECTED_ROWS); |
| 47 | + if (v == "weights" || v == "bias" || v == "weights2" || v == "bias2" || |
| 48 | + v == "weights3" || v == "bias3" || v == "weights4" || v == "bias4") { |
| 49 | + var->SetPersistable(true); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + // depthwise conv with MKL-DNN |
| 54 | + SetOp(&prog, "depthwise_conv2d", "conv1", |
| 55 | + std::vector<std::string>({"a", "weights", "bias"}), |
| 56 | + std::vector<std::string>({"b"}), true); |
| 57 | + // depthwise conv without MKL-DNN |
| 58 | + SetOp(&prog, "depthwise_conv2d", "conv2", |
| 59 | + std::vector<std::string>({"b", "weights2", "bias2"}), |
| 60 | + std::vector<std::string>({"c"}), false); |
| 61 | + // conv with MKL-DNN |
| 62 | + SetOp(&prog, "conv2d", "conv3", |
| 63 | + std::vector<std::string>({"c", "weights3", "bias3"}), |
| 64 | + std::vector<std::string>({"d"}), true); |
| 65 | + // conv without MKL-dNN |
| 66 | + SetOp(&prog, "conv2d", "conv4", |
| 67 | + std::vector<std::string>({"d", "weights4", "bias4"}), |
| 68 | + std::vector<std::string>({"e"}), false); |
| 69 | + |
| 70 | + return prog; |
| 71 | +} |
| 72 | + |
| 73 | +TEST(DepthwiseConvMKLDNNPass, basic) { |
| 74 | + auto prog = BuildProgramDesc(); |
| 75 | + |
| 76 | + std::unique_ptr<ir::Graph> graph(new ir::Graph(prog)); |
| 77 | + |
| 78 | + auto pass = PassRegistry::Instance().Get("depthwise_conv_mkldnn_pass"); |
| 79 | + |
| 80 | + struct counters { |
| 81 | + int mkldnn_depthwise_conv_nodes; |
| 82 | + int other_depthwise_conv_nodes; |
| 83 | + int mkldnn_conv_nodes; |
| 84 | + int other_conv_nodes; |
| 85 | + }; |
| 86 | + |
| 87 | + counters before{1, 1, 1, 1}; |
| 88 | + |
| 89 | + graph = pass->Apply(std::move(graph)); |
| 90 | + |
| 91 | + // initialize counters before loop |
| 92 | + counters after{0, 0, 0, 0}; |
| 93 | + |
| 94 | + for (auto* node : graph->Nodes()) { |
| 95 | + if (node->IsOp()) { |
| 96 | + auto* op = node->Op(); |
| 97 | + if (op->Type() == "conv2d") { |
| 98 | + if (boost::get<bool>(op->GetAttr("use_mkldnn"))) |
| 99 | + after.mkldnn_conv_nodes++; |
| 100 | + else |
| 101 | + after.other_conv_nodes++; |
| 102 | + } else if (op->Type() == "depthwise_conv2d") { |
| 103 | + if (boost::get<bool>(op->GetAttr("use_mkldnn"))) |
| 104 | + after.mkldnn_depthwise_conv_nodes++; |
| 105 | + else |
| 106 | + after.other_depthwise_conv_nodes++; |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + EXPECT_EQ(after.other_depthwise_conv_nodes, |
| 112 | + before.other_depthwise_conv_nodes); |
| 113 | + EXPECT_EQ(after.other_conv_nodes, before.other_conv_nodes); |
| 114 | + EXPECT_EQ(after.mkldnn_depthwise_conv_nodes, |
| 115 | + before.mkldnn_depthwise_conv_nodes - 1); |
| 116 | + EXPECT_EQ(after.mkldnn_conv_nodes, before.mkldnn_conv_nodes + 1); |
| 117 | +} |
| 118 | + |
| 119 | +} // namespace ir |
| 120 | +} // namespace framework |
| 121 | +} // namespace paddle |
| 122 | + |
| 123 | +USE_PASS(depthwise_conv_mkldnn_pass); |
0 commit comments