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

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 file_path = dump_path + '/' + *kernel_name + '_' + "output_" + std::to_string(j);
  85. if (IsDeviceTargetGPU()) {
  86. DumpGPUMemToFile(file_path, node->fullname_with_scope(), NOT_NULL(addr), int_shapes, type, device_type,
  87. trans_flag, j, debugger);
  88. } else {
  89. DumpMemToFile(file_path, NOT_NULL(addr), int_shapes, type, trans_flag);
  90. }
  91. }
  92. }
  93. void E2eDump::DumpInput(const session::KernelGraph *graph, const std::string &dump_path, const Debugger *debugger) {
  94. MS_EXCEPTION_IF_NULL(graph);
  95. auto &dump_json_parser = DumpJsonParser::GetInstance();
  96. if (!dump_json_parser.InputNeedDump()) {
  97. return;
  98. }
  99. MS_LOG(INFO) << "Start e2e dump input";
  100. bool trans_flag = dump_json_parser.trans_flag();
  101. const auto &apply_kernels = graph->execution_order();
  102. for (const auto &node : apply_kernels) {
  103. MS_EXCEPTION_IF_NULL(node);
  104. std::string kernel_name = node->fullname_with_scope();
  105. if (!dump_json_parser.NeedDump(kernel_name)) {
  106. continue;
  107. }
  108. DumpJsonParser::GetInstance().MatchKernel(kernel_name);
  109. DumpInputImpl(node, trans_flag, dump_path, &kernel_name, debugger);
  110. }
  111. }
  112. void E2eDump::DumpInputImpl(const CNodePtr &node, bool trans_flag, const std::string &dump_path,
  113. std::string *kernel_name, const Debugger *debugger) {
  114. MS_EXCEPTION_IF_NULL(node);
  115. GetFileKernelName(NOT_NULL(kernel_name));
  116. auto input_size = AnfAlgo::GetInputTensorNum(node);
  117. for (size_t j = 0; j < input_size; ++j) {
  118. auto kernel_with_index = AnfAlgo::GetPrevNodeOutput(node, j);
  119. auto input = kernel_with_index.first;
  120. auto index = kernel_with_index.second;
  121. if (!AnfAlgo::OutputAddrExist(input, index)) {
  122. continue;
  123. }
  124. auto addr = AnfAlgo::GetOutputAddr(input, index);
  125. std::string tensor_name;
  126. size_t slot;
  127. if (IsDeviceTargetGPU()) {
  128. auto input_kernel = node->input(j + 1);
  129. std::string input_kernel_name = input_kernel->fullname_with_scope();
  130. tensor_name = input_kernel_name;
  131. slot = 0;
  132. } else {
  133. tensor_name = node->fullname_with_scope();
  134. slot = j;
  135. }
  136. ShapeVector int_shapes;
  137. GetDumpIntShape(input, index, NOT_NULL(&int_shapes), trans_flag);
  138. auto type = AnfAlgo::GetOutputInferDataType(input, index);
  139. auto device_type = AnfAlgo::GetOutputDeviceDataType(input, index);
  140. std::string file_path = dump_path + '/' + *kernel_name + '_' + "input_" + std::to_string(j);
  141. if (IsDeviceTargetGPU()) {
  142. DumpGPUMemToFile(file_path, tensor_name, NOT_NULL(addr), int_shapes, type, device_type, trans_flag, slot,
  143. debugger);
  144. } else {
  145. DumpMemToFile(file_path, NOT_NULL(addr), int_shapes, type, trans_flag);
  146. }
  147. }
  148. }
  149. void E2eDump::DumpSingleAnfNode(const AnfNodePtr &anf_node, const size_t output_index, const std::string &dump_path,
  150. bool trans_flag, std::map<std::string, size_t> *const_map, const Debugger *debugger) {
  151. MS_EXCEPTION_IF_NULL(anf_node);
  152. auto &dump_json_parser = DumpJsonParser::GetInstance();
  153. if (!anf_node->isa<Parameter>() && !anf_node->isa<ValueNode>()) {
  154. return;
  155. }
  156. std::string node_name = anf_node->fullname_with_scope();
  157. std::string dump_name = node_name;
  158. if (anf_node->isa<ValueNode>()) {
  159. auto iter = const_map->find(node_name);
  160. if (iter == const_map->end()) {
  161. return;
  162. }
  163. dump_name = std::string("cst") + std::to_string(iter->second);
  164. }
  165. if (!dump_json_parser.NeedDump(node_name)) {
  166. return;
  167. }
  168. DumpJsonParser::GetInstance().MatchKernel(node_name);
  169. GetFileKernelName(NOT_NULL(&node_name));
  170. // check if output address exists, if not, return;
  171. if (!AnfAlgo::OutputAddrExist(anf_node, output_index)) {
  172. return;
  173. }
  174. auto addr = AnfAlgo::GetOutputAddr(anf_node, output_index);
  175. MS_EXCEPTION_IF_NULL(addr);
  176. ShapeVector int_shapes;
  177. GetDumpIntShape(anf_node, output_index, NOT_NULL(&int_shapes), trans_flag);
  178. auto type = AnfAlgo::GetOutputInferDataType(anf_node, output_index);
  179. auto device_type = AnfAlgo::GetOutputDeviceDataType(anf_node, output_index);
  180. std::string file_path = dump_path + '/' + dump_name + '_' + "output_0";
  181. if (IsDeviceTargetGPU()) {
  182. DumpGPUMemToFile(file_path, node_name, NOT_NULL(addr), int_shapes, type, device_type, trans_flag, 0, debugger);
  183. } else {
  184. DumpMemToFile(file_path, NOT_NULL(addr), int_shapes, type, trans_flag);
  185. }
  186. }
  187. void E2eDump::DumpParametersAndConst(const session::KernelGraph *graph, const std::string &dump_path,
  188. const Debugger *debugger) {
  189. MS_EXCEPTION_IF_NULL(graph);
  190. auto &dump_json_parser = DumpJsonParser::GetInstance();
  191. MS_LOG(INFO) << "Start e2e dump parameters and Const values";
  192. bool trans_flag = dump_json_parser.trans_flag();
  193. std::map<std::string, size_t> const_map;
  194. GetConstantId(graph, &const_map);
  195. // dump parameters
  196. const auto &parameters = graph->inputs();
  197. for (auto &item : parameters) {
  198. DumpSingleAnfNode(item, PARAMETER_OUTPUT_INDEX, dump_path, trans_flag, &const_map, debugger);
  199. }
  200. // dump const values
  201. auto value_nodes = graph->graph_value_nodes();
  202. for (const auto &value_node : value_nodes) {
  203. DumpSingleAnfNode(value_node, VALUE_NODE_OUTPUT_INDEX, dump_path, trans_flag, &const_map, debugger);
  204. }
  205. }
  206. bool E2eDump::DumpData(const session::KernelGraph *graph, uint32_t device_id, const Debugger *debugger) {
  207. MS_EXCEPTION_IF_NULL(graph);
  208. auto &dump_json_parser = DumpJsonParser::GetInstance();
  209. uint32_t graph_id = graph->graph_id();
  210. if (starting_graph_id == INT32_MAX) {
  211. starting_graph_id = graph_id;
  212. }
  213. if (starting_graph_id == graph_id) {
  214. dump_json_parser.UpdateDumpIter();
  215. }
  216. if (!dump_json_parser.GetIterDumpFlag()) {
  217. return true;
  218. }
  219. MS_LOG(INFO) << "Start e2e dump. Current iteration is " << dump_json_parser.cur_dump_iter();
  220. MS_LOG(INFO) << "Current graph id is " << graph_id;
  221. std::string dump_path = GenerateDumpPath(graph_id, &device_id);
  222. DumpInput(graph, dump_path, debugger);
  223. DumpOutput(graph, dump_path, debugger);
  224. DumpParametersAndConst(graph, dump_path, debugger);
  225. return true;
  226. }
  227. } // namespace mindspore