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

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /**
  2. * Copyright 2020 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. NotNull<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:" << std::to_string(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. ShapeVector int_shapes;
  82. GetDumpIntShape(node, j, NOT_NULL(&int_shapes), trans_flag);
  83. auto type = AnfAlgo::GetOutputInferDataType(node, j);
  84. auto device_type = AnfAlgo::GetOutputDeviceDataType(node, j);
  85. std::string op_type = AnfAlgo::GetCNodeName(node);
  86. uint32_t task_id = 0;
  87. uint32_t stream_id = 0;
  88. uint64_t timestamp = GetTimeStamp();
  89. std::string file_path = dump_path + '/' + op_type + '.' + *kernel_name + '.' + std::to_string(task_id) + '.' +
  90. std::to_string(stream_id) + '.' + std::to_string(timestamp) + ".output." +
  91. std::to_string(j);
  92. if (IsDeviceTargetGPU()) {
  93. DumpGPUMemToFile(file_path, node->fullname_with_scope(), NOT_NULL(addr), int_shapes, type, device_type,
  94. trans_flag, j, debugger);
  95. } else {
  96. DumpMemToFile(file_path, NOT_NULL(addr), int_shapes, type, trans_flag);
  97. }
  98. }
  99. }
  100. uint64_t E2eDump::GetTimeStamp() {
  101. auto cur_sys_time = std::chrono::system_clock::now();
  102. uint64_t timestamp = std::chrono::duration_cast<std::chrono::microseconds>(cur_sys_time.time_since_epoch()).count();
  103. return timestamp;
  104. }
  105. void E2eDump::DumpInput(const session::KernelGraph *graph, const std::string &dump_path, const Debugger *debugger) {
  106. MS_EXCEPTION_IF_NULL(graph);
  107. auto &dump_json_parser = DumpJsonParser::GetInstance();
  108. if (!dump_json_parser.InputNeedDump()) {
  109. return;
  110. }
  111. MS_LOG(INFO) << "Start e2e dump input";
  112. bool trans_flag = dump_json_parser.trans_flag();
  113. const auto &apply_kernels = graph->execution_order();
  114. for (const auto &node : apply_kernels) {
  115. MS_EXCEPTION_IF_NULL(node);
  116. std::string kernel_name = node->fullname_with_scope();
  117. if (!dump_json_parser.NeedDump(kernel_name)) {
  118. continue;
  119. }
  120. DumpJsonParser::GetInstance().MatchKernel(kernel_name);
  121. DumpInputImpl(node, trans_flag, dump_path, &kernel_name, debugger);
  122. }
  123. }
  124. void E2eDump::DumpInputImpl(const CNodePtr &node, bool trans_flag, const std::string &dump_path,
  125. std::string *kernel_name, const Debugger *debugger) {
  126. MS_EXCEPTION_IF_NULL(node);
  127. GetFileKernelName(NOT_NULL(kernel_name));
  128. auto input_size = AnfAlgo::GetInputTensorNum(node);
  129. for (size_t j = 0; j < input_size; ++j) {
  130. auto kernel_with_index = AnfAlgo::GetPrevNodeOutput(node, j);
  131. auto input = kernel_with_index.first;
  132. auto index = kernel_with_index.second;
  133. if (!AnfAlgo::OutputAddrExist(input, index)) {
  134. continue;
  135. }
  136. auto addr = AnfAlgo::GetOutputAddr(input, index);
  137. std::string tensor_name;
  138. size_t slot;
  139. if (IsDeviceTargetGPU()) {
  140. auto input_kernel = node->input(j + 1);
  141. std::string input_kernel_name = input_kernel->fullname_with_scope();
  142. tensor_name = input_kernel_name;
  143. slot = 0;
  144. } else {
  145. tensor_name = node->fullname_with_scope();
  146. slot = j;
  147. }
  148. ShapeVector int_shapes;
  149. GetDumpIntShape(input, index, NOT_NULL(&int_shapes), trans_flag);
  150. auto type = AnfAlgo::GetOutputInferDataType(input, index);
  151. auto device_type = AnfAlgo::GetOutputDeviceDataType(input, index);
  152. std::string op_type = AnfAlgo::GetCNodeName(node);
  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 + '.' + *kernel_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, NOT_NULL(addr), int_shapes, type, device_type, trans_flag, slot,
  160. debugger);
  161. } else {
  162. DumpMemToFile(file_path, NOT_NULL(addr), int_shapes, type, trans_flag);
  163. }
  164. }
  165. }
  166. void E2eDump::DumpSingleAnfNode(const AnfNodePtr &anf_node, const size_t output_index, const std::string &dump_path,
  167. bool trans_flag, std::map<std::string, size_t> *const_map, const Debugger *debugger) {
  168. MS_EXCEPTION_IF_NULL(anf_node);
  169. auto &dump_json_parser = DumpJsonParser::GetInstance();
  170. if (!anf_node->isa<Parameter>() && !anf_node->isa<ValueNode>()) {
  171. return;
  172. }
  173. std::string node_name = anf_node->fullname_with_scope();
  174. std::string dump_name = node_name;
  175. if (anf_node->isa<ValueNode>()) {
  176. auto iter = const_map->find(node_name);
  177. if (iter == const_map->end()) {
  178. return;
  179. }
  180. dump_name = std::string("cst") + std::to_string(iter->second);
  181. }
  182. if (!dump_json_parser.NeedDump(node_name)) {
  183. return;
  184. }
  185. DumpJsonParser::GetInstance().MatchKernel(node_name);
  186. GetFileKernelName(NOT_NULL(&node_name));
  187. // check if output address exists, if not, return;
  188. if (!AnfAlgo::OutputAddrExist(anf_node, output_index)) {
  189. return;
  190. }
  191. auto addr = AnfAlgo::GetOutputAddr(anf_node, output_index);
  192. MS_EXCEPTION_IF_NULL(addr);
  193. ShapeVector int_shapes;
  194. GetDumpIntShape(anf_node, output_index, NOT_NULL(&int_shapes), trans_flag);
  195. auto type = AnfAlgo::GetOutputInferDataType(anf_node, output_index);
  196. auto device_type = AnfAlgo::GetOutputDeviceDataType(anf_node, output_index);
  197. std::string file_path = dump_path + '/' + dump_name + "_output_0";
  198. if (IsDeviceTargetGPU()) {
  199. DumpGPUMemToFile(file_path, node_name, NOT_NULL(addr), int_shapes, type, device_type, trans_flag, 0, debugger);
  200. } else {
  201. DumpMemToFile(file_path, NOT_NULL(addr), int_shapes, type, trans_flag);
  202. }
  203. }
  204. void E2eDump::DumpParametersAndConst(const session::KernelGraph *graph, const std::string &dump_path,
  205. const Debugger *debugger) {
  206. MS_EXCEPTION_IF_NULL(graph);
  207. auto &dump_json_parser = DumpJsonParser::GetInstance();
  208. MS_LOG(INFO) << "Start e2e dump parameters and Const values";
  209. bool trans_flag = dump_json_parser.trans_flag();
  210. std::map<std::string, size_t> const_map;
  211. GetConstantId(graph, &const_map);
  212. // dump parameters
  213. const auto &parameters = graph->inputs();
  214. for (auto &item : parameters) {
  215. DumpSingleAnfNode(item, PARAMETER_OUTPUT_INDEX, dump_path, trans_flag, &const_map, debugger);
  216. }
  217. // dump const values
  218. auto value_nodes = graph->graph_value_nodes();
  219. for (const auto &value_node : value_nodes) {
  220. DumpSingleAnfNode(value_node, VALUE_NODE_OUTPUT_INDEX, dump_path, trans_flag, &const_map, debugger);
  221. }
  222. }
  223. void E2eDump::DumpSetup(const session::KernelGraph *graph, uint32_t device_id) {
  224. auto &dump_json_parser = DumpJsonParser::GetInstance();
  225. uint32_t cur_iter = dump_json_parser.cur_dump_iter();
  226. if (dump_json_parser.AsyncDumpEnabled() && dump_json_parser.IsDumpIter(cur_iter)) {
  227. auto zero_dir_dump_path =
  228. dump_json_parser.path() + "/rank_" + std::to_string(device_id) + "/_/" + std::to_string(graph->graph_id()) + "/0";
  229. auto root_cur_iter_dump_path = dump_json_parser.path() + "/rank_" + std::to_string(device_id) + "/" +
  230. dump_json_parser.net_name() + "/" + std::to_string(graph->graph_id());
  231. auto cur_iter_dump_path = root_cur_iter_dump_path + "/" + std::to_string(cur_iter);
  232. MS_LOG(INFO) << "zero_dir_dump_path: " << zero_dir_dump_path;
  233. MS_LOG(INFO) << "root_cur_iter_dump_path: " << root_cur_iter_dump_path;
  234. MS_LOG(INFO) << "cur_iter_dump_path: " << cur_iter_dump_path;
  235. // create cur_iter_dump_path dirs
  236. bool status = Common::CreateNotExistDirs(root_cur_iter_dump_path);
  237. if (!status) {
  238. MS_LOG(EXCEPTION) << "Failed at CreateNotExistDirs for " << root_cur_iter_dump_path;
  239. return;
  240. }
  241. // create symlink to active dump dir for the iteration in final dump dir
  242. std::string command = "ln -fs " + zero_dir_dump_path + " " + cur_iter_dump_path;
  243. MS_LOG(INFO) << "ln command: " << command;
  244. if (system(command.c_str())) {
  245. MS_LOG(EXCEPTION) << "failed to create symlink to active dump dir for the iteration in final dump dir.";
  246. }
  247. }
  248. }
  249. bool E2eDump::DumpData(const session::KernelGraph *graph, uint32_t device_id, const Debugger *debugger) {
  250. MS_EXCEPTION_IF_NULL(graph);
  251. auto &dump_json_parser = DumpJsonParser::GetInstance();
  252. uint32_t graph_id = graph->graph_id();
  253. if (starting_graph_id == INT32_MAX) {
  254. starting_graph_id = graph_id;
  255. }
  256. if (starting_graph_id == graph_id) {
  257. dump_json_parser.UpdateDumpIter();
  258. }
  259. if (dump_json_parser.GetIterDumpFlag()) {
  260. MS_LOG(INFO) << "Start e2e dump. Current iteration is " << dump_json_parser.cur_dump_iter();
  261. MS_LOG(INFO) << "Current graph id is " << graph_id;
  262. std::string dump_path = GenerateDumpPath(graph_id, &device_id);
  263. DumpInput(graph, dump_path, debugger);
  264. DumpOutput(graph, dump_path, debugger);
  265. DumpParametersAndConst(graph, dump_path, debugger);
  266. return true;
  267. } else if (dump_json_parser.AsyncDumpEnabled()) {
  268. uint32_t prev_dump_iter = dump_json_parser.cur_dump_iter() - 1;
  269. auto zero_dir_dump_path =
  270. dump_json_parser.path() + "/rank_" + std::to_string(device_id) + "/_/" + std::to_string(graph->graph_id()) + "/0";
  271. auto cur_iter_dump_path = dump_json_parser.path() + "/rank_" + std::to_string(device_id) + "/" +
  272. dump_json_parser.net_name() + "/" + std::to_string(graph->graph_id()) + "/" +
  273. std::to_string(prev_dump_iter);
  274. MS_LOG(INFO) << "zero_dir_dump_path: " << zero_dir_dump_path;
  275. MS_LOG(INFO) << "cur_iter_dump_path: " << cur_iter_dump_path;
  276. if (dump_json_parser.IsDumpIter(prev_dump_iter)) {
  277. // remove symlink to active dump dir
  278. std::string command = "rm -f " + cur_iter_dump_path;
  279. MS_LOG(INFO) << "rm command: " << command;
  280. if (system(command.c_str())) {
  281. MS_LOG(EXCEPTION) << "failed to remove symlink to active dump dir.";
  282. }
  283. // create actual dir for iteration in final dump dir
  284. bool status = Common::CreateNotExistDirs(cur_iter_dump_path);
  285. if (!status) {
  286. MS_LOG(EXCEPTION) << "failed at CreateNotExistDirs for " << cur_iter_dump_path;
  287. }
  288. // move contents from active dump dir to final dump dir
  289. command = "mv " + zero_dir_dump_path + "/* " + cur_iter_dump_path + "/.";
  290. MS_LOG(INFO) << "mv command: " << command;
  291. if (system(command.c_str())) {
  292. MS_LOG(EXCEPTION) << "Ascend runtime has changed the dump dir structure!!!";
  293. }
  294. } else {
  295. // delete contents from active dump dir
  296. std::string command = "rm -f " + zero_dir_dump_path + "/*";
  297. MS_LOG(INFO) << "rm command: " << command;
  298. if (system(command.c_str())) {
  299. MS_LOG(EXCEPTION) << "Ascend runtime has changed the dump dir structure!!!";
  300. }
  301. }
  302. return true;
  303. }
  304. return false;
  305. }
  306. } // namespace mindspore