1 /** 2 * Copyright 2022 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef MINDSPORE_LITE_TOOLS_OPTIMIZER_FUSION_MUL_REDUCE_FUSION_H 18 #define MINDSPORE_LITE_TOOLS_OPTIMIZER_FUSION_MUL_REDUCE_FUSION_H 19 20 #include <map> 21 #include <string> 22 #include <utility> 23 #include "include/backend/optimizer/pass.h" 24 #include "tools/optimizer/graph/preprocess_dynamic_shape.h" 25 26 namespace mindspore { 27 namespace opt { 28 class MulReduceFusion : public Pass { 29 public: Pass(name)30 explicit MulReduceFusion(const std::string &name = "MulReduceFusion") : Pass(name) {} 31 ~MulReduceFusion() override = default; 32 bool Run(const FuncGraphPtr &func_graph) override; 33 34 private: 35 int ProcessOp(const FuncGraphPtr &func_graph, const CNodePtr &cnode); 36 int PostProcess(const FuncGraphPtr &func_graph); 37 int PostProcessSqueezeWithConcat(const FuncGraphPtr &func_graph, const CNodePtr &cnode); 38 int GenerateMatmul(const FuncGraphPtr &func_graph, const CNodePtr &cnode); 39 int GenerateSqueeze(const FuncGraphPtr &func_graph, const CNodePtr &cnode); 40 int GenerateMul(const FuncGraphPtr &func_graph, const CNodePtr &cnode); 41 int ProcessGather(); 42 bool CheckBasicCond(const FuncGraphPtr &func_graph, const CNodePtr &cnode); 43 bool CheckAxisCond(const CNodePtr &cnode); 44 bool CheckShapeCond(const CNodePtr &cnode); 45 bool CheckGatherOp(const FuncGraphPtr &func_graph, const CNodePtr &cnode); 46 bool CheckConcatOp(const FuncGraphPtr &func_graph, const CNodePtr &cnode); 47 bool exchange_{false}; // determine if exchange the two inputs of mul. 48 bool transpose_a_{false}; // determine matmul a-matrix's attr. 49 bool transpose_b_{false}; // determine matmul b-matrix's attr. 50 bool keep_dim_{false}; // record the keep-dim attr of reduce. 51 int axis_{0}; // record the axis of reduce. 52 int reduce_mode_{0}; // record the reduce_mode of reduce. 53 float coeff_{1.0f}; // valid when reduce_mode_ is reduce_mean, we can break it down into reduce_sum * coeff_. 54 int concat_axis_{0}; // record the new axis for concat-op in PostProcess. 55 CNodePtr gather_{nullptr}; // gather's first input, valid when reduce_mode_ is reduce_mean. 56 DynamicShapePreprocessor preprocessor_; 57 std::map<CNodePtr, std::pair<int, int>> 58 squeeze_infos_; // record generated-squeeze(<op, <axis, out-dims>>) which is used to post-fusion. 59 }; 60 } // namespace opt 61 } // namespace mindspore 62 #endif // MINDSPORE_LITE_TOOLS_OPTIMIZER_FUSION_MUL_REDUCE_FUSION_H 63