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

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