fc_gru_fuse_pass.cc 6.9 KB
Newer Older
T
tensor-tang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/fluid/framework/ir/fc_gru_fuse_pass.h"
#include <string>
#include "paddle/fluid/framework/lod_tensor.h"

namespace paddle {
namespace framework {
namespace ir {

T
tensor-tang 已提交
23 24
static void BuildPattern(PDPattern* pattern, const std::string& name_scope,
                         bool with_fc_bias) {
T
tensor-tang 已提交
25 26 27 28 29 30
  PDNode* x = pattern->NewNode(name_scope, "x")
                  ->assert_is_op_input("mul")
                  ->assert_var_not_persistable();
  auto* fc_out = patterns::FC(pattern, name_scope, x, with_fc_bias);
  fc_out->AsIntermediate();  // fc_out is a tmp var, will be removed after fuse.
  patterns::GRU(pattern, name_scope, fc_out);
T
tensor-tang 已提交
31
  VLOG(3) << "fc_gru pattern \n" << pattern->DotString();
T
tensor-tang 已提交
32 33
}

T
tensor-tang 已提交
34 35
static int BuildFusion(Graph* graph, const std::string& name_scope,
                       Scope* scope, bool with_fc_bias) {
T
tensor-tang 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
  GraphPatternDetector gpd;
  auto* pattern = gpd.mutable_pattern();

  BuildPattern(pattern, name_scope, with_fc_bias);

  // Create New OpDesc
  auto gru_creater = [&](int gru, int x, int weight_x, int weight_h, int bias,
                         int hidden, int fc_bias) {
#define GET_NODE(x) auto* x##_n = graph->RetriveNode(x);
    GET_NODE(x);
    GET_NODE(weight_x);
    GET_NODE(weight_h);
    GET_NODE(bias);
    GET_NODE(hidden);
    GET_NODE(gru);

    OpDesc op_desc;
    op_desc.SetType("fusion_gru");
T
tensor-tang 已提交
54 55

#define NEW_NAME(x) name_scope + "/at." #x ".new"
T
tensor-tang 已提交
56 57 58 59
#define SET_IN(Key, node__) op_desc.SetInput(#Key, {node__##_n->Name()});
    SET_IN(X, x);
    SET_IN(WeightX, weight_x);
    SET_IN(WeightH, weight_h);
T
tensor-tang 已提交
60 61 62 63 64
    if (with_fc_bias) {
      op_desc.SetInput("Bias", {NEW_NAME(bias) + bias_n->Name()});
    } else {
      SET_IN(Bias, bias);
    }
T
tensor-tang 已提交
65
#undef SET_IN
T
tensor-tang 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    op_desc.SetInput("H0", {});
    op_desc.SetOutput("Hidden", {hidden_n->Name()});
    op_desc.SetAttr("is_reverse", gru_n->Op()->GetAttr("is_reverse"));
    // TODO(TJ): This should be a option for infer
    op_desc.SetAttr("use_seq", true);

#define SET_IMTERMEDIATE_OUT(key) op_desc.SetOutput(#key, {NEW_NAME(key)})
    SET_IMTERMEDIATE_OUT(ReorderedH0);
    SET_IMTERMEDIATE_OUT(XX);
    SET_IMTERMEDIATE_OUT(BatchedInput);
    SET_IMTERMEDIATE_OUT(BatchedOut);
#undef SET_IMTERMEDIATE_OUT

    auto* op = graph->CreateOpNode(&op_desc);
    PADDLE_ENFORCE(graph->Has(kParamScopeAttr));
    auto* scope = graph->Get<Scope*>(kParamScopeAttr);
    PADDLE_ENFORCE(scope);
T
tensor-tang 已提交
83
    if (with_fc_bias) {
T
tensor-tang 已提交
84 85 86 87 88 89 90
      // Fusion GRU bias = fcbias + grubias
      auto* fusion_bias_var = scope->Var(NEW_NAME(bias) + bias_n->Name());
      auto* out_bias_tensor =
          fusion_bias_var->GetMutable<framework::LoDTensor>();
      PADDLE_ENFORCE(fusion_bias_var);
      GET_NODE(fc_bias);
      PADDLE_ENFORCE(fc_bias_n);
T
tensor-tang 已提交
91
      auto* gru_bias_var = scope->FindVar(bias_n->Name());
T
tensor-tang 已提交
92
      auto* fc_bias_var = scope->FindVar(fc_bias_n->Name());
T
tensor-tang 已提交
93
      PADDLE_ENFORCE(gru_bias_var);
T
tensor-tang 已提交
94
      PADDLE_ENFORCE(fc_bias_var);
T
tensor-tang 已提交
95 96 97
      const auto& gru_bias_tenosr = gru_bias_var->Get<framework::LoDTensor>();
      const auto& fc_bias_tensor = fc_bias_var->Get<framework::LoDTensor>();
      // new bias = fc bias + gru bias
T
tensor-tang 已提交
98 99 100
      out_bias_tensor->Resize(gru_bias_tenosr.dims());
      auto* data = out_bias_tensor->mutable_data<float>(platform::CPUPlace());
      for (int i = 0; i < out_bias_tensor->numel(); i++) {
T
tensor-tang 已提交
101 102 103 104 105 106
        data[i] =
            fc_bias_tensor.data<float>()[i] + gru_bias_tenosr.data<float>()[i];
      }
    }
#undef GET_NODE

T
tensor-tang 已提交
107 108 109 110 111 112 113 114
#define NEW_IMTERMEDIATE_OUT(key) \
  scope->Var(NEW_NAME(key))->GetMutable<framework::LoDTensor>()
    NEW_IMTERMEDIATE_OUT(ReorderedH0);
    NEW_IMTERMEDIATE_OUT(XX);
    NEW_IMTERMEDIATE_OUT(BatchedInput);
    NEW_IMTERMEDIATE_OUT(BatchedOut);
#undef NEW_NAME
#undef NEW_IMTERMEDIATE_OUT
T
tensor-tang 已提交
115 116 117 118

    IR_NODE_LINK_TO(x_n, op);
    IR_NODE_LINK_TO(weight_x_n, op);
    IR_NODE_LINK_TO(weight_h_n, op);
T
tensor-tang 已提交
119
    IR_NODE_LINK_TO(bias_n, op);  // actually should link to new bias if have
T
tensor-tang 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    IR_NODE_LINK_TO(op, hidden_n);
    // h0?
    return op;
  };

  int fusion_count{0};
  auto handler = [&](const GraphPatternDetector::subgraph_t& subgraph,
                     Graph* g) {
#define GET_NODE(name__)                                \
  std::string name__##key = name_scope + "/" + #name__; \
  auto* name__##n = pattern->RetrieveNode(name__##key); \
  PADDLE_ENFORCE(name__##n);                            \
  PADDLE_ENFORCE(subgraph.count(name__##n));            \
  Node* name__##_n = subgraph.at(name__##n);            \
  int name__ __attribute__((unused)) = name__##_n->id();

    GET_NODE(x);
T
tensor-tang 已提交
137
    GET_NODE(w);  // fc weight
T
tensor-tang 已提交
138 139 140 141 142 143
    GET_NODE(mul);
    GET_NODE(fc_out);
    GET_NODE(Weight);
    GET_NODE(gru);
    GET_NODE(Bias);
    GET_NODE(Hidden);
T
tensor-tang 已提交
144 145 146 147
    // nodes need be removed
    GET_NODE(BatchGate);
    GET_NODE(BatchResetHiddenPrev);
    GET_NODE(BatchHidden);
T
tensor-tang 已提交
148 149

    if (with_fc_bias) {
T
tensor-tang 已提交
150
      GET_NODE(mul_out);
T
tensor-tang 已提交
151 152 153 154 155
      GET_NODE(fc_bias);
      GET_NODE(elementwise_add);
      gru_creater(gru, x, w, Weight, Bias, Hidden, fc_bias);
      // Remove unneeded nodes.
      std::unordered_set<const Node*> marked_nodes(
T
tensor-tang 已提交
156 157
          {mul_n, gru_n, elementwise_add_n, fc_bias_n, fc_out_n, mul_out_n,
           BatchGate_n, BatchResetHiddenPrev_n, BatchHidden_n});
T
tensor-tang 已提交
158 159 160 161
      GraphSafeRemoveNodes(graph, marked_nodes);
    } else {
      gru_creater(gru, x, w, Weight, Bias, Hidden, -1);
      // Remove unneeded nodes.
T
tensor-tang 已提交
162 163
      std::unordered_set<const Node*> marked_nodes(
          {mul_n, gru_n, BatchGate_n, BatchResetHiddenPrev_n, BatchHidden_n});
T
tensor-tang 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
      GraphSafeRemoveNodes(graph, marked_nodes);
    }
#undef GET_NODE

    ++fusion_count;
  };

  gpd(graph, handler);

  return fusion_count;
}

std::unique_ptr<ir::Graph> MulGRUFusePass::ApplyImpl(
    std::unique_ptr<ir::Graph> graph) const {
  FusePassBase::Init(name_scope_, graph.get());

  int fusion_count = BuildFusion(graph.get(), name_scope_, param_scope(),
                                 false /*with_fc_bias*/);

  AddStatis(fusion_count);
  return graph;
}

std::unique_ptr<ir::Graph> FCGRUFusePass::ApplyImpl(
    std::unique_ptr<ir::Graph> graph) const {
  FusePassBase::Init(name_scope_, graph.get());

  int fusion_count = BuildFusion(graph.get(), name_scope_, param_scope(),
                                 true /*with_fc_bias*/);

  AddStatis(fusion_count);
  return graph;
}

}  // namespace ir
}  // namespace framework
}  // namespace paddle

T
tensor-tang 已提交
202 203
REGISTER_PASS(mul_gru_fuse_pass, paddle::framework::ir::MulGRUFusePass);
REGISTER_PASS(fc_gru_fuse_pass, paddle::framework::ir::FCGRUFusePass);