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