|
| 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_concat_relu_mkldnn_fuse_pass.h" |
| 16 | +#include <vector> |
| 17 | +#include "paddle/fluid/platform/enforce.h" |
| 18 | + |
| 19 | +namespace paddle { |
| 20 | +namespace framework { |
| 21 | +namespace ir { |
| 22 | + |
| 23 | +void ConvConcatReLUFusePass::FindConcatWithConvs( |
| 24 | + ir::Graph* graph, |
| 25 | + std::unordered_map<const Node*, int>* concat_with_convs_counter) const { |
| 26 | + GraphPatternDetector gpd; |
| 27 | + patterns::ConcatReLU concat_relu_pattern{gpd.mutable_pattern(), |
| 28 | + "concat_relu"}; |
| 29 | + concat_relu_pattern(); |
| 30 | + |
| 31 | + int found_count = 0; |
| 32 | + auto handler = [&](const GraphPatternDetector::subgraph_t& subgraph, |
| 33 | + Graph* g) { |
| 34 | + VLOG(4) << "Find Concats with Convs"; |
| 35 | + GET_IR_NODE_FROM_SUBGRAPH(concat_op, concat_op, concat_relu_pattern); |
| 36 | + GET_IR_NODE_FROM_SUBGRAPH(relu_op, relu_op, concat_relu_pattern); |
| 37 | + |
| 38 | + auto concat_inputs = concat_op->inputs; |
| 39 | + |
| 40 | + for (auto node : concat_inputs) { |
| 41 | + auto prev_op_node = node->inputs; |
| 42 | + PADDLE_ENFORCE_EQ(prev_op_node.size(), 1); |
| 43 | + auto* conv_op = prev_op_node[0]; |
| 44 | + if (conv_op->Op()->Type() != "conv2d") return; |
| 45 | + |
| 46 | + FuseOptions fuse_option = FindFuseOption(*conv_op, *relu_op); |
| 47 | + if (fuse_option == DO_NOT_FUSE) { |
| 48 | + return; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + (*concat_with_convs_counter)[concat_op] = concat_inputs.size(); |
| 53 | + found_count++; |
| 54 | + }; |
| 55 | + gpd(graph, handler); |
| 56 | + AddStatis(found_count); |
| 57 | +} |
| 58 | + |
| 59 | +void ConvConcatReLUFusePass::FuseConvConcatReLU( |
| 60 | + ir::Graph* graph, |
| 61 | + std::unordered_map<const Node*, int>* concat_with_convs_counter) const { |
| 62 | + GraphPatternDetector gpd; |
| 63 | + auto pattern = gpd.mutable_pattern(); |
| 64 | + patterns::ConvConcatReLU conv_concat_relu(pattern, name_scope_); |
| 65 | + conv_concat_relu(); |
| 66 | + |
| 67 | + int found_count = 0; |
| 68 | + auto handler = [&](const GraphPatternDetector::subgraph_t& subgraph, |
| 69 | + Graph* g) { |
| 70 | + VLOG(4) << "handle ConvConcatReLU fuse"; |
| 71 | + |
| 72 | + GET_IR_NODE_FROM_SUBGRAPH(conv_op, conv_op, conv_concat_relu); |
| 73 | + GET_IR_NODE_FROM_SUBGRAPH(conv_out, conv_out, conv_concat_relu); |
| 74 | + GET_IR_NODE_FROM_SUBGRAPH(concat_op, concat_op, conv_concat_relu); |
| 75 | + GET_IR_NODE_FROM_SUBGRAPH(concat_out, concat_out, conv_concat_relu); |
| 76 | + GET_IR_NODE_FROM_SUBGRAPH(relu_op, relu_op, conv_concat_relu); |
| 77 | + GET_IR_NODE_FROM_SUBGRAPH(relu_out, relu_out, conv_concat_relu); |
| 78 | + |
| 79 | + if (!concat_with_convs_counter->count(concat_op)) { |
| 80 | + VLOG(4) << "this concat has input from non-conv2d operator"; |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + // Transform Conv node into ConvReLU node. |
| 85 | + OpDesc* conv_desc = conv_op->Op(); |
| 86 | + conv_desc->SetAttr("fuse_relu", true); |
| 87 | + |
| 88 | + // Remove ReLU when all Convs were transformed. |
| 89 | + auto number_of_unfused_convs_left = |
| 90 | + --(*concat_with_convs_counter)[concat_op]; |
| 91 | + if (number_of_unfused_convs_left == 0) { |
| 92 | + OpDesc* concat_desc = concat_op->Op(); |
| 93 | + concat_desc->SetOutput("Out", |
| 94 | + std::vector<std::string>({relu_out->Name()})); |
| 95 | + GraphSafeRemoveNodes(graph, {relu_op, concat_out}); |
| 96 | + IR_NODE_LINK_TO(concat_op, relu_out); |
| 97 | + } |
| 98 | + |
| 99 | + found_count++; |
| 100 | + }; |
| 101 | + gpd(graph, handler); |
| 102 | + AddStatis(found_count); |
| 103 | +} |
| 104 | + |
| 105 | +void ConvConcatReLUFusePass::ApplyImpl(ir::Graph* graph) const { |
| 106 | + PADDLE_ENFORCE(graph); |
| 107 | + FusePassBase::Init(name_scope_, graph); |
| 108 | + |
| 109 | + std::unordered_map<const Node*, int> concat_with_convs_counter; |
| 110 | + FindConcatWithConvs(graph, &concat_with_convs_counter); |
| 111 | + FuseConvConcatReLU(graph, &concat_with_convs_counter); |
| 112 | +} |
| 113 | + |
| 114 | +} // namespace ir |
| 115 | +} // namespace framework |
| 116 | +} // namespace paddle |
| 117 | + |
| 118 | +REGISTER_PASS(conv_concat_relu_mkldnn_fuse_pass, |
| 119 | + paddle::framework::ir::ConvConcatReLUFusePass); |
0 commit comments