1 /** 2 * Copyright 2021 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_GRAPH_DECREASE_TRANSPOSE_ALGO_H_ 18 #define MINDSPORE_LITE_TOOLS_OPTIMIZER_GRAPH_DECREASE_TRANSPOSE_ALGO_H_ 19 20 #include <vector> 21 #include <memory> 22 #include <set> 23 #include <string> 24 #include <unordered_map> 25 #include "include/backend/optimizer/optimizer.h" 26 #include "include/common/utils/utils.h" 27 #include "tools/optimizer/common/format_utils.h" 28 #include "tools/optimizer/format/delete_redundant_transpose.h" 29 #include "tools/optimizer/graph/transpose_strategy.h" 30 31 using mindspore::converter::FmkType; 32 namespace mindspore { 33 namespace opt { 34 class DecreaseTransposeAlgo : public Pass { 35 public: 36 explicit DecreaseTransposeAlgo(FmkType fmk_type = FmkType::kFmkTypeMs, bool train_flag = false, 37 bool surrounded_all_trans = true) 38 : Pass("DecreaseTransposeAlgo"), fmk_type_(fmk_type), train_flag_(train_flag), all_trans_(surrounded_all_trans) {} 39 ~DecreaseTransposeAlgo() override = default; 40 bool Run(const FuncGraphPtr &func_graph) override; 41 42 private: 43 STATUS InsertPostTransNode(const FuncGraphPtr &func_graph, const CNodePtr &cnode, const std::vector<int> &perm); 44 STATUS GenNewInput(const FuncGraphPtr &func_graph, const CNodePtr &cnode, const std::vector<int> perm, bool before, 45 size_t index = 0); 46 bool DecreaseTransposeForSingleOp(const FuncGraphPtr &func_graph); 47 bool DecreaseTransposeForMultiOp(const FuncGraphPtr &func_graph); 48 STATUS PostTransposeFusion(const FuncGraphPtr &func_graph, const CNodePtr &cnode); 49 50 STATUS HandleGraphSingleNode(const FuncGraphPtr &func_graph, const TransTypePair &trans_info, const CNodePtr &cnode); 51 STATUS HandleGraphMultiNode(const FuncGraphPtr &func_graph, const CNodePtr &cnode, 52 std::set<CNodePtr> *visit_transposes); 53 STATUS InsertPreTransNode(const FuncGraphPtr &func_graph, const CNodePtr &cnode, TransTypePair *trans_insert_info); 54 STATUS DoPreInsert(const FuncGraphPtr &func_graph, const CNodePtr &cnode, FormatTransNodeType trans_type); 55 STATUS InsertPreTransForNonTransInOut(const FuncGraphPtr &func_graph, const AnfNodeIndexSet ¬_trans_in_nodes, 56 const AnfNodeIndexSet ¬_trans_out_nodes, TransTypePair trans_info); 57 int SetSubGraphInput(const CNodePtr &cnode, const FuncGraphPtr &sub_graph); 58 int ResetSubGraphInput(); 59 int SetSubGraphOutput(const FuncGraphPtr &sub_graph); 60 int ModifyCNodeFormat(const CNodePtr &cnode, FormatTransNodeType pre_trans_type); 61 bool IsNeedGenNewInput(const FuncGraphPtr &func_graph, const CNodePtr &cnode, size_t index); 62 FmkType fmk_type_{converter::kFmkTypeMs}; 63 bool train_flag_{false}; 64 bool all_trans_{false}; 65 NodeInferShape node_infer_shape_; 66 TransposeStrategy transpose_strategy_; 67 DeleteRedundantTranspose delete_redundant_transpose_; 68 std::unordered_map<FuncGraphPtr, std::vector<AnfNodePtr>> sub_inputs_map_; 69 }; 70 } // namespace opt 71 } // namespace mindspore 72 73 #endif // MINDSPORE_LITE_TOOLS_OPTIMIZER_GRAPH_DECREASE_TRANSPOSE_ALGO_H_ 74