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 13 kB

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