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