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