You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

tensor_data.h 14 kB

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