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.

tensorprint_utils.cc 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /**
  2. * Copyright 2020-2021 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. #include "utils/tensorprint_utils.h"
  17. #include <atomic>
  18. #include <fstream>
  19. #include <memory>
  20. #include <string>
  21. #include <vector>
  22. #include "ir/tensor.h"
  23. #include "pybind11/pybind11.h"
  24. #include "utils/utils.h"
  25. #include "utils/ms_utils.h"
  26. #include "utils/shape_utils.h"
  27. namespace py = pybind11;
  28. namespace mindspore {
  29. #ifndef NO_DLIB
  30. static std::map<aclDataType, TypeId> print_acl_data_type_map = {
  31. {ACL_INT8, TypeId::kNumberTypeInt8}, {ACL_UINT8, TypeId::kNumberTypeUInt8},
  32. {ACL_INT16, TypeId::kNumberTypeInt16}, {ACL_UINT16, TypeId::kNumberTypeUInt16},
  33. {ACL_INT32, TypeId::kNumberTypeInt32}, {ACL_UINT32, TypeId::kNumberTypeUInt32},
  34. {ACL_INT64, TypeId::kNumberTypeInt64}, {ACL_UINT64, TypeId::kNumberTypeUInt64},
  35. {ACL_FLOAT16, TypeId::kNumberTypeFloat16}, {ACL_FLOAT, TypeId::kNumberTypeFloat32},
  36. {ACL_DOUBLE, TypeId::kNumberTypeFloat64}, {ACL_BOOL, TypeId::kNumberTypeBool}};
  37. static std::map<aclDataType, size_t> acl_data_type_size_map = {
  38. {ACL_INT8, sizeof(int8_t)}, {ACL_UINT8, sizeof(uint8_t)}, {ACL_INT16, sizeof(int16_t)},
  39. {ACL_UINT16, sizeof(uint16_t)}, {ACL_INT32, sizeof(int32_t)}, {ACL_UINT32, sizeof(uint32_t)},
  40. {ACL_INT64, sizeof(int64_t)}, {ACL_UINT64, sizeof(uint64_t)}, {ACL_FLOAT16, sizeof(float) / 2},
  41. {ACL_FLOAT, sizeof(float)}, {ACL_DOUBLE, sizeof(double)}, {ACL_BOOL, sizeof(bool)}};
  42. std::string GetParseType(const aclDataType &acl_data_type) {
  43. static const std::map<aclDataType, std::string> print_tensor_parse_map = {
  44. {ACL_INT8, "Int8"}, {ACL_UINT8, "Uint8"}, {ACL_INT16, "Int16"}, {ACL_UINT16, "Uint16"},
  45. {ACL_INT32, "Int32"}, {ACL_UINT32, "Uint32"}, {ACL_INT64, "Int64"}, {ACL_UINT64, "Uint64"},
  46. {ACL_FLOAT16, "Float16"}, {ACL_FLOAT, "Float32"}, {ACL_DOUBLE, "Float64"}, {ACL_BOOL, "Bool"}};
  47. auto type_iter = print_tensor_parse_map.find(acl_data_type);
  48. if (type_iter == print_tensor_parse_map.end()) {
  49. MS_LOG(EXCEPTION) << "type of tensor need to print is not support " << acl_data_type;
  50. }
  51. return type_iter->second;
  52. }
  53. bool PrintTensorToString(const char *str_data_ptr, mindspore::tensor::Tensor *const print_tensor,
  54. const size_t &memory_size) {
  55. MS_EXCEPTION_IF_NULL(str_data_ptr);
  56. MS_EXCEPTION_IF_NULL(print_tensor);
  57. auto *tensor_data_ptr = static_cast<uint8_t *>(print_tensor->data_c());
  58. MS_EXCEPTION_IF_NULL(tensor_data_ptr);
  59. size_t dest_size = static_cast<size_t>(print_tensor->data().nbytes());
  60. size_t target_size = memory_size;
  61. auto cp_ret = memcpy_s(tensor_data_ptr, dest_size, str_data_ptr, target_size);
  62. if (cp_ret != EOK) {
  63. MS_LOG(ERROR) << "Print op Failed to copy the memory to py::tensor " << cp_ret;
  64. return false;
  65. }
  66. return true;
  67. }
  68. template <typename T>
  69. void PrintScalarToString(const char *str_data_ptr, const aclDataType &acl_data_type, std::ostringstream *const buf) {
  70. MS_EXCEPTION_IF_NULL(str_data_ptr);
  71. MS_EXCEPTION_IF_NULL(buf);
  72. *buf << "Tensor(shape=[], dtype=" << GetParseType(acl_data_type) << ", value=";
  73. const T *data_ptr = reinterpret_cast<const T *>(str_data_ptr);
  74. if constexpr (std::is_same<T, int8_t>::value || std::is_same<T, uint8_t>::value) {
  75. const int int_data = static_cast<int>(*data_ptr);
  76. *buf << int_data << ")\n";
  77. } else {
  78. *buf << *data_ptr << ")\n";
  79. }
  80. }
  81. void PrintScalarToBoolString(const char *str_data_ptr, const aclDataType &acl_data_type,
  82. std::ostringstream *const buf) {
  83. MS_EXCEPTION_IF_NULL(str_data_ptr);
  84. MS_EXCEPTION_IF_NULL(buf);
  85. const bool *data_ptr = reinterpret_cast<const bool *>(str_data_ptr);
  86. *buf << "Tensor(shape=[], dtype=" << GetParseType(acl_data_type) << ", value=";
  87. if (*data_ptr) {
  88. *buf << "True)\n";
  89. } else {
  90. *buf << "False)\n";
  91. }
  92. }
  93. void convertDataItem2Scalar(const char *str_data_ptr, const aclDataType &acl_data_type, std::ostringstream *const buf) {
  94. MS_EXCEPTION_IF_NULL(str_data_ptr);
  95. MS_EXCEPTION_IF_NULL(buf);
  96. auto type_iter = print_acl_data_type_map.find(acl_data_type);
  97. auto type_id = type_iter->second;
  98. if (type_id == TypeId::kNumberTypeBool) {
  99. PrintScalarToBoolString(str_data_ptr, acl_data_type, buf);
  100. } else if (type_id == TypeId::kNumberTypeInt8) {
  101. PrintScalarToString<int8_t>(str_data_ptr, acl_data_type, buf);
  102. } else if (type_id == TypeId::kNumberTypeUInt8) {
  103. PrintScalarToString<uint8_t>(str_data_ptr, acl_data_type, buf);
  104. } else if (type_id == TypeId::kNumberTypeInt16) {
  105. PrintScalarToString<int16_t>(str_data_ptr, acl_data_type, buf);
  106. } else if (type_id == TypeId::kNumberTypeUInt16) {
  107. PrintScalarToString<uint16_t>(str_data_ptr, acl_data_type, buf);
  108. } else if (type_id == TypeId::kNumberTypeInt32) {
  109. PrintScalarToString<int32_t>(str_data_ptr, acl_data_type, buf);
  110. } else if (type_id == TypeId::kNumberTypeUInt32) {
  111. PrintScalarToString<uint32_t>(str_data_ptr, acl_data_type, buf);
  112. } else if (type_id == TypeId::kNumberTypeInt64) {
  113. PrintScalarToString<int64_t>(str_data_ptr, acl_data_type, buf);
  114. } else if (type_id == TypeId::kNumberTypeUInt64) {
  115. PrintScalarToString<uint64_t>(str_data_ptr, acl_data_type, buf);
  116. } else if (type_id == TypeId::kNumberTypeFloat16) {
  117. PrintScalarToString<float16>(str_data_ptr, acl_data_type, buf);
  118. } else if (type_id == TypeId::kNumberTypeFloat32) {
  119. PrintScalarToString<float>(str_data_ptr, acl_data_type, buf);
  120. } else if (type_id == TypeId::kNumberTypeFloat64) {
  121. PrintScalarToString<double>(str_data_ptr, acl_data_type, buf);
  122. } else {
  123. MS_LOG(EXCEPTION) << "Cannot print scalar because of unsupported data type: " << GetParseType(acl_data_type) << ".";
  124. }
  125. }
  126. bool judgeLengthValid(const size_t str_len, const aclDataType &acl_data_type) {
  127. auto type_iter = acl_data_type_size_map.find(acl_data_type);
  128. if (type_iter == acl_data_type_size_map.end()) {
  129. MS_LOG(EXCEPTION) << "type of scalar to print is not support.";
  130. }
  131. return str_len == type_iter->second;
  132. }
  133. bool ConvertDataset2Tensor(acltdtDataset *acl_dataset) {
  134. // Acquire Python GIL
  135. py::gil_scoped_acquire gil_acquire;
  136. std::ostringstream buf;
  137. bool ret_end_sequence = false;
  138. size_t acl_dataset_size = acltdtGetDatasetSize(acl_dataset);
  139. for (size_t i = 0; i < acl_dataset_size; i++) {
  140. acltdtDataItem *item = acltdtGetDataItem(acl_dataset, i);
  141. if (acltdtGetTensorTypeFromItem(item) == ACL_TENSOR_DATA_END_OF_SEQUENCE) {
  142. ret_end_sequence = true;
  143. MS_LOG(INFO) << "end of sequence" << std::endl;
  144. break;
  145. }
  146. size_t dim_num = acltdtGetDimNumFromItem(item);
  147. void *acl_addr = acltdtGetDataAddrFromItem(item);
  148. size_t acl_data_size = acltdtGetDataSizeFromItem(item);
  149. aclDataType acl_data_type = acltdtGetDataTypeFromItem(item);
  150. char *acl_data = reinterpret_cast<char *>(acl_addr);
  151. acl_data = const_cast<char *>(reinterpret_cast<std::string *>(acl_data)->c_str());
  152. MS_EXCEPTION_IF_NULL(acl_data);
  153. ShapeVector tensorShape;
  154. tensorShape.resize(dim_num);
  155. if (acltdtGetDimsFromItem(item, tensorShape.data(), dim_num) != ACL_SUCCESS) {
  156. MS_LOG(ERROR) << "ACL failed to get dim-size from acl channel data";
  157. }
  158. if ((tensorShape.size() == 1 && tensorShape[0] == 0) || tensorShape.size() == 0) {
  159. if (!judgeLengthValid(acl_data_size, acl_data_type)) {
  160. MS_LOG(EXCEPTION) << "Print op receive data length is invalid.";
  161. }
  162. convertDataItem2Scalar(acl_data, acl_data_type, &buf);
  163. continue;
  164. }
  165. if (acl_data_type == ACL_STRING) {
  166. std::string data(reinterpret_cast<const char *>(acl_data), acl_data_size);
  167. buf << data << std::endl;
  168. } else {
  169. auto type_iter = print_acl_data_type_map.find(acl_data_type);
  170. if (type_iter == print_acl_data_type_map.end()) {
  171. MS_LOG(ERROR) << "type of tensor need to print is not support " << GetParseType(acl_data_type);
  172. continue;
  173. }
  174. auto type_id = type_iter->second;
  175. mindspore::tensor::Tensor print_tensor(type_id, tensorShape);
  176. if (PrintTensorToString(acl_data, &print_tensor, acl_data_size)) {
  177. buf << print_tensor.ToStringNoLimit() << std::endl;
  178. }
  179. }
  180. }
  181. std::cout << buf.str() << std::endl;
  182. return ret_end_sequence;
  183. }
  184. bool SaveDataset2File(acltdtDataset *acl_dataset, const std::string &print_file_path, prntpb::Print print,
  185. std::fstream *output) {
  186. bool ret_end_thread = false;
  187. for (size_t i = 0; i < acltdtGetDatasetSize(acl_dataset); i++) {
  188. acltdtDataItem *item = acltdtGetDataItem(acl_dataset, i);
  189. MS_EXCEPTION_IF_NULL(item);
  190. acltdtTensorType acl_tensor_type = acltdtGetTensorTypeFromItem(item);
  191. if (acl_tensor_type == ACL_TENSOR_DATA_END_OF_SEQUENCE) {
  192. MS_LOG(INFO) << "Acl channel received end-of-sequence for print op.";
  193. ret_end_thread = true;
  194. break;
  195. } else if (acl_tensor_type == ACL_TENSOR_DATA_ABNORMAL) {
  196. MS_LOG(INFO) << "Acl channel received abnormal for print op.";
  197. return true;
  198. } else if (acl_tensor_type == ACL_TENSOR_DATA_UNDEFINED) {
  199. MS_LOG(INFO) << "Acl channel received undefined message type for print op.";
  200. return false;
  201. }
  202. prntpb::Print_Value *value = print.add_value();
  203. size_t dim_num = acltdtGetDimNumFromItem(item);
  204. void *acl_addr = acltdtGetDataAddrFromItem(item);
  205. size_t acl_data_size = acltdtGetDataSizeFromItem(item);
  206. aclDataType acl_data_type = acltdtGetDataTypeFromItem(item);
  207. char *acl_data = reinterpret_cast<char *>(acl_addr);
  208. acl_data = const_cast<char *>(reinterpret_cast<std::string *>(acl_data)->c_str());
  209. MS_EXCEPTION_IF_NULL(acl_data);
  210. ShapeVector tensorShape;
  211. tensorShape.resize(dim_num);
  212. if (acltdtGetDimsFromItem(item, tensorShape.data(), dim_num) != ACL_SUCCESS) {
  213. MS_LOG(ERROR) << "ACL failed to get dim-size from acl channel data";
  214. }
  215. if ((tensorShape.size() == 1 && tensorShape[0] == 0) || tensorShape.size() == 0) {
  216. if (!judgeLengthValid(acl_data_size, acl_data_type)) {
  217. MS_LOG(ERROR) << "Print op receive data length is invalid.";
  218. ret_end_thread = true;
  219. }
  220. }
  221. if (acl_data_type == ACL_STRING) {
  222. std::string data(reinterpret_cast<const char *>(acl_data), acl_data_size);
  223. value->set_desc(data);
  224. } else {
  225. auto parse_type = GetParseType(acl_data_type);
  226. prntpb::TensorProto *tensor = value->mutable_tensor();
  227. if (tensorShape.size() > 1 || (tensorShape.size() == 1 && tensorShape[0] != 1)) {
  228. for (const auto &dim : tensorShape) {
  229. tensor->add_dims(static_cast<::google::protobuf::int64>(dim));
  230. }
  231. }
  232. tensor->set_tensor_type(parse_type);
  233. std::string data(reinterpret_cast<const char *>(acl_data), acl_data_size);
  234. tensor->set_tensor_content(data);
  235. }
  236. if (!print.SerializeToOstream(output)) {
  237. MS_LOG(ERROR) << "Save print file:" << print_file_path << " fail.";
  238. ret_end_thread = true;
  239. break;
  240. }
  241. print.Clear();
  242. }
  243. return ret_end_thread;
  244. }
  245. void TensorPrint::operator()() {
  246. prntpb::Print print;
  247. if (print_file_path_ == "") {
  248. while (true) {
  249. acltdtDataset *acl_dataset = acltdtCreateDataset();
  250. if (acl_dataset == nullptr) {
  251. MS_LOG(ERROR) << "Failed to create acl dateaset.";
  252. break;
  253. }
  254. if (acltdtReceiveTensor(acl_handle_, acl_dataset, -1 /* no timeout */) != ACL_SUCCESS) {
  255. MS_LOG(ERROR) << "AclHandle failed to receive tensor.";
  256. break;
  257. }
  258. if (ConvertDataset2Tensor(acl_dataset)) {
  259. break;
  260. }
  261. }
  262. } else {
  263. ChangeFileMode(print_file_path_, S_IWUSR);
  264. std::fstream output(print_file_path_, std::ios::out | std::ios::trunc | std::ios::binary);
  265. while (true) {
  266. acltdtDataset *acl_dataset = acltdtCreateDataset();
  267. if (acl_dataset == nullptr) {
  268. MS_LOG(ERROR) << "Failed to create acl dateaset.";
  269. break;
  270. }
  271. if (acltdtReceiveTensor(acl_handle_, acl_dataset, -1 /* no timeout */) != ACL_SUCCESS) {
  272. MS_LOG(ERROR) << "Acltdt failed to receive tensor.";
  273. break;
  274. }
  275. if (SaveDataset2File(acl_dataset, print_file_path_, print, &output)) {
  276. break;
  277. }
  278. }
  279. output.close();
  280. ChangeFileMode(print_file_path_, S_IRUSR);
  281. }
  282. }
  283. #endif
  284. } // namespace mindspore