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