|
| 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/conv_bias_mkldnn_fuse_pass.h" |
| 16 | +#include <functional> |
| 17 | +#include <string> |
| 18 | +#include <vector> |
| 19 | +#include "paddle/fluid/framework/lod_tensor.h" |
| 20 | +#include "paddle/fluid/platform/enforce.h" |
| 21 | + |
| 22 | +namespace paddle { |
| 23 | +namespace framework { |
| 24 | +namespace ir { |
| 25 | + |
| 26 | +template <typename BinaryOperation> |
| 27 | +LoDTensor tensor_apply_eltwise(const LoDTensor& vec_a, const LoDTensor& vec_b, |
| 28 | + BinaryOperation f) { |
| 29 | + PADDLE_ENFORCE_EQ(vec_a.dims(), vec_b.dims()); |
| 30 | + LoDTensor vec_y; |
| 31 | + vec_y.Resize(vec_a.dims()); |
| 32 | + const float* a = vec_a.data<float>(); |
| 33 | + const float* b = vec_b.data<float>(); |
| 34 | + float* y = vec_y.mutable_data<float>(platform::CPUPlace()); |
| 35 | + for (int i = 0; i < vec_a.numel(); i++) { |
| 36 | + y[i] = f(a[i], b[i]); |
| 37 | + } |
| 38 | + return vec_y; |
| 39 | +} |
| 40 | + |
| 41 | +std::unique_ptr<ir::Graph> ConvBiasFusePass::ApplyImpl( |
| 42 | + std::unique_ptr<ir::Graph> graph) const { |
| 43 | + PADDLE_ENFORCE(graph.get()); |
| 44 | + FusePassBase::Init(name_scope_, graph.get()); |
| 45 | + |
| 46 | + auto* scope = param_scope(); |
| 47 | + PADDLE_ENFORCE(scope); |
| 48 | + |
| 49 | + GraphPatternDetector gpd; |
| 50 | + auto* conv_input = |
| 51 | + gpd.mutable_pattern() |
| 52 | + ->NewNode(patterns::PDNodeName(name_scope_, "conv_input")) |
| 53 | + ->AsInput() |
| 54 | + ->assert_is_op_input("conv2d", "Input"); |
| 55 | + patterns::ConvBias conv_bias_pattern(gpd.mutable_pattern(), name_scope_); |
| 56 | + conv_bias_pattern(conv_input); |
| 57 | + int found_conv_bias_count = 0; |
| 58 | + auto handler = [&](const GraphPatternDetector::subgraph_t& subgraph, |
| 59 | + Graph* g) { |
| 60 | + VLOG(4) << "handle ConvBias fuse"; |
| 61 | + GET_IR_NODE_FROM_SUBGRAPH(conv_weight, conv_weight, |
| 62 | + conv_bias_pattern); // Filter |
| 63 | + GET_IR_NODE_FROM_SUBGRAPH(conv_out, conv_out, conv_bias_pattern); // tmp |
| 64 | + GET_IR_NODE_FROM_SUBGRAPH(conv, conv, conv_bias_pattern); // CONV op |
| 65 | + // bias |
| 66 | + GET_IR_NODE_FROM_SUBGRAPH(eltwise_bias, eltwise_bias, conv_bias_pattern); |
| 67 | + // output |
| 68 | + GET_IR_NODE_FROM_SUBGRAPH(eltwise_out, eltwise_out, conv_bias_pattern); |
| 69 | + // elementwise_add op |
| 70 | + GET_IR_NODE_FROM_SUBGRAPH(eltwise, eltwise, conv_bias_pattern); |
| 71 | + |
| 72 | + PADDLE_ENFORCE(subgraph.count(conv_input)); |
| 73 | + |
| 74 | + // check if fuse can be done and if MKL-DNN should be used |
| 75 | + FuseOptions fuse_option = FindFuseOption(*conv, *eltwise); |
| 76 | + if (fuse_option == DO_NOT_FUSE || fuse_option == FUSE_NATIVE) { |
| 77 | + VLOG(3) << "do not perform conv+bias fuse"; |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + auto* eltwise_bias_tensor = |
| 82 | + scope->FindVar(eltwise_bias->Name())->GetMutable<LoDTensor>(); |
| 83 | + |
| 84 | + auto input_names = conv->Op()->InputNames(); |
| 85 | + bool has_bias = std::find(input_names.begin(), input_names.end(), "Bias") != |
| 86 | + input_names.end(); |
| 87 | + if (has_bias && conv->Op()->Input("Bias").size() > 0) { |
| 88 | + auto conv_bias_names = conv->Op()->Input("Bias"); |
| 89 | + // add eltwise bias to existing conv bias |
| 90 | + PADDLE_ENFORCE_EQ(conv_bias_names.size(), 1); |
| 91 | + auto* conv_bias_var = scope->FindVar(conv_bias_names[0]); |
| 92 | + auto* conv_bias_tensor = conv_bias_var->GetMutable<LoDTensor>(); |
| 93 | + PADDLE_ENFORCE_EQ(conv_bias_tensor->dims(), eltwise_bias_tensor->dims()); |
| 94 | + *conv_bias_tensor = tensor_apply_eltwise( |
| 95 | + *conv_bias_tensor, *eltwise_bias_tensor, std::plus<float>()); |
| 96 | + |
| 97 | + conv->Op()->SetOutput("Output", |
| 98 | + std::vector<std::string>({eltwise_out->Name()})); |
| 99 | + |
| 100 | + GraphSafeRemoveNodes(graph.get(), {eltwise, conv_out}); |
| 101 | + |
| 102 | + IR_NODE_LINK_TO(conv, eltwise_out); |
| 103 | + } else { |
| 104 | + // take eltwise bias as conv bias |
| 105 | + OpDesc desc; |
| 106 | + |
| 107 | + desc.SetInput( |
| 108 | + "Input", std::vector<std::string>({subgraph.at(conv_input)->Name()})); |
| 109 | + desc.SetInput("Filter", std::vector<std::string>({conv_weight->Name()})); |
| 110 | + desc.SetInput("Bias", std::vector<std::string>({eltwise_bias->Name()})); |
| 111 | + desc.SetOutput("Output", std::vector<std::string>({eltwise_out->Name()})); |
| 112 | + desc.SetType("conv2d"); |
| 113 | + |
| 114 | + for (auto& attr : conv->Op()->GetAttrMap()) { |
| 115 | + desc.SetAttr(attr.first, attr.second); |
| 116 | + } |
| 117 | + auto conv_bias_node = g->CreateOpNode(&desc); |
| 118 | + |
| 119 | + IR_NODE_LINK_TO(subgraph.at(conv_input), conv_bias_node); |
| 120 | + IR_NODE_LINK_TO(conv_weight, conv_bias_node); |
| 121 | + IR_NODE_LINK_TO(eltwise_bias, conv_bias_node); |
| 122 | + IR_NODE_LINK_TO(conv_bias_node, eltwise_out); |
| 123 | + |
| 124 | + GraphSafeRemoveNodes(graph.get(), {conv, eltwise, conv_out}); |
| 125 | + } |
| 126 | + |
| 127 | + found_conv_bias_count++; |
| 128 | + }; |
| 129 | + gpd(graph.get(), handler); |
| 130 | + AddStatis(found_conv_bias_count); |
| 131 | + return graph; |
| 132 | +} |
| 133 | +} // namespace ir |
| 134 | +} // namespace framework |
| 135 | +} // namespace paddle |
| 136 | +REGISTER_PASS(conv_bias_mkldnn_fuse_pass, |
| 137 | + paddle::framework::ir::ConvBiasFusePass); |
0 commit comments