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

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