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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. #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. std::vector<size_t> CheckRealOutput(const std::string &node_name, const size_t &output_size) {
  41. // define a vector containing real output number
  42. std::vector<size_t> real_outputs;
  43. // P.BatchNorm is used for training and inference
  44. // can add the filter list for more operators here....
  45. if (node_name == "BatchNorm") {
  46. MS_LOG(INFO) << "loading node named " << node_name;
  47. (void)real_outputs.insert(real_outputs.end(), {0, 3, 4});
  48. } else {
  49. // by default, TensorLoader will load all outputs
  50. for (size_t j = 0; j < output_size; ++j) {
  51. real_outputs.push_back(j);
  52. }
  53. }
  54. return real_outputs;
  55. }
  56. void LoadInputs(const CNodePtr &cnode, const KernelLaunchInfo *launch_info_, uint32_t exec_order_,
  57. uint32_t root_graph_id) {
  58. // get inputs
  59. auto kernel_inputs = launch_info_->inputs_;
  60. auto input_size = AnfAlgo::GetInputTensorNum(cnode);
  61. for (size_t j = 0; j < input_size; ++j) {
  62. auto input_kernel = cnode->input(j + 1);
  63. std::string input_kernel_name = GetKernelNodeName(input_kernel);
  64. auto addr = kernel_inputs[j];
  65. auto type = AnfAlgo::GetOutputInferDataType(input_kernel, PARAMETER_OUTPUT_INDEX);
  66. // For example, this happens with the Depend op
  67. if (type == kMetaTypeNone) {
  68. continue;
  69. }
  70. #ifdef ENABLE_GPU
  71. auto format = kOpFormat_DEFAULT;
  72. auto gpu_addr = std::make_unique<device::gpu::GPUDeviceAddress>(addr->addr, addr->size, format, type);
  73. string input_tensor_name = input_kernel_name + ':' + "0";
  74. ShapeVector int_shapes = trans::GetRuntimePaddingShape(input_kernel, PARAMETER_OUTPUT_INDEX);
  75. auto ret =
  76. gpu_addr->LoadMemToHost(input_tensor_name, exec_order_, format, int_shapes, type, 0, true, root_graph_id);
  77. if (!ret) {
  78. MS_LOG(ERROR) << "LoadMemToHost:"
  79. << ", tensor_name:" << input_tensor_name << ", host_format:" << format << ".!";
  80. }
  81. #endif
  82. }
  83. }
  84. void LoadOutputs(const CNodePtr &cnode, const KernelLaunchInfo *launch_info_, uint32_t exec_order_,
  85. uint32_t root_graph_id) {
  86. // get outputs
  87. auto kernel_outputs = launch_info_->outputs_;
  88. auto output_size = AnfAlgo::GetOutputTensorNum(cnode);
  89. auto node_name = AnfAlgo::GetCNodeName(cnode);
  90. std::string kernel_name = GetKernelNodeName(cnode);
  91. std::vector<size_t> real_outputs = CheckRealOutput(node_name, output_size);
  92. for (size_t j : real_outputs) {
  93. auto addr = kernel_outputs[j];
  94. auto type = AnfAlgo::GetOutputInferDataType(cnode, j);
  95. // For example, this happens with the Depend op
  96. if (type == kMetaTypeNone) {
  97. continue;
  98. }
  99. #ifdef ENABLE_GPU
  100. auto format = kOpFormat_DEFAULT;
  101. auto gpu_addr = std::make_unique<device::gpu::GPUDeviceAddress>(addr->addr, addr->size, format, type);
  102. string tensor_name = kernel_name + ':' + std::to_string(j);
  103. ShapeVector int_shapes = trans::GetRuntimePaddingShape(cnode, j);
  104. auto ret = gpu_addr->LoadMemToHost(tensor_name, exec_order_, format, int_shapes, type, j, false, root_graph_id);
  105. if (!ret) {
  106. MS_LOG(ERROR) << "LoadMemToHost:"
  107. << ", tensor_name:" << tensor_name << ", host_format:" << format << ".!";
  108. }
  109. #endif
  110. }
  111. }
  112. bool CheckReadData(const CNodePtr &cnode) {
  113. auto debugger = Debugger::GetInstance();
  114. if (!debugger) {
  115. return false;
  116. }
  117. bool read_data = false;
  118. auto &dump_json_parser = DumpJsonParser::GetInstance();
  119. bool dump_enabled = dump_json_parser.DumpEnabledForIter();
  120. MS_LOG(DEBUG) << "dump_enabled: " << dump_enabled;
  121. std::string kernel_name = GetKernelNodeName(cnode);
  122. if (dump_enabled) {
  123. if (dump_json_parser.NeedDump(kernel_name)) {
  124. read_data = true;
  125. }
  126. } else if (debugger->debugger_enabled()) {
  127. read_data = debugger->ReadNodeDataRequired(cnode);
  128. }
  129. return read_data;
  130. }
  131. void ReadDataAndDump(const CNodePtr &cnode, const KernelLaunchInfo *launch_info_, uint32_t exec_order_) {
  132. auto debugger = Debugger::GetInstance();
  133. if (!debugger) {
  134. return;
  135. }
  136. auto &dump_json_parser = DumpJsonParser::GetInstance();
  137. bool dump_enabled = debugger->DumpDataEnabledIteration();
  138. auto kernel_graph = std::dynamic_pointer_cast<KernelGraph>(cnode->func_graph());
  139. MS_EXCEPTION_IF_NULL(kernel_graph);
  140. auto root_graph_id = kernel_graph->root_graph_id();
  141. if (debugger->debugger_enabled() || dump_json_parser.InputNeedDump()) {
  142. LoadInputs(cnode, launch_info_, exec_order_, root_graph_id);
  143. }
  144. if (debugger->debugger_enabled() || dump_json_parser.OutputNeedDump()) {
  145. LoadOutputs(cnode, launch_info_, exec_order_, root_graph_id);
  146. }
  147. // Dump kernel
  148. if (dump_enabled) {
  149. MS_EXCEPTION_IF_NULL(kernel_graph);
  150. auto graph_id = kernel_graph->graph_id();
  151. debugger->DumpSingleNode(cnode, graph_id);
  152. // Clear Dumped data when online debugger is not enabled
  153. if (!debugger->debugger_enabled()) {
  154. debugger->ClearCurrentData();
  155. }
  156. }
  157. // check if the node is last kernel
  158. bool last_kernel = !AnfAlgo::IsInplaceNode(cnode, "skip");
  159. debugger->PostExecuteNode(cnode, last_kernel);
  160. }
  161. void ReadDataAndDumpAscend(const CNodePtr &cnode, 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 = dump_json_parser.DumpEnabledForIter();
  168. MS_LOG(DEBUG) << "dump_enabled: " << dump_enabled;
  169. auto kernel_graph = std::dynamic_pointer_cast<KernelGraph>(cnode->func_graph());
  170. MS_EXCEPTION_IF_NULL(kernel_graph);
  171. auto root_graph_id = kernel_graph->root_graph_id();
  172. debugger->LoadNodeOutputs(cnode, exec_order_, root_graph_id);
  173. // Dump kernel
  174. if (dump_enabled) {
  175. MS_EXCEPTION_IF_NULL(kernel_graph);
  176. auto graph_id = kernel_graph->graph_id();
  177. debugger->DumpSingleNode(cnode, graph_id);
  178. // Clear Dumped data when online debugger is not enabled
  179. if (!debugger->debugger_enabled()) {
  180. debugger->ClearCurrentData();
  181. }
  182. }
  183. }
  184. std::string CheckDatasetSinkMode(const KernelGraphPtr &graph_ptr) {
  185. std::string error_info = "";
  186. bool sink_mode = ConfigManager::GetInstance().dataset_mode() || graph_ptr->IsDatasetGraph();
  187. auto debugger = Debugger::GetInstance();
  188. auto context = MsContext::GetInstance();
  189. MS_EXCEPTION_IF_NULL(context);
  190. bool is_gpu = (context->get_param<std::string>(MS_CTX_DEVICE_TARGET) == kGPUDevice);
  191. if (debugger->CheckDebuggerDumpEnabled() && sink_mode && is_gpu) {
  192. error_info = "e2e_dump is not supported on GPU with dataset_sink_mode=True. Please set dataset_sink_mode=False";
  193. }
  194. if (debugger->CheckDebuggerEnabled() && sink_mode) {
  195. error_info = "Debugger is not supported with dataset_sink_mode=True. Please set dataset_sink_mode=False";
  196. }
  197. return error_info;
  198. }
  199. void LoadDataForDebugger(const KernelGraphPtr &graph_ptr) {
  200. auto context = MsContext::GetInstance();
  201. MS_EXCEPTION_IF_NULL(context);
  202. if (context->get_param<std::string>(MS_CTX_DEVICE_TARGET) != kAscendDevice) {
  203. return;
  204. }
  205. #ifdef ENABLE_DEBUGGER
  206. auto debugger = Debugger::GetInstance();
  207. MS_EXCEPTION_IF_NULL(debugger);
  208. if (!debugger->CheckDebuggerEnabled()) {
  209. return;
  210. }
  211. MS_LOG(INFO) << "Start load step";
  212. debugger->SetGraphPtr(graph_ptr);
  213. // load output
  214. debugger->LoadGraphOutputs();
  215. // load parameters
  216. debugger->LoadParametersAndConst();
  217. #endif
  218. }
  219. void DumpSetup(const KernelGraphPtr &graph) {
  220. MS_LOG(DEBUG) << "Start!";
  221. MS_EXCEPTION_IF_NULL(graph);
  222. E2eDump::DumpSetup(graph.get());
  223. MS_LOG(DEBUG) << "Finish!";
  224. }
  225. void Dump(const KernelGraphPtr &graph, uint32_t rank_id) {
  226. MS_LOG(DEBUG) << "Start!";
  227. MS_EXCEPTION_IF_NULL(graph);
  228. E2eDump::DumpRunIter(graph, rank_id);
  229. E2eDump::DumpData(graph.get(), rank_id);
  230. MS_LOG(DEBUG) << "Finish!";
  231. }
  232. uint32_t GetRankID() {
  233. uint32_t rank_id = 0;
  234. auto ms_context = MsContext::GetInstance();
  235. MS_EXCEPTION_IF_NULL(ms_context);
  236. auto env_rank_id = common::GetEnv("RANK_ID");
  237. if (ms_context->get_param<bool>(MS_CTX_ENABLE_HCCL) && !env_rank_id.empty()) {
  238. // get actual rank id if it's distribution training case.
  239. rank_id = GetRankId();
  240. }
  241. return rank_id;
  242. }
  243. void SuperKernelE2eDump(const KernelGraphPtr &graph) {
  244. #ifndef ENABLE_SECURITY
  245. Dump(graph, GetRankID());
  246. DumpSetup(graph);
  247. #endif
  248. }
  249. #ifdef ENABLE_D
  250. int32_t DumpDataCallBack(const DumpChunk *dump_chunk, int32_t size) {
  251. MS_LOG(DEBUG) << "ADX DumpDataCallBack is called";
  252. string file_name = dump_chunk->fileName;
  253. uint32_t isLastChunk = dump_chunk->isLastChunk;
  254. // parse chunk header
  255. auto debugger = Debugger::GetInstance();
  256. MS_EXCEPTION_IF_NULL(debugger);
  257. auto dump_data_build = debugger->LoadDumpDataBuilder(file_name);
  258. if (dump_data_build == nullptr) {
  259. MS_LOG(ERROR) << "Failed to load dump data builder for node " << file_name;
  260. return 0;
  261. }
  262. if (!dump_data_build->CopyDumpChunk(dump_chunk)) {
  263. return 1;
  264. }
  265. if (isLastChunk == 1) {
  266. // construct dump data object
  267. debugger::dump::DumpData dump_data;
  268. std::vector<char> data_buf;
  269. if (!dump_data_build->ConstructDumpData(&dump_data, &data_buf)) {
  270. MS_LOG(ERROR) << "Failed to parse data for node " << file_name;
  271. return 0;
  272. }
  273. // convert and save to files
  274. auto separator = file_name.rfind("/");
  275. auto path_name = file_name.substr(0, separator);
  276. auto file_base_name = file_name.substr(separator + 1);
  277. if (file_base_name.rfind("Opdebug.Node_OpDebug.") == 0) {
  278. // save overflow data
  279. E2eDump::DumpOpDebugToFile(file_name, dump_data, data_buf.data());
  280. } else {
  281. // save tensor data
  282. // generate fully qualified file name
  283. // before: op_type.op_name.task_id.stream_id.timestamp
  284. // after: op_type.op_name_no_scope.task_id.stream_id.timestamp
  285. size_t first_dot = file_base_name.find(".");
  286. size_t second_dot = file_base_name.size();
  287. const int kNumDots = 3;
  288. int nth_dot_from_back = 0;
  289. while (nth_dot_from_back != kNumDots && second_dot != std::string::npos) {
  290. second_dot = file_base_name.rfind(".", second_dot - 1);
  291. nth_dot_from_back++;
  292. }
  293. if (first_dot == std::string::npos || second_dot == std::string::npos) {
  294. MS_LOG(ERROR) << "Failed to generate fully qualified file name for " << file_name;
  295. return 0;
  296. }
  297. auto op_type = file_base_name.substr(0, first_dot);
  298. auto task_stream_timestamp = file_base_name.substr(second_dot);
  299. std::string op_name = dump_data.op_name();
  300. auto op_name_no_scope = GetOpNameWithoutScope(op_name, "/");
  301. E2eDump::DumpTensorToFile(path_name + "/" + op_type + "." + op_name_no_scope + task_stream_timestamp, dump_data,
  302. data_buf.data());
  303. }
  304. debugger->ClearDumpDataBuilder(file_name);
  305. }
  306. return 0;
  307. }
  308. #endif
  309. } // namespace mindspore