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.

e2e_dump.cc 19 kB

4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /**
  2. * Copyright 2020-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/data_dump/e2e_dump.h"
  17. #include <unistd.h>
  18. #include <algorithm>
  19. #include <map>
  20. #include <vector>
  21. #include "debug/data_dump/dump_json_parser.h"
  22. #include "common/trans.h"
  23. #include "debug/anf_ir_utils.h"
  24. #include "debug/common.h"
  25. #include "backend/session/anf_runtime_algorithm.h"
  26. #include "utils/ms_context.h"
  27. #include "runtime/device/kernel_runtime_manager.h"
  28. #include "utils/config_manager.h"
  29. #ifdef ENABLE_DEBUGGER
  30. #include "debug/debug_services.h"
  31. #include "debug/tensor_load.h"
  32. #include "debug/debugger/debugger.h"
  33. #endif
  34. namespace mindspore {
  35. bool E2eDump::IsDeviceTargetGPU() {
  36. auto context = MsContext::GetInstance();
  37. MS_EXCEPTION_IF_NULL(context);
  38. return context->get_param<std::string>(MS_CTX_DEVICE_TARGET) == kGPUDevice;
  39. }
  40. void E2eDump::DumpGPUMemToFile(const std::string &file_path, const std::string &original_kernel_name,
  41. const device::DeviceAddress &addr, const ShapeVector &int_shapes,
  42. const TypeId &host_type, const TypeId &device_type, bool trans_flag, size_t slot,
  43. const Debugger *debugger) {
  44. #ifdef ENABLE_DEBUGGER
  45. auto format = kOpFormat_DEFAULT;
  46. MS_EXCEPTION_IF_NULL(debugger);
  47. auto ret = debugger->DumpTensorToFile(original_kernel_name, trans_flag, file_path, format, int_shapes, host_type,
  48. device_type, addr.format(), slot);
  49. if (!ret) {
  50. MS_LOG(ERROR) << "DumpTensorToFile Failed: flag:" << trans_flag << ", path:" << file_path
  51. << ", host_format:" << format;
  52. }
  53. #endif
  54. }
  55. void E2eDump::DumpOutput(const session::KernelGraph *graph, const std::string &dump_path, const Debugger *debugger) {
  56. MS_EXCEPTION_IF_NULL(graph);
  57. auto &dump_json_parser = DumpJsonParser::GetInstance();
  58. if (!dump_json_parser.OutputNeedDump()) {
  59. return;
  60. }
  61. MS_LOG(INFO) << "Start e2e dump output";
  62. bool trans_flag = dump_json_parser.trans_flag();
  63. const auto &apply_kernels = graph->execution_order();
  64. for (const auto &node : apply_kernels) {
  65. MS_EXCEPTION_IF_NULL(node);
  66. std::string kernel_name = GetKernelNodeName(node);
  67. if (!dump_json_parser.NeedDump(kernel_name)) {
  68. continue;
  69. }
  70. DumpJsonParser::GetInstance().MatchKernel(kernel_name);
  71. DumpOutputImpl(node, trans_flag, dump_path, &kernel_name, debugger);
  72. }
  73. }
  74. void E2eDump::DumpOutputSingleNode(const CNodePtr &node, const std::string &dump_path, const Debugger *debugger) {
  75. auto &dump_json_parser = DumpJsonParser::GetInstance();
  76. if (!dump_json_parser.OutputNeedDump()) {
  77. return;
  78. }
  79. bool trans_flag = dump_json_parser.trans_flag();
  80. MS_EXCEPTION_IF_NULL(node);
  81. std::string kernel_name = GetKernelNodeName(node);
  82. if (!dump_json_parser.NeedDump(kernel_name)) {
  83. return;
  84. }
  85. DumpJsonParser::GetInstance().MatchKernel(kernel_name);
  86. DumpOutputImpl(node, trans_flag, dump_path, &kernel_name, debugger);
  87. }
  88. void E2eDump::DumpOutputImpl(const CNodePtr &node, bool trans_flag, const std::string &dump_path,
  89. std::string *kernel_name, const Debugger *debugger) {
  90. MS_EXCEPTION_IF_NULL(node);
  91. GetFileKernelName(NOT_NULL(kernel_name));
  92. auto output_size = AnfAlgo::GetOutputTensorNum(node);
  93. for (size_t j = 0; j < output_size; ++j) {
  94. if (!AnfAlgo::OutputAddrExist(node, j)) {
  95. continue;
  96. }
  97. auto addr = AnfAlgo::GetOutputAddr(node, j);
  98. MS_EXCEPTION_IF_NULL(addr);
  99. ShapeVector int_shapes;
  100. GetDumpIntShape(node, j, NOT_NULL(&int_shapes), trans_flag);
  101. auto type = AnfAlgo::GetOutputInferDataType(node, j);
  102. auto device_type = AnfAlgo::GetOutputDeviceDataType(node, j);
  103. std::string op_type = AnfAlgo::GetCNodeName(node);
  104. std::string op_name = GetOpNameWithoutScope(*kernel_name);
  105. uint32_t task_id = 0;
  106. uint32_t stream_id = 0;
  107. uint64_t timestamp = GetTimeStamp();
  108. std::string file_path = dump_path + '/' + op_type + '.' + op_name + '.' + std::to_string(task_id) + '.' +
  109. std::to_string(stream_id) + '.' + std::to_string(timestamp) + ".output." +
  110. std::to_string(j);
  111. if (IsDeviceTargetGPU()) {
  112. DumpGPUMemToFile(file_path, GetKernelNodeName(node), *addr, int_shapes, type, device_type, trans_flag, j,
  113. debugger);
  114. } else {
  115. DumpMemToFile(file_path, *addr, int_shapes, type, trans_flag);
  116. }
  117. }
  118. }
  119. void E2eDump::DumpInput(const session::KernelGraph *graph, const std::string &dump_path, const Debugger *debugger) {
  120. MS_EXCEPTION_IF_NULL(graph);
  121. auto &dump_json_parser = DumpJsonParser::GetInstance();
  122. if (!dump_json_parser.InputNeedDump()) {
  123. return;
  124. }
  125. MS_LOG(INFO) << "Start e2e dump input";
  126. bool trans_flag = dump_json_parser.trans_flag();
  127. const auto &apply_kernels = graph->execution_order();
  128. for (const auto &node : apply_kernels) {
  129. MS_EXCEPTION_IF_NULL(node);
  130. std::string kernel_name = GetKernelNodeName(node);
  131. if (!dump_json_parser.NeedDump(kernel_name)) {
  132. continue;
  133. }
  134. DumpJsonParser::GetInstance().MatchKernel(kernel_name);
  135. DumpInputImpl(node, trans_flag, dump_path, &kernel_name, debugger);
  136. }
  137. }
  138. void E2eDump::DumpInputSingleNode(const CNodePtr &node, const std::string &dump_path, const Debugger *debugger) {
  139. auto &dump_json_parser = DumpJsonParser::GetInstance();
  140. if (!dump_json_parser.InputNeedDump()) {
  141. return;
  142. }
  143. bool trans_flag = dump_json_parser.trans_flag();
  144. MS_EXCEPTION_IF_NULL(node);
  145. std::string kernel_name = GetKernelNodeName(node);
  146. if (!dump_json_parser.NeedDump(kernel_name)) {
  147. return;
  148. }
  149. DumpJsonParser::GetInstance().MatchKernel(kernel_name);
  150. DumpInputImpl(node, trans_flag, dump_path, &kernel_name, debugger);
  151. }
  152. void E2eDump::DumpInputImpl(const CNodePtr &node, bool trans_flag, const std::string &dump_path,
  153. std::string *kernel_name, const Debugger *debugger) {
  154. MS_EXCEPTION_IF_NULL(node);
  155. GetFileKernelName(NOT_NULL(kernel_name));
  156. auto input_size = AnfAlgo::GetInputTensorNum(node);
  157. for (size_t j = 0; j < input_size; ++j) {
  158. auto kernel_with_index = AnfAlgo::GetPrevNodeOutput(node, j);
  159. auto input = kernel_with_index.first;
  160. auto index = kernel_with_index.second;
  161. if (!AnfAlgo::OutputAddrExist(input, index)) {
  162. continue;
  163. }
  164. auto addr = AnfAlgo::GetOutputAddr(input, index);
  165. MS_EXCEPTION_IF_NULL(addr);
  166. std::string tensor_name;
  167. size_t slot;
  168. if (IsDeviceTargetGPU()) {
  169. auto input_kernel = node->input(j + 1);
  170. std::string input_kernel_name = GetKernelNodeName(input_kernel);
  171. tensor_name = input_kernel_name;
  172. slot = 0;
  173. } else {
  174. tensor_name = GetKernelNodeName(node);
  175. slot = j;
  176. }
  177. ShapeVector int_shapes;
  178. GetDumpIntShape(input, index, NOT_NULL(&int_shapes), trans_flag);
  179. auto type = AnfAlgo::GetOutputInferDataType(input, index);
  180. auto device_type = AnfAlgo::GetOutputDeviceDataType(input, index);
  181. std::string op_type = AnfAlgo::GetCNodeName(node);
  182. std::string op_name = GetOpNameWithoutScope(*kernel_name);
  183. uint64_t timestamp = GetTimeStamp();
  184. uint32_t task_id = 0;
  185. uint32_t stream_id = 0;
  186. std::string file_path = dump_path + '/' + op_type + '.' + op_name + '.' + std::to_string(task_id) + '.' +
  187. std::to_string(stream_id) + '.' + std::to_string(timestamp) + ".input." + std::to_string(j);
  188. if (IsDeviceTargetGPU()) {
  189. DumpGPUMemToFile(file_path, tensor_name, *addr, int_shapes, type, device_type, trans_flag, slot, debugger);
  190. } else {
  191. DumpMemToFile(file_path, *addr, int_shapes, type, trans_flag);
  192. }
  193. }
  194. }
  195. void E2eDump::DumpSingleAnfNode(const AnfNodePtr &anf_node, const size_t output_index, const std::string &dump_path,
  196. bool trans_flag, std::map<std::string, size_t> *const_map, const Debugger *debugger) {
  197. MS_EXCEPTION_IF_NULL(anf_node);
  198. auto &dump_json_parser = DumpJsonParser::GetInstance();
  199. if ((!anf_node->isa<Parameter>() && !anf_node->isa<ValueNode>()) || IsValueNode<StringImm>(anf_node)) {
  200. return;
  201. }
  202. std::string node_name = GetKernelNodeName(anf_node);
  203. std::string dump_name = node_name;
  204. if (anf_node->isa<ValueNode>()) {
  205. auto iter = const_map->find(node_name);
  206. if (iter == const_map->end()) {
  207. return;
  208. }
  209. dump_name = std::string("cst") + std::to_string(iter->second);
  210. }
  211. if (!dump_json_parser.NeedDump(node_name)) {
  212. return;
  213. }
  214. DumpJsonParser::GetInstance().MatchKernel(node_name);
  215. GetFileKernelName(NOT_NULL(&node_name));
  216. // check if output address exists, if not, return;
  217. if (!AnfAlgo::OutputAddrExist(anf_node, output_index)) {
  218. return;
  219. }
  220. auto addr = AnfAlgo::GetOutputAddr(anf_node, output_index);
  221. MS_EXCEPTION_IF_NULL(addr);
  222. ShapeVector int_shapes;
  223. GetDumpIntShape(anf_node, output_index, NOT_NULL(&int_shapes), trans_flag);
  224. auto type = AnfAlgo::GetOutputInferDataType(anf_node, output_index);
  225. auto device_type = AnfAlgo::GetOutputDeviceDataType(anf_node, output_index);
  226. uint64_t timestamp = GetTimeStamp();
  227. uint32_t task_id = 0;
  228. uint32_t stream_id = 0;
  229. std::string file_path = dump_path + "/Parameter." + dump_name + '.' + std::to_string(task_id) + '.' +
  230. std::to_string(stream_id) + '.' + std::to_string(timestamp) + ".output.0";
  231. if (IsDeviceTargetGPU()) {
  232. DumpGPUMemToFile(file_path, node_name, *addr, int_shapes, type, device_type, trans_flag, 0, debugger);
  233. } else {
  234. DumpMemToFile(file_path, *addr, int_shapes, type, trans_flag);
  235. }
  236. }
  237. void E2eDump::DumpParametersAndConst(const session::KernelGraph *graph, const std::string &dump_path,
  238. const Debugger *debugger) {
  239. MS_EXCEPTION_IF_NULL(graph);
  240. auto &dump_json_parser = DumpJsonParser::GetInstance();
  241. if (!dump_json_parser.OutputNeedDump()) {
  242. return;
  243. }
  244. MS_LOG(INFO) << "Start e2e dump parameters and Const values";
  245. bool trans_flag = dump_json_parser.trans_flag();
  246. std::map<std::string, size_t> const_map;
  247. GetConstantId(graph, &const_map);
  248. // dump parameters
  249. const auto &parameters = graph->inputs();
  250. for (auto &item : parameters) {
  251. DumpSingleAnfNode(item, PARAMETER_OUTPUT_INDEX, dump_path, trans_flag, &const_map, debugger);
  252. }
  253. // dump const values
  254. auto value_nodes = graph->graph_value_nodes();
  255. for (const auto &value_node : value_nodes) {
  256. DumpSingleAnfNode(value_node, VALUE_NODE_OUTPUT_INDEX, dump_path, trans_flag, &const_map, debugger);
  257. }
  258. }
  259. void E2eDump::DumpSetup(const session::KernelGraph *graph, uint32_t rank_id) {
  260. auto &dump_json_parser = DumpJsonParser::GetInstance();
  261. uint32_t cur_iter = dump_json_parser.cur_dump_iter();
  262. uint32_t graph_id = graph->graph_id();
  263. bool sink_mode = (ConfigManager::GetInstance().dataset_mode() || E2eDump::isDatasetGraph(graph));
  264. if (dump_json_parser.async_dump_enabled() || dump_json_parser.e2e_dump_enabled()) {
  265. if (starting_graph_id == INT32_MAX) {
  266. starting_graph_id = graph_id;
  267. } else if (starting_graph_id == graph_id) {
  268. dump_json_parser.UpdateDumpIter();
  269. }
  270. MS_LOG(DEBUG) << "sink_mode = " << sink_mode;
  271. }
  272. if (dump_json_parser.async_dump_enabled() && dump_json_parser.IsDumpIter(cur_iter) && !sink_mode) {
  273. auto zero_dir_dump_path =
  274. dump_json_parser.path() + "/rank_" + std::to_string(rank_id) + "/_/" + std::to_string(graph->graph_id()) + "/0";
  275. auto root_cur_iter_dump_path = dump_json_parser.path() + "/rank_" + std::to_string(rank_id) + "/" +
  276. dump_json_parser.net_name() + "/" + std::to_string(graph->graph_id());
  277. auto cur_iter_dump_path = root_cur_iter_dump_path + "/" + std::to_string(cur_iter);
  278. MS_LOG(INFO) << "zero_dir_dump_path: " << zero_dir_dump_path;
  279. MS_LOG(INFO) << "root_cur_iter_dump_path: " << root_cur_iter_dump_path;
  280. MS_LOG(INFO) << "cur_iter_dump_path: " << cur_iter_dump_path;
  281. // create cur_iter_dump_path dirs
  282. bool status = Common::CreateNotExistDirs(root_cur_iter_dump_path);
  283. if (!status) {
  284. MS_LOG(EXCEPTION) << "Failed at CreateNotExistDirs for " << root_cur_iter_dump_path;
  285. return;
  286. }
  287. }
  288. }
  289. bool E2eDump::DumpData(const session::KernelGraph *graph, uint32_t rank_id, const Debugger *debugger) {
  290. MS_EXCEPTION_IF_NULL(graph);
  291. bool success = false;
  292. auto &dump_json_parser = DumpJsonParser::GetInstance();
  293. uint32_t graph_id = graph->graph_id();
  294. bool sink_mode = (ConfigManager::GetInstance().dataset_mode() || E2eDump::isDatasetGraph(graph));
  295. if (dump_json_parser.GetIterDumpFlag()) {
  296. MS_LOG(INFO) << "Start e2e dump. Current iteration is " << dump_json_parser.cur_dump_iter();
  297. MS_LOG(INFO) << "Current graph id is " << graph_id;
  298. std::string dump_path = GenerateDumpPath(graph_id, rank_id);
  299. DumpInput(graph, dump_path, debugger);
  300. DumpOutput(graph, dump_path, debugger);
  301. DumpParametersAndConst(graph, dump_path, debugger);
  302. success = true;
  303. } else if (dump_json_parser.async_dump_enabled() && !sink_mode) {
  304. uint32_t current_iter = dump_json_parser.cur_dump_iter();
  305. auto zero_dir_dump_path =
  306. dump_json_parser.path() + "/rank_" + std::to_string(rank_id) + "/_/" + std::to_string(graph->graph_id()) + "/0";
  307. auto cur_iter_dump_path = dump_json_parser.path() + "/rank_" + std::to_string(rank_id) + "/" +
  308. dump_json_parser.net_name() + "/" + std::to_string(graph->graph_id()) + "/" +
  309. std::to_string(current_iter);
  310. MS_LOG(INFO) << "zero_dir_dump_path: " << zero_dir_dump_path;
  311. MS_LOG(INFO) << "cur_iter_dump_path: " << cur_iter_dump_path;
  312. if (dump_json_parser.IsDumpIter(current_iter)) {
  313. // create actual dir for iteration in final dump dir
  314. bool status = Common::CreateNotExistDirs(cur_iter_dump_path);
  315. if (!status) {
  316. MS_LOG(EXCEPTION) << "failed at CreateNotExistDirs for " << cur_iter_dump_path;
  317. }
  318. // test if zero_dir_dump_path exists (may not if there was
  319. // no data dumped, for example for an overflow dump)
  320. MS_LOG(INFO) << "Check " << zero_dir_dump_path << " exists.";
  321. bool dir_exists = DumpDirExists(zero_dir_dump_path);
  322. if (dir_exists) {
  323. // move contents from active dump dir to final dump dir
  324. MS_LOG(INFO) << "Move contents from " << zero_dir_dump_path << " to " << cur_iter_dump_path;
  325. bool move_files = MoveDumpFiles(zero_dir_dump_path, cur_iter_dump_path);
  326. if (!move_files) {
  327. MS_LOG(INFO) << "Issue with moving contents.";
  328. }
  329. } else {
  330. MS_LOG(INFO) << "active dump dir, not created yet";
  331. }
  332. } else {
  333. // test if zero_dir_dump_path exists (may not if there was
  334. // no data dumped, for example for an overflow dump)
  335. MS_LOG(INFO) << "Check " << zero_dir_dump_path << " exists.";
  336. bool dir_exists = DumpDirExists(zero_dir_dump_path);
  337. if (dir_exists) {
  338. // delete contents from active dump dir
  339. MS_LOG(INFO) << "Delete contents from active dump dir " << zero_dir_dump_path;
  340. bool delete_contents = DeleteDirContents(zero_dir_dump_path);
  341. if (!delete_contents) {
  342. MS_LOG(EXCEPTION) << "Ascend runtime has changed the dump dir structure!!!";
  343. }
  344. } else {
  345. MS_LOG(INFO) << "active dump dir, not created yet";
  346. }
  347. }
  348. success = true;
  349. }
  350. return success;
  351. }
  352. bool E2eDump::DumpSingleNodeData(const CNodePtr &node, uint32_t graph_id, uint32_t rank_id, const Debugger *debugger) {
  353. bool success = false;
  354. auto &dump_json_parser = DumpJsonParser::GetInstance();
  355. if (dump_json_parser.GetIterDumpFlag()) {
  356. std::string dump_path = GenerateDumpPath(graph_id, rank_id);
  357. DumpInputSingleNode(node, dump_path, debugger);
  358. DumpOutputSingleNode(node, dump_path, debugger);
  359. success = true;
  360. }
  361. return success;
  362. }
  363. bool E2eDump::DumpParametersAndConstData(const session::KernelGraph *graph, uint32_t rank_id,
  364. const Debugger *debugger) {
  365. bool success = false;
  366. uint32_t graph_id = graph->graph_id();
  367. auto &dump_json_parser = DumpJsonParser::GetInstance();
  368. if (dump_json_parser.GetIterDumpFlag()) {
  369. MS_LOG(INFO) << "DumpParametersAndConst. Current iteration is " << dump_json_parser.cur_dump_iter();
  370. MS_LOG(INFO) << "Current graph id is " << graph_id;
  371. std::string dump_path = GenerateDumpPath(graph_id, rank_id);
  372. DumpParametersAndConst(graph, dump_path, debugger);
  373. success = true;
  374. }
  375. return success;
  376. }
  377. bool E2eDump::isDatasetGraph(const session::KernelGraph *graph) {
  378. // check if there is GetNext or InitDataSetQueue node
  379. const auto &nodes = graph->execution_order();
  380. for (const auto &node : nodes) {
  381. auto node_name = AnfAlgo::GetCNodeName(node);
  382. if (node_name == "GetNext" || node_name == "InitDataSetQueue") {
  383. return true;
  384. }
  385. }
  386. return false;
  387. }
  388. bool E2eDump::DumpDirExists(const std::string &dump_path) {
  389. DIR *dir = opendir(dump_path.c_str());
  390. if (dir) {
  391. MS_LOG(INFO) << "Dump dir " << dump_path << " exists";
  392. closedir(dir);
  393. return true;
  394. }
  395. return false;
  396. }
  397. bool E2eDump::MoveDumpFiles(const std::string &first_dir, const std::string &second_dir) {
  398. DIR *d_handle = opendir(first_dir.c_str());
  399. struct dirent *next_file;
  400. while ((next_file = readdir(d_handle)) != NULL) {
  401. if (next_file->d_type != DT_REG) {
  402. continue;
  403. }
  404. // build the path for each file in the folder
  405. std::string file_name = next_file->d_name;
  406. std::string old_file_path = first_dir + "/" + file_name;
  407. std::string new_file_path = second_dir + "/" + file_name;
  408. if (rename(old_file_path.c_str(), new_file_path.c_str()) != 0) {
  409. closedir(d_handle);
  410. return false;
  411. }
  412. }
  413. closedir(d_handle);
  414. return true;
  415. }
  416. bool E2eDump::DeleteDirContents(const std::string &dir_path) {
  417. DIR *d_handle = opendir(dir_path.c_str());
  418. struct dirent *next_file;
  419. while ((next_file = readdir(d_handle)) != NULL) {
  420. if (next_file->d_type != DT_REG) {
  421. continue;
  422. }
  423. // build the path for each file in the folder
  424. std::string file_name = next_file->d_name;
  425. std::string file_path = dir_path + "/" + file_name;
  426. int res = remove(file_path.c_str());
  427. if (res != 0) {
  428. // Could not remove the file
  429. closedir(d_handle);
  430. return false;
  431. }
  432. }
  433. closedir(d_handle);
  434. return true;
  435. }
  436. } // namespace mindspore