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