• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2019 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_CCSRC_DEBUG_TENSOR_DATA_H_
17 #define MINDSPORE_CCSRC_DEBUG_TENSOR_DATA_H_
18 
19 #include <algorithm>
20 #include <vector>
21 #include <string>
22 #include <cstring>
23 #include <iostream>
24 #include "mindspore/core/utils/log_adapter.h"
25 #ifdef ONLINE_DBG_MODE
26 #include "ir/tensor.h"
27 #endif
28 
29 #ifdef ONLINE_DBG_MODE
30 namespace mindspore {
31 #endif
32 
33 namespace MsTypeId {
34 typedef enum MsTypeId : unsigned int {
35   kTypeUnknown = 0,
36   kMetaTypeBegin = kTypeUnknown,
37   kMetaTypeType,  // Type
38   kMetaTypeAnything,
39   kMetaTypeObject,
40   kMetaTypeTypeType,  // TypeType
41   kMetaTypeProblem,
42   kMetaTypeExternal,
43   kMetaTypeNone,
44   kMetaTypeNull,
45   kMetaTypeEllipsis,
46   kMetaTypeEnd,
47   //
48   // Object types
49   //
50   kObjectTypeBegin = kMetaTypeEnd,
51   kObjectTypeNumber,
52   kObjectTypeString,
53   kObjectTypeList,
54   kObjectTypeTuple,
55   kObjectTypeSlice,
56   kObjectTypeKeyword,
57   kObjectTypeTensorType,
58   kObjectTypeRowTensorType,
59   kObjectTypeSparseTensorType,
60   kObjectTypeUndeterminedType,
61   kObjectTypeClass,
62   kObjectTypeDictionary,
63   kObjectTypeFunction,
64   kObjectTypeJTagged,
65   kObjectTypeSymbolicKeyType,
66   kObjectTypeEnvType,
67   kObjectTypeRefKey,
68   kObjectTypeRef,
69   kObjectTypeEnd,
70   //
71   // Number Types
72   //
73   kNumberTypeBegin = kObjectTypeEnd,
74   kNumberTypeBool,
75   kNumberTypeInt,
76   kNumberTypeInt8,
77   kNumberTypeInt16,
78   kNumberTypeInt32,
79   kNumberTypeInt64,
80   kNumberTypeUInt,
81   kNumberTypeUInt8,
82   kNumberTypeUInt16,
83   kNumberTypeUInt32,
84   kNumberTypeUInt64,
85   kNumberTypeFloat,
86   kNumberTypeFloat16,
87   kNumberTypeFloat32,
88   kNumberTypeFloat64,
89   kNumberTypeComplex64,
90   kNumberTypeEnd
91 } MsTypeId;
92 }  // namespace MsTypeId
93 
94 typedef enum DbgDataType : unsigned int {
95   DT_UNDEFINED = 0,
96   // Basic types.
97   DT_BOOL = 1,  // bool
98 
99   DT_INT8 = 2,   // int8_t
100   DT_INT16 = 3,  // int16_t
101   DT_INT32 = 4,  // int32_t
102   DT_INT64 = 5,  // int64_t
103 
104   DT_UINT8 = 6,   // uint8_t
105   DT_UINT16 = 7,  // uint16_t
106   DT_UINT32 = 8,  // uint32_t
107   DT_UINT64 = 9,  // uint64_t
108 
109   DT_FLOAT16 = 10,  // float 16
110   DT_FLOAT32 = 11,  // float 32
111   DT_FLOAT64 = 12,  // float 64
112 
113   DT_STRING = 13,  // string
114   DT_TENSOR = 14,  // tensor
115   DT_GRAPH = 15,   // graph
116 
117   // list type
118   DT_BOOLS = 16,  // list of bool
119 
120   DT_INTS8 = 17,   // list of int8_t
121   DT_INTS16 = 18,  // list of int16_t
122   DT_INTS32 = 19,  // list of int32_t
123   DT_INTS64 = 20,  // list of int64_t
124 
125   DT_UINTS8 = 21,   // list of uint8_t
126   DT_UINTS16 = 22,  // list of uint16_t
127   DT_UINTS32 = 23,  // list of uint32_t
128   DT_UINTS64 = 24,  // list of uint64_t
129 
130   DT_FLOATS16 = 25,  // list of float16
131   DT_FLOATS32 = 26,  // list of float32
132   DT_FLOATS64 = 27,  // list of float64
133 
134   DT_STRINGS = 28,  // list of string
135   DT_TENSORS = 29,  // list of tensor
136   DT_GRAPHS = 30,   // list of graph
137 
138   DT_TUPLE = 31,  // tuple
139   DT_LIST = 32,   // list
140   DT_DICT = 33,   // dictionary
141 
142   // other types
143   DT_NONE = 34,      // None
144   DT_SYM_INST = 35,  // Symbolic Key Instance
145 
146   // type related type
147   DT_BASE_INT = 36,    // type generic int
148   DT_BASE_UINT = 37,   // type generate unsigned int
149   DT_BASE_FLOAT = 38,  // type generate float
150   DT_TYPE = 39,        // type type
151   DT_ANYTHING = 40,    // type anything
152   DT_REFKEY = 41,      // type refkey
153   DT_REF = 42          // type ref
154 } DbgDataType;
155 
156 class TensorData {
157  public:
TensorData()158   TensorData() : slot_(0), execution_order_(-1) {}
159 
TensorData(const TensorData & obj)160   TensorData(const TensorData &obj) {
161     MS_LOG(INFO) << "Copy Constructor";
162     this->name_ = obj.name_;
163     this->execution_order_ = obj.execution_order_;
164     this->slot_ = obj.slot_;
165     this->data_ptr_ = obj.data_ptr_;
166     this->size_ = obj.size_;
167     this->data_type_ = obj.data_type_;
168     this->data_type_size_ = obj.data_type_size_;
169     this->shape_ = obj.shape_;
170     this->iteration_ = obj.iteration_;
171     this->device_id_ = obj.device_id_;
172     this->data_ptr_ = obj.data_ptr_;
173     this->root_graph_id_ = obj.root_graph_id_;
174     this->is_output_ = obj.is_output_;
175 #ifdef ONLINE_DBG_MODE
176     this->tensor_ptr_ = obj.tensor_ptr_;
177 #endif
178   }
179 
180 #ifdef OFFLINE_DBG_MODE
~TensorData()181   ~TensorData() { DeleteDataPtr(); }
182 #else
~TensorData()183   ~TensorData() {}
184 #endif
185 
DeleteDataPtr()186   void DeleteDataPtr() {
187     if (this->data_ptr_ != NULL) {
188       delete this->data_ptr_;
189       this->data_ptr_ = NULL;
190       this->size_ = 0;
191     }
192   }
193 
GetName()194   std::string GetName() const { return this->name_; }
195 
GetTimeStamp()196   std::string GetTimeStamp() const { return this->time_stamp_; }
197 
GetSlot()198   size_t GetSlot() const { return this->slot_; }
199 
GetExecutionOrder()200   int GetExecutionOrder() const { return this->execution_order_; }
201 
SetExecutionOrder(int execution_order)202   void SetExecutionOrder(int execution_order) { this->execution_order_ = execution_order; }
203 
SetName(const std::string & name)204   void SetName(const std::string &name) { this->name_ = name; }
205 
SetTimeStamp(const std::string & time_stamp)206   void SetTimeStamp(const std::string &time_stamp) { this->time_stamp_ = time_stamp; }
207 
208 #ifdef ONLINE_DBG_MODE
SetTensor(mindspore::tensor::TensorPtr out_tensor)209   void SetTensor(mindspore::tensor::TensorPtr out_tensor) { this->tensor_ptr_ = out_tensor; }
210 #endif
211 
SetSlot(size_t slot)212   void SetSlot(size_t slot) { this->slot_ = slot; }
213 
GetDataPtr()214   const char *GetDataPtr() const { return this->data_ptr_; }
215 
SetDataPtr(char * data_ptr)216   void SetDataPtr(char *data_ptr) { this->data_ptr_ = data_ptr; }
217 
GetNumElements()218   uint32_t GetNumElements() { return size_ / data_type_size_; }
219 
GetByteSize()220   uint64_t GetByteSize() const { return this->size_; }
221 
SetByteSize(uint64_t size)222   void SetByteSize(uint64_t size) { this->size_ = size; }
223 
GetShape()224   std::vector<int64_t> GetShape() const { return this->shape_; }
225 
SetShape(std::vector<int64_t> shape)226   void SetShape(std::vector<int64_t> shape) { this->shape_ = shape; }
227 
GetIteration()228   unsigned int GetIteration() const { return this->iteration_; }
229 
SetIteration(unsigned int iteration)230   void SetIteration(unsigned int iteration) { this->iteration_ = iteration; }
231 
GetDeviceId()232   unsigned int GetDeviceId() const { return this->device_id_; }
233 
SetDeviceId(unsigned int device_id)234   void SetDeviceId(unsigned int device_id) { this->device_id_ = device_id; }
235 
GetRootGraphId()236   unsigned int GetRootGraphId() const { return this->root_graph_id_; }
237 
SetRootGraphId(unsigned int root_graph_id)238   void SetRootGraphId(unsigned int root_graph_id) { this->root_graph_id_ = root_graph_id; }
239 
GetType()240   DbgDataType GetType() const { return this->data_type_; }
241 
SetType(unsigned int type)242   void SetType(unsigned int type) { ConvertMsToDbgType(type); }
243 
SetType(std::string type_name)244   void SetType(std::string type_name) { ConvertStringToDbgType(type_name); }
245 
GetIsOutput()246   bool GetIsOutput() const { return this->is_output_; }
247 
SetIsOutput(bool is_output)248   void SetIsOutput(bool is_output) { this->is_output_ = is_output; }
249 
ConvertMsToDbgType(uint32_t type)250   void ConvertMsToDbgType(uint32_t type) {
251     switch (type) {
252       case MsTypeId::kNumberTypeBool:
253         this->data_type_ = DbgDataType::DT_BOOL;
254         this->data_type_size_ = 1;
255         break;
256       case MsTypeId::kNumberTypeInt8:
257         this->data_type_ = DbgDataType::DT_INT8;
258         this->data_type_size_ = 1;
259         break;
260       case MsTypeId::kNumberTypeInt16:
261         this->data_type_ = DbgDataType::DT_INT16;
262         this->data_type_size_ = 2;
263         break;
264       case MsTypeId::kNumberTypeInt32:
265         this->data_type_ = DbgDataType::DT_INT32;
266         this->data_type_size_ = 4;
267         break;
268       case MsTypeId::kNumberTypeInt64:
269         this->data_type_ = DbgDataType::DT_INT64;
270         this->data_type_size_ = 8;
271         break;
272       case MsTypeId::kNumberTypeUInt8:
273         this->data_type_ = DbgDataType::DT_UINT8;
274         this->data_type_size_ = 1;
275         break;
276       case MsTypeId::kNumberTypeUInt16:
277         this->data_type_ = DbgDataType::DT_UINT16;
278         this->data_type_size_ = 2;
279         break;
280       case MsTypeId::kNumberTypeUInt32:
281         this->data_type_ = DbgDataType::DT_UINT32;
282         this->data_type_size_ = 4;
283         break;
284       case MsTypeId::kNumberTypeUInt64:
285         this->data_type_ = DbgDataType::DT_UINT64;
286         this->data_type_size_ = 8;
287         break;
288       case MsTypeId::kNumberTypeFloat16:
289         this->data_type_ = DbgDataType::DT_FLOAT16;
290         this->data_type_size_ = 2;
291         break;
292       case MsTypeId::kNumberTypeFloat32:
293         this->data_type_ = DbgDataType::DT_FLOAT32;
294         this->data_type_size_ = 4;
295         break;
296       case MsTypeId::kNumberTypeFloat64:
297         this->data_type_ = DbgDataType::DT_FLOAT64;
298         this->data_type_size_ = 8;
299         break;
300       case MsTypeId::kNumberTypeInt:
301         this->data_type_ = DbgDataType::DT_BASE_INT;
302         this->data_type_size_ = 4;
303         break;
304       case MsTypeId::kNumberTypeUInt:
305         this->data_type_ = DbgDataType::DT_BASE_UINT;
306         this->data_type_size_ = 4;
307         break;
308       case MsTypeId::kNumberTypeFloat:
309         this->data_type_ = DbgDataType::DT_BASE_FLOAT;
310         this->data_type_size_ = 4;
311         break;
312       default:
313         MS_LOG(EXCEPTION) << "Unexpected type id: " << type;
314     }
315   }
316 
ConvertNpyStringToDbgType(const std::string & type_name)317   bool ConvertNpyStringToDbgType(const std::string &type_name) {
318     if (type_name == "b1") {
319       this->data_type_ = DbgDataType::DT_BOOL;
320       this->data_type_size_ = 1;
321       return true;
322     } else if (type_name == "i1") {
323       this->data_type_ = DbgDataType::DT_INT8;
324       this->data_type_size_ = 1;
325       return true;
326     } else if (type_name == "i2") {
327       this->data_type_ = DbgDataType::DT_INT16;
328       this->data_type_size_ = 2;
329       return true;
330     } else if (type_name == "i4") {
331       this->data_type_ = DbgDataType::DT_INT32;
332       this->data_type_size_ = 4;
333       return true;
334     } else if (type_name == "i8") {
335       this->data_type_ = DbgDataType::DT_INT64;
336       this->data_type_size_ = 8;
337       return true;
338     } else if (type_name == "u1") {
339       this->data_type_ = DbgDataType::DT_UINT8;
340       this->data_type_size_ = 1;
341       return true;
342     } else if (type_name == "u2") {
343       this->data_type_ = DbgDataType::DT_UINT16;
344       this->data_type_size_ = 2;
345       return true;
346     } else if (type_name == "u4") {
347       this->data_type_ = DbgDataType::DT_UINT32;
348       this->data_type_size_ = 4;
349       return true;
350     } else if (type_name == "u8") {
351       this->data_type_ = DbgDataType::DT_UINT64;
352       this->data_type_size_ = 8;
353       return true;
354     } else if (type_name == "f2") {
355       this->data_type_ = DbgDataType::DT_FLOAT16;
356       this->data_type_size_ = 2;
357       return true;
358     } else if (type_name == "f4") {
359       this->data_type_ = DbgDataType::DT_FLOAT32;
360       this->data_type_size_ = 4;
361       return true;
362     } else if (type_name == "f8") {
363       this->data_type_ = DbgDataType::DT_FLOAT64;
364       this->data_type_size_ = 8;
365       return true;
366     } else {
367       return false;
368     }
369   }
370 
ConvertStringToDbgType(const std::string & type_name)371   void ConvertStringToDbgType(const std::string &type_name) {
372     std::string type_name_lower = type_name;
373     std::string trans_true_prefix = "kNumberType";
374     if (type_name.find(trans_true_prefix) == 0) {
375       type_name_lower = type_name.substr(trans_true_prefix.length());
376     }
377     (void)std::transform(type_name_lower.begin(), type_name_lower.end(), type_name_lower.begin(), ::tolower);
378     if (type_name_lower == "bool") {
379       this->data_type_ = DbgDataType::DT_BOOL;
380       this->data_type_size_ = 1;
381     } else if (type_name_lower == "int8") {
382       this->data_type_ = DbgDataType::DT_INT8;
383       this->data_type_size_ = 1;
384     } else if (type_name_lower == "int16") {
385       this->data_type_ = DbgDataType::DT_INT16;
386       this->data_type_size_ = 2;
387     } else if (type_name_lower == "int32") {
388       this->data_type_ = DbgDataType::DT_INT32;
389       this->data_type_size_ = 4;
390     } else if (type_name_lower == "int64") {
391       this->data_type_ = DbgDataType::DT_INT64;
392       this->data_type_size_ = 8;
393     } else if (type_name_lower == "uint8") {
394       this->data_type_ = DbgDataType::DT_UINT8;
395       this->data_type_size_ = 1;
396     } else if (type_name_lower == "uint16") {
397       this->data_type_ = DbgDataType::DT_UINT16;
398       this->data_type_size_ = 2;
399     } else if (type_name_lower == "uint32") {
400       this->data_type_ = DbgDataType::DT_UINT32;
401       this->data_type_size_ = 4;
402     } else if (type_name_lower == "uint64") {
403       this->data_type_ = DbgDataType::DT_UINT64;
404       this->data_type_size_ = 8;
405     } else if (type_name_lower == "float16") {
406       this->data_type_ = DbgDataType::DT_FLOAT16;
407       this->data_type_size_ = 2;
408     } else if (type_name_lower == "float32") {
409       this->data_type_ = DbgDataType::DT_FLOAT32;
410       this->data_type_size_ = 4;
411     } else if (type_name_lower == "float64") {
412       this->data_type_ = DbgDataType::DT_FLOAT64;
413       this->data_type_size_ = 8;
414     } else if (type_name_lower == "") {
415       this->data_type_ = DbgDataType::DT_UNDEFINED;
416       this->data_type_size_ = 0;
417     } else {
418       if (!ConvertNpyStringToDbgType(type_name_lower)) {
419         MS_LOG(EXCEPTION) << "Unexpected type name: " << type_name;
420       }
421     }
422   }
423 
424  private:
425   char *data_ptr_{nullptr};                           // pointer to the pre-allocated memory
426   uint64_t size_{0};                                  // size_ in bytes
427   DbgDataType data_type_{DbgDataType::DT_UNDEFINED};  // internal debugger type
428   unsigned int data_type_size_{0};
429   std::vector<int64_t> shape_;
430   std::string name_;
431   uint64_t slot_;
432   unsigned int iteration_{0};
433   unsigned int device_id_{0};
434   unsigned int root_graph_id_{0};
435   bool is_output_{true};
436   int execution_order_{-1};
437   std::string time_stamp_;
438 
439 #ifdef ONLINE_DBG_MODE
440   mindspore::tensor::TensorPtr tensor_ptr_{nullptr};
441 #endif
442 };
443 #ifdef ONLINE_DBG_MODE
444 }  // namespace mindspore
445 #endif
446 #endif  // MINDSPORE_CCSRC_DEBUG_TENSOR_DATA_H_
447