|
| 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/fc_gru_fuse_pass.h" |
| 16 | +#include <string> |
| 17 | +#include "paddle/fluid/framework/lod_tensor.h" |
| 18 | + |
| 19 | +namespace paddle { |
| 20 | +namespace framework { |
| 21 | +namespace ir { |
| 22 | + |
| 23 | +static void BuildPattern(PDPattern* pattern, const std::string& name_scope, |
| 24 | + bool with_fc_bias) { |
| 25 | + PDNode* x = pattern->NewNode(name_scope, "x") |
| 26 | + ->assert_is_op_input("mul") |
| 27 | + ->assert_var_not_persistable(); |
| 28 | + auto* fc_out = patterns::FC(pattern, name_scope, x, with_fc_bias); |
| 29 | + fc_out->AsIntermediate(); // fc_out is a tmp var, will be removed after fuse. |
| 30 | + patterns::GRU(pattern, name_scope, fc_out); |
| 31 | + VLOG(3) << "fc_gru pattern \n" << pattern->DotString(); |
| 32 | +} |
| 33 | + |
| 34 | +static int BuildFusion(Graph* graph, const std::string& name_scope, |
| 35 | + Scope* scope, bool with_fc_bias) { |
| 36 | + GraphPatternDetector gpd; |
| 37 | + auto* pattern = gpd.mutable_pattern(); |
| 38 | + |
| 39 | + BuildPattern(pattern, name_scope, with_fc_bias); |
| 40 | + |
| 41 | + // Create New OpDesc |
| 42 | + auto gru_creater = [&](int gru, int x, int weight_x, int weight_h, int bias, |
| 43 | + int hidden, int fc_bias) { |
| 44 | +#define GET_NODE(x) auto* x##_n = graph->RetriveNode(x); |
| 45 | + GET_NODE(x); |
| 46 | + GET_NODE(weight_x); |
| 47 | + GET_NODE(weight_h); |
| 48 | + GET_NODE(bias); |
| 49 | + GET_NODE(hidden); |
| 50 | + GET_NODE(gru); |
| 51 | + |
| 52 | + OpDesc op_desc; |
| 53 | + op_desc.SetType("fusion_gru"); |
| 54 | + |
| 55 | +#define NEW_NAME(x) name_scope + "/at." #x ".new" |
| 56 | +#define SET_IN(Key, node__) op_desc.SetInput(#Key, {node__##_n->Name()}); |
| 57 | + SET_IN(X, x); |
| 58 | + SET_IN(WeightX, weight_x); |
| 59 | + SET_IN(WeightH, weight_h); |
| 60 | + if (with_fc_bias) { |
| 61 | + op_desc.SetInput("Bias", {NEW_NAME(bias) + bias_n->Name()}); |
| 62 | + } else { |
| 63 | + SET_IN(Bias, bias); |
| 64 | + } |
| 65 | +#undef SET_IN |
| 66 | + op_desc.SetInput("H0", {}); |
| 67 | + op_desc.SetOutput("Hidden", {hidden_n->Name()}); |
| 68 | + op_desc.SetAttr("is_reverse", gru_n->Op()->GetAttr("is_reverse")); |
| 69 | + // TODO(TJ): This should be a option for infer |
| 70 | + op_desc.SetAttr("use_seq", true); |
| 71 | + |
| 72 | +#define SET_IMTERMEDIATE_OUT(key) op_desc.SetOutput(#key, {NEW_NAME(key)}) |
| 73 | + SET_IMTERMEDIATE_OUT(ReorderedH0); |
| 74 | + SET_IMTERMEDIATE_OUT(XX); |
| 75 | + SET_IMTERMEDIATE_OUT(BatchedInput); |
| 76 | + SET_IMTERMEDIATE_OUT(BatchedOut); |
| 77 | +#undef SET_IMTERMEDIATE_OUT |
| 78 | + |
| 79 | + auto* op = graph->CreateOpNode(&op_desc); |
| 80 | + PADDLE_ENFORCE(graph->Has(kParamScopeAttr)); |
| 81 | + auto* scope = graph->Get<Scope*>(kParamScopeAttr); |
| 82 | + PADDLE_ENFORCE(scope); |
| 83 | + if (with_fc_bias) { |
| 84 | + // Fusion GRU bias = fcbias + grubias |
| 85 | + auto* fusion_bias_var = scope->Var(NEW_NAME(bias) + bias_n->Name()); |
| 86 | + auto* out_bias_tensor = |
| 87 | + fusion_bias_var->GetMutable<framework::LoDTensor>(); |
| 88 | + PADDLE_ENFORCE(fusion_bias_var); |
| 89 | + GET_NODE(fc_bias); |
| 90 | + PADDLE_ENFORCE(fc_bias_n); |
| 91 | + auto* gru_bias_var = scope->FindVar(bias_n->Name()); |
| 92 | + auto* fc_bias_var = scope->FindVar(fc_bias_n->Name()); |
| 93 | + PADDLE_ENFORCE(gru_bias_var); |
| 94 | + PADDLE_ENFORCE(fc_bias_var); |
| 95 | + const auto& gru_bias_tenosr = gru_bias_var->Get<framework::LoDTensor>(); |
| 96 | + const auto& fc_bias_tensor = fc_bias_var->Get<framework::LoDTensor>(); |
| 97 | + // new bias = fc bias + gru bias |
| 98 | + out_bias_tensor->Resize(gru_bias_tenosr.dims()); |
| 99 | + auto* data = out_bias_tensor->mutable_data<float>(platform::CPUPlace()); |
| 100 | + for (int i = 0; i < out_bias_tensor->numel(); i++) { |
| 101 | + data[i] = |
| 102 | + fc_bias_tensor.data<float>()[i] + gru_bias_tenosr.data<float>()[i]; |
| 103 | + } |
| 104 | + } |
| 105 | +#undef GET_NODE |
| 106 | + |
| 107 | +#define NEW_IMTERMEDIATE_OUT(key) \ |
| 108 | + scope->Var(NEW_NAME(key))->GetMutable<framework::LoDTensor>() |
| 109 | + NEW_IMTERMEDIATE_OUT(ReorderedH0); |
| 110 | + NEW_IMTERMEDIATE_OUT(XX); |
| 111 | + NEW_IMTERMEDIATE_OUT(BatchedInput); |
| 112 | + NEW_IMTERMEDIATE_OUT(BatchedOut); |
| 113 | +#undef NEW_NAME |
| 114 | +#undef NEW_IMTERMEDIATE_OUT |
| 115 | + |
| 116 | + IR_NODE_LINK_TO(x_n, op); |
| 117 | + IR_NODE_LINK_TO(weight_x_n, op); |
| 118 | + IR_NODE_LINK_TO(weight_h_n, op); |
| 119 | + IR_NODE_LINK_TO(bias_n, op); // actually should link to new bias if have |
| 120 | + IR_NODE_LINK_TO(op, hidden_n); |
| 121 | + // h0? |
| 122 | + return op; |
| 123 | + }; |
| 124 | + |
| 125 | + int fusion_count{0}; |
| 126 | + auto handler = [&](const GraphPatternDetector::subgraph_t& subgraph, |
| 127 | + Graph* g) { |
| 128 | +#define GET_NODE(name__) \ |
| 129 | + std::string name__##key = name_scope + "/" + #name__; \ |
| 130 | + auto* name__##n = pattern->RetrieveNode(name__##key); \ |
| 131 | + PADDLE_ENFORCE(name__##n); \ |
| 132 | + PADDLE_ENFORCE(subgraph.count(name__##n)); \ |
| 133 | + Node* name__##_n = subgraph.at(name__##n); \ |
| 134 | + int name__ __attribute__((unused)) = name__##_n->id(); |
| 135 | + |
| 136 | + GET_NODE(x); |
| 137 | + GET_NODE(w); // fc weight |
| 138 | + GET_NODE(mul); |
| 139 | + GET_NODE(fc_out); |
| 140 | + GET_NODE(Weight); |
| 141 | + GET_NODE(gru); |
| 142 | + GET_NODE(Bias); |
| 143 | + GET_NODE(Hidden); |
| 144 | + // nodes need be removed |
| 145 | + GET_NODE(BatchGate); |
| 146 | + GET_NODE(BatchResetHiddenPrev); |
| 147 | + GET_NODE(BatchHidden); |
| 148 | + |
| 149 | + if (with_fc_bias) { |
| 150 | + GET_NODE(mul_out); |
| 151 | + GET_NODE(fc_bias); |
| 152 | + GET_NODE(elementwise_add); |
| 153 | + gru_creater(gru, x, w, Weight, Bias, Hidden, fc_bias); |
| 154 | + // Remove unneeded nodes. |
| 155 | + std::unordered_set<const Node*> marked_nodes( |
| 156 | + {mul_n, gru_n, elementwise_add_n, fc_bias_n, fc_out_n, mul_out_n, |
| 157 | + BatchGate_n, BatchResetHiddenPrev_n, BatchHidden_n}); |
| 158 | + GraphSafeRemoveNodes(graph, marked_nodes); |
| 159 | + } else { |
| 160 | + gru_creater(gru, x, w, Weight, Bias, Hidden, -1); |
| 161 | + // Remove unneeded nodes. |
| 162 | + std::unordered_set<const Node*> marked_nodes( |
| 163 | + {mul_n, gru_n, BatchGate_n, BatchResetHiddenPrev_n, BatchHidden_n}); |
| 164 | + GraphSafeRemoveNodes(graph, marked_nodes); |
| 165 | + } |
| 166 | +#undef GET_NODE |
| 167 | + |
| 168 | + ++fusion_count; |
| 169 | + }; |
| 170 | + |
| 171 | + gpd(graph, handler); |
| 172 | + |
| 173 | + return fusion_count; |
| 174 | +} |
| 175 | + |
| 176 | +std::unique_ptr<ir::Graph> MulGRUFusePass::ApplyImpl( |
| 177 | + std::unique_ptr<ir::Graph> graph) const { |
| 178 | + FusePassBase::Init(name_scope_, graph.get()); |
| 179 | + |
| 180 | + int fusion_count = BuildFusion(graph.get(), name_scope_, param_scope(), |
| 181 | + false /*with_fc_bias*/); |
| 182 | + |
| 183 | + AddStatis(fusion_count); |
| 184 | + return graph; |
| 185 | +} |
| 186 | + |
| 187 | +std::unique_ptr<ir::Graph> FCGRUFusePass::ApplyImpl( |
| 188 | + std::unique_ptr<ir::Graph> graph) const { |
| 189 | + FusePassBase::Init(name_scope_, graph.get()); |
| 190 | + |
| 191 | + int fusion_count = BuildFusion(graph.get(), name_scope_, param_scope(), |
| 192 | + true /*with_fc_bias*/); |
| 193 | + |
| 194 | + AddStatis(fusion_count); |
| 195 | + return graph; |
| 196 | +} |
| 197 | + |
| 198 | +} // namespace ir |
| 199 | +} // namespace framework |
| 200 | +} // namespace paddle |
| 201 | + |
| 202 | +REGISTER_PASS(mul_gru_fuse_pass, paddle::framework::ir::MulGRUFusePass); |
| 203 | +REGISTER_PASS(fc_gru_fuse_pass, paddle::framework::ir::FCGRUFusePass); |
0 commit comments