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

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