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.3 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * Copyright 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 "debug/data_dump/dump_utils.h"
  17. #include <map>
  18. #include <vector>
  19. #include <algorithm>
  20. #include "common/trans.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. std::string GenerateDumpPath(uint32_t graph_id, uint32_t rank_id) {
  38. auto &dump_json_parser = DumpJsonParser::GetInstance();
  39. std::string net_name = dump_json_parser.net_name();
  40. std::string iterator = std::to_string(dump_json_parser.cur_dump_iter());
  41. std::string dump_path = dump_json_parser.path();
  42. if (dump_path.back() != '/') {
  43. dump_path += "/";
  44. }
  45. dump_path += ("rank_" + std::to_string(rank_id) + "/" + net_name + "/" + std::to_string(graph_id) + "/" + iterator);
  46. return dump_path;
  47. }
  48. void GetFileKernelName(NotNull<std::string *> kernel_name) {
  49. const std::string strsrc = "/";
  50. const std::string strdst = "--";
  51. std::string::size_type pos = 0;
  52. std::string::size_type srclen = strsrc.size();
  53. std::string::size_type dstlen = strdst.size();
  54. while ((pos = kernel_name->find(strsrc, pos)) != std::string::npos) {
  55. kernel_name->replace(pos, srclen, strdst);
  56. pos += dstlen;
  57. }
  58. }
  59. void SetConstNodeId(const AnfNodePtr &node, std::map<std::string, size_t> *const_map) {
  60. MS_EXCEPTION_IF_NULL(node);
  61. if (!node->isa<ValueNode>()) {
  62. return;
  63. }
  64. std::string node_name = GetKernelNodeName(node);
  65. MS_EXCEPTION_IF_NULL(const_map);
  66. auto iter = const_map->find(node_name);
  67. if (iter == const_map->end()) {
  68. auto const_idx = const_map->size() + 1;
  69. (*const_map)[node_name] = const_idx;
  70. }
  71. }
  72. void GetCNodeConstantId(const CNodePtr &node, std::map<std::string, size_t> *const_map) {
  73. MS_EXCEPTION_IF_NULL(node);
  74. auto &inputs = node->inputs();
  75. if (inputs.empty()) {
  76. MS_LOG(EXCEPTION) << "Inputs of apply node is empty";
  77. }
  78. AnfNodePtr op = inputs[0];
  79. // CNode/ConstGraph/Const/Parameter
  80. MS_EXCEPTION_IF_NULL(op);
  81. if (op->isa<CNode>() || IsValueNode<FuncGraph>(op) || op->isa<Parameter>()) {
  82. MS_LOG(WARNING) << "Operator must be a primitive.";
  83. } else {
  84. // process OP inputs
  85. for (size_t i = 1; i < inputs.size(); ++i) {
  86. SetConstNodeId(inputs[i], const_map);
  87. }
  88. }
  89. }
  90. void GetConstantId(const session::KernelGraph *graph, std::map<std::string, size_t> *const_map) {
  91. MS_EXCEPTION_IF_NULL(graph);
  92. std::vector<AnfNodePtr> nodes = TopoSort(graph->get_return(), SuccIncoming, AlwaysInclude);
  93. for (const AnfNodePtr &node : nodes) {
  94. MS_EXCEPTION_IF_NULL(node);
  95. if (!node->isa<CNode>()) {
  96. continue;
  97. }
  98. auto cnode = node->cast<CNodePtr>();
  99. MS_EXCEPTION_IF_NULL(cnode);
  100. if (cnode != graph->get_return()) {
  101. GetCNodeConstantId(cnode, const_map);
  102. } else {
  103. SetConstNodeId(cnode->input(1), const_map);
  104. }
  105. }
  106. }
  107. void GetDumpIntShape(const AnfNodePtr &node, size_t index, NotNull<ShapeVector *> int_shapes, bool trans_flag) {
  108. if (trans_flag) {
  109. *int_shapes = trans::GetRuntimePaddingShape(node, index);
  110. } else {
  111. auto shape = AnfAlgo::GetOutputDeviceShape(node, index);
  112. (void)std::transform(shape.begin(), shape.end(), std::back_inserter(*int_shapes),
  113. [](size_t inner_item) { return SizeToInt(inner_item); });
  114. }
  115. }
  116. void DumpMemToFile(const std::string &file_path, const device::DeviceAddress &addr, const ShapeVector &int_shapes,
  117. const TypeId &type, bool trans_flag) {
  118. auto format = kOpFormat_DEFAULT;
  119. auto ret = addr.DumpMemToFile(file_path, format, int_shapes, type, trans_flag);
  120. if (!ret) {
  121. MS_LOG(ERROR) << "DumpMemToFile Failed: flag:" << trans_flag << ", path:" << file_path << ", host_format:" << format
  122. << ".!";
  123. }
  124. }
  125. uint64_t GetTimeStamp() {
  126. auto cur_sys_time = std::chrono::system_clock::now();
  127. uint64_t timestamp = std::chrono::duration_cast<std::chrono::microseconds>(cur_sys_time.time_since_epoch()).count();
  128. return timestamp;
  129. }
  130. std::string GetOpNameWithoutScope(const std::string &fullname_with_scope, const std::string &separator) {
  131. std::size_t found = fullname_with_scope.rfind(separator);
  132. std::string op_name;
  133. if (found != std::string::npos) {
  134. op_name = fullname_with_scope.substr(found + separator.length());
  135. }
  136. return op_name;
  137. }
  138. void DumpToFile(const std::string &file_name, const std::string &dump_str) {
  139. if (dump_str.empty()) {
  140. MS_LOG(ERROR) << "Failed to dump empty tensor data.";
  141. return;
  142. }
  143. auto real_path = Common::CreatePrefixPath(file_name);
  144. if (!real_path.has_value()) {
  145. MS_LOG(ERROR) << "CreatePrefixPath failed.";
  146. return;
  147. }
  148. std::string real_path_str = real_path.value();
  149. ChangeFileMode(real_path_str, S_IWUSR);
  150. std::ofstream file(real_path_str, std::ofstream::out | std::ofstream::trunc);
  151. if (!file.is_open()) {
  152. MS_LOG(EXCEPTION) << "Open file " << real_path_str << "failed: " << ErrnoToString(errno);
  153. }
  154. file << dump_str;
  155. if (file.bad()) {
  156. file.close();
  157. MS_LOG(EXCEPTION) << "Dump string to file " << real_path_str << " failed: " << ErrnoToString(errno);
  158. }
  159. file.close();
  160. ChangeFileMode(real_path_str, S_IRUSR);
  161. }
  162. } // namespace mindspore