• 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 #ifndef MINDSPORE_INCLUDE_API_TYPES_H
17 #define MINDSPORE_INCLUDE_API_TYPES_H
18 
19 #include <cstddef>
20 #include <string>
21 #include <vector>
22 #include <memory>
23 #include <functional>
24 #include "include/api/data_type.h"
25 #include "include/api/dual_abi_helper.h"
26 #include "include/api/format.h"
27 
28 #ifndef MS_API
29 #ifdef _WIN32
30 #define MS_API __declspec(dllexport)
31 #else
32 #define MS_API __attribute__((visibility("default")))
33 #endif
34 #endif
35 
36 namespace mindspore {
37 enum ModelType : uint32_t {
38   kMindIR = 0,
39   kAIR = 1,
40   kOM = 2,
41   kONNX = 3,
42   kFlatBuffer = 4,
43   // insert new data type here
44   kUnknownType = 0xFFFFFFFF
45 };
46 
47 enum QuantizationType : uint32_t { kNoQuant = 0, kWeightQuant = 1, kFullQuant = 2, kUnknownQuantType = 0xFFFFFFFF };
48 
49 enum OptimizationLevel : uint32_t {
50   kO0 = 0,    // Do not change
51   kO2 = 2,    // Cast network to float16, keep batchnorm and loss in float32,
52   kO3 = 3,    // Cast network to float16, including bacthnorm
53   kAuto = 4,  // Choose optimization based on device
54   kOptimizationType = 0xFFFFFFFF
55 };
56 
57 struct QuantParam {
58   int bit_num;
59   double scale;
60   int32_t zero_point;
61 };
62 
63 class Allocator;
64 /// \brief The MSTensor class defines a tensor in MindSpore.
65 class MS_API MSTensor {
66  public:
67   class Impl;
68   /// \brief Creates a MSTensor object, whose data need to be copied before accessed by Model, must be used in pairs
69   /// with DestroyTensorPtr.
70   ///
71   /// \param[in] name The name of the MSTensor.
72   /// \param[in] type The data type of the MSTensor.
73   /// \param[in] shape The shape of the MSTensor.
74   /// \param[in] data The data pointer that points to allocated memory.
75   /// \param[in] data_len The length of the memory, in bytes.
76   ///
77   /// \return A pointer of MSTensor.
78   static inline MSTensor *CreateTensor(const std::string &name, DataType type, const std::vector<int64_t> &shape,
79                                        const void *data, size_t data_len) noexcept;
80 
81   /// \brief Creates a MSTensor object, whose data can be directly accessed by Model, must be used in pairs with
82   /// DestroyTensorPtr.
83   ///
84   /// \param[in] name The name of the MSTensor.
85   /// \param[in] type The data type of the MSTensor.
86   /// \param[in] shape The shape of the MSTensor.
87   /// \param[in] data The data pointer that points to allocated memory.
88   /// \param[in] data_len The length of the memory, in bytes.
89   ///
90   /// \return A pointer of MSTensor.
91   static inline MSTensor *CreateRefTensor(const std::string &name, DataType type, const std::vector<int64_t> &shape,
92                                           const void *data, size_t data_len) noexcept;
93 
94   /// \brief Creates a MSTensor object, whose device data can be directly accessed by Model, must be used in pairs with
95   /// DestroyTensorPtr.
96   ///
97   /// \param[in] name The name of the MSTensor.
98   /// \param[in] type The data type of the MSTensor.
99   /// \param[in] shape The shape of the MSTensor.
100   /// \param[in] data The data pointer that points to device memory.
101   /// \param[in] data_len The length of the memory, in bytes.
102   ///
103   /// \return A pointer of MSTensor.
104   static inline MSTensor *CreateDevTensor(const std::string &name, DataType type, const std::vector<int64_t> &shape,
105                                           const void *data, size_t data_len) noexcept;
106 
107   /// \brief Creates a MSTensor object from local image file, must be used in pairs with DestroyTensorPtr.
108   ///
109   /// \param[in] image_file Path of image file.
110   ///
111   /// \return A pointer of MSTensor.
112   static inline MSTensor *CreateImageTensor(const std::string &image_file) noexcept;
113 
114   /// \brief Create a string type MSTensor object whose data can be accessed by Model only after being copied, must be
115   /// used in pair with DestroyTensorPtr.
116   ///
117   /// \param[in] name The name of the MSTensor.
118   /// \param[in] str A vector container containing several strings.
119   ///
120   /// \return A pointer of MSTensor.
121   static inline MSTensor *StringsToTensor(const std::string &name, const std::vector<std::string> &str);
122 
123   /// \brief Parse the string type MSTensor object into strings.
124   ///
125   /// \param[in] tensor A MSTensor object.
126   ///
127   /// \return A vector container containing several strings.
128   static inline std::vector<std::string> TensorToStrings(const MSTensor &tensor);
129 
130   /// \brief Destroy an object created by Clone, StringsToTensor, CreateRefTensor, CreateDevTensor or CreateTensor. Do
131   /// not use it to destroy MSTensor from other sources.
132   ///
133   /// \param[in] tensor A MSTensor object.
134   static void DestroyTensorPtr(MSTensor *tensor) noexcept;
135 
136   MSTensor();
137   explicit MSTensor(const std::shared_ptr<Impl> &impl);
138   inline MSTensor(const std::string &name, DataType type, const std::vector<int64_t> &shape, const void *data,
139                   size_t data_len);
140   explicit MSTensor(std::nullptr_t);
141   ~MSTensor();
142 
143   /// \brief Obtains the name of the MSTensor.
144   ///
145   /// \return The name of the MSTensor.
146   inline std::string Name() const;
147 
148   /// \brief Obtains the data type of the MSTensor.
149   ///
150   /// \return The data type of the MSTensor.
151   enum DataType DataType() const;
152 
153   /// \brief Obtains the shape of the MSTensor.
154   ///
155   /// \return The shape of the MSTensor.
156   const std::vector<int64_t> &Shape() const;
157 
158   /// \brief Obtains the number of elements of the MSTensor.
159   ///
160   /// \return The number of elements of the MSTensor.
161   int64_t ElementNum() const;
162 
163   /// \brief Obtains a shared pointer to the copy of data of the MSTensor. The data can be read on host.
164   ///
165   /// \return A shared pointer to the copy of data of the MSTensor.
166   std::shared_ptr<const void> Data() const;
167 
168   /// \brief Obtains the pointer to the data of the MSTensor. If the MSTensor is a device tensor, the data cannot be
169   /// accessed directly on host.
170   ///
171   /// \return A pointer to the data of the MSTensor.
172   void *MutableData();
173 
174   /// \brief Obtains the length of the data of the MSTensor, in bytes.
175   ///
176   /// \return The length of the data of the MSTensor, in bytes.
177   size_t DataSize() const;
178 
179   /// \brief Get whether the MSTensor data is const data
180   ///
181   /// \return Const flag of MSTensor
182   bool IsConst() const;
183 
184   /// \brief Gets the boolean value that indicates whether the memory of MSTensor is on device.
185   ///
186   /// \return The boolean value that indicates whether the memory of MSTensor is on device.
187   bool IsDevice() const;
188 
189   /// \brief Gets a deep copy of the MSTensor, must be used in pair with DestroyTensorPtr.
190   ///
191   /// \return A pointer points to a deep copy of the MSTensor.
192   MSTensor *Clone() const;
193 
194   /// \brief Gets the boolean value that indicates whether the MSTensor is valid.
195   ///
196   /// \return The boolean value that indicates whether the MSTensor is valid.
197   bool operator==(std::nullptr_t) const;
198 
199   /// \brief Gets the boolean value that indicates whether the MSTensor is valid.
200   ///
201   /// \return The boolean value that indicates whether the MSTensor is valid.
202   bool operator!=(std::nullptr_t) const;
203 
204   /// \brief Get the boolean value that indicates whether the MSTensor equals tensor.
205   ///
206   /// \param[in] another MSTensor.
207   ///
208   /// \return The boolean value that indicates whether the MSTensor equals tensor.
209   bool operator==(const MSTensor &tensor) const;
210 
211   /// \brief Set the shape of for the MSTensor. Only valid for Lite.
212   ///
213   /// \param[in] Shape of the MSTensor, a vector of int64_t.
214   void SetShape(const std::vector<int64_t> &shape);
215 
216   /// \brief Set the data type for the MSTensor. Only valid for Lite.
217   ///
218   /// \param[in] The data type of the MSTensor.
219   void SetDataType(enum DataType data_type);
220 
221   /// \brief Set the name for the MSTensor. Only valid for Lite.
222   ///
223   /// \param[in] The name of the MSTensor.
224   void SetTensorName(const std::string &name);
225 
226   /// \brief Set the Allocator for the MSTensor. Only valid for Lite.
227   ///
228   /// \param[in] A pointer to Allocator.
229   void SetAllocator(std::shared_ptr<Allocator> allocator);
230 
231   /// \brief Obtain the Allocator of the MSTensor. Only valid for Lite.
232   ///
233   /// \return A pointer to Allocator.
234   std::shared_ptr<Allocator> allocator() const;
235 
236   /// \brief Set the format for the MSTensor. Only valid for Lite.
237   ///
238   /// \param[in] The format of the MSTensor.
239   void SetFormat(mindspore::Format format);
240 
241   /// \brief Obtain the format of the MSTensor. Only valid for Lite.
242   ///
243   /// \return The format of the MSTensor.
244   mindspore::Format format() const;
245 
246   /// \brief Set the data for the MSTensor. Only valid for Lite.
247   ///
248   /// \param[in] A pointer to the data of the MSTensor.
249   void SetData(void *data);
250 
251   /// \brief Get the quantization parameters of the MSTensor. Only valid for Lite.
252   ///
253   /// \return The quantization parameters of the MSTensor.
254   std::vector<QuantParam> QuantParams() const;
255 
256   /// \brief Set the quantization parameters for the MSTensor. Only valid for Lite.
257   ///
258   /// \param[in] The quantization parameters of the MSTensor.
259   void SetQuantParams(std::vector<QuantParam> quant_params);
260 
impl()261   const std::shared_ptr<Impl> impl() const { return impl_; }
262 
263  private:
264   // api without std::string
265   static MSTensor *CreateTensor(const std::vector<char> &name, enum DataType type, const std::vector<int64_t> &shape,
266                                 const void *data, size_t data_len) noexcept;
267   static MSTensor *CreateRefTensor(const std::vector<char> &name, enum DataType type, const std::vector<int64_t> &shape,
268                                    const void *data, size_t data_len) noexcept;
269   static MSTensor *CreateDevTensor(const std::vector<char> &name, enum DataType type, const std::vector<int64_t> &shape,
270                                    const void *data, size_t data_len) noexcept;
271   static MSTensor *CreateImageTensor(const std::vector<char> &image_file) noexcept;
272   static MSTensor *CharStringsToTensor(const std::vector<char> &name, const std::vector<std::vector<char>> &str);
273   static std::vector<std::vector<char>> TensorToStringChars(const MSTensor &tensor);
274 
275   MSTensor(const std::vector<char> &name, enum DataType type, const std::vector<int64_t> &shape, const void *data,
276            size_t data_len);
277   std::vector<char> CharName() const;
278 
279   friend class ModelImpl;
280   std::shared_ptr<Impl> impl_;
281 };
282 
283 class MS_API Buffer {
284  public:
285   Buffer();
286   Buffer(const void *data, size_t data_len);
287   ~Buffer();
288 
289   const void *Data() const;
290   void *MutableData();
291   size_t DataSize() const;
292 
293   bool ResizeData(size_t data_len);
294   bool SetData(const void *data, size_t data_len);
295 
296   Buffer Clone() const;
297 
298  private:
299   class Impl;
300   std::shared_ptr<Impl> impl_;
301 };
302 
CreateTensor(const std::string & name,enum DataType type,const std::vector<int64_t> & shape,const void * data,size_t data_len)303 MSTensor *MSTensor::CreateTensor(const std::string &name, enum DataType type, const std::vector<int64_t> &shape,
304                                  const void *data, size_t data_len) noexcept {
305   return CreateTensor(StringToChar(name), type, shape, data, data_len);
306 }
307 
CreateRefTensor(const std::string & name,enum DataType type,const std::vector<int64_t> & shape,const void * data,size_t data_len)308 MSTensor *MSTensor::CreateRefTensor(const std::string &name, enum DataType type, const std::vector<int64_t> &shape,
309                                     const void *data, size_t data_len) noexcept {
310   return CreateRefTensor(StringToChar(name), type, shape, data, data_len);
311 }
312 
CreateDevTensor(const std::string & name,enum DataType type,const std::vector<int64_t> & shape,const void * data,size_t data_len)313 MSTensor *MSTensor::CreateDevTensor(const std::string &name, enum DataType type, const std::vector<int64_t> &shape,
314                                     const void *data, size_t data_len) noexcept {
315   return CreateDevTensor(StringToChar(name), type, shape, data, data_len);
316 }
317 
CreateImageTensor(const std::string & image_file)318 MSTensor *MSTensor::CreateImageTensor(const std::string &image_file) noexcept {
319   return CreateImageTensor(StringToChar(image_file));
320 }
321 
StringsToTensor(const std::string & name,const std::vector<std::string> & str)322 MSTensor *MSTensor::StringsToTensor(const std::string &name, const std::vector<std::string> &str) {
323   return CharStringsToTensor(StringToChar(name), VectorStringToChar(str));
324 }
325 
TensorToStrings(const MSTensor & tensor)326 std::vector<std::string> MSTensor::TensorToStrings(const MSTensor &tensor) {
327   return VectorCharToString(TensorToStringChars(tensor));
328 }
329 
MSTensor(const std::string & name,enum DataType type,const std::vector<int64_t> & shape,const void * data,size_t data_len)330 MSTensor::MSTensor(const std::string &name, enum DataType type, const std::vector<int64_t> &shape, const void *data,
331                    size_t data_len)
332     : MSTensor(StringToChar(name), type, shape, data, data_len) {}
333 
Name()334 std::string MSTensor::Name() const { return CharToString(CharName()); }
335 
336 using Key = struct Key {
337   const size_t max_key_len = 32;
338   size_t len;
339   unsigned char key[32];
KeyKey340   Key() : len(0) {}
341   explicit Key(const char *dec_key, size_t key_len);
342 };
343 
344 constexpr char kDecModeAesGcm[] = "AES-GCM";
345 
346 /// \brief CallBackParam defined input arguments for callBack function.
347 struct MSCallBackParam {
348   std::string node_name; /**< node name argument */
349   std::string node_type; /**< node type argument */
350 };
351 
352 /// \brief KernelCallBack defined the function pointer for callBack.
353 using MSKernelCallBack = std::function<bool(const std::vector<MSTensor> &inputs, const std::vector<MSTensor> &outputs,
354                                             const MSCallBackParam &opInfo)>;
355 
356 std::vector<char> CharVersion();
Version()357 inline std::string Version() { return CharToString(CharVersion()); }
358 
359 }  // namespace mindspore
360 #endif  // MINDSPORE_INCLUDE_API_TYPES_H
361