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 5.4 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. namespace mindspore {
  27. uint32_t ConvertPhysicalDeviceId(uint32_t device_id) {
  28. auto context = MsContext::GetInstance();
  29. MS_EXCEPTION_IF_NULL(context);
  30. auto device_target = context->get_param<std::string>(MS_CTX_DEVICE_TARGET);
  31. auto kernel_runtime = device::KernelRuntimeManager::Instance().GetSingleKernelRuntime(device_target, device_id);
  32. MS_EXCEPTION_IF_NULL(kernel_runtime);
  33. return kernel_runtime->device_id();
  34. }
  35. std::string GenerateDumpPath(uint32_t graph_id, uint32_t rank_id) {
  36. auto &dump_json_parser = DumpJsonParser::GetInstance();
  37. std::string net_name = dump_json_parser.net_name();
  38. std::string iterator = std::to_string(dump_json_parser.cur_dump_iter());
  39. std::string dump_path = dump_json_parser.path();
  40. if (dump_path.back() != '/') {
  41. dump_path += "/";
  42. }
  43. dump_path += ("rank_" + std::to_string(rank_id) + "/" + net_name + "/" + std::to_string(graph_id) + "/" + iterator);
  44. return dump_path;
  45. }
  46. void GetFileKernelName(NotNull<std::string *> kernel_name) {
  47. const std::string strsrc = "/";
  48. const std::string strdst = "--";
  49. std::string::size_type pos = 0;
  50. std::string::size_type srclen = strsrc.size();
  51. std::string::size_type dstlen = strdst.size();
  52. while ((pos = kernel_name->find(strsrc, pos)) != std::string::npos) {
  53. kernel_name->replace(pos, srclen, strdst);
  54. pos += dstlen;
  55. }
  56. }
  57. void SetConstNodeId(const AnfNodePtr &node, std::map<std::string, size_t> *const_map) {
  58. MS_EXCEPTION_IF_NULL(node);
  59. if (!node->isa<ValueNode>()) {
  60. return;
  61. }
  62. std::string node_name = GetKernelNodeName(node);
  63. MS_EXCEPTION_IF_NULL(const_map);
  64. auto iter = const_map->find(node_name);
  65. if (iter == const_map->end()) {
  66. auto const_idx = const_map->size() + 1;
  67. (*const_map)[node_name] = const_idx;
  68. }
  69. }
  70. void GetCNodeConstantId(const CNodePtr &node, std::map<std::string, size_t> *const_map) {
  71. MS_EXCEPTION_IF_NULL(node);
  72. auto &inputs = node->inputs();
  73. if (inputs.empty()) {
  74. MS_LOG(EXCEPTION) << "Inputs of apply node is empty";
  75. }
  76. AnfNodePtr op = inputs[0];
  77. // CNode/ConstGraph/Const/Parameter
  78. MS_EXCEPTION_IF_NULL(op);
  79. if (op->isa<CNode>() || IsValueNode<FuncGraph>(op) || op->isa<Parameter>()) {
  80. MS_LOG(WARNING) << "Operator must be a primitive.";
  81. } else {
  82. // process OP inputs
  83. for (size_t i = 1; i < inputs.size(); ++i) {
  84. SetConstNodeId(inputs[i], const_map);
  85. }
  86. }
  87. }
  88. void GetConstantId(const session::KernelGraph *graph, std::map<std::string, size_t> *const_map) {
  89. MS_EXCEPTION_IF_NULL(graph);
  90. std::vector<AnfNodePtr> nodes = TopoSort(graph->get_return(), SuccIncoming, AlwaysInclude);
  91. for (const AnfNodePtr &node : nodes) {
  92. MS_EXCEPTION_IF_NULL(node);
  93. if (!node->isa<CNode>()) {
  94. continue;
  95. }
  96. auto cnode = node->cast<CNodePtr>();
  97. MS_EXCEPTION_IF_NULL(cnode);
  98. if (cnode != graph->get_return()) {
  99. GetCNodeConstantId(cnode, const_map);
  100. } else {
  101. SetConstNodeId(cnode->input(1), const_map);
  102. }
  103. }
  104. }
  105. void GetDumpIntShape(const AnfNodePtr &node, size_t index, NotNull<ShapeVector *> int_shapes, bool trans_flag) {
  106. if (trans_flag) {
  107. *int_shapes = trans::GetRuntimePaddingShape(node, index);
  108. } else {
  109. auto shape = AnfAlgo::GetOutputDeviceShape(node, index);
  110. (void)std::transform(shape.begin(), shape.end(), std::back_inserter(*int_shapes),
  111. [](size_t inner_item) { return SizeToInt(inner_item); });
  112. }
  113. }
  114. void DumpMemToFile(const std::string &file_path, const device::DeviceAddress &addr, const ShapeVector &int_shapes,
  115. const TypeId &type, bool trans_flag) {
  116. auto format = kOpFormat_DEFAULT;
  117. auto ret = addr.DumpMemToFile(file_path, format, int_shapes, type, trans_flag);
  118. if (!ret) {
  119. MS_LOG(ERROR) << "DumpMemToFile Failed: flag:" << trans_flag << ", path:" << file_path << ", host_format:" << format
  120. << ".!";
  121. }
  122. }
  123. uint64_t GetTimeStamp() {
  124. auto cur_sys_time = std::chrono::system_clock::now();
  125. uint64_t timestamp = std::chrono::duration_cast<std::chrono::microseconds>(cur_sys_time.time_since_epoch()).count();
  126. return timestamp;
  127. }
  128. std::string GetOpNameWithoutScope(const std::string &fullname_with_scope) {
  129. const std::string separator("--");
  130. std::size_t found = fullname_with_scope.rfind(separator);
  131. std::string op_name;
  132. if (found != std::string::npos) {
  133. op_name = fullname_with_scope.substr(found + separator.length());
  134. }
  135. return op_name;
  136. }
  137. } // namespace mindspore