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