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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. bool GetIsOutput() { return is_output; }
  187. void SetIsOutput(bool is_output) { this->is_output = is_output; }
  188. void ConvertMsToDbgType(uint32_t type) {
  189. switch (type) {
  190. case MsTypeId::kNumberTypeBool:
  191. this->data_type = DbgDataType::DT_BOOL;
  192. this->data_type_size = 1;
  193. break;
  194. case MsTypeId::kNumberTypeInt8:
  195. this->data_type = DbgDataType::DT_INT8;
  196. this->data_type_size = 1;
  197. break;
  198. case MsTypeId::kNumberTypeInt16:
  199. this->data_type = DbgDataType::DT_INT16;
  200. this->data_type_size = 2;
  201. break;
  202. case MsTypeId::kNumberTypeInt32:
  203. this->data_type = DbgDataType::DT_INT32;
  204. this->data_type_size = 4;
  205. break;
  206. case MsTypeId::kNumberTypeInt64:
  207. this->data_type = DbgDataType::DT_INT64;
  208. this->data_type_size = 8;
  209. break;
  210. case MsTypeId::kNumberTypeUInt8:
  211. this->data_type = DbgDataType::DT_UINT8;
  212. this->data_type_size = 1;
  213. break;
  214. case MsTypeId::kNumberTypeUInt16:
  215. this->data_type = DbgDataType::DT_UINT16;
  216. this->data_type_size = 2;
  217. break;
  218. case MsTypeId::kNumberTypeUInt32:
  219. this->data_type = DbgDataType::DT_UINT32;
  220. this->data_type_size = 4;
  221. break;
  222. case MsTypeId::kNumberTypeUInt64:
  223. this->data_type = DbgDataType::DT_UINT64;
  224. this->data_type_size = 8;
  225. break;
  226. case MsTypeId::kNumberTypeFloat16:
  227. this->data_type = DbgDataType::DT_FLOAT16;
  228. this->data_type_size = 2;
  229. break;
  230. case MsTypeId::kNumberTypeFloat32:
  231. this->data_type = DbgDataType::DT_FLOAT32;
  232. this->data_type_size = 4;
  233. break;
  234. case MsTypeId::kNumberTypeFloat64:
  235. this->data_type = DbgDataType::DT_FLOAT64;
  236. this->data_type_size = 8;
  237. break;
  238. case MsTypeId::kNumberTypeInt:
  239. this->data_type = DbgDataType::DT_BASE_INT;
  240. this->data_type_size = 4;
  241. break;
  242. case MsTypeId::kNumberTypeUInt:
  243. this->data_type = DbgDataType::DT_BASE_UINT;
  244. this->data_type_size = 4;
  245. break;
  246. case MsTypeId::kNumberTypeFloat:
  247. this->data_type = DbgDataType::DT_BASE_FLOAT;
  248. this->data_type_size = 4;
  249. break;
  250. default:
  251. MS_LOG(EXCEPTION) << "Unexpected type id: " << type;
  252. }
  253. }
  254. bool ConvertNpyStringToDbgType(const std::string &type_name) {
  255. if (type_name == "b1") {
  256. this->data_type = DbgDataType::DT_BOOL;
  257. this->data_type_size = 1;
  258. return true;
  259. } else if (type_name == "i1") {
  260. this->data_type = DbgDataType::DT_INT8;
  261. this->data_type_size = 1;
  262. return true;
  263. } else if (type_name == "i2") {
  264. this->data_type = DbgDataType::DT_INT16;
  265. this->data_type_size = 2;
  266. return true;
  267. } else if (type_name == "i4") {
  268. this->data_type = DbgDataType::DT_INT32;
  269. this->data_type_size = 4;
  270. return true;
  271. } else if (type_name == "i8") {
  272. this->data_type = DbgDataType::DT_INT64;
  273. this->data_type_size = 8;
  274. return true;
  275. } else if (type_name == "u1") {
  276. this->data_type = DbgDataType::DT_UINT8;
  277. this->data_type_size = 1;
  278. return true;
  279. } else if (type_name == "u2") {
  280. this->data_type = DbgDataType::DT_UINT16;
  281. this->data_type_size = 2;
  282. return true;
  283. } else if (type_name == "u4") {
  284. this->data_type = DbgDataType::DT_UINT32;
  285. this->data_type_size = 4;
  286. return true;
  287. } else if (type_name == "u8") {
  288. this->data_type = DbgDataType::DT_UINT64;
  289. this->data_type_size = 8;
  290. return true;
  291. } else if (type_name == "f2") {
  292. this->data_type = DbgDataType::DT_FLOAT16;
  293. this->data_type_size = 2;
  294. return true;
  295. } else if (type_name == "f4") {
  296. this->data_type = DbgDataType::DT_FLOAT32;
  297. this->data_type_size = 4;
  298. return true;
  299. } else if (type_name == "f8") {
  300. this->data_type = DbgDataType::DT_FLOAT64;
  301. this->data_type_size = 8;
  302. return true;
  303. } else {
  304. return false;
  305. }
  306. }
  307. void ConvertStringToDbgType(const std::string &type_name) {
  308. std::string type_name_lower = type_name;
  309. std::string trans_true_prefix = "kNumberType";
  310. if (type_name.find(trans_true_prefix) == 0) {
  311. type_name_lower = type_name.substr(trans_true_prefix.length());
  312. }
  313. (void)std::transform(type_name_lower.begin(), type_name_lower.end(), type_name_lower.begin(), ::tolower);
  314. if (type_name_lower == "bool") {
  315. this->data_type = DbgDataType::DT_BOOL;
  316. this->data_type_size = 1;
  317. } else if (type_name_lower == "int8") {
  318. this->data_type = DbgDataType::DT_INT8;
  319. this->data_type_size = 1;
  320. } else if (type_name_lower == "int16") {
  321. this->data_type = DbgDataType::DT_INT16;
  322. this->data_type_size = 2;
  323. } else if (type_name_lower == "int32") {
  324. this->data_type = DbgDataType::DT_INT32;
  325. this->data_type_size = 4;
  326. } else if (type_name_lower == "int64") {
  327. this->data_type = DbgDataType::DT_INT64;
  328. this->data_type_size = 8;
  329. } else if (type_name_lower == "uint8") {
  330. this->data_type = DbgDataType::DT_UINT8;
  331. this->data_type_size = 1;
  332. } else if (type_name_lower == "uint16") {
  333. this->data_type = DbgDataType::DT_UINT16;
  334. this->data_type_size = 2;
  335. } else if (type_name_lower == "uint32") {
  336. this->data_type = DbgDataType::DT_UINT32;
  337. this->data_type_size = 4;
  338. } else if (type_name_lower == "uint64") {
  339. this->data_type = DbgDataType::DT_UINT64;
  340. this->data_type_size = 8;
  341. } else if (type_name_lower == "float16") {
  342. this->data_type = DbgDataType::DT_FLOAT16;
  343. this->data_type_size = 2;
  344. } else if (type_name_lower == "float32") {
  345. this->data_type = DbgDataType::DT_FLOAT32;
  346. this->data_type_size = 4;
  347. } else if (type_name_lower == "float64") {
  348. this->data_type = DbgDataType::DT_FLOAT64;
  349. this->data_type_size = 8;
  350. } else if (type_name_lower == "") {
  351. this->data_type = DbgDataType::DT_UNDEFINED;
  352. this->data_type_size = 0;
  353. } else {
  354. if (!ConvertNpyStringToDbgType(type_name_lower)) {
  355. MS_LOG(EXCEPTION) << "Unexpected type name: " << type_name;
  356. }
  357. }
  358. }
  359. private:
  360. char *data_ptr; // pointer to the pre-allocated memory
  361. uint64_t size; // size in bytes
  362. DbgDataType data_type; // internal debugger type
  363. unsigned int data_type_size;
  364. std::vector<int64_t> shape;
  365. std::string name;
  366. uint64_t slot;
  367. unsigned int iteration;
  368. unsigned int device_id;
  369. unsigned int root_graph_id;
  370. bool is_output;
  371. int execution_order;
  372. #ifdef ONLINE_DBG_MODE
  373. mindspore::tensor::TensorPtr tensor_ptr;
  374. #endif
  375. };
  376. #ifdef ONLINE_DBG_MODE
  377. } // namespace mindspore
  378. #endif
  379. #endif // MINDSPORE_CCSRC_DEBUG_TENSOR_DATA_H_