|
| 1 | +// Copyright (c) 2019 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 <memory> |
| 16 | +#include <string> |
| 17 | +#include "lite/backends/xpu/math.h" |
| 18 | +#include "lite/core/optimizer/mir/pass_registry.h" |
| 19 | +#include "lite/core/optimizer/mir/pattern_matcher_high_api.h" |
| 20 | + |
| 21 | +namespace paddle { |
| 22 | +namespace lite { |
| 23 | +namespace mir { |
| 24 | +namespace fusion { |
| 25 | + |
| 26 | +class XPUQuickGELUFuser : public FuseBase { |
| 27 | + public: |
| 28 | + explicit XPUQuickGELUFuser(const std::string& op_type, |
| 29 | + const std::string& act_type) { |
| 30 | + op_type_ = op_type; |
| 31 | + act_type_ = act_type; |
| 32 | + } |
| 33 | + |
| 34 | + void BuildPattern() override { |
| 35 | + auto scale_teller = [](const Node* node) -> bool { |
| 36 | + bool bias_after_scale = |
| 37 | + const_cast<Node*>(node)->AsStmt().op_info()->GetAttr<bool>( |
| 38 | + "bias_after_scale"); |
| 39 | + bool has_act = const_cast<Node*>(node)->AsStmt().op_info()->HasAttr( |
| 40 | + "activation_type"); |
| 41 | + return bias_after_scale && (!has_act); |
| 42 | + }; |
| 43 | + |
| 44 | + /* _____________________ |
| 45 | + / \ |
| 46 | + Create node: X----scale----sigmoid---elementwise_mul---output |
| 47 | + */ |
| 48 | + auto* x = VarNode("x")->assert_is_op_input("scale", "X"); |
| 49 | + auto* scale = OpNode("scale", "scale") |
| 50 | + ->assert_is_op("scale") |
| 51 | + ->assert_node_satisfied(scale_teller); |
| 52 | + auto* scale_out = VarNode("scale_out"); |
| 53 | + auto* sigmoid = OpNode("sigmoid", act_type_); |
| 54 | + auto* sigmoid_out = VarNode("sigmoid_out"); |
| 55 | + auto* element_mul = |
| 56 | + OpNode("elementwise_mul", op_type_) |
| 57 | + ->assert_op_attr_satisfied<int>( |
| 58 | + "axis", [](int attr) { return attr == -1 || attr == 0; }); |
| 59 | + auto* output = VarNode("Out"); |
| 60 | + |
| 61 | + // Construct the topological structure for scale-sigmoid-elementwise_mul |
| 62 | + *x >> *scale >> *scale_out >> *sigmoid >> *sigmoid_out; |
| 63 | + std::vector<PMNode*> element_mul_inputs{x, sigmoid_out}; |
| 64 | + element_mul_inputs >> *element_mul >> *output; |
| 65 | + |
| 66 | + // Some op specialities. |
| 67 | + scale->AsIntermediate(); |
| 68 | + scale_out->AsIntermediate(); |
| 69 | + sigmoid->AsIntermediate(); |
| 70 | + sigmoid_out->AsIntermediate(); |
| 71 | + element_mul->AsIntermediate(); |
| 72 | + } |
| 73 | + |
| 74 | + cpp::OpDesc GenOpDesc(const key2nodes_t& matched) { |
| 75 | + auto op_desc = *matched.at("scale")->stmt()->op_info(); |
| 76 | + float scale_val = op_desc.GetAttr<float>("scale"); |
| 77 | + op_desc.mutable_inputs()->clear(); |
| 78 | + op_desc.mutable_outputs()->clear(); |
| 79 | + op_desc.SetType("__xpu__quick_gelu"); |
| 80 | + op_desc.SetInput("X", {matched.at("x")->arg()->name}); |
| 81 | + op_desc.SetOutput("Out", {matched.at("Out")->arg()->name}); |
| 82 | + op_desc.SetAttr("scale", scale_val); |
| 83 | + return op_desc; |
| 84 | + } |
| 85 | + |
| 86 | + void InsertNewNode(SSAGraph* graph, const key2nodes_t& matched) override { |
| 87 | + // get op_desc for gelu op. |
| 88 | + auto op_desc = GenOpDesc(matched); |
| 89 | + // Create gelu op. |
| 90 | + auto gelu_op = LiteOpRegistry::Global().Create("__xpu__quick_gelu"); |
| 91 | + |
| 92 | + // find scope and valid_places of original scale op. |
| 93 | + auto scale = matched.at("scale")->stmt()->op(); |
| 94 | + auto* scope = scale->scope(); |
| 95 | + auto& valid_places = scale->valid_places(); |
| 96 | + |
| 97 | + // set gelu op's scope and valid_places which aligned with scale op. |
| 98 | + gelu_op->Attach(op_desc, scope); |
| 99 | + auto* new_op_node = graph->GraphCreateInstructNode(gelu_op, valid_places); |
| 100 | + |
| 101 | + // link IO to the new op node. |
| 102 | + IR_NODE_LINK_TO(matched.at("x"), new_op_node); |
| 103 | + IR_NODE_LINK_TO(new_op_node, matched.at("Out")); |
| 104 | + } |
| 105 | + |
| 106 | + private: |
| 107 | + std::string op_type_; |
| 108 | + std::string act_type_; |
| 109 | +}; |
| 110 | + |
| 111 | +} // namespace fusion |
| 112 | + |
| 113 | +class XPUQuickGELUFusePass : public ProgramPass { |
| 114 | + public: |
| 115 | + void Apply(const std::unique_ptr<SSAGraph>& graph) override { |
| 116 | + fusion::XPUQuickGELUFuser fuser("elementwise_mul", "sigmoid"); |
| 117 | + fuser(graph.get()); |
| 118 | + } |
| 119 | +}; |
| 120 | + |
| 121 | +} // namespace mir |
| 122 | +} // namespace lite |
| 123 | +} // namespace paddle |
| 124 | + |
| 125 | +REGISTER_MIR_PASS(__xpu__quick_gelu_fuse_pass, |
| 126 | + paddle::lite::mir::XPUQuickGELUFusePass) |
| 127 | + .BindTargets({TARGET(kXPU)}) |
| 128 | + .BindKernel("__xpu__quick_gelu"); |
0 commit comments