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_UNIFY_FORMAT_PASS_H_ 18 #define MINDSPORE_LITE_TOOLS_OPTIMIZER_GRAPH_UNIFY_FORMAT_PASS_H_ 19 20 #include <vector> 21 #include <memory> 22 #include <set> 23 #include <string> 24 #include <unordered_map> 25 #include "backend/optimizer/common/optimizer.h" 26 #include "utils/utils.h" 27 #include "tools/converter/converter_flags.h" 28 #include "tools/optimizer/common/format_utils.h" 29 #include "tools/optimizer/format/delete_redundant_transpose.h" 30 #include "tools/optimizer/graph/transpose_strategy.h" 31 32 using mindspore::converter::FmkType; 33 namespace mindspore { 34 namespace opt { 35 class DecreaseTransposeAlgo : public Pass { 36 public: 37 explicit DecreaseTransposeAlgo(FmkType fmk_type = FmkType::kFmkTypeMs, bool train_flag = false) 38 : Pass("DecreaseTransposeAlgo"), fmk_type_(fmk_type), train_flag_(train_flag) {} 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 InsertPreTransNode(const FuncGraphPtr &func_graph, const CNodePtr &cnode, const std::vector<int> &perm); 45 STATUS GenNewInput(const FuncGraphPtr &func_graph, const CNodePtr &cnode, const std::vector<int> perm, bool before, 46 size_t index = 0); 47 bool RunDoFixFormat(const FuncGraphPtr &func_graph, const CNodePtr &cnode); 48 bool DoFixFormat(const FuncGraphPtr &func_graph); 49 bool DecreaseTransposeForSingleOp(const FuncGraphPtr &func_graph); 50 bool DecreaseTransposeForMultiOp(const FuncGraphPtr &func_graph); 51 STATUS PostTransposeFusion(const FuncGraphPtr &func_graph, const CNodePtr &cnode); 52 53 STATUS HandleGraphMultiNode(const FuncGraphPtr &func_graph, const CNodePtr &cnode, 54 std::set<CNodePtr> *visit_transposes); 55 STATUS InsertPreTransNode(const FuncGraphPtr &func_graph, const CNodePtr &cnode, TransTypePair *trans_insert_info); 56 int SetSubGraphInput(const CNodePtr &cnode, const FuncGraphPtr &sub_graph); 57 int ResetSubGraphInput(); 58 int SetSubGraphOutput(const FuncGraphPtr &sub_graph); 59 int SetSubGraphAbstract(const CNodePtr &cnode, const FuncGraphPtr &sub_graph); 60 int ModifyCNodeFormat(const CNodePtr &cnode, FormatTransNodeType pre_trans_type); 61 FmkType fmk_type_{converter::kFmkTypeMs}; 62 bool train_flag_{false}; 63 NodeInferShape node_infer_shape_; 64 TransposeStrategy transpose_strategy_; 65 DeleteRedundantTranspose delete_redundant_transpose_; 66 std::unordered_map<FuncGraphPtr, std::vector<AnfNodePtr>> sub_inputs_map_; 67 }; 68 } // namespace opt 69 } // namespace mindspore 70 71 #endif // MINDSPORE_LITE_TOOLS_OPTIMIZER_GRAPH_UNIFY_FORMAT_PASS_H_ 72