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

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