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 8.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**
  2. * Copyright 2020 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 <thread>
  19. #include <fstream>
  20. #include <memory>
  21. #include <string>
  22. #include <vector>
  23. #include "ir/tensor.h"
  24. #include "device/convert_tensor_utils.h"
  25. #include "./securec.h"
  26. #ifndef NO_DLIB
  27. #include "tdt/tsd_client.h"
  28. #include "tdt/tdt_host_interface.h"
  29. #include "tdt/data_common.h"
  30. #endif
  31. namespace mindspore {
  32. const char kShapeSeperator[] = ",";
  33. const char kShapeScalar[] = "[0]";
  34. const char kShapeNone[] = "[]";
  35. static std::map<std::string, TypeId> print_type_map = {
  36. {"int8_t", TypeId::kNumberTypeInt8}, {"uint8_t", TypeId::kNumberTypeUInt8},
  37. {"int16_t", TypeId::kNumberTypeInt16}, {"uint16_t", TypeId::kNumberTypeUInt16},
  38. {"int32_t", TypeId::kNumberTypeInt32}, {"uint32_t", TypeId::kNumberTypeUInt32},
  39. {"int64_t", TypeId::kNumberTypeInt64}, {"uint64_t", TypeId::kNumberTypeUInt64},
  40. {"float16", TypeId::kNumberTypeFloat16}, {"float", TypeId::kNumberTypeFloat32},
  41. {"double", TypeId::kNumberTypeFloat64}, {"bool", TypeId::kNumberTypeBool}};
  42. static std::map<std::string, size_t> type_size_map = {
  43. {"int8_t", sizeof(int8_t)}, {"uint8_t", sizeof(uint8_t)}, {"int16_t", sizeof(int16_t)},
  44. {"uint16_t", sizeof(uint16_t)}, {"int32_t", sizeof(int32_t)}, {"uint32_t", sizeof(uint32_t)},
  45. {"int64_t", sizeof(int64_t)}, {"uint64_t", sizeof(uint64_t)}, {"float16", sizeof(float) / 2},
  46. {"float", sizeof(float)}, {"double", sizeof(double)}, {"bool", sizeof(bool)}};
  47. bool ParseTensorShape(const std::string &input_shape_str, std::vector<int> *const tensor_shape, size_t *dims) {
  48. if (tensor_shape == nullptr) {
  49. return false;
  50. }
  51. MS_EXCEPTION_IF_NULL(dims);
  52. std::string shape_str = input_shape_str;
  53. if (shape_str.size() <= 2) {
  54. return false;
  55. }
  56. (void)shape_str.erase(shape_str.begin());
  57. shape_str.pop_back();
  58. shape_str += kShapeSeperator;
  59. string::size_type pos_begin = 0;
  60. string::size_type pos_end = shape_str.find(kShapeSeperator);
  61. while (pos_end != std::string::npos) {
  62. string dim_str = shape_str.substr(pos_begin, pos_end - pos_begin);
  63. tensor_shape->emplace_back(std::stoi(dim_str));
  64. (*dims) = (*dims) * std::stoul(dim_str);
  65. pos_begin = pos_end + sizeof(kShapeSeperator) - 1;
  66. pos_end = shape_str.find(kShapeSeperator, pos_begin);
  67. }
  68. return true;
  69. }
  70. bool PrintTensorToString(const char *str_data_ptr, mindspore::tensor::Tensor *const print_tensor,
  71. const size_t &memory_size) {
  72. MS_EXCEPTION_IF_NULL(str_data_ptr);
  73. MS_EXCEPTION_IF_NULL(print_tensor);
  74. auto *tensor_data_ptr = static_cast<uint8_t *>(print_tensor->data_c(true));
  75. MS_EXCEPTION_IF_NULL(tensor_data_ptr);
  76. auto cp_ret =
  77. memcpy_s(tensor_data_ptr, static_cast<size_t>(print_tensor->data().nbytes()), str_data_ptr, memory_size);
  78. if (cp_ret != EOK) {
  79. MS_LOG(ERROR) << "Print op Failed to copy the memory to py::tensor " << cp_ret;
  80. return false;
  81. }
  82. return true;
  83. }
  84. template <typename T>
  85. void PrintScalarToString(const char *str_data_ptr, const string &tensor_type, std::ostringstream *const buf) {
  86. MS_EXCEPTION_IF_NULL(str_data_ptr);
  87. MS_EXCEPTION_IF_NULL(buf);
  88. const T *data_ptr = reinterpret_cast<const T *>(str_data_ptr);
  89. *buf << "Tensor shape:[1] " << tensor_type;
  90. *buf << "\nval:";
  91. *buf << *data_ptr << "\n";
  92. }
  93. void PrintScalarToBoolString(const char *str_data_ptr, const string &tensor_type, std::ostringstream *const buf) {
  94. MS_EXCEPTION_IF_NULL(str_data_ptr);
  95. MS_EXCEPTION_IF_NULL(buf);
  96. const bool *data_ptr = reinterpret_cast<const bool *>(str_data_ptr);
  97. *buf << "Tensor shape:[1] " << tensor_type;
  98. *buf << "\nval:";
  99. if (*data_ptr) {
  100. *buf << "True\n";
  101. } else {
  102. *buf << "False\n";
  103. }
  104. }
  105. void convertDataItem2Scalar(const char *str_data_ptr, const string &tensor_type, std::ostringstream *const buf) {
  106. MS_EXCEPTION_IF_NULL(str_data_ptr);
  107. MS_EXCEPTION_IF_NULL(buf);
  108. auto type_iter = print_type_map.find(tensor_type);
  109. auto type_id = type_iter->second;
  110. if (type_id == TypeId::kNumberTypeBool) {
  111. PrintScalarToBoolString(str_data_ptr, tensor_type, buf);
  112. } else if (type_id == TypeId::kNumberTypeInt8) {
  113. PrintScalarToString<int8_t>(str_data_ptr, tensor_type, buf);
  114. } else if (type_id == TypeId::kNumberTypeUInt8) {
  115. PrintScalarToString<uint8_t>(str_data_ptr, tensor_type, buf);
  116. } else if (type_id == TypeId::kNumberTypeInt16) {
  117. PrintScalarToString<int16_t>(str_data_ptr, tensor_type, buf);
  118. } else if (type_id == TypeId::kNumberTypeUInt16) {
  119. PrintScalarToString<uint16_t>(str_data_ptr, tensor_type, buf);
  120. } else if (type_id == TypeId::kNumberTypeInt32) {
  121. PrintScalarToString<int32_t>(str_data_ptr, tensor_type, buf);
  122. } else if (type_id == TypeId::kNumberTypeUInt32) {
  123. PrintScalarToString<uint32_t>(str_data_ptr, tensor_type, buf);
  124. } else if (type_id == TypeId::kNumberTypeInt64) {
  125. PrintScalarToString<int64_t>(str_data_ptr, tensor_type, buf);
  126. } else if (type_id == TypeId::kNumberTypeUInt64) {
  127. PrintScalarToString<uint64_t>(str_data_ptr, tensor_type, buf);
  128. } else if (type_id == TypeId::kNumberTypeFloat16) {
  129. PrintScalarToString<float16>(str_data_ptr, tensor_type, buf);
  130. } else if (type_id == TypeId::kNumberTypeFloat32) {
  131. PrintScalarToString<float>(str_data_ptr, tensor_type, buf);
  132. } else if (type_id == TypeId::kNumberTypeFloat64) {
  133. PrintScalarToString<double>(str_data_ptr, tensor_type, buf);
  134. } else {
  135. MS_LOG(EXCEPTION) << "Cannot print scalar because of unsupport data type: " << tensor_type << ".";
  136. }
  137. } // namespace mindspore
  138. bool judgeLengthValid(const size_t str_len, const string &tensor_type) {
  139. auto type_iter = type_size_map.find(tensor_type);
  140. if (type_iter == type_size_map.end()) {
  141. MS_LOG(EXCEPTION) << "type of scalar to print is not support.";
  142. }
  143. return str_len == type_iter->second;
  144. }
  145. #ifndef NO_DLIB
  146. bool ConvertDataItem2Tensor(const std::vector<tdt::DataItem> &items) {
  147. // Acquire Python GIL
  148. py::gil_scoped_acquire gil_acquire;
  149. std::ostringstream buf;
  150. bool ret_end_sequence = false;
  151. for (auto &item : items) {
  152. if (item.dataType_ == tdt::TDT_END_OF_SEQUENCE) {
  153. ret_end_sequence = true;
  154. break;
  155. }
  156. std::shared_ptr<std::string> str_data_ptr = std::static_pointer_cast<std::string>(item.dataPtr_);
  157. MS_EXCEPTION_IF_NULL(str_data_ptr);
  158. if (item.tensorShape_ == kShapeScalar || item.tensorShape_ == kShapeNone) {
  159. if (!judgeLengthValid(str_data_ptr->size(), item.tensorType_)) {
  160. MS_LOG(EXCEPTION) << "Print op receive data length is invalid.";
  161. }
  162. convertDataItem2Scalar(str_data_ptr->data(), item.tensorType_, &buf);
  163. continue;
  164. }
  165. std::vector<int> tensor_shape;
  166. size_t totaldims = 1;
  167. if (!ParseTensorShape(item.tensorShape_, &tensor_shape, &totaldims)) {
  168. MS_LOG(ERROR) << "Tensor print can not parse tensor shape, receive info" << item.tensorShape_;
  169. continue;
  170. }
  171. if (item.tensorType_ == "string") {
  172. std::string data(reinterpret_cast<const char *>(str_data_ptr->c_str()), item.dataLen_);
  173. buf << data << std::endl;
  174. } else {
  175. auto type_iter = print_type_map.find(item.tensorType_);
  176. if (type_iter == print_type_map.end()) {
  177. MS_LOG(ERROR) << "type of tensor need to print is not support " << item.tensorType_;
  178. continue;
  179. }
  180. auto type_id = type_iter->second;
  181. mindspore::tensor::Tensor print_tensor(type_id, tensor_shape);
  182. auto memory_size = totaldims * type_size_map[item.tensorType_];
  183. if (PrintTensorToString(str_data_ptr->data(), &print_tensor, memory_size)) {
  184. buf << print_tensor.ToStringRepr() << std::endl;
  185. }
  186. }
  187. }
  188. std::cout << buf.str() << std::endl;
  189. return ret_end_sequence;
  190. }
  191. void TensorPrint::operator()() {
  192. while (true) {
  193. std::vector<tdt::DataItem> bundle;
  194. if (tdt::TdtHostPopData("_npu_log", bundle) != 0) {
  195. break;
  196. }
  197. if (ConvertDataItem2Tensor(bundle)) {
  198. break;
  199. }
  200. }
  201. }
  202. #endif
  203. } // namespace mindspore