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.

dump_utils.cc 6.1 kB

4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * Copyright 2021-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. #include "debug/data_dump/dump_utils.h"
  17. #include <map>
  18. #include <vector>
  19. #include <algorithm>
  20. #include "utils/ms_device_shape_transfer.h"
  21. #include "utils/ms_context.h"
  22. #include "debug/anf_ir_utils.h"
  23. #include "debug/data_dump/dump_json_parser.h"
  24. #include "backend/session/anf_runtime_algorithm.h"
  25. #include "runtime/device/kernel_runtime_manager.h"
  26. #include "utils/utils.h"
  27. #include "debug/common.h"
  28. namespace mindspore {
  29. uint32_t ConvertPhysicalDeviceId(uint32_t device_id) {
  30. auto context = MsContext::GetInstance();
  31. MS_EXCEPTION_IF_NULL(context);
  32. auto device_target = context->get_param<std::string>(MS_CTX_DEVICE_TARGET);
  33. auto kernel_runtime = device::KernelRuntimeManager::Instance().GetSingleKernelRuntime(device_target, device_id);
  34. MS_EXCEPTION_IF_NULL(kernel_runtime);
  35. return kernel_runtime->device_id();
  36. }
  37. /*
  38. * Feature group: Dump.
  39. * Target device group: Ascend, GPU and CPU.
  40. * Runtime category: Old runtime, MindRT.
  41. * Description: Generate dir path to dump data. It will be in these formats:
  42. * 1) tensor/statistic: /dump_path/rank_{rank_id}/{net_name}/{graph_id}/{iter_num}.
  43. * 2) constant data: /dump_path/rank_{rank_id}/{net_name}/{graph_id}/constants/.
  44. */
  45. std::string GenerateDumpPath(uint32_t graph_id, uint32_t rank_id, bool is_cst) {
  46. auto &dump_json_parser = DumpJsonParser::GetInstance();
  47. std::string net_name = dump_json_parser.net_name();
  48. std::string iterator = std::to_string(dump_json_parser.cur_dump_iter());
  49. std::string dump_path = dump_json_parser.path();
  50. if (dump_path.back() != '/') {
  51. dump_path += "/";
  52. }
  53. if (is_cst) {
  54. dump_path += ("rank_" + std::to_string(rank_id) + "/" + net_name + "/" + std::to_string(graph_id) + "/constants/");
  55. } else {
  56. dump_path +=
  57. ("rank_" + std::to_string(rank_id) + "/" + net_name + "/" + std::to_string(graph_id) + "/" + iterator + "/");
  58. }
  59. return dump_path;
  60. }
  61. void GetFileKernelName(NotNull<std::string *> kernel_name) {
  62. const std::string strsrc = "/";
  63. const std::string strdst = "--";
  64. std::string::size_type pos = 0;
  65. std::string::size_type srclen = strsrc.size();
  66. std::string::size_type dstlen = strdst.size();
  67. while ((pos = kernel_name->find(strsrc, pos)) != std::string::npos) {
  68. kernel_name->replace(pos, srclen, strdst);
  69. pos += dstlen;
  70. }
  71. }
  72. /*
  73. * Feature group: Dump.
  74. * Target device group: Ascend, GPU and CPU.
  75. * Runtime category: Old runtime, MindRT.
  76. * Description: Get the actual tensor shape for dumping based on trans_flag option in configuration json file.
  77. */
  78. void GetDumpIntShape(const AnfNodePtr &node, size_t index, NotNull<ShapeVector *> int_shapes, bool trans_flag) {
  79. if (trans_flag) {
  80. *int_shapes = trans::GetRuntimePaddingShape(node, index);
  81. } else {
  82. auto shape = AnfAlgo::GetOutputDeviceShape(node, index);
  83. (void)std::transform(shape.begin(), shape.end(), std::back_inserter(*int_shapes),
  84. [](size_t inner_item) { return SizeToInt(inner_item); });
  85. }
  86. }
  87. /*
  88. * Feature group: Dump.
  89. * Target device group: Ascend, CPU.
  90. * Runtime category: Old runtime, MindRT.
  91. * Description: Dump the data in memory into file path.
  92. */
  93. void DumpMemToFile(const std::string &file_path, const device::DeviceAddress &addr, const ShapeVector &int_shapes,
  94. const TypeId &type, bool trans_flag) {
  95. auto format = kOpFormat_DEFAULT;
  96. auto ret = addr.DumpMemToFile(file_path, format, int_shapes, type, trans_flag);
  97. if (!ret) {
  98. MS_LOG(ERROR) << "DumpMemToFile Failed: flag:" << trans_flag << ", path:" << file_path << ", host_format:" << format
  99. << ".!";
  100. }
  101. }
  102. uint64_t GetTimeStamp() {
  103. auto cur_sys_time = std::chrono::system_clock::now();
  104. uint64_t timestamp = std::chrono::duration_cast<std::chrono::microseconds>(cur_sys_time.time_since_epoch()).count();
  105. return timestamp;
  106. }
  107. /*
  108. * Feature group: Dump.
  109. * Target device group: Ascend, GPU, CPU.
  110. * Runtime category: Old runtime, MindRT.
  111. * Description: Remove scope from operator name. The default separator is "--".
  112. */
  113. std::string GetOpNameWithoutScope(const std::string &fullname_with_scope, const std::string &separator) {
  114. std::size_t found = fullname_with_scope.rfind(separator);
  115. std::string op_name;
  116. if (found != std::string::npos) {
  117. op_name = fullname_with_scope.substr(found + separator.length());
  118. }
  119. return op_name;
  120. }
  121. /*
  122. * Feature group: Dump.
  123. * Target device group: Ascend, GPU, CPU.
  124. * Runtime category: Old runtime, MindRT.
  125. * Description: Dump string content into file path. Current purpose is to save operator overflow information in json
  126. * file in ascend a+m dump mode.
  127. */
  128. void DumpToFile(const std::string &file_name, const std::string &dump_str) {
  129. if (dump_str.empty()) {
  130. MS_LOG(ERROR) << "Failed to dump empty tensor data.";
  131. return;
  132. }
  133. auto real_path = Common::CreatePrefixPath(file_name);
  134. if (!real_path.has_value()) {
  135. MS_LOG(ERROR) << "CreatePrefixPath failed.";
  136. return;
  137. }
  138. std::string real_path_str = real_path.value();
  139. ChangeFileMode(real_path_str, S_IWUSR);
  140. std::ofstream file(real_path_str, std::ofstream::out | std::ofstream::trunc);
  141. if (!file.is_open()) {
  142. MS_LOG(EXCEPTION) << "Open file " << real_path_str << "failed: " << ErrnoToString(errno);
  143. }
  144. file << dump_str;
  145. if (file.bad()) {
  146. file.close();
  147. MS_LOG(EXCEPTION) << "Dump string to file " << real_path_str << " failed: " << ErrnoToString(errno);
  148. }
  149. file.close();
  150. ChangeFileMode(real_path_str, S_IRUSR);
  151. }
  152. } // namespace mindspore