• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020 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 PARALLEL_AUTO_PARALLEL_REC_COST_H_
18 #define PARALLEL_AUTO_PARALLEL_REC_COST_H_
19 
20 #include <iostream>
21 #include <memory>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
26 #include "frontend/parallel/auto_parallel/rec_core/rec_graph.h"
27 #include "frontend/parallel/auto_parallel/rec_core/rec_strategy.h"
28 
29 namespace mindspore {
30 namespace parallel {
31 #define DOUBLE_MAX (std::numeric_limits<double>::max)()
32 
33 double CostRedis(const Graph::NodeType &node,
34                  const std::vector<std::pair<std::string, StrategyRec>> &node_name_to_strategy,
35                  const std::vector<std::vector<float>> &mode, const Graph &graph);
36 
37 double CostRedisWithAdjacentNode(const std::vector<std::pair<std::string, StrategyRec>> &node_name_to_strategy,
38                                  const std::vector<std::vector<float>> &mode, size_t i_strategy, size_t i_node,
39                                  double tensor_size, bool is_search_forward);
40 
41 // class CostMatMul is used to compute the cost of MatMul operator.
42 class CostMatMul {
43  public:
44   StrategyRec GetOptimalStr(const Graph::NodeType &node,
45                             const std::vector<std::pair<std::string, StrategyRec>> &node_name_to_strategy,
46                             const Graph &graph);
47 
48   double GetMinCostIn(const OperatorRec &op);
49 
50  private:
StrConcatDimI(int64_t a,int64_t b)51   double StrConcatDimI(int64_t a, int64_t b) {
52     cost_in_i_ = (static_cast<double>(a) * static_cast<double>(b)) / 2.0;
53 
54     return cost_in_i_;
55   }
56 
StrConcatDimJ(int64_t a,int64_t b)57   double StrConcatDimJ(int64_t a, int64_t b) {
58     cost_in_j_ = (static_cast<double>(a) * static_cast<double>(b)) / 2.0;
59 
60     return cost_in_j_;
61   }
62 
StrReduceDimK(int64_t a,int64_t b)63   double StrReduceDimK(int64_t a, int64_t b) {
64     cost_in_k_ = (static_cast<double>(a) * static_cast<double>(b)) / 2.0;
65 
66     return cost_in_k_;
67   }
68 
69   StrategyRec ChoseStr(const std::vector<double> &cost_op, StrategyRec str);
70 
71   double cost_in_i_ = 0;
72 
73   double cost_in_j_ = 0;
74 
75   double cost_in_k_ = 0;
76 };  // class CostMatMul is used to compute the cost of MatMul operator.
77 
78 // class CostConvolution is used to compute the cost of Conv operator.
79 class CostConvolution {
80  public:
81   StrategyRec GetOptimalStr(const Graph::NodeType &node,
82                             const std::vector<std::pair<std::string, StrategyRec>> &node_name_to_strategy,
83                             const Graph &graph, bool channel_partition);
84 
85   double GetMinCostIn(const Graph::NodeType &node);
86 
87  private:
StrDimB(int64_t TensorFilter)88   double StrDimB(int64_t TensorFilter) {
89     cost_in_b_ = static_cast<double>((TensorFilter) / 2.0);
90 
91     return cost_in_b_;
92   }
93 
StrDimI(int64_t TensorIn,int64_t TensorFilter)94   double StrDimI(int64_t TensorIn, int64_t TensorFilter) {
95     cost_in_i_ = static_cast<double>((TensorIn + TensorFilter) / 2.0);
96 
97     return cost_in_i_;
98   }
99 
StrDimJ(int64_t TensorIn,int64_t TensorFilter)100   double StrDimJ(int64_t TensorIn, int64_t TensorFilter) {
101     cost_in_j_ = static_cast<double>((TensorIn + TensorFilter) / 2.0);
102 
103     return cost_in_j_;
104   }
105 
StrDimK(int64_t TensorIn)106   double StrDimK(int64_t TensorIn) {
107     cost_in_k_ = static_cast<double>((TensorIn) / 2.0);
108 
109     return cost_in_k_;
110   }
111 
StrDimDI(int64_t TensorIn,int64_t TensorOut)112   double StrDimDI(int64_t TensorIn, int64_t TensorOut) {
113     cost_in_di_ = static_cast<double>((TensorIn + TensorOut) / 2.0);
114 
115     return cost_in_di_;
116   }
117 
StrDimDJ(int64_t TensorIn,int64_t TensorOut)118   double StrDimDJ(int64_t TensorIn, int64_t TensorOut) {
119     cost_in_dj_ = static_cast<double>((TensorIn + TensorOut) / 2.0);
120 
121     return cost_in_dj_;
122   }
123 
StrDimQ(int64_t TensorOut)124   double StrDimQ(int64_t TensorOut) {
125     cost_in_q_ = static_cast<double>((TensorOut) / 2.0);
126 
127     return cost_in_q_;
128   }
129 
130   StrategyRec ChoseStr(const std::vector<double> &cost_op, StrategyRec str);
131 
132   double cost_in_b_ = 0;
133 
134   double cost_in_i_ = 0;
135 
136   double cost_in_j_ = 0;
137 
138   double cost_in_k_ = 0;
139 
140   double cost_in_di_ = 0;
141 
142   double cost_in_dj_ = 0;
143 
144   double cost_in_q_ = 0;
145 };  // class CostConvolution is used to compute the cost of Conv operator.
146 
147 // class CostPooling is used to compute the cost of Pooling operator.
148 class CostPooling {
149  public:
150   StrategyRec GetOptimalStr(const Graph::NodeType &node,
151                             const std::vector<std::pair<std::string, StrategyRec>> &node_name_to_strategy,
152                             const Graph &graph);
153 
GetMinCostIn()154   double GetMinCostIn() const { return cost_in_; }
155 
156  private:
157   StrategyRec ChoseStr(const std::vector<double> &cost_op, StrategyRec str);
158 
159   double cost_in_ = 0;
160 };  // class CostPooling is used to compute the cost of Pooling operator.
161 
162 // class CostReshape is used to compute the cost of Reshape operator.
163 class CostReshape {
164  public:
165   StrategyRec GetOptimalStr(const Graph::NodeType &node) const;
166 
GetMinCostIn()167   double GetMinCostIn() const { return cost_in_; }
168 
169  private:
170   StrategyRec ChoseStr(StrategyRec str) const;
171 
172   double cost_in_ = 0;
173 };  // class CostReshape is used to compute the cost of Reshape operator.
174 
175 // class CostCommon is used to compute the cost of an element-wise operator
176 class CostCommon {
177  public:
178   virtual StrategyRec GetOptimalStr(const Graph::NodeType &node,
179                                     const std::vector<std::pair<std::string, StrategyRec>> &node_name_to_strategy,
180                                     const Graph &graph);
181 
GetMinCostIn()182   virtual double GetMinCostIn() const { return cost_in_; }
183 
184  protected:
185   virtual StrategyRec ChoseStr(const std::vector<double> &cost_op, StrategyRec str);
186 
187   double cost_in_ = 0;
188 };  // class CostCommon is used to compute the cost of an element-wise operator
189 
190 // class CostBiasAdd is used to compute the cost of the addition between a tensor and a bias
191 class CostBiasAdd : public CostCommon {
192   StrategyRec ChoseStr(const std::vector<double> &cost_op, StrategyRec str);
193 };
194 // class CostAdd is used to compute the cost of Add operator.
195 class CostTensorAdd : public CostCommon {
196   StrategyRec ChoseStr(const std::vector<double> &cost_op, StrategyRec str);
197 };
198 
199 // all the following operation are element-wise and have the same cost
200 class CostReLU : public CostCommon {};
201 class CostLog : public CostCommon {};
202 class CostExp : public CostCommon {};
203 class CostAdd : public CostCommon {};
204 class CostSub : public CostCommon {};
205 class CostMul : public CostCommon {};
206 class CostDiv : public CostCommon {};
207 class CostSqueeze : public CostCommon {};
208 class CostCast : public CostCommon {};
209 
210 // class BatchParallel is used to compute the cost of BatchParallel operator.
211 class CostBatchParallel {
212  public:
213   virtual StrategyRec GetOptimalStr(const Graph::NodeType &node);
214 
GetMaxCostIn()215   virtual double GetMaxCostIn() const { return DOUBLE_MAX; }
216 
217  protected:
218   virtual StrategyRec ChoseStr(const std::vector<double> &cost_op, StrategyRec str);
219 
220   double cost_in_ = 0;
221 };  // class BatchParallel is used to compute the cost of BatchParallel operator.
222 
223 class CostBatchNorm : public CostBatchParallel {};
224 class CostOneHot : public CostBatchParallel {};
225 class CostPRelu : public CostBatchParallel {};
226 class CostSoftmax : public CostBatchParallel {};
227 
228 class CostSoftmaxCrossEntropyWithLogits : public CostBatchParallel {
229   StrategyRec ChoseStr(const std::vector<double> &cost_op, StrategyRec str);
230 };
231 }  // namespace parallel
232 }  // namespace mindspore
233 #endif  // PARALLEL_AUTO_PARALLEL_REC_COST_H_
234