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.

debugger_utils.cc 6.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/debugger/debugger_utils.h"
  17. #include <iostream>
  18. #include <vector>
  19. #include <memory>
  20. #include <string>
  21. #include "debug/anf_ir_utils.h"
  22. #include "debug/debugger/debugger.h"
  23. #include "runtime/device/gpu/gpu_device_address.h"
  24. #include "debug/data_dump/dump_json_parser.h"
  25. #include "backend/session/anf_runtime_algorithm.h"
  26. #include "backend/kernel_compiler/kernel.h"
  27. using mindspore::kernel::AddressPtr;
  28. using mindspore::kernel::KernelLaunchInfo;
  29. using AddressPtrList = std::vector<mindspore::kernel::AddressPtr>;
  30. using KernelGraph = mindspore::session::KernelGraph;
  31. using AnfAlgo = mindspore::session::AnfRuntimeAlgorithm;
  32. namespace mindspore {
  33. static const size_t PARAMETER_OUTPUT_INDEX = 0;
  34. std::vector<size_t> CheckRealOutput(const std::string &node_name, const size_t &output_size) {
  35. // define a vector containing real output number
  36. std::vector<size_t> real_outputs;
  37. // P.BatchNorm is used for training and inference
  38. // can add the filter list for more operators here....
  39. if (node_name == "BatchNorm") {
  40. MS_LOG(INFO) << "loading node named " << node_name;
  41. (void)real_outputs.insert(real_outputs.end(), {0, 3, 4});
  42. } else {
  43. // by default, TensorLoader will load all outputs
  44. for (size_t j = 0; j < output_size; ++j) {
  45. real_outputs.push_back(j);
  46. }
  47. }
  48. return real_outputs;
  49. }
  50. void LoadInputs(const CNodePtr &cnode, const KernelLaunchInfo *launch_info_, uint32_t exec_order_) {
  51. // get inputs
  52. auto kernel_inputs = launch_info_->inputs_;
  53. auto input_size = AnfAlgo::GetInputTensorNum(cnode);
  54. for (size_t j = 0; j < input_size; ++j) {
  55. auto input_kernel = cnode->input(j + 1);
  56. std::string input_kernel_name = GetKernelNodeName(input_kernel);
  57. auto addr = kernel_inputs[j];
  58. auto type = AnfAlgo::GetOutputInferDataType(input_kernel, PARAMETER_OUTPUT_INDEX);
  59. // For example, this happens with the Depend op
  60. if (type == kMetaTypeNone) {
  61. continue;
  62. }
  63. #ifdef ENABLE_GPU
  64. auto format = kOpFormat_DEFAULT;
  65. auto gpu_addr = std::make_unique<device::gpu::GPUDeviceAddress>(addr->addr, addr->size, format, type);
  66. string input_tensor_name = input_kernel_name + ':' + "0";
  67. ShapeVector int_shapes = trans::GetRuntimePaddingShape(input_kernel, PARAMETER_OUTPUT_INDEX);
  68. auto ret = gpu_addr->LoadMemToHost(input_tensor_name, exec_order_, format, int_shapes, type, 0, true);
  69. if (!ret) {
  70. MS_LOG(ERROR) << "LoadMemToHost:"
  71. << ", tensor_name:" << input_tensor_name << ", host_format:" << format << ".!";
  72. }
  73. #endif
  74. }
  75. }
  76. void LoadOutputs(const CNodePtr &cnode, const KernelLaunchInfo *launch_info_, uint32_t exec_order_) {
  77. // get outputs
  78. auto kernel_outputs = launch_info_->outputs_;
  79. auto output_size = AnfAlgo::GetOutputTensorNum(cnode);
  80. auto node_name = AnfAlgo::GetCNodeName(cnode);
  81. std::string kernel_name = GetKernelNodeName(cnode);
  82. std::vector<size_t> real_outputs = CheckRealOutput(node_name, output_size);
  83. for (size_t j : real_outputs) {
  84. auto addr = kernel_outputs[j];
  85. auto type = AnfAlgo::GetOutputInferDataType(cnode, j);
  86. // For example, this happens with the Depend op
  87. if (type == kMetaTypeNone) {
  88. continue;
  89. }
  90. #ifdef ENABLE_GPU
  91. auto format = kOpFormat_DEFAULT;
  92. auto gpu_addr = std::make_unique<device::gpu::GPUDeviceAddress>(addr->addr, addr->size, format, type);
  93. string tensor_name = kernel_name + ':' + std::to_string(j);
  94. ShapeVector int_shapes = trans::GetRuntimePaddingShape(cnode, j);
  95. auto ret = gpu_addr->LoadMemToHost(tensor_name, exec_order_, format, int_shapes, type, j, false);
  96. if (!ret) {
  97. MS_LOG(ERROR) << "LoadMemToHost:"
  98. << ", tensor_name:" << tensor_name << ", host_format:" << format << ".!";
  99. }
  100. #endif
  101. }
  102. }
  103. bool CheckReadData(const CNodePtr &cnode) {
  104. auto debugger = Debugger::GetInstance();
  105. if (!debugger) {
  106. return false;
  107. }
  108. bool read_data = false;
  109. auto &dump_json_parser = DumpJsonParser::GetInstance();
  110. bool dump_enabled = debugger->DumpDataEnabledIteration();
  111. std::string kernel_name = GetKernelNodeName(cnode);
  112. if (dump_enabled) {
  113. auto dump_mode = dump_json_parser.dump_mode();
  114. // dump the node if dump_mode is 0, which means all kernels, or if this kernel is in the kernels list
  115. if ((dump_mode == 0) || ((dump_mode == 1) && dump_json_parser.NeedDump(kernel_name))) {
  116. read_data = true;
  117. }
  118. } else if (debugger->debugger_enabled()) {
  119. read_data = debugger->ReadNodeDataRequired(cnode);
  120. }
  121. return read_data;
  122. }
  123. void ReadDataAndDump(const CNodePtr &cnode, const KernelLaunchInfo *launch_info_, uint32_t exec_order_) {
  124. auto debugger = Debugger::GetInstance();
  125. if (!debugger) {
  126. return;
  127. }
  128. auto &dump_json_parser = DumpJsonParser::GetInstance();
  129. bool dump_enabled = debugger->DumpDataEnabledIteration();
  130. if (debugger->debugger_enabled() || dump_json_parser.InputNeedDump()) {
  131. LoadInputs(cnode, launch_info_, exec_order_);
  132. }
  133. if (debugger->debugger_enabled() || dump_json_parser.OutputNeedDump()) {
  134. LoadOutputs(cnode, launch_info_, exec_order_);
  135. }
  136. // Dump kernel
  137. if (dump_enabled) {
  138. auto kernel_graph = std::dynamic_pointer_cast<KernelGraph>(cnode->func_graph());
  139. MS_EXCEPTION_IF_NULL(kernel_graph);
  140. auto graph_id = kernel_graph->graph_id();
  141. debugger->DumpSingleNode(cnode, graph_id);
  142. // Clear Dumped data when online debugger is not enabled
  143. if (!debugger->debugger_enabled()) {
  144. debugger->ClearCurrentData();
  145. }
  146. }
  147. // check if the node is last kernel
  148. bool last_kernel = !AnfAlgo::IsInplaceNode(cnode, "skip");
  149. debugger->PostExecuteNode(cnode, last_kernel);
  150. }
  151. } // namespace mindspore