fc_fuse_pass.cc 9.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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"
16

17
#include <string>
W
wanghuancoder 已提交
18

19
#include "paddle/fluid/framework/op_version_registry.h"
20 21 22 23 24 25
#include "paddle/fluid/platform/enforce.h"

namespace paddle {
namespace framework {
namespace ir {

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
FCFusePass::FCFusePass() {
  AddOpCompat(OpCompat("mul"))
      .AddInput("X")
      .IsTensor()
      .End()
      .AddInput("Y")
      .IsTensor()
      .End()
      .AddOutput("Out")
      .IsTensor()
      .End()
      .AddAttr("x_num_col_dims")
      .IsNumGE(1)
      .End()
      .AddAttr("y_num_col_dims")
41
      .IsNumEQ(1)
42 43 44 45 46 47 48 49 50 51 52 53 54
      .End();

  AddOpCompat(OpCompat("elementwise_add"))
      .AddInput("X")
      .IsTensor()
      .End()
      .AddInput("Y")
      .IsTensor()
      .End()
      .AddOutput("Out")
      .IsTensor()
      .End()
      .AddAttr("axis")
W
Wilber 已提交
55 56 57 58 59 60
      .IsNumMatch<int>([](int axis) -> bool {
        if (axis == -1 || axis >= 1) {
          return true;
        }
        return false;
      })
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
      .End();

  AddOpCompat(OpCompat("relu"))
      .AddInput("X")
      .IsTensor()
      .End()
      .AddOutput("Out")
      .IsTensor()
      .End();

  AddOpCompat(OpCompat("fc"))
      .AddInput("Input")
      .IsTensor()
      .End()
      .AddInput("W")
      .IsTensor()
      .End()
      .AddInput("Bias")
      .IsTensor()
      .End()
      .AddOutput("Out")
      .IsTensor()
      .End()
      .AddAttr("in_num_col_dims")
      .IsNumGE(1)
      .End()
      .AddAttr("activation_type")
      .IsStringIn({"relu", ""})
      .End();
}

92
void FCFusePass::ApplyImpl(ir::Graph* graph) const {
93 94
  PADDLE_ENFORCE_NOT_NULL(
      graph, platform::errors::InvalidArgument("Graph cannot be nullptr."));
95
  FusePassBase::Init("fc_fuse", graph);
96

97 98 99 100
  int found_fc_count = 0;
  for (bool with_relu : {true, false}) {
    found_fc_count += ApplyFCPattern(graph, with_relu);
  }
101

102 103 104 105
  AddStatis(found_fc_count);
}

int FCFusePass::ApplyFCPattern(Graph* graph, bool with_relu) const {
106
  GraphPatternDetector gpd;
107 108 109 110
  auto* x = gpd.mutable_pattern()
                ->NewNode("fc_fuse/x")
                ->AsInput()
                ->assert_is_op_input("mul", "X");
Y
Yan Chunwei 已提交
111
  patterns::FC fc_pattern(gpd.mutable_pattern(), "fc_fuse");
112
  fc_pattern(x, true /*with bias*/, with_relu);
113

Y
Yan Chunwei 已提交
114
  int found_fc_count = 0;
115
  auto handler = [&](const GraphPatternDetector::subgraph_t& subgraph,
116
                     Graph* g) {
117 118 119 120
    if (subgraph.count(x) <= 0) {
      LOG(WARNING) << "The subgraph is empty.";
      return;
    }
121 122 123 124
    if (!IsCompat(subgraph, g)) {
      LOG(WARNING) << "Pass in op compat failed.";
      return;
    }
125

M
minqiyang 已提交
126
    VLOG(4) << "handle FC fuse";
Y
Yan Chunwei 已提交
127
    GET_IR_NODE_FROM_SUBGRAPH(w, w, fc_pattern);
128
    GET_IR_NODE_FROM_SUBGRAPH(bias, bias, fc_pattern);
129 130
    GET_IR_NODE_FROM_SUBGRAPH(
        elementwise_add_out, elementwise_add_out, fc_pattern);
Y
Yan Chunwei 已提交
131 132 133
    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);
J
Jason 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

    // Only support 2D-Tensor as weight for FC
    std::vector<int64_t> w_shape = w->Var()->GetShape();
    size_t w_rank = w_shape.size();
    if (w_rank != 2) return;

    // axis of elementwise_add should be -1 or x_num_col_dims
    auto x_num_col_dims =
        BOOST_GET_CONST(int, mul->Op()->GetAttr("x_num_col_dims"));
    auto axis = BOOST_GET_CONST(int, elementwise_add->Op()->GetAttr("axis"));
    if (axis != -1 && axis != x_num_col_dims) return;

    // Shape of bias should be [1, out_size] or [out_size]
    std::vector<int64_t> b_shape = bias->Var()->GetShape();
    if (b_shape.size() == 1) {
      if (b_shape[0] != w_shape[1]) {
        return;
      }
    } else if (b_shape.size() == 2) {
      if (b_shape[0] != 1 || b_shape[1] != w_shape[1]) {
        return;
      }
    } else {
      return;
    }

160 161 162 163 164 165 166 167
    Node* relu = nullptr;
    Node* relu_out = nullptr;
    if (with_relu) {
      GET_IR_NODE_FROM_SUBGRAPH(tmp_relu, relu, fc_pattern);
      GET_IR_NODE_FROM_SUBGRAPH(tmp_relu_out, relu_out, fc_pattern);
      relu = tmp_relu;
      relu_out = tmp_relu_out;
    }
168 169

    // Create an FC Node.
170
    OpDesc desc(mul->Op()->Block());
171 172 173 174 175 176 177 178 179 180 181 182 183
    desc.SetType("fc");

    // Set inputs of fc
    desc.SetInput("Input", {subgraph.at(x)->Name()});
    desc.SetInput("W", {w->Name()});
    desc.SetInput("Bias", {bias->Name()});

    // Set output of fc
    std::string fc_out_name =
        with_relu ? relu_out->Name() : elementwise_add_out->Name();
    desc.SetOutput("Out", std::vector<std::string>({fc_out_name}));

    // Set attrs of fc
T
Tao Luo 已提交
184
    desc.SetAttr("in_num_col_dims", mul->Op()->GetAttr("x_num_col_dims"));
185 186
    std::string activation_type = with_relu ? "relu" : "";
    desc.SetAttr("activation_type", activation_type);
187

188
    // This is to add padding for dimension 128 on concern of MKL performance
189 190 191
    bool use_gpu = Has("use_gpu") ? Get<bool>("use_gpu") : false;
    bool use_fc_padding =
        Has("use_fc_padding") ? Get<bool>("use_fc_padding") : true;
192 193 194 195
    const std::string& w_name = patterns::UniqueKey(w->Name());
    VarDesc w_key(w_name);
    w_key.SetPersistable(true);
    auto* w_node = g->CreateVarNode(&w_key);
196
    if (!use_gpu && use_fc_padding) {
197 198 199 200 201 202 203
      auto* scope = param_scope();
      auto* weight = scope->FindVar(w->Name())->GetMutable<LoDTensor>();
      auto* weight_data = weight->data<float>();
      auto weight_dims = weight->dims();
      int weight_num = product(weight_dims);
      int w_h = weight_dims[0];
      int w_w = weight_dims[1];
204
      if (w_h % 128 == 0 && w_w % 128 == 0) {
205 206 207
        auto* w_var = scope->Var(w_name);
        auto* w_tensor = w_var->GetMutable<framework::LoDTensor>();

208
        auto* weight_data_tmp = new float[weight_num];
209
        for (int i = 0; i < w_h; i++) {
210 211
          memcpy(weight_data_tmp + i * w_w,
                 weight_data + i * w_w,
212 213
                 w_w * sizeof(float));
        }
214
        w_tensor->Resize(DDim{weight_dims[0] + 4, weight_dims[1] + 4});
215
        auto* weight_data_new =
216
            w_tensor->mutable_data<float>(platform::CPUPlace());
217
        for (int i = 0; i < w_h; i++) {
218 219
          memcpy(weight_data_new + i * (w_w + 4),
                 weight_data_tmp + i * w_w,
220 221 222
                 w_w * sizeof(float));
        }
        delete[] weight_data_tmp;
223
        desc.SetInput("W", {w_name});
224
        desc.SetAttr("padding_weights", true);
225
        desc.Flush();
226 227 228
      }
    }

229 230
    // For anakin subgraph int8
    // When in anakin subgraph int8 mode, the pattern like "fake_quant + mul +
231
    // fake_dequant" can be detected by the quant_dequant_fuse_pass. This pass
232
    // will add "input_scale" which are extracted from
233 234 235 236
    // fake_quant op and fake_dequant op to mul op, and then delete the
    // fake_quant op and fake_dequant op in the graph. If the mul op has the
    // scale info, we should add those to the fused fc.
    auto* mul_op_desc = mul->Op();
237 238
    auto* elementwise_add_op_desc = elementwise_add->Op();

239 240
    if (mul_op_desc->HasAttr("enable_int8")) {
      desc.SetAttr("enable_int8", mul_op_desc->GetAttr("enable_int8"));
241 242
    }

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
    if (mul_op_desc->HasAttr("Input_scale")) {
      desc.SetAttr("Input_scale", mul_op_desc->GetAttr("Input_scale"));
    }

    bool inscale_flag = false;
    bool outscale_flag = false;

    if (mul_op_desc->HasAttr("X")) {
      desc.SetAttr("X", mul_op_desc->GetAttr("X"));
      inscale_flag = true;
    }
    if (elementwise_add_op_desc->HasAttr("Out")) {
      desc.SetAttr("Out", elementwise_add_op_desc->GetAttr("Out"));
      outscale_flag = true;
    }
    desc.SetAttr("support_int8", inscale_flag && outscale_flag);

260 261 262 263
    // if we can find out_threshold in elementwise_add, then set it as the
    // out_thrshold of fc
    auto out_threshold_attr =
        elementwise_add_op_desc->GetNullableAttr("out_threshold");
R
Ruibiao Chen 已提交
264
    if (out_threshold_attr.index()) {
265 266 267 268 269 270
      VLOG(4) << "setting out_threshold: "
              << BOOST_GET_CONST(float, out_threshold_attr);
      desc.SetAttr("out_threshold", out_threshold_attr);
    }
    desc.Flush();

271 272 273 274 275
    if (!IsCompat(desc)) {
      LOG(WARNING) << "Fc fuse pass in out fc op compat failed.";
      return;
    }

276
    auto fc_node = g->CreateOpNode(&desc);  // OpDesc will be copied.
277 278 279 280 281 282
    if (with_relu) {
      GraphSafeRemoveNodes(
          graph, {mul, elementwise_add, mul_out, elementwise_add_out, relu});
    } else {
      GraphSafeRemoveNodes(graph, {mul, elementwise_add, mul_out});
    }
283

Y
Yan Chunwei 已提交
284
    IR_NODE_LINK_TO(subgraph.at(x), fc_node);
285 286 287 288 289 290
    if (desc.GetAttrIfExists<bool>("padding_weights")) {
      IR_NODE_LINK_TO(w_node, fc_node);
    } else {
      GraphSafeRemoveNodes(g, {w_node});
      IR_NODE_LINK_TO(w, fc_node);
    }
291 292 293 294 295 296
    IR_NODE_LINK_TO(bias, fc_node);
    if (with_relu) {
      IR_NODE_LINK_TO(fc_node, relu_out);
    } else {
      IR_NODE_LINK_TO(fc_node, elementwise_add_out);
    }
Y
Yan Chunwei 已提交
297 298

    found_fc_count++;
299
  };
300
  gpd(graph, handler);
301
  return found_fc_count;
302 303 304 305 306 307
}

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

308 309
REGISTER_PASS(fc_fuse_pass, paddle::framework::ir::FCFusePass)
    .RequirePassAttr("use_gpu");
310 311 312 313
REGISTER_PASS_CAPABILITY(fc_fuse_pass)
    .AddCombination(
        paddle::framework::compatible::OpVersionComparatorCombination()
            .EQ("mul", 0)
314
            .LE("elementwise_add", 1)
315 316
            .EQ("relu", 0)
            .EQ("fc", 0));