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