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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. std::string GetParseType(const std::string &tensorType_) {
  48. static const std::map<std::string, std::string> print_parse_map = {
  49. {"int8_t", "Int8"}, {"uint8_t", "Uint8"}, {"int16_t", "Int16"}, {"uint16_t", "Uint16"},
  50. {"int32_t", "Int32"}, {"uint32_t", "Uint32"}, {"int64_t", "Int64"}, {"uint64_t", "Uint64"},
  51. {"float16", "Float16"}, {"float", "Float32"}, {"double", "Float64"}, {"bool", "Bool"}};
  52. auto type_iter = print_parse_map.find(tensorType_);
  53. if (type_iter == print_parse_map.end()) {
  54. MS_LOG(EXCEPTION) << "type of tensor need to print is not support " << tensorType_;
  55. }
  56. return type_iter->second;
  57. }
  58. bool ParseTensorShape(const std::string &input_shape_str, std::vector<int> *const tensor_shape, size_t *dims) {
  59. if (tensor_shape == nullptr) {
  60. return false;
  61. }
  62. MS_EXCEPTION_IF_NULL(dims);
  63. std::string shape_str = input_shape_str;
  64. if (shape_str.size() <= 2) {
  65. return false;
  66. }
  67. (void)shape_str.erase(shape_str.begin());
  68. shape_str.pop_back();
  69. shape_str += kShapeSeperator;
  70. string::size_type pos_begin = 0;
  71. string::size_type pos_end = shape_str.find(kShapeSeperator);
  72. while (pos_end != std::string::npos) {
  73. string dim_str = shape_str.substr(pos_begin, pos_end - pos_begin);
  74. tensor_shape->emplace_back(std::stoi(dim_str));
  75. (*dims) = (*dims) * std::stoul(dim_str);
  76. pos_begin = pos_end + sizeof(kShapeSeperator) - 1;
  77. pos_end = shape_str.find(kShapeSeperator, pos_begin);
  78. }
  79. return true;
  80. }
  81. bool PrintTensorToString(const char *str_data_ptr, mindspore::tensor::Tensor *const print_tensor,
  82. const size_t &memory_size) {
  83. MS_EXCEPTION_IF_NULL(str_data_ptr);
  84. MS_EXCEPTION_IF_NULL(print_tensor);
  85. auto *tensor_data_ptr = static_cast<uint8_t *>(print_tensor->data_c());
  86. MS_EXCEPTION_IF_NULL(tensor_data_ptr);
  87. auto cp_ret =
  88. memcpy_s(tensor_data_ptr, static_cast<size_t>(print_tensor->data().nbytes()), str_data_ptr, memory_size);
  89. if (cp_ret != EOK) {
  90. MS_LOG(ERROR) << "Print op Failed to copy the memory to py::tensor " << cp_ret;
  91. return false;
  92. }
  93. return true;
  94. }
  95. template <typename T>
  96. void PrintScalarToString(const char *str_data_ptr, const string &tensor_type, std::ostringstream *const buf) {
  97. MS_EXCEPTION_IF_NULL(str_data_ptr);
  98. MS_EXCEPTION_IF_NULL(buf);
  99. const T *data_ptr = reinterpret_cast<const T *>(str_data_ptr);
  100. *buf << "Tensor shape:[1] " << tensor_type;
  101. *buf << "\nval:";
  102. *buf << *data_ptr << "\n";
  103. }
  104. void PrintScalarToBoolString(const char *str_data_ptr, const string &tensor_type, std::ostringstream *const buf) {
  105. MS_EXCEPTION_IF_NULL(str_data_ptr);
  106. MS_EXCEPTION_IF_NULL(buf);
  107. const bool *data_ptr = reinterpret_cast<const bool *>(str_data_ptr);
  108. *buf << "Tensor shape:[1] " << tensor_type;
  109. *buf << "\nval:";
  110. if (*data_ptr) {
  111. *buf << "True\n";
  112. } else {
  113. *buf << "False\n";
  114. }
  115. }
  116. void convertDataItem2Scalar(const char *str_data_ptr, const string &tensor_type, std::ostringstream *const buf) {
  117. MS_EXCEPTION_IF_NULL(str_data_ptr);
  118. MS_EXCEPTION_IF_NULL(buf);
  119. auto type_iter = print_type_map.find(tensor_type);
  120. auto type_id = type_iter->second;
  121. if (type_id == TypeId::kNumberTypeBool) {
  122. PrintScalarToBoolString(str_data_ptr, tensor_type, buf);
  123. } else if (type_id == TypeId::kNumberTypeInt8) {
  124. PrintScalarToString<int8_t>(str_data_ptr, tensor_type, buf);
  125. } else if (type_id == TypeId::kNumberTypeUInt8) {
  126. PrintScalarToString<uint8_t>(str_data_ptr, tensor_type, buf);
  127. } else if (type_id == TypeId::kNumberTypeInt16) {
  128. PrintScalarToString<int16_t>(str_data_ptr, tensor_type, buf);
  129. } else if (type_id == TypeId::kNumberTypeUInt16) {
  130. PrintScalarToString<uint16_t>(str_data_ptr, tensor_type, buf);
  131. } else if (type_id == TypeId::kNumberTypeInt32) {
  132. PrintScalarToString<int32_t>(str_data_ptr, tensor_type, buf);
  133. } else if (type_id == TypeId::kNumberTypeUInt32) {
  134. PrintScalarToString<uint32_t>(str_data_ptr, tensor_type, buf);
  135. } else if (type_id == TypeId::kNumberTypeInt64) {
  136. PrintScalarToString<int64_t>(str_data_ptr, tensor_type, buf);
  137. } else if (type_id == TypeId::kNumberTypeUInt64) {
  138. PrintScalarToString<uint64_t>(str_data_ptr, tensor_type, buf);
  139. } else if (type_id == TypeId::kNumberTypeFloat16) {
  140. PrintScalarToString<float16>(str_data_ptr, tensor_type, buf);
  141. } else if (type_id == TypeId::kNumberTypeFloat32) {
  142. PrintScalarToString<float>(str_data_ptr, tensor_type, buf);
  143. } else if (type_id == TypeId::kNumberTypeFloat64) {
  144. PrintScalarToString<double>(str_data_ptr, tensor_type, buf);
  145. } else {
  146. MS_LOG(EXCEPTION) << "Cannot print scalar because of unsupport data type: " << tensor_type << ".";
  147. }
  148. }
  149. bool judgeLengthValid(const size_t str_len, const string &tensor_type) {
  150. auto type_iter = type_size_map.find(tensor_type);
  151. if (type_iter == type_size_map.end()) {
  152. MS_LOG(EXCEPTION) << "type of scalar to print is not support.";
  153. }
  154. return str_len == type_iter->second;
  155. }
  156. #ifndef NO_DLIB
  157. bool ConvertDataItem2Tensor(const std::vector<tdt::DataItem> &items) {
  158. // Acquire Python GIL
  159. py::gil_scoped_acquire gil_acquire;
  160. std::ostringstream buf;
  161. bool ret_end_sequence = false;
  162. for (auto &item : items) {
  163. if (item.dataType_ == tdt::TDT_END_OF_SEQUENCE) {
  164. ret_end_sequence = true;
  165. break;
  166. }
  167. std::shared_ptr<std::string> str_data_ptr = std::static_pointer_cast<std::string>(item.dataPtr_);
  168. MS_EXCEPTION_IF_NULL(str_data_ptr);
  169. if (item.tensorShape_ == kShapeScalar || item.tensorShape_ == kShapeNone) {
  170. if (!judgeLengthValid(str_data_ptr->size(), item.tensorType_)) {
  171. MS_LOG(EXCEPTION) << "Print op receive data length is invalid.";
  172. }
  173. convertDataItem2Scalar(str_data_ptr->data(), item.tensorType_, &buf);
  174. continue;
  175. }
  176. std::vector<int> tensor_shape;
  177. size_t totaldims = 1;
  178. if (!ParseTensorShape(item.tensorShape_, &tensor_shape, &totaldims)) {
  179. MS_LOG(ERROR) << "Tensor print can not parse tensor shape, receive info" << item.tensorShape_;
  180. continue;
  181. }
  182. if (item.tensorType_ == "string") {
  183. std::string data(reinterpret_cast<const char *>(str_data_ptr->c_str()), item.dataLen_);
  184. buf << data << std::endl;
  185. } else {
  186. auto type_iter = print_type_map.find(item.tensorType_);
  187. if (type_iter == print_type_map.end()) {
  188. MS_LOG(ERROR) << "type of tensor need to print is not support " << item.tensorType_;
  189. continue;
  190. }
  191. auto type_id = type_iter->second;
  192. mindspore::tensor::Tensor print_tensor(type_id, tensor_shape);
  193. auto memory_size = totaldims * type_size_map[item.tensorType_];
  194. if (PrintTensorToString(str_data_ptr->data(), &print_tensor, memory_size)) {
  195. buf << print_tensor.ToStringRepr() << std::endl;
  196. }
  197. }
  198. }
  199. std::cout << buf.str() << std::endl;
  200. return ret_end_sequence;
  201. }
  202. bool SaveDataItem2File(const std::vector<tdt::DataItem> &items, const std::string &print_file_path, prntpb::Print print,
  203. std::fstream *output) {
  204. bool ret_end_thread = false;
  205. for (auto &item : items) {
  206. if (item.dataType_ == tdt::TDT_END_OF_SEQUENCE) {
  207. ret_end_thread = true;
  208. break;
  209. }
  210. prntpb::Print_Value *value = print.add_value();
  211. std::shared_ptr<std::string> str_data_ptr = std::static_pointer_cast<std::string>(item.dataPtr_);
  212. MS_EXCEPTION_IF_NULL(str_data_ptr);
  213. if (item.tensorShape_ == kShapeScalar || item.tensorShape_ == kShapeNone) {
  214. if (!judgeLengthValid(str_data_ptr->size(), item.tensorType_)) {
  215. MS_LOG(ERROR) << "Print op receive data length is invalid.";
  216. ret_end_thread = true;
  217. }
  218. }
  219. std::vector<int> tensor_shape;
  220. size_t totaldims = 1;
  221. if (!ParseTensorShape(item.tensorShape_, &tensor_shape, &totaldims)) {
  222. MS_LOG(ERROR) << "Tensor print can not parse tensor shape, receive info" << item.tensorShape_;
  223. ret_end_thread = true;
  224. }
  225. if (item.tensorType_ == "string") {
  226. std::string data(reinterpret_cast<const char *>(str_data_ptr->c_str()), item.dataLen_);
  227. value->set_desc(data);
  228. } else {
  229. auto parse_type = GetParseType(item.tensorType_);
  230. prntpb::TensorProto *tensor = value->mutable_tensor();
  231. if (!(item.tensorShape_ == kShapeScalar) && !(item.tensorShape_ == kShapeNone)) {
  232. for (const auto &dim : tensor_shape) {
  233. tensor->add_dims(static_cast<::google::protobuf::int64>(dim));
  234. }
  235. }
  236. tensor->set_tensor_type(parse_type);
  237. std::string data(reinterpret_cast<const char *>(str_data_ptr->c_str()), item.dataLen_);
  238. tensor->set_tensor_content(data);
  239. }
  240. if (!print.SerializeToOstream(output)) {
  241. MS_LOG(ERROR) << "Save print file:" << print_file_path << " fail.";
  242. ret_end_thread = true;
  243. }
  244. print.Clear();
  245. }
  246. return ret_end_thread;
  247. }
  248. void TensorPrint::operator()() {
  249. prntpb::Print print;
  250. auto ms_context = MsContext::GetInstance();
  251. MS_EXCEPTION_IF_NULL(ms_context);
  252. std::string print_file_path = ms_context->print_file_path();
  253. if (print_file_path == "") {
  254. while (true) {
  255. std::vector<tdt::DataItem> bundle;
  256. if (tdt::TdtHostPopData("_npu_log", bundle) != 0) {
  257. break;
  258. }
  259. if (ConvertDataItem2Tensor(bundle)) {
  260. break;
  261. }
  262. }
  263. } else {
  264. std::fstream output(print_file_path, std::ios::out | std::ios::trunc | std::ios::binary);
  265. while (true) {
  266. std::vector<tdt::DataItem> bundle;
  267. if (tdt::TdtHostPopData("_npu_log", bundle) != 0) {
  268. break;
  269. }
  270. if (SaveDataItem2File(bundle, print_file_path, print, &output)) {
  271. break;
  272. }
  273. }
  274. output.close();
  275. std::string path_string = print_file_path;
  276. if (chmod(common::SafeCStr(path_string), S_IRUSR) == -1) {
  277. MS_LOG(ERROR) << "Modify file:" << print_file_path << " to r fail.";
  278. return;
  279. }
  280. }
  281. }
  282. #endif
  283. } // namespace mindspore