fc_fuse_pass.cc 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// 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_fuse_pass.h"
#include <string>
#include <vector>
#include "paddle/fluid/platform/enforce.h"

namespace paddle {
namespace framework {
namespace ir {

std::unique_ptr<ir::Graph> FCFusePass::ApplyImpl(
    std::unique_ptr<ir::Graph> graph) const {
  PADDLE_ENFORCE(graph.get());
Y
Yan Chunwei 已提交
27
  FusePassBase::Init("fc_fuse", graph.get());
28 29 30

  std::unordered_set<Node*> nodes2delete;

31
  GraphPatternDetector gpd;
32 33 34 35
  auto* x = gpd.mutable_pattern()
                ->NewNode("fc_fuse/x")
                ->AsInput()
                ->assert_is_op_input("mul", "X");
Y
Yan Chunwei 已提交
36 37
  patterns::FC fc_pattern(gpd.mutable_pattern(), "fc_fuse");
  fc_pattern(x, true /*with bias*/);
38

Y
Yan Chunwei 已提交
39
  int found_fc_count = 0;
40
  auto handler = [&](const GraphPatternDetector::subgraph_t& subgraph,
41
                     Graph* g) {
42
    VLOG(40) << "handle FC fuse";
Y
Yan Chunwei 已提交
43 44 45 46 47 48
    GET_IR_NODE_FROM_SUBGRAPH(w, w, fc_pattern);
    GET_IR_NODE_FROM_SUBGRAPH(fc_bias, bias, fc_pattern);
    GET_IR_NODE_FROM_SUBGRAPH(fc_out, Out, fc_pattern);
    GET_IR_NODE_FROM_SUBGRAPH(mul, mul, fc_pattern);
    GET_IR_NODE_FROM_SUBGRAPH(elementwise_add, elementwise_add, fc_pattern);
    GET_IR_NODE_FROM_SUBGRAPH(mul_out, mul_out, fc_pattern);
49 50 51

    // Create an FC Node.
    OpDesc desc;
Y
Yan Chunwei 已提交
52
    std::string fc_x_in = subgraph.at(x)->Name();
53 54 55
    std::string fc_Y_in = w->Name();
    std::string fc_bias_in = fc_bias->Name();
    std::string fc_out_out = fc_out->Name();
56 57 58
    desc.SetInput("Input", std::vector<std::string>({fc_x_in}));
    desc.SetInput("W", std::vector<std::string>({fc_Y_in}));
    desc.SetInput("Bias", std::vector<std::string>({fc_bias_in}));
59
    desc.SetOutput("Out", std::vector<std::string>({fc_out_out}));
60 61
    desc.SetType("fc");
    auto fc_node = g->CreateOpNode(&desc);  // OpDesc will be copied.
62
    GraphSafeRemoveNodes(graph.get(), {mul, elementwise_add, mul_out});
63

Y
Yan Chunwei 已提交
64 65
    PADDLE_ENFORCE(subgraph.count(x));
    IR_NODE_LINK_TO(subgraph.at(x), fc_node);
66 67 68
    IR_NODE_LINK_TO(w, fc_node);
    IR_NODE_LINK_TO(fc_bias, fc_node);
    IR_NODE_LINK_TO(fc_node, fc_out);
Y
Yan Chunwei 已提交
69 70

    found_fc_count++;
71 72 73 74
  };

  gpd(graph.get(), handler);

Y
Yan Chunwei 已提交
75
  AddStatis(found_fc_count);
76 77 78 79 80 81 82 83
  return graph;
}

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

REGISTER_PASS(fc_fuse_pass, paddle::framework::ir::FCFusePass);