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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 = device_addr->LoadMemToHost(input_tensor_name, UintToInt(exec_order), format, int_shapes, type, 0, true,
  86. 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 =
  119. device_addr->LoadMemToHost(tensor_name, UintToInt(exec_order), format, int_shapes, type, j, false, root_graph_id);
  120. if (!ret) {
  121. MS_LOG(ERROR) << "LoadMemToHost:"
  122. << ", tensor_name:" << tensor_name << ", host_format:" << format << ".!";
  123. }
  124. }
  125. }
  126. /*
  127. * Feature group: Dump, Online debugger.
  128. * Target device group: Ascend, GPU.
  129. * Runtime category: MindRT.
  130. * Description: Returns true if the node needs to be read for Dump or online debugger. This function is used by GPU
  131. * and Ascend kernel-by-kernel mindRT.
  132. */
  133. bool CheckReadData(const CNodePtr &cnode) {
  134. auto debugger = Debugger::GetInstance();
  135. if (!debugger) {
  136. return false;
  137. }
  138. bool read_data = false;
  139. auto &dump_json_parser = DumpJsonParser::GetInstance();
  140. bool dump_enabled = dump_json_parser.DumpEnabledForIter();
  141. MS_LOG(DEBUG) << "dump_enabled: " << dump_enabled;
  142. std::string kernel_name = GetKernelNodeName(cnode);
  143. if (dump_enabled) {
  144. if (dump_json_parser.NeedDump(kernel_name)) {
  145. read_data = true;
  146. }
  147. } else if (debugger->debugger_enabled()) {
  148. read_data = debugger->ReadNodeDataRequired(cnode);
  149. }
  150. return read_data;
  151. }
  152. bool IsDeviceTargetGPU() {
  153. auto context = MsContext::GetInstance();
  154. MS_EXCEPTION_IF_NULL(context);
  155. return context->get_param<std::string>(MS_CTX_DEVICE_TARGET) == kGPUDevice;
  156. }
  157. /*
  158. * Feature group: Dump, Online debugger.
  159. * Target device group: Ascend, GPU.
  160. * Runtime category: MindRT.
  161. * Description: Load inputs and outputs of the given node if needed and dump them if dump is enabled, then it performs
  162. * PostExecuteNode function on the given node for GPU.
  163. */
  164. void ReadDataAndDump(const CNodePtr &cnode, const KernelLaunchInfo *launch_info, uint32_t exec_order,
  165. const DeviceContext *device_context) {
  166. auto debugger = Debugger::GetInstance();
  167. if (!debugger) {
  168. return;
  169. }
  170. auto &dump_json_parser = DumpJsonParser::GetInstance();
  171. bool dump_enabled = dump_json_parser.DumpEnabledForIter();
  172. MS_LOG(DEBUG) << "dump_enabled: " << dump_enabled;
  173. auto kernel_graph = std::dynamic_pointer_cast<KernelGraph>(cnode->func_graph());
  174. MS_EXCEPTION_IF_NULL(kernel_graph);
  175. auto root_graph_id = kernel_graph->root_graph_id();
  176. if (debugger->debugger_enabled() || dump_json_parser.InputNeedDump()) {
  177. LoadInputs(cnode, launch_info, exec_order, root_graph_id, device_context);
  178. }
  179. if (debugger->debugger_enabled() || dump_json_parser.OutputNeedDump()) {
  180. LoadOutputs(cnode, launch_info, exec_order, root_graph_id, device_context);
  181. }
  182. // Dump kernel
  183. if (dump_enabled) {
  184. MS_EXCEPTION_IF_NULL(kernel_graph);
  185. auto graph_id = kernel_graph->graph_id();
  186. // for GPU, nodes are dumped in graph_id directory.
  187. if (IsDeviceTargetGPU()) {
  188. debugger->DumpSingleNode(cnode, graph_id);
  189. } else {
  190. // for Ascend, node are dumped in root_graph_id directory.
  191. debugger->DumpSingleNode(cnode, root_graph_id, launch_info);
  192. }
  193. // Clear Dumped data when online debugger is not enabled
  194. if (!debugger->debugger_enabled()) {
  195. debugger->ClearCurrentData();
  196. }
  197. }
  198. if (IsDeviceTargetGPU()) {
  199. // check if the node is last kernel
  200. bool last_kernel = !AnfAlgo::IsInplaceNode(cnode, "skip");
  201. debugger->PostExecuteNode(cnode, last_kernel);
  202. }
  203. }
  204. /*
  205. * Feature group: Dump, Online Debugger.
  206. * Target device group: Ascend, GPU.
  207. * Runtime category: MindRT.
  208. * Description: Returns the error_info when sink_mode is true and we are in online debugger mode or dump mode for
  209. * GPU, if everything is normal the error_info string will be empty.
  210. */
  211. std::string CheckDatasetSinkMode(const KernelGraphPtr &graph_ptr) {
  212. std::string error_info = "";
  213. bool sink_mode = ConfigManager::GetInstance().dataset_mode() || graph_ptr->IsDatasetGraph();
  214. auto debugger = Debugger::GetInstance();
  215. if (debugger->CheckDebuggerDumpEnabled() && sink_mode && IsDeviceTargetGPU()) {
  216. error_info = "e2e_dump is not supported on GPU with dataset_sink_mode=True. Please set dataset_sink_mode=False";
  217. }
  218. if (debugger->CheckDebuggerEnabled() && sink_mode) {
  219. error_info = "Debugger is not supported with dataset_sink_mode=True. Please set dataset_sink_mode=False";
  220. }
  221. return error_info;
  222. }
  223. /*
  224. * Feature group: Online Debugger.
  225. * Target device group: Ascend.
  226. * Runtime category: MindRT.
  227. * Description: Loads graph's outputs and parameters for Ascend super kernel mode.
  228. */
  229. void LoadDataForDebugger(const KernelGraphPtr &graph_ptr) {
  230. auto context = MsContext::GetInstance();
  231. MS_EXCEPTION_IF_NULL(context);
  232. if (context->get_param<std::string>(MS_CTX_DEVICE_TARGET) != kAscendDevice) {
  233. return;
  234. }
  235. #ifdef ENABLE_DEBUGGER
  236. auto debugger = Debugger::GetInstance();
  237. MS_EXCEPTION_IF_NULL(debugger);
  238. if (!debugger->CheckDebuggerEnabled()) {
  239. return;
  240. }
  241. MS_LOG(INFO) << "Start load step";
  242. debugger->SetGraphPtr(graph_ptr);
  243. // load output
  244. debugger->LoadGraphOutputs();
  245. // load parameters
  246. debugger->LoadParametersAndConst();
  247. #endif
  248. }
  249. void Dump(const KernelGraphPtr &graph, uint32_t rank_id) {
  250. MS_LOG(DEBUG) << "Start!";
  251. MS_EXCEPTION_IF_NULL(graph);
  252. E2eDump::DumpData(graph.get(), rank_id);
  253. MS_LOG(DEBUG) << "Finish!";
  254. }
  255. uint32_t GetRankID() {
  256. uint32_t rank_id = 0;
  257. auto ms_context = MsContext::GetInstance();
  258. MS_EXCEPTION_IF_NULL(ms_context);
  259. auto env_rank_id = common::GetEnv("RANK_ID");
  260. if (ms_context->get_param<bool>(MS_CTX_ENABLE_HCCL) && !env_rank_id.empty()) {
  261. // get actual rank id if it's distribution training case.
  262. rank_id = GetRankId();
  263. }
  264. return rank_id;
  265. }
  266. void SuperKernelE2eDump(const KernelGraphPtr &graph) {
  267. #ifndef ENABLE_SECURITY
  268. Dump(graph, GetRankID());
  269. #endif
  270. }
  271. #ifdef ENABLE_D
  272. /*
  273. * Feature group: Dump.
  274. * Target device group: Ascend.
  275. * Runtime category: Old runtime, MindRT.
  276. * Description: It is a function to be registered to Adx server for a + m dump feature with the following steps:
  277. * 1) Merge chunks into one memory segment after receiving all the data for one node.
  278. * 2) Parse dump data object.
  279. * 3) Convert data from device to host format.
  280. * 4) Dump to disk based on configuration.
  281. */
  282. int32_t DumpDataCallBack(const DumpChunk *dump_chunk, int32_t size) {
  283. MS_LOG(DEBUG) << "ADX DumpDataCallBack is called";
  284. MS_LOG(DEBUG) << "The dump_chunk size is: " << size;
  285. string file_name = dump_chunk->fileName;
  286. uint32_t isLastChunk = dump_chunk->isLastChunk;
  287. // parse chunk header
  288. auto debugger = Debugger::GetInstance();
  289. MS_EXCEPTION_IF_NULL(debugger);
  290. auto dump_data_build = debugger->LoadDumpDataBuilder(file_name);
  291. if (dump_data_build == nullptr) {
  292. MS_LOG(ERROR) << "Failed to load dump data builder for node " << file_name;
  293. return 0;
  294. }
  295. if (!dump_data_build->CopyDumpChunk(dump_chunk)) {
  296. return 1;
  297. }
  298. if (isLastChunk == 1) {
  299. // construct dump data object
  300. debugger::dump::DumpData dump_data;
  301. std::vector<char> data_buf;
  302. if (!dump_data_build->ConstructDumpData(&dump_data, &data_buf)) {
  303. MS_LOG(ERROR) << "Failed to parse data for node " << file_name;
  304. return 0;
  305. }
  306. // convert and save to files
  307. auto separator = file_name.rfind("/");
  308. auto path_name = file_name.substr(0, separator);
  309. auto file_base_name = file_name.substr(separator + 1);
  310. if (file_base_name.rfind("Opdebug.Node_OpDebug.") == 0) {
  311. // save overflow data
  312. E2eDump::DumpOpDebugToFile(file_name, dump_data, data_buf.data());
  313. } else {
  314. // save tensor data
  315. // generate fully qualified file name
  316. // before: op_type.op_name.task_id.stream_id.timestamp
  317. // after: op_type.op_name_no_scope.task_id.stream_id.timestamp
  318. size_t first_dot = file_base_name.find(".");
  319. size_t second_dot = file_base_name.size();
  320. const int kNumDots = 3;
  321. int nth_dot_from_back = 0;
  322. while (nth_dot_from_back != kNumDots && second_dot != std::string::npos) {
  323. second_dot = file_base_name.rfind(".", second_dot - 1);
  324. nth_dot_from_back++;
  325. }
  326. if (first_dot == std::string::npos || second_dot == std::string::npos) {
  327. MS_LOG(ERROR) << "Failed to generate fully qualified file name for " << file_name;
  328. return 0;
  329. }
  330. auto op_type = file_base_name.substr(0, first_dot);
  331. auto task_stream_timestamp = file_base_name.substr(second_dot);
  332. std::string op_name = dump_data.op_name();
  333. auto op_name_no_scope = GetOpNameWithoutScope(op_name, "/");
  334. E2eDump::DumpTensorToFile(path_name + "/" + op_type + "." + op_name_no_scope + task_stream_timestamp, dump_data,
  335. data_buf.data());
  336. }
  337. debugger->ClearDumpDataBuilder(file_name);
  338. }
  339. return 0;
  340. }
  341. #endif
  342. } // namespace mindspore