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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /**
  2. * Copyright 2021-2022 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 "plugin/device/gpu/hal/device/gpu_device_address.h"
  24. #include "debug/data_dump/dump_json_parser.h"
  25. #ifdef ENABLE_D
  26. #include "debug/dump_data_builder.h"
  27. #endif
  28. #include "backend/common/session/anf_runtime_algorithm.h"
  29. #include "kernel/kernel.h"
  30. #include "debug/data_dump/e2e_dump.h"
  31. #include "utils/config_manager.h"
  32. #include "backend/common/session/session_basic.h"
  33. constexpr int kFailure = 1;
  34. using mindspore::kernel::AddressPtr;
  35. using mindspore::kernel::KernelLaunchInfo;
  36. using AddressPtrList = std::vector<mindspore::kernel::AddressPtr>;
  37. using KernelGraph = mindspore::session::KernelGraph;
  38. using AnfAlgo = mindspore::session::AnfRuntimeAlgorithm;
  39. namespace mindspore {
  40. /*
  41. * Feature group: Online debugger.
  42. * Target device group: GPU.
  43. * Runtime category: MindRT.
  44. * Description: Returns a vector containing real output number.
  45. */
  46. std::vector<size_t> CheckRealOutput(const std::string &node_name, const size_t &output_size) {
  47. std::vector<size_t> real_outputs;
  48. // P.BatchNorm is used for training and inference
  49. // can add the filter list for more operators here....
  50. if (node_name == "BatchNorm") {
  51. MS_LOG(INFO) << "loading node named " << node_name;
  52. (void)real_outputs.insert(real_outputs.end(), {0, 3, 4});
  53. } else {
  54. // by default, TensorLoader will load all outputs
  55. for (size_t j = 0; j < output_size; ++j) {
  56. real_outputs.push_back(j);
  57. }
  58. }
  59. return real_outputs;
  60. }
  61. /*
  62. * Feature group: Dump, Online debugger.
  63. * Target device group: GPU.
  64. * Runtime category: MindRT.
  65. * Description: Get kernel inputs from launch_info and load the inputs from device to host.
  66. */
  67. void LoadInputs(const CNodePtr &cnode, const KernelLaunchInfo *launch_info, uint32_t exec_order, uint32_t root_graph_id,
  68. const DeviceContext *device_context) {
  69. // get inputs
  70. auto kernel_inputs = launch_info->inputs_;
  71. auto input_size = AnfAlgo::GetInputTensorNum(cnode);
  72. for (size_t j = 0; j < input_size; ++j) {
  73. auto input_kernel = cnode->input(j + 1);
  74. std::string input_kernel_name = GetKernelNodeName(input_kernel);
  75. auto addr = kernel_inputs[j];
  76. auto type = AnfAlgo::GetOutputInferDataType(input_kernel, PARAMETER_OUTPUT_INDEX);
  77. // For example, this happens with the Depend op
  78. if (type == kMetaTypeNone) {
  79. continue;
  80. }
  81. auto format = kOpFormat_DEFAULT;
  82. auto device_addr = device_context->CreateDeviceAddress(addr->addr, addr->size, format, type);
  83. string input_tensor_name = input_kernel_name + ':' + "0";
  84. ShapeVector int_shapes = trans::GetRuntimePaddingShape(input_kernel, PARAMETER_OUTPUT_INDEX);
  85. auto ret =
  86. device_addr->LoadMemToHost(input_tensor_name, exec_order, format, int_shapes, type, 0, true, root_graph_id);
  87. if (!ret) {
  88. MS_LOG(ERROR) << "LoadMemToHost:"
  89. << ", tensor_name:" << input_tensor_name << ", host_format:" << format << ".!";
  90. }
  91. }
  92. }
  93. /*
  94. * Feature group: Dump, Online debugger.
  95. * Target device group: GPU.
  96. * Runtime category: MindRT.
  97. * Description: Get kernel outputs from launch_info and load the inputs from device to host.
  98. */
  99. void LoadOutputs(const CNodePtr &cnode, const KernelLaunchInfo *launch_info, uint32_t exec_order,
  100. uint32_t root_graph_id, const DeviceContext *device_context) {
  101. // get outputs
  102. auto kernel_outputs = launch_info->outputs_;
  103. auto output_size = AnfAlgo::GetOutputTensorNum(cnode);
  104. auto node_name = AnfAlgo::GetCNodeName(cnode);
  105. std::string kernel_name = GetKernelNodeName(cnode);
  106. std::vector<size_t> real_outputs = CheckRealOutput(node_name, output_size);
  107. for (size_t j : real_outputs) {
  108. auto addr = kernel_outputs[j];
  109. auto type = AnfAlgo::GetOutputInferDataType(cnode, j);
  110. // For example, this happens with the Depend op
  111. if (type == kMetaTypeNone) {
  112. continue;
  113. }
  114. auto format = kOpFormat_DEFAULT;
  115. auto device_addr = device_context->CreateDeviceAddress(addr->addr, addr->size, format, type);
  116. string tensor_name = kernel_name + ':' + std::to_string(j);
  117. ShapeVector int_shapes = trans::GetRuntimePaddingShape(cnode, j);
  118. auto ret = device_addr->LoadMemToHost(tensor_name, exec_order, format, int_shapes, type, j, false, root_graph_id);
  119. if (!ret) {
  120. MS_LOG(ERROR) << "LoadMemToHost:"
  121. << ", tensor_name:" << tensor_name << ", host_format:" << format << ".!";
  122. }
  123. }
  124. }
  125. /*
  126. * Feature group: Dump, Online debugger.
  127. * Target device group: Ascend, GPU.
  128. * Runtime category: MindRT.
  129. * Description: Returns true if the node needs to be read for Dump or online debugger. This function is used by GPU
  130. * and Ascend kernel-by-kernel mindRT.
  131. */
  132. bool CheckReadData(const CNodePtr &cnode) {
  133. auto debugger = Debugger::GetInstance();
  134. if (!debugger) {
  135. return false;
  136. }
  137. bool read_data = false;
  138. auto &dump_json_parser = DumpJsonParser::GetInstance();
  139. bool dump_enabled = dump_json_parser.DumpEnabledForIter();
  140. MS_LOG(DEBUG) << "dump_enabled: " << dump_enabled;
  141. std::string kernel_name = GetKernelNodeName(cnode);
  142. if (dump_enabled) {
  143. if (dump_json_parser.NeedDump(kernel_name)) {
  144. read_data = true;
  145. }
  146. } else if (debugger->debugger_enabled()) {
  147. read_data = debugger->ReadNodeDataRequired(cnode);
  148. }
  149. return read_data;
  150. }
  151. bool IsDeviceTargetGPU() {
  152. auto context = MsContext::GetInstance();
  153. MS_EXCEPTION_IF_NULL(context);
  154. return context->get_param<std::string>(MS_CTX_DEVICE_TARGET) == kGPUDevice;
  155. }
  156. /*
  157. * Feature group: Dump, Online debugger.
  158. * Target device group: Ascend, GPU.
  159. * Runtime category: MindRT.
  160. * Description: Load inputs and outputs of the given node if needed and dump them if dump is enabled, then it performs
  161. * PostExecuteNode function on the given node for GPU.
  162. */
  163. void ReadDataAndDump(const CNodePtr &cnode, const KernelLaunchInfo *launch_info, uint32_t exec_order,
  164. const DeviceContext *device_context) {
  165. auto debugger = Debugger::GetInstance();
  166. if (!debugger) {
  167. return;
  168. }
  169. auto &dump_json_parser = DumpJsonParser::GetInstance();
  170. bool dump_enabled = dump_json_parser.DumpEnabledForIter();
  171. MS_LOG(DEBUG) << "dump_enabled: " << dump_enabled;
  172. auto kernel_graph = std::dynamic_pointer_cast<KernelGraph>(cnode->func_graph());
  173. MS_EXCEPTION_IF_NULL(kernel_graph);
  174. auto root_graph_id = kernel_graph->root_graph_id();
  175. if (debugger->debugger_enabled() || dump_json_parser.InputNeedDump()) {
  176. LoadInputs(cnode, launch_info, exec_order, root_graph_id, device_context);
  177. }
  178. if (debugger->debugger_enabled() || dump_json_parser.OutputNeedDump()) {
  179. LoadOutputs(cnode, launch_info, exec_order, root_graph_id, device_context);
  180. }
  181. // Dump kernel
  182. if (dump_enabled) {
  183. MS_EXCEPTION_IF_NULL(kernel_graph);
  184. auto graph_id = kernel_graph->graph_id();
  185. debugger->DumpSingleNode(cnode, graph_id);
  186. // Clear Dumped data when online debugger is not enabled
  187. if (!debugger->debugger_enabled()) {
  188. debugger->ClearCurrentData();
  189. }
  190. }
  191. if (IsDeviceTargetGPU()) {
  192. // check if the node is last kernel
  193. bool last_kernel = !AnfAlgo::IsInplaceNode(cnode, "skip");
  194. debugger->PostExecuteNode(cnode, last_kernel);
  195. }
  196. }
  197. /*
  198. * Feature group: Dump, Online Debugger.
  199. * Target device group: Ascend, GPU.
  200. * Runtime category: MindRT.
  201. * Description: Returns the error_info when sink_mode is true and we are in online debugger mode or dump mode for
  202. * GPU, if everything is normal the error_info string will be empty.
  203. */
  204. std::string CheckDatasetSinkMode(const KernelGraphPtr &graph_ptr) {
  205. std::string error_info = "";
  206. bool sink_mode = ConfigManager::GetInstance().dataset_mode() || graph_ptr->IsDatasetGraph();
  207. auto debugger = Debugger::GetInstance();
  208. if (debugger->CheckDebuggerDumpEnabled() && sink_mode && IsDeviceTargetGPU()) {
  209. error_info = "e2e_dump is not supported on GPU with dataset_sink_mode=True. Please set dataset_sink_mode=False";
  210. }
  211. if (debugger->CheckDebuggerEnabled() && sink_mode) {
  212. error_info = "Debugger is not supported with dataset_sink_mode=True. Please set dataset_sink_mode=False";
  213. }
  214. return error_info;
  215. }
  216. /*
  217. * Feature group: Online Debugger.
  218. * Target device group: Ascend.
  219. * Runtime category: MindRT.
  220. * Description: Loads graph's outputs and parameters for Ascend super kernel mode.
  221. */
  222. void LoadDataForDebugger(const KernelGraphPtr &graph_ptr) {
  223. auto context = MsContext::GetInstance();
  224. MS_EXCEPTION_IF_NULL(context);
  225. if (context->get_param<std::string>(MS_CTX_DEVICE_TARGET) != kAscendDevice) {
  226. return;
  227. }
  228. #ifdef ENABLE_DEBUGGER
  229. auto debugger = Debugger::GetInstance();
  230. MS_EXCEPTION_IF_NULL(debugger);
  231. if (!debugger->CheckDebuggerEnabled()) {
  232. return;
  233. }
  234. MS_LOG(INFO) << "Start load step";
  235. debugger->SetGraphPtr(graph_ptr);
  236. // load output
  237. debugger->LoadGraphOutputs();
  238. // load parameters
  239. debugger->LoadParametersAndConst();
  240. #endif
  241. }
  242. void Dump(const KernelGraphPtr &graph, uint32_t rank_id) {
  243. MS_LOG(DEBUG) << "Start!";
  244. MS_EXCEPTION_IF_NULL(graph);
  245. E2eDump::DumpData(graph.get(), rank_id);
  246. MS_LOG(DEBUG) << "Finish!";
  247. }
  248. uint32_t GetRankID() {
  249. uint32_t rank_id = 0;
  250. auto ms_context = MsContext::GetInstance();
  251. MS_EXCEPTION_IF_NULL(ms_context);
  252. auto env_rank_id = common::GetEnv("RANK_ID");
  253. if (ms_context->get_param<bool>(MS_CTX_ENABLE_HCCL) && !env_rank_id.empty()) {
  254. // get actual rank id if it's distribution training case.
  255. rank_id = GetRankId();
  256. }
  257. return rank_id;
  258. }
  259. void SuperKernelE2eDump(const KernelGraphPtr &graph) {
  260. #ifndef ENABLE_SECURITY
  261. Dump(graph, GetRankID());
  262. #endif
  263. }
  264. #ifdef ENABLE_D
  265. /*
  266. * Feature group: Dump.
  267. * Target device group: Ascend.
  268. * Runtime category: Old runtime, MindRT.
  269. * Description: It is a function to be registered to Adx server for a + m dump feature with the following steps:
  270. * 1) Merge chunks into one memory segment after receiving all the data for one node.
  271. * 2) Parse dump data object.
  272. * 3) Convert data from device to host format.
  273. * 4) Dump to disk based on configuration.
  274. */
  275. int32_t DumpDataCallBack(const DumpChunk *dump_chunk, int32_t size) {
  276. MS_LOG(DEBUG) << "ADX DumpDataCallBack is called";
  277. string file_name = dump_chunk->fileName;
  278. uint32_t isLastChunk = dump_chunk->isLastChunk;
  279. // parse chunk header
  280. auto debugger = Debugger::GetInstance();
  281. MS_EXCEPTION_IF_NULL(debugger);
  282. auto dump_data_build = debugger->LoadDumpDataBuilder(file_name);
  283. if (dump_data_build == nullptr) {
  284. MS_LOG(ERROR) << "Failed to load dump data builder for node " << file_name;
  285. return 0;
  286. }
  287. if (!dump_data_build->CopyDumpChunk(dump_chunk)) {
  288. return 1;
  289. }
  290. if (isLastChunk == 1) {
  291. // construct dump data object
  292. debugger::dump::DumpData dump_data;
  293. std::vector<char> data_buf;
  294. if (!dump_data_build->ConstructDumpData(&dump_data, &data_buf)) {
  295. MS_LOG(ERROR) << "Failed to parse data for node " << file_name;
  296. return 0;
  297. }
  298. // convert and save to files
  299. auto separator = file_name.rfind("/");
  300. auto path_name = file_name.substr(0, separator);
  301. auto file_base_name = file_name.substr(separator + 1);
  302. if (file_base_name.rfind("Opdebug.Node_OpDebug.") == 0) {
  303. // save overflow data
  304. E2eDump::DumpOpDebugToFile(file_name, dump_data, data_buf.data());
  305. } else {
  306. // save tensor data
  307. // generate fully qualified file name
  308. // before: op_type.op_name.task_id.stream_id.timestamp
  309. // after: op_type.op_name_no_scope.task_id.stream_id.timestamp
  310. size_t first_dot = file_base_name.find(".");
  311. size_t second_dot = file_base_name.size();
  312. const int kNumDots = 3;
  313. int nth_dot_from_back = 0;
  314. while (nth_dot_from_back != kNumDots && second_dot != std::string::npos) {
  315. second_dot = file_base_name.rfind(".", second_dot - 1);
  316. nth_dot_from_back++;
  317. }
  318. if (first_dot == std::string::npos || second_dot == std::string::npos) {
  319. MS_LOG(ERROR) << "Failed to generate fully qualified file name for " << file_name;
  320. return 0;
  321. }
  322. auto op_type = file_base_name.substr(0, first_dot);
  323. auto task_stream_timestamp = file_base_name.substr(second_dot);
  324. std::string op_name = dump_data.op_name();
  325. auto op_name_no_scope = GetOpNameWithoutScope(op_name, "/");
  326. E2eDump::DumpTensorToFile(path_name + "/" + op_type + "." + op_name_no_scope + task_stream_timestamp, dump_data,
  327. data_buf.data());
  328. }
  329. debugger->ClearDumpDataBuilder(file_name);
  330. }
  331. return 0;
  332. }
  333. #endif
  334. } // namespace mindspore