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_TRANSPOSE_STRATEGY_H_ 18 #define MINDSPORE_LITE_TOOLS_OPTIMIZER_GRAPH_TRANSPOSE_STRATEGY_H_ 19 20 #include <vector> 21 #include <memory> 22 #include <string> 23 #include "schema/inner/model_generated.h" 24 #include "tools/converter/converter_flags.h" 25 #include "tools/optimizer/common/format_utils.h" 26 #include "tools/optimizer/graph/node_infershape.h" 27 28 using mindspore::converter::FmkType; 29 namespace mindspore { 30 namespace opt { 31 class TransposeStrategy { 32 public: 33 TransposeStrategy() = default; 34 ~TransposeStrategy() = default; Init(FmkType fmk_type,bool train_flag)35 void Init(FmkType fmk_type, bool train_flag) { 36 fmk_type_ = fmk_type; 37 train_flag_ = train_flag; 38 node_infer_shape_.Init(fmk_type, train_flag); 39 } 40 AnfNodePtr TransposePairFuseWhenInsert(const FuncGraphPtr &func_graph, const CNodePtr &code, 41 const std::vector<int> &perm, bool before, size_t index); 42 bool CanFusionIfInsert(const FuncGraphPtr &func_graph, const CNodePtr &cnode, TransTypePair *trans_info, 43 TransTypePair *trans_insert_info); 44 STATUS ChangeOpAxis(const FuncGraphPtr &func_graph, const CNodePtr &cnode, FormatTransNodeType trans_type); 45 bool CanChangeOpAxis(const CNodePtr &cnode); 46 47 private: 48 AnfNodePtr TransposeDependOnShape(const FuncGraphPtr &func_graph, const CNodePtr &cnode, const std::vector<int> &perm, 49 bool before, size_t index); 50 STATUS TransposeInsertDependOnShape(const FuncGraphPtr &func_graph, const CNodePtr &cnode, bool before, size_t index); 51 bool IsInOutCanFuison(const std::vector<AnfNodePtr> &nodes, size_t *trans_count, FormatTransNodeType *trans_type); 52 void DecidePreAndPostTransType(TransTypePair *trans_info, TransTypePair *trans_insert_info) const; 53 FmkType fmk_type_{converter::kFmkTypeMs}; 54 bool train_flag_{false}; 55 NodeInferShape node_infer_shape_; 56 }; 57 } // namespace opt 58 } // namespace mindspore 59 60 #endif // MINDSPORE_LITE_TOOLS_OPTIMIZER_GRAPH_TRANSPOSE_STRATEGY_H_ 61