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