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

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