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 10 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 "backend/session/anf_runtime_algorithm.h"
  23. #include "utils/ms_context.h"
  24. #include "runtime/device/kernel_runtime_manager.h"
  25. #ifdef ENABLE_DEBUGGER
  26. #include "debug/debug_services.h"
  27. #include "debug/tensor_load.h"
  28. #include "debug/debugger/debugger.h"
  29. #endif
  30. namespace mindspore {
  31. bool E2eDump::IsDeviceTargetGPU() {
  32. auto context = MsContext::GetInstance();
  33. MS_EXCEPTION_IF_NULL(context);
  34. return context->get_param<std::string>(MS_CTX_DEVICE_TARGET) == kGPUDevice;
  35. }
  36. void E2eDump::DumpGPUMemToFile(const std::string &file_path, const std::string &original_kernel_name,
  37. NotNull<const device::DeviceAddress *> addr, const ShapeVector &int_shapes,
  38. const TypeId &host_type, const TypeId &device_type, bool trans_flag, size_t slot,
  39. const Debugger *debugger) {
  40. #ifdef ENABLE_DEBUGGER
  41. auto format = kOpFormat_DEFAULT;
  42. MS_EXCEPTION_IF_NULL(debugger);
  43. auto ret = debugger->DumpTensorToFile(original_kernel_name, trans_flag, file_path, format, int_shapes, host_type,
  44. device_type, addr->format(), slot);
  45. if (!ret) {
  46. MS_LOG(ERROR) << "DumpTensorToFile Failed: flag:" << std::to_string(trans_flag) << ", path:" << file_path
  47. << ", host_format:" << format;
  48. }
  49. #endif
  50. }
  51. void E2eDump::DumpOutput(const session::KernelGraph *graph, const std::string &dump_path, const Debugger *debugger) {
  52. MS_EXCEPTION_IF_NULL(graph);
  53. auto &dump_json_parser = DumpJsonParser::GetInstance();
  54. if (!dump_json_parser.OutputNeedDump()) {
  55. return;
  56. }
  57. MS_LOG(INFO) << "Start e2e dump output";
  58. bool trans_flag = dump_json_parser.trans_flag();
  59. const auto &apply_kernels = graph->execution_order();
  60. for (const auto &node : apply_kernels) {
  61. MS_EXCEPTION_IF_NULL(node);
  62. std::string kernel_name = node->fullname_with_scope();
  63. if (!dump_json_parser.NeedDump(kernel_name)) {
  64. continue;
  65. }
  66. DumpJsonParser::GetInstance().MatchKernel(kernel_name);
  67. DumpOutputImpl(node, trans_flag, dump_path, &kernel_name, debugger);
  68. }
  69. }
  70. void E2eDump::DumpOutputImpl(const CNodePtr &node, bool trans_flag, const std::string &dump_path,
  71. std::string *kernel_name, const Debugger *debugger) {
  72. MS_EXCEPTION_IF_NULL(node);
  73. GetFileKernelName(NOT_NULL(kernel_name));
  74. auto output_size = AnfAlgo::GetOutputTensorNum(node);
  75. for (size_t j = 0; j < output_size; ++j) {
  76. if (!AnfAlgo::OutputAddrExist(node, j)) {
  77. continue;
  78. }
  79. auto addr = AnfAlgo::GetOutputAddr(node, j);
  80. ShapeVector int_shapes;
  81. GetDumpIntShape(node, j, NOT_NULL(&int_shapes), trans_flag);
  82. auto type = AnfAlgo::GetOutputInferDataType(node, j);
  83. auto device_type = AnfAlgo::GetOutputDeviceDataType(node, j);
  84. std::string op_type = AnfAlgo::GetCNodeName(node);
  85. uint32_t task_id = 0;
  86. uint32_t stream_id = 0;
  87. uint64_t timestamp = GetTimeStamp();
  88. std::string file_path = dump_path + '/' + op_type + '.' + *kernel_name + '.' + std::to_string(task_id) + '.' +
  89. std::to_string(stream_id) + '.' + std::to_string(timestamp) + ".output." +
  90. std::to_string(j);
  91. if (IsDeviceTargetGPU()) {
  92. DumpGPUMemToFile(file_path, node->fullname_with_scope(), NOT_NULL(addr), int_shapes, type, device_type,
  93. trans_flag, j, debugger);
  94. } else {
  95. DumpMemToFile(file_path, NOT_NULL(addr), int_shapes, type, trans_flag);
  96. }
  97. }
  98. }
  99. uint64_t E2eDump::GetTimeStamp() {
  100. auto cur_sys_time = std::chrono::system_clock::now();
  101. uint64_t timestamp = std::chrono::duration_cast<std::chrono::microseconds>(cur_sys_time.time_since_epoch()).count();
  102. return timestamp;
  103. }
  104. void E2eDump::DumpInput(const session::KernelGraph *graph, const std::string &dump_path, const Debugger *debugger) {
  105. MS_EXCEPTION_IF_NULL(graph);
  106. auto &dump_json_parser = DumpJsonParser::GetInstance();
  107. if (!dump_json_parser.InputNeedDump()) {
  108. return;
  109. }
  110. MS_LOG(INFO) << "Start e2e dump input";
  111. bool trans_flag = dump_json_parser.trans_flag();
  112. const auto &apply_kernels = graph->execution_order();
  113. for (const auto &node : apply_kernels) {
  114. MS_EXCEPTION_IF_NULL(node);
  115. std::string kernel_name = node->fullname_with_scope();
  116. if (!dump_json_parser.NeedDump(kernel_name)) {
  117. continue;
  118. }
  119. DumpJsonParser::GetInstance().MatchKernel(kernel_name);
  120. DumpInputImpl(node, trans_flag, dump_path, &kernel_name, debugger);
  121. }
  122. }
  123. void E2eDump::DumpInputImpl(const CNodePtr &node, bool trans_flag, const std::string &dump_path,
  124. std::string *kernel_name, const Debugger *debugger) {
  125. MS_EXCEPTION_IF_NULL(node);
  126. GetFileKernelName(NOT_NULL(kernel_name));
  127. auto input_size = AnfAlgo::GetInputTensorNum(node);
  128. for (size_t j = 0; j < input_size; ++j) {
  129. auto kernel_with_index = AnfAlgo::GetPrevNodeOutput(node, j);
  130. auto input = kernel_with_index.first;
  131. auto index = kernel_with_index.second;
  132. if (!AnfAlgo::OutputAddrExist(input, index)) {
  133. continue;
  134. }
  135. auto addr = AnfAlgo::GetOutputAddr(input, index);
  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. 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 + '.' + *kernel_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, NOT_NULL(addr), int_shapes, type, device_type, trans_flag, slot,
  159. debugger);
  160. } else {
  161. DumpMemToFile(file_path, NOT_NULL(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. std::string file_path = dump_path + '/' + dump_name + "_output_0";
  197. if (IsDeviceTargetGPU()) {
  198. DumpGPUMemToFile(file_path, node_name, NOT_NULL(addr), int_shapes, type, device_type, trans_flag, 0, debugger);
  199. } else {
  200. DumpMemToFile(file_path, NOT_NULL(addr), int_shapes, type, trans_flag);
  201. }
  202. }
  203. void E2eDump::DumpParametersAndConst(const session::KernelGraph *graph, const std::string &dump_path,
  204. const Debugger *debugger) {
  205. MS_EXCEPTION_IF_NULL(graph);
  206. auto &dump_json_parser = DumpJsonParser::GetInstance();
  207. MS_LOG(INFO) << "Start e2e dump parameters and Const values";
  208. bool trans_flag = dump_json_parser.trans_flag();
  209. std::map<std::string, size_t> const_map;
  210. GetConstantId(graph, &const_map);
  211. // dump parameters
  212. const auto &parameters = graph->inputs();
  213. for (auto &item : parameters) {
  214. DumpSingleAnfNode(item, PARAMETER_OUTPUT_INDEX, dump_path, trans_flag, &const_map, debugger);
  215. }
  216. // dump const values
  217. auto value_nodes = graph->graph_value_nodes();
  218. for (const auto &value_node : value_nodes) {
  219. DumpSingleAnfNode(value_node, VALUE_NODE_OUTPUT_INDEX, dump_path, trans_flag, &const_map, debugger);
  220. }
  221. }
  222. bool E2eDump::DumpData(const session::KernelGraph *graph, uint32_t device_id, const Debugger *debugger) {
  223. MS_EXCEPTION_IF_NULL(graph);
  224. auto &dump_json_parser = DumpJsonParser::GetInstance();
  225. uint32_t graph_id = graph->graph_id();
  226. if (starting_graph_id == INT32_MAX) {
  227. starting_graph_id = graph_id;
  228. }
  229. if (starting_graph_id == graph_id) {
  230. dump_json_parser.UpdateDumpIter();
  231. }
  232. if (!dump_json_parser.GetIterDumpFlag()) {
  233. return true;
  234. }
  235. MS_LOG(INFO) << "Start e2e dump. Current iteration is " << dump_json_parser.cur_dump_iter();
  236. MS_LOG(INFO) << "Current graph id is " << graph_id;
  237. std::string dump_path = GenerateDumpPath(graph_id, &device_id);
  238. DumpInput(graph, dump_path, debugger);
  239. DumpOutput(graph, dump_path, debugger);
  240. DumpParametersAndConst(graph, dump_path, debugger);
  241. return true;
  242. }
  243. } // namespace mindspore