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