|
| 1 | +// Copyright (c) 2020 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/mkldnn/fc_act_mkldnn_fuse_pass.h" |
| 16 | +#include "paddle/fluid/framework/ir/graph_pattern_detector.h" |
| 17 | +#include "paddle/fluid/framework/op_version_registry.h" |
| 18 | +#include "paddle/fluid/platform/enforce.h" |
| 19 | +#include "paddle/fluid/string/pretty_log.h" |
| 20 | + |
| 21 | +namespace paddle { |
| 22 | +namespace framework { |
| 23 | +namespace ir { |
| 24 | + |
| 25 | +using string::PrettyLogDetail; |
| 26 | + |
| 27 | +void FuseFCActOneDNNPass::ApplyImpl(Graph *graph) const { |
| 28 | + std::vector<std::string> act_types = {"gelu", "tanh", "sigmoid", |
| 29 | + "hard_swish"}; |
| 30 | + |
| 31 | + for (std::string act_type : act_types) FuseFCAct(graph, act_type); |
| 32 | +} |
| 33 | + |
| 34 | +void FuseFCActOneDNNPass::FuseFCAct(Graph *graph, |
| 35 | + const std::string &act_type) const { |
| 36 | + PADDLE_ENFORCE_NOT_NULL( |
| 37 | + graph, platform::errors::InvalidArgument("Graph cannot be nullptr.")); |
| 38 | + FusePassBase::Init("fc_act", graph); |
| 39 | + |
| 40 | + GraphPatternDetector gpd; |
| 41 | + patterns::FCActOneDNN fc_act_pattern(gpd.mutable_pattern(), "fc_act"); |
| 42 | + fc_act_pattern(act_type); |
| 43 | + |
| 44 | + int found_fc_act_count = 0; |
| 45 | + auto handler = [&](const GraphPatternDetector::subgraph_t &subgraph, |
| 46 | + Graph *g) { |
| 47 | + VLOG(4) << "Fuse fc with activation op."; |
| 48 | + // FC output |
| 49 | + GET_IR_NODE_FROM_SUBGRAPH(fc_out, fc_out, fc_act_pattern); |
| 50 | + // ACT output |
| 51 | + GET_IR_NODE_FROM_SUBGRAPH(act_out, act_out, fc_act_pattern); |
| 52 | + // ops |
| 53 | + GET_IR_NODE_FROM_SUBGRAPH(fc, fc, fc_act_pattern); |
| 54 | + GET_IR_NODE_FROM_SUBGRAPH(act, act, fc_act_pattern); |
| 55 | + |
| 56 | + auto *fc_op = fc->Op(); |
| 57 | + auto *act_op = act->Op(); |
| 58 | + |
| 59 | + if (fc_op->HasAttr("use_mkldnn")) { |
| 60 | + PADDLE_ENFORCE( |
| 61 | + BOOST_GET_CONST(bool, fc_op->GetAttr("use_mkldnn")), |
| 62 | + platform::errors::PreconditionNotMet( |
| 63 | + "The FC+Act fusion may happen only when oneDNN library " |
| 64 | + "is used.")); |
| 65 | + } |
| 66 | + |
| 67 | + if (act_type == "gelu" && act_op->HasAttr("approximate")) { |
| 68 | + bool approximate = BOOST_GET_CONST(bool, act_op->GetAttr("approximate")); |
| 69 | + std::string type = approximate ? "_tanh" : "_erf"; |
| 70 | + fc_op->SetAttr("activation_type", act_type + type); |
| 71 | + } else |
| 72 | + fc_op->SetAttr("activation_type", act_type); |
| 73 | + |
| 74 | + fc_op->SetAttr("use_mkldnn", true); |
| 75 | + |
| 76 | + fc_op->SetOutput("Out", {act_out->Name()}); |
| 77 | + |
| 78 | + IR_OP_VAR_LINK(fc, act_out); |
| 79 | + GraphSafeRemoveNodes(g, {act, fc_out}); |
| 80 | + found_fc_act_count++; |
| 81 | + }; |
| 82 | + |
| 83 | + gpd(graph, handler); |
| 84 | + AddStatis(found_fc_act_count); |
| 85 | + PrettyLogDetail("--- fused %d fc with %s activation", found_fc_act_count, |
| 86 | + act_type); |
| 87 | +} |
| 88 | + |
| 89 | +} // namespace ir |
| 90 | +} // namespace framework |
| 91 | +} // namespace paddle |
| 92 | + |
| 93 | +REGISTER_PASS(fc_act_mkldnn_fuse_pass, |
| 94 | + paddle::framework::ir::FuseFCActOneDNNPass); |
| 95 | +REGISTER_PASS_CAPABILITY(fc_act_mkldnn_fuse_pass) |
| 96 | + .AddCombination( |
| 97 | + paddle::framework::compatible::OpVersionComparatorCombination() |
| 98 | + .LE("fc", 0) |
| 99 | + .LE("gelu", 0) |
| 100 | + .LE("sigmoid", 0) |
| 101 | + .LE("hard_swish", 0) |
| 102 | + .LE("tanh", 0)); |
0 commit comments