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