• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PARALLEL_SPLITER_H
18 #define MINDSPORE_LITE_TOOLS_OPTIMIZER_PARALLEL_SPLITER_H
19 #include <vector>
20 #include <string>
21 #include <set>
22 #include <unordered_map>
23 #include "schema/inner/model_generated.h"
24 #include "utils/utils.h"
25 #include "tools/optimizer/common/gllo_utils.h"
26 #include "tools/converter/converter_flags.h"
27 #include "include/context.h"
28 #include "include/lite_types.h"
29 namespace mindspore {
30 namespace opt {
31 struct IntCompare {
operatorIntCompare32   bool operator()(const int &lhs, const int &rhs) const { return lhs > rhs; }
33 };
34 
35 class Spliter {
36  public:
37   static Spliter *GetInstance();
38   Spliter(const Spliter &) = delete;
39   Spliter &operator=(const Spliter &) = delete;
40 
41   // record the global numbers of matched multi_conv nodes
42   void RecordGraphInfo(const FuncGraphPtr &func_graph);
43 
44   // update current input node's output. if candidate node has been recorded, we will be ignore it, otherwise record it.
45   void UpdateNodeOutputs(const std::string &input_node_name, const AnfNodePtr &candidate_output);
46 
47   // update current node's input shapes.
48   void UpdateNodeInputShapes(const std::string &node_name, const std::vector<ShapeVector> &input_shapes);
49 
50   // update current node's output shapes.
51   void UpdateNodeOutputShapes(const std::string &node_name, const std::vector<ShapeVector> &output_shapes);
52 
graph_node_outputs()53   std::unordered_map<std::string, std::vector<AnfNodePtr>> graph_node_outputs() const { return graph_node_outputs_; }
54 
graph_node_output_shapes()55   std::unordered_map<std::string, std::vector<ShapeVector>> graph_node_output_shapes() const {
56     return graph_node_output_shapes_;
57   }
58 
graph_node_input_shapes()59   std::unordered_map<std::string, std::vector<ShapeVector>> graph_node_input_shapes() const {
60     return graph_node_input_shapes_;
61   }
62 
graph_match_multi_numbers()63   std::set<int, IntCompare> graph_match_multi_numbers() const { return match_numbers_; }
64 
nodes_inputs()65   std::unordered_map<AnfNodePtr, std::set<AnfNodePtr>> nodes_inputs() const { return nodes_inputs_; }
66 
nodes_outputs()67   std::unordered_map<AnfNodePtr, std::set<AnfNodePtr>> nodes_outputs() const { return nodes_outputs_; }
68 
69   void VisitNodesInputs(const FuncGraphPtr &func_graph);
70 
71   void VisitNodesOutputs(const FuncGraphPtr &func_graph);
72 
73  private:
74   Spliter() = default;
75   virtual ~Spliter() = default;
76 
77  private:
78   std::unordered_map<std::string, std::vector<AnfNodePtr>> graph_node_outputs_;
79   std::unordered_map<std::string, std::vector<ShapeVector>> graph_node_output_shapes_;
80   std::unordered_map<std::string, std::vector<ShapeVector>> graph_node_input_shapes_;
81   std::unordered_map<AnfNodePtr, std::set<AnfNodePtr>> nodes_inputs_;
82   std::unordered_map<AnfNodePtr, std::set<AnfNodePtr>> nodes_outputs_;
83   std::unordered_map<AnfNodePtr, bool> match_visited_;
84   std::set<int, IntCompare> match_numbers_;
85 };
86 }  // namespace opt
87 }  // namespace mindspore
88 #endif  // MINDSPORE_LITE_TOOLS_OPTIMIZER_PARALLEL_SPLITER_H
89