• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2019-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 MINDSPORE_CCSRC_TRANSFORM_GRAPH_IR_UTIL_H_
18 #define MINDSPORE_CCSRC_TRANSFORM_GRAPH_IR_UTIL_H_
19 
20 #include <string>
21 #include <vector>
22 #include <algorithm>
23 #include <memory>
24 #include "securec/include/securec.h"
25 #include "ir/anf.h"
26 #include "ir/dtype.h"
27 #include "ir/tensor.h"
28 #include "include/transform/graph_ir/types.h"
29 #include "utils/shape_utils.h"
30 
31 namespace mindspore {
32 namespace transform {
33 class TransformUtil {
34  public:
35   /*
36    * Parameters:
37    *     type: [MeDataType] the data type for ME tensor
38    * Return:
39    *     [GeDataType] the data type for ge tensor
40    * */
41   static std::vector<int64_t> ConvertIntToList(int64_t data, int size);
42 
43   /*
44    * Parameters:
45    *     type: [MeDataType] the data type for ME tensor
46    * Return:
47    *     [GeDataType] the data type for ge tensor
48    * */
49   static GeDataType ConvertDataType(const MeDataType &type);
50 
51   /*
52    * Parameters:
53    *     type: [string] the data format in ME op
54    * Return:
55    *     [GeFormat] the data format for ge tensor
56    * */
57   static GeFormat ConvertFormat(const std::string &format, size_t shape_size);
58 
59   /*
60    * Parameters:
61    *     tensor: [MeTensorPtr] the me tensor to get description from
62    *     format: [string] the data format in ME
63    *     is_input: [bool] whether the tensor is used as input, default:false
64    * Return:
65    *     [shared_ptr<GeTensorDesc>] the shared pointer of ge tensor description
66    * */
67   static std::shared_ptr<GeTensorDesc> GetGeTensorDesc(const ShapeVector &ori_shape, const MeDataType &me_type,
68                                                        const std::string &ori_format, const ShapeVector &dev_shape = {},
69                                                        const std::string &dev_format = {});
70 
71   /*
72    * Parameters:
73    *     tensor: [MeTensor] the data tensor in ME
74    *     format: [string] the data format in ME op
75    *     copy: [bool] whether copy tensor data, default:true
76    * Return:
77    *     [GeTensor] the data tensor in GE
78    * */
79   static GeTensorPtr ConvertTensor(const MeTensorPtr &tensor, const std::string &format, bool copy = true);
80 
81   /*
82    * Parameters:
83    *     me_tensors: [vector<MeTensorPtr>] the data tensors in ME
84    *     format: [string] the data format in ME op
85    * Return:
86    *     [std::vector<GeTensorPtr>] the data tensors in GE
87    * */
88   static std::vector<GeTensorPtr> ConvertInputTensors(const std::vector<MeTensorPtr> &me_tensors,
89                                                       const std::string &format);
90 
91   /*
92    * Parameters:
93    *     Value: [Scalar] the scalar value
94    * Return:
95    *     [GeTensorPtr] the data tensors in GE
96    * */
97   static GeTensorPtr ConvertScalar(const ValuePtr &val);
98   /*
99    * Parameters:
100    *     tensor: [GeTensor] the data tensor in GE
101    * Return:
102    *     [MeTensor] the data tensor in ME
103    * */
104   static MeTensorPtr ConvertGeTensor(const GeTensorPtr &tensor);
105 
106   /*
107    * Parameters:
108    *     tensor: [GeTensor] the data tensor in GE
109    *     me_type: [TypeId] the type of created Me tensor
110    * Return:
111    *     [MeTensor] the data tensor in ME
112    * */
113   static MeTensorPtr ConvertGeTensor(const GeTensorPtr &tensor, const TypeId &me_type);
114 
115   /*
116    * Parameters:
117    *     tensor: [GeTensor] the data tensor in GE
118    *     request_dims [ShapeVector] the output Me tensors must adjust to this shapes
119    * Return:
120    *     [MeTensor] the data tensor in ME
121    * */
122   static MeTensorPtr ConvertGeTensor(GeTensorPtr ge_tensor, const ShapeVector &request_dims, bool ref_mem = false);
123   /*
124    * Parameters:
125    *     ge_tensors: [std::vector<GeTensorPtr>] the data tensor in GE
126    *     request_dims [std::vector<ShapeVector>] the output Me tensors must adjust to this shapes
127    * Return:
128    *     [std::vector<MeTensorPtr>] the data tensor in ME
129    * */
130   static std::vector<MeTensorPtr> ConvertGeTensors(const std::vector<GeTensorPtr> &ge_tensors,
131                                                    const std::vector<ShapeVector> &request_dims);
132   /*
133    * Parameters:
134    *     ge_tensors: [std::vector<GeTensorPtr>] the data tensor in GE
135    * Return:
136    *     [std::vector<MeTensorPtr>] the data tensor in ME
137    * */
138   static std::vector<MeTensorPtr> ConvertGeTensors(const std::vector<GeTensorPtr> &ge_tensors);
139   /*
140    * Parameters:
141    *     ge_tensor: [GeTensor] the data tensor in GE
142    *     me_dims: [ShapeVector] the shape of created Me tensor
143    *     me_type: [TypeId] the type of created Me tensor
144    * Return:
145    *     [MeTensor] the data tensor in ME
146    * */
147   static MeTensorPtr GenerateMeTensor(const GeTensorPtr &ge_tensor, const ShapeVector &me_dims, const TypeId &me_type,
148                                       bool ref_mem = false);
149   /*
150    * Parameters:
151    *     type: [GeDataType] the ge tensor data type
152    * Return:
153    *     [MeDataType] the me tensor data type
154    * */
155   static MeDataType ConvertGeDataType(const GeDataType &type);
156 
157   /*
158    * Parameters:
159    *     me_dims: [ShapeVector] the me shape
160    * Return:
161    *     [GeShape] the ge shape
162    * */
163   static GeShape ConvertMeShape(const ShapeVector &me_dims);
164 
165   /*
166    * Parameters:
167    *     ge_shape: [GeShape] the ge shape
168    * Return:
169    *     [vector<int>] the me shape
170    * */
171   static ShapeVector ConvertGeShape(const GeShape &ge_shape);
172 
173   /* Function:
174    *     Convert GeShape to Me request shape, Support pattern:
175    *         {1, x, 1, 1} --> {x}
176    *         {x, 1, 1, 1} --> {x}
177    *         {x, x, 1, 1} --> {x, x}
178    *         {x, x, x, 1} --> {x, x, x}
179    *         {x, x, x, x} --> {x, x, x, x}
180    *      If unmatch upon patterns, return original ge dims
181    * Parameters:
182    *     ge_shape: [GeShape] the ge shape
183    *     request_dims: [vector<int>] request dims
184    * Return:
185    *     [vector<int>] the me shape
186    * */
187   static ShapeVector ConvertGeShape(const GeShape &ge_shape, const ShapeVector &request_dims);
188 
189   /*
190    * Parameters:
191    *     vec: [ShapeVector] the vector to print
192    * Return:
193    *     [string] value string
194    * */
195   template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
PrintVector(const std::vector<T> & vec)196   static std::string PrintVector(const std::vector<T> &vec) {
197     const int MAX_PRINT_NUM = 100;
198     std::stringstream ss;
199     ss << "{ ";
200     int i = 0;
201     for (auto it = vec.begin(); it != vec.end(); ++it) {
202       ss << std::to_string(*it) << ", ";
203       i++;
204       if (i >= MAX_PRINT_NUM) {
205         break;
206       }
207     }
208 
209     if (i >= MAX_PRINT_NUM) {
210       ss << "... to be continue}";
211     } else {
212       ss << "}";
213     }
214     return ss.str();
215   }
216 
217   /*
218    * Parameters:
219    *     ge_tensor: [GeTensorPtr] the ge tensor
220    * Return:
221    *     [stringstream] value string
222    * */
223   static std::string PrintGeTensor(const GeTensorPtr ge_tensor);
224 
225   /*
226    * Parameters:
227    *     data: [uint8_t *] the ge tensor data pointer
228    *     size: [size_t] the ge tensor data bytes
229    * Return:
230    *     [shared_ptr<std::vector<T>]  vector pointer
231    * */
232   template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
MakeVector(const uint8_t * const data,size_t size)233   static std::vector<T> MakeVector(const uint8_t *const data, size_t size) {
234     auto dest = std::vector<T>(size / sizeof(T));
235     if (data == nullptr) {
236       return dest;
237     }
238 
239     errno_t ret = memcpy_s(dest.data(), dest.size() * sizeof(T), data, size);
240     if (EOK != ret) {
241       return std::vector<T>();
242     }
243     return dest;
244   }
245 
246   /*
247    * Parameters:
248    *     anf_name: [string] the anf node name
249    * Return:
250    *     [string] operator name
251    * */
252   static std::string NormOpName(const std::string &anf_name);
253 };
254 }  // namespace transform
255 }  // namespace mindspore
256 
257 #endif  // MINDSPORE_CCSRC_TRANSFORM_GRAPH_IR_UTIL_H_
258