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 32 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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 <unistd.h>
  18. #include <sstream>
  19. #include <algorithm>
  20. #include <map>
  21. #include <memory>
  22. #include <set>
  23. #include <utility>
  24. #include <vector>
  25. #include "debug/data_dump/dump_json_parser.h"
  26. #include "utils/ms_device_shape_transfer.h"
  27. #include "debug/anf_ir_utils.h"
  28. #include "debug/common.h"
  29. #include "backend/session/anf_runtime_algorithm.h"
  30. #include "utils/ms_context.h"
  31. #include "runtime/device/kernel_runtime_manager.h"
  32. #include "utils/config_manager.h"
  33. #include "utils/file_utils.h"
  34. #include "debug/data_dump/tensor_stat_dump.h"
  35. #include "abstract/utils.h"
  36. #ifdef ENABLE_DEBUGGER
  37. #include "debug/debug_services.h"
  38. #include "debug/tensor_load.h"
  39. #include "debug/debugger/debugger.h"
  40. #endif
  41. namespace mindspore {
  42. #ifdef ENABLE_D
  43. using ProtoFormat = debugger::dump::OutputFormat;
  44. using ProtoDataType = debugger::dump::OutputDataType;
  45. constexpr int kDhaAtomicAddInfoSize = 128;
  46. constexpr int kL2AtomicAddInfoSize = 128;
  47. constexpr int kAiCoreInfoSize = 256;
  48. constexpr int kDhaAtomicAddStatusSize = 256;
  49. constexpr int kL2AtomicAddStatusSize = 256;
  50. constexpr int kUint64Size = sizeof(uint64_t);
  51. const std::set<std::pair<std::string, std::string>> kSuppTransFormatPair = {
  52. // {device format, host format}
  53. {kOpFormat_FRAC_Z, kOpFormat_NCHW}, {kOpFormat_FRAC_NZ, kOpFormat_NCHW},
  54. {kOpFormat_NC1HWC0, kOpFormat_NCHW}, {kOpFormat_C1HWNCoC0, kOpFormat_NCHW},
  55. {kOpFormat_NC1HWC0_C04, kOpFormat_NCHW}, {kOpFormat_NDC1HWC0, kOpFormat_NCHW},
  56. {kOpFormat_FRACTAL_Z_3D, kOpFormat_NCHW}};
  57. const std::map<ProtoFormat, std::string> kFormatToStringMap = {
  58. {ProtoFormat::FORMAT_NCHW, kOpFormat_NCHW},
  59. {ProtoFormat::FORMAT_NHWC, kOpFormat_NHWC},
  60. {ProtoFormat::FORMAT_ND, kOpFormat_ND},
  61. {ProtoFormat::FORMAT_NC1HWC0, kOpFormat_NC1HWC0},
  62. {ProtoFormat::FORMAT_FRACTAL_Z, kOpFormat_FRAC_Z},
  63. {ProtoFormat::FORMAT_NC1HWC0_C04, kOpFormat_NC1HWC0_C04},
  64. {ProtoFormat::FORMAT_FRACTAL_Z_C04, kOpFormat_FRACTAL_Z_C04},
  65. {ProtoFormat::FORMAT_NC1KHKWHWC0, kOpFormat_NC1KHKWHWC0},
  66. {ProtoFormat::FORMAT_HWCN, kOpFormat_HWCN},
  67. {ProtoFormat::FORMAT_NDHWC, kOpFormat_NDHWC},
  68. {ProtoFormat::FORMAT_NCDHW, kOpFormat_NCDHW},
  69. {ProtoFormat::FORMAT_DHWCN, kOpFormat_DHWCN},
  70. {ProtoFormat::FORMAT_DHWNC, kOpFormat_DHWNC},
  71. {ProtoFormat::FORMAT_NDC1HWC0, kOpFormat_NDC1HWC0},
  72. {ProtoFormat::FORMAT_FRACTAL_Z_3D, kOpFormat_FRACTAL_Z_3D},
  73. {ProtoFormat::FORMAT_C1HWNCoC0, kOpFormat_C1HWNCoC0},
  74. {ProtoFormat::FORMAT_FRACTAL_NZ, kOpFormat_FRAC_NZ},
  75. {ProtoFormat::FORMAT_FRACTAL_ZN_LSTM, kOpFormat_FRACTAL_ZN_LSTM}};
  76. const std::map<ProtoDataType, mindspore::TypeId> kDataTypetoMSTypeMap = {
  77. {ProtoDataType::DT_UNDEFINED, mindspore::TypeId::kTypeUnknown},
  78. {ProtoDataType::DT_FLOAT, mindspore::TypeId::kNumberTypeFloat32},
  79. {ProtoDataType::DT_FLOAT16, mindspore::TypeId::kNumberTypeFloat16},
  80. {ProtoDataType::DT_INT8, mindspore::TypeId::kNumberTypeInt8},
  81. {ProtoDataType::DT_UINT8, mindspore::TypeId::kNumberTypeUInt8},
  82. {ProtoDataType::DT_INT16, mindspore::TypeId::kNumberTypeInt16},
  83. {ProtoDataType::DT_UINT16, mindspore::TypeId::kNumberTypeUInt16},
  84. {ProtoDataType::DT_INT32, mindspore::TypeId::kNumberTypeInt32},
  85. {ProtoDataType::DT_INT64, mindspore::TypeId::kNumberTypeInt64},
  86. {ProtoDataType::DT_UINT32, mindspore::TypeId::kNumberTypeUInt32},
  87. {ProtoDataType::DT_UINT64, mindspore::TypeId::kNumberTypeUInt64},
  88. {ProtoDataType::DT_BOOL, mindspore::TypeId::kNumberTypeBool},
  89. {ProtoDataType::DT_DOUBLE, mindspore::TypeId::kNumberTypeFloat64},
  90. {ProtoDataType::DT_STRING, mindspore::TypeId::kObjectTypeString}};
  91. #endif
  92. bool E2eDump::IsDeviceTargetGPU() {
  93. auto context = MsContext::GetInstance();
  94. MS_EXCEPTION_IF_NULL(context);
  95. return context->get_param<std::string>(MS_CTX_DEVICE_TARGET) == kGPUDevice;
  96. }
  97. void E2eDump::DumpGPUMemToFile(const std::string &file_path, const std::string &original_kernel_name,
  98. const device::DeviceAddress &addr, const ShapeVector &int_shapes,
  99. const TypeId &host_type, const TypeId &device_type, bool trans_flag, size_t slot,
  100. const Debugger *debugger) {
  101. #ifdef ENABLE_DEBUGGER
  102. auto format = kOpFormat_DEFAULT;
  103. MS_EXCEPTION_IF_NULL(debugger);
  104. auto ret = debugger->DumpTensorToFile(original_kernel_name, trans_flag, file_path, format, int_shapes, host_type,
  105. device_type, addr.format(), slot);
  106. if (!ret) {
  107. MS_LOG(INFO) << "DumpTensorToFile Failed: flag:" << trans_flag << ", path:" << file_path
  108. << ", host_format:" << format;
  109. }
  110. #endif
  111. }
  112. void E2eDump::DumpOutput(const session::KernelGraph *graph, const std::string &dump_path, const Debugger *debugger) {
  113. MS_EXCEPTION_IF_NULL(graph);
  114. auto &dump_json_parser = DumpJsonParser::GetInstance();
  115. if (!dump_json_parser.OutputNeedDump()) {
  116. return;
  117. }
  118. MS_LOG(INFO) << "Start e2e dump output";
  119. bool trans_flag = dump_json_parser.trans_flag();
  120. const auto &apply_kernels = graph->execution_order();
  121. for (const auto &node : apply_kernels) {
  122. MS_EXCEPTION_IF_NULL(node);
  123. std::string kernel_name = GetKernelNodeName(node);
  124. if (!dump_json_parser.NeedDump(kernel_name)) {
  125. continue;
  126. }
  127. DumpJsonParser::GetInstance().MatchKernel(kernel_name);
  128. DumpOutputImpl(node, trans_flag, dump_path, &kernel_name, debugger);
  129. }
  130. }
  131. void E2eDump::DumpOutputSingleNode(const CNodePtr &node, const std::string &dump_path, const Debugger *debugger) {
  132. auto &dump_json_parser = DumpJsonParser::GetInstance();
  133. if (!dump_json_parser.OutputNeedDump()) {
  134. return;
  135. }
  136. bool trans_flag = dump_json_parser.trans_flag();
  137. MS_EXCEPTION_IF_NULL(node);
  138. std::string kernel_name = GetKernelNodeName(node);
  139. if (!dump_json_parser.NeedDump(kernel_name)) {
  140. return;
  141. }
  142. DumpJsonParser::GetInstance().MatchKernel(kernel_name);
  143. DumpOutputImpl(node, trans_flag, dump_path, &kernel_name, debugger);
  144. }
  145. void E2eDump::DumpOutputImpl(const CNodePtr &node, bool trans_flag, const std::string &dump_path,
  146. std::string *kernel_name, const Debugger *debugger) {
  147. MS_EXCEPTION_IF_NULL(node);
  148. GetFileKernelName(NOT_NULL(kernel_name));
  149. auto output_size = AnfAlgo::GetOutputTensorNum(node);
  150. for (size_t j = 0; j < output_size; ++j) {
  151. if (!AnfAlgo::OutputAddrExist(node, j)) {
  152. continue;
  153. }
  154. auto addr = AnfAlgo::GetOutputAddr(node, j);
  155. MS_EXCEPTION_IF_NULL(addr);
  156. ShapeVector int_shapes;
  157. GetDumpIntShape(node, j, NOT_NULL(&int_shapes), trans_flag);
  158. auto type = AnfAlgo::GetOutputInferDataType(node, j);
  159. auto device_type = AnfAlgo::GetOutputDeviceDataType(node, j);
  160. std::string op_type = AnfAlgo::GetCNodeName(node);
  161. std::string op_name = GetOpNameWithoutScope(*kernel_name);
  162. uint32_t task_id = 0;
  163. uint32_t stream_id = 0;
  164. uint64_t timestamp = GetTimeStamp();
  165. std::string file_path = dump_path + '/' + op_type + '.' + op_name + '.' + std::to_string(task_id) + '.' +
  166. std::to_string(stream_id) + '.' + std::to_string(timestamp) + ".output." +
  167. std::to_string(j);
  168. if (IsDeviceTargetGPU()) {
  169. if (DumpJsonParser::GetInstance().IsStatisticDump()) {
  170. TensorStatDump stat_dump(op_type, op_name, task_id, stream_id, timestamp, false, j, j);
  171. stat_dump.DumpTensorStatsToFile(GetKernelNodeName(node), dump_path, debugger);
  172. }
  173. if (DumpJsonParser::GetInstance().IsTensorDump()) {
  174. DumpGPUMemToFile(file_path, GetKernelNodeName(node), *addr, int_shapes, type, device_type, trans_flag, j,
  175. debugger);
  176. }
  177. } else {
  178. DumpMemToFile(file_path, *addr, int_shapes, type, trans_flag);
  179. }
  180. }
  181. }
  182. void E2eDump::DumpInput(const session::KernelGraph *graph, const std::string &dump_path, const Debugger *debugger) {
  183. MS_EXCEPTION_IF_NULL(graph);
  184. auto &dump_json_parser = DumpJsonParser::GetInstance();
  185. if (!dump_json_parser.InputNeedDump()) {
  186. return;
  187. }
  188. MS_LOG(INFO) << "Start e2e dump input";
  189. bool trans_flag = dump_json_parser.trans_flag();
  190. const auto &apply_kernels = graph->execution_order();
  191. for (const auto &node : apply_kernels) {
  192. MS_EXCEPTION_IF_NULL(node);
  193. std::string kernel_name = GetKernelNodeName(node);
  194. if (!dump_json_parser.NeedDump(kernel_name)) {
  195. continue;
  196. }
  197. DumpJsonParser::GetInstance().MatchKernel(kernel_name);
  198. DumpInputImpl(node, trans_flag, dump_path, &kernel_name, debugger);
  199. }
  200. }
  201. void E2eDump::DumpInputSingleNode(const CNodePtr &node, const std::string &dump_path, const Debugger *debugger) {
  202. auto &dump_json_parser = DumpJsonParser::GetInstance();
  203. if (!dump_json_parser.InputNeedDump()) {
  204. return;
  205. }
  206. bool trans_flag = dump_json_parser.trans_flag();
  207. MS_EXCEPTION_IF_NULL(node);
  208. std::string kernel_name = GetKernelNodeName(node);
  209. if (!dump_json_parser.NeedDump(kernel_name)) {
  210. return;
  211. }
  212. DumpJsonParser::GetInstance().MatchKernel(kernel_name);
  213. DumpInputImpl(node, trans_flag, dump_path, &kernel_name, debugger);
  214. }
  215. void E2eDump::DumpInputImpl(const CNodePtr &node, bool trans_flag, const std::string &dump_path,
  216. std::string *kernel_name, const Debugger *debugger) {
  217. MS_EXCEPTION_IF_NULL(node);
  218. GetFileKernelName(NOT_NULL(kernel_name));
  219. auto input_size = AnfAlgo::GetInputTensorNum(node);
  220. for (size_t j = 0; j < input_size; ++j) {
  221. auto kernel_with_index = AnfAlgo::GetPrevNodeOutput(node, j);
  222. auto input = kernel_with_index.first;
  223. auto index = kernel_with_index.second;
  224. if (!AnfAlgo::OutputAddrExist(input, index)) {
  225. continue;
  226. }
  227. auto addr = AnfAlgo::GetOutputAddr(input, index);
  228. MS_EXCEPTION_IF_NULL(addr);
  229. std::string tensor_name = GetKernelNodeName(node);
  230. size_t slot = j;
  231. if (IsDeviceTargetGPU()) {
  232. auto input_kernel = node->input(j + 1);
  233. std::string input_kernel_name = GetKernelNodeName(input_kernel);
  234. tensor_name = input_kernel_name;
  235. slot = 0;
  236. }
  237. ShapeVector int_shapes;
  238. GetDumpIntShape(input, index, NOT_NULL(&int_shapes), trans_flag);
  239. auto type = AnfAlgo::GetOutputInferDataType(input, index);
  240. auto device_type = AnfAlgo::GetOutputDeviceDataType(input, index);
  241. std::string op_type = AnfAlgo::GetCNodeName(node);
  242. std::string op_name = GetOpNameWithoutScope(*kernel_name);
  243. uint64_t timestamp = GetTimeStamp();
  244. uint32_t task_id = 0;
  245. uint32_t stream_id = 0;
  246. std::string file_path = dump_path + '/' + op_type + '.' + op_name + '.' + std::to_string(task_id) + '.' +
  247. std::to_string(stream_id) + '.' + std::to_string(timestamp) + ".input." + std::to_string(j);
  248. MS_EXCEPTION_IF_NULL(addr);
  249. if (IsDeviceTargetGPU()) {
  250. if (DumpJsonParser::GetInstance().IsStatisticDump()) {
  251. TensorStatDump stat_dump(op_type, op_name, task_id, stream_id, timestamp, true, j, slot);
  252. stat_dump.DumpTensorStatsToFile(tensor_name, dump_path, debugger);
  253. }
  254. if (DumpJsonParser::GetInstance().IsTensorDump()) {
  255. DumpGPUMemToFile(file_path, tensor_name, *addr, int_shapes, type, device_type, trans_flag, slot, debugger);
  256. }
  257. } else {
  258. DumpMemToFile(file_path, *addr, int_shapes, type, trans_flag);
  259. }
  260. }
  261. }
  262. void E2eDump::DumpSingleAnfNode(const AnfNodePtr &anf_node, const size_t output_index, const std::string &dump_path,
  263. bool trans_flag, const Debugger *debugger) {
  264. MS_EXCEPTION_IF_NULL(anf_node);
  265. auto &dump_json_parser = DumpJsonParser::GetInstance();
  266. if ((!anf_node->isa<Parameter>() && !anf_node->isa<ValueNode>()) || IsValueNode<StringImm>(anf_node)) {
  267. return;
  268. }
  269. std::string node_name = GetKernelNodeName(anf_node);
  270. if (!dump_json_parser.NeedDump(node_name)) {
  271. return;
  272. }
  273. DumpJsonParser::GetInstance().MatchKernel(node_name);
  274. GetFileKernelName(NOT_NULL(&node_name));
  275. std::string dump_name = node_name;
  276. const std::string cst_prefix = "Default--";
  277. if (anf_node->isa<ValueNode>()) {
  278. if (dump_name.find(cst_prefix) == std::string::npos) {
  279. MS_LOG(INFO) << "Incorrect constant format: " << dump_name;
  280. return;
  281. }
  282. dump_name = node_name.substr(cst_prefix.length());
  283. trans_flag = false;
  284. }
  285. // check if output address exists, if not, return;
  286. if (!AnfAlgo::OutputAddrExist(anf_node, output_index)) {
  287. return;
  288. }
  289. auto addr = AnfAlgo::GetOutputAddr(anf_node, output_index);
  290. MS_EXCEPTION_IF_NULL(addr);
  291. ShapeVector int_shapes;
  292. GetDumpIntShape(anf_node, output_index, NOT_NULL(&int_shapes), trans_flag);
  293. auto type = AnfAlgo::GetOutputInferDataType(anf_node, output_index);
  294. auto device_type = AnfAlgo::GetOutputDeviceDataType(anf_node, output_index);
  295. uint64_t timestamp = GetTimeStamp();
  296. uint32_t task_id = 0;
  297. uint32_t stream_id = 0;
  298. std::string file_path = dump_path + "/Parameter." + dump_name + '.' + std::to_string(task_id) + '.' +
  299. std::to_string(stream_id) + '.' + std::to_string(timestamp) + ".output.0";
  300. if (IsDeviceTargetGPU()) {
  301. if (dump_json_parser.IsStatisticDump()) {
  302. TensorStatDump stat_dump("Parameter", dump_name, task_id, stream_id, timestamp, false, 0, 0);
  303. stat_dump.DumpTensorStatsToFile(node_name, dump_path, debugger);
  304. }
  305. if (dump_json_parser.IsTensorDump()) {
  306. DumpGPUMemToFile(file_path, node_name, *addr, int_shapes, type, device_type, trans_flag, 0, debugger);
  307. }
  308. } else {
  309. DumpMemToFile(file_path, *addr, int_shapes, type, trans_flag);
  310. }
  311. }
  312. void E2eDump::DumpParameters(const session::KernelGraph *graph, const std::string &dump_path,
  313. const Debugger *debugger) {
  314. MS_EXCEPTION_IF_NULL(graph);
  315. auto &dump_json_parser = DumpJsonParser::GetInstance();
  316. if (!dump_json_parser.OutputNeedDump()) {
  317. return;
  318. }
  319. MS_LOG(INFO) << "Start e2e dump parameters";
  320. bool trans_flag = dump_json_parser.trans_flag();
  321. // dump parameters
  322. const auto &parameters = graph->inputs();
  323. for (auto &item : parameters) {
  324. DumpSingleAnfNode(item, PARAMETER_OUTPUT_INDEX, dump_path, trans_flag, debugger);
  325. }
  326. }
  327. void E2eDump::DumpConstantData(const session::KernelGraph *graph, uint32_t rank_id, const Debugger *debugger) {
  328. MS_EXCEPTION_IF_NULL(graph);
  329. auto &dump_json_parser = DumpJsonParser::GetInstance();
  330. if (!IsDeviceTargetGPU() || !dump_json_parser.e2e_dump_enabled()) {
  331. return;
  332. }
  333. uint32_t graph_id = graph->graph_id();
  334. std::string cst_path = GenerateDumpPath(graph_id, rank_id, true);
  335. if (!Common::FileExists(cst_path)) {
  336. DumpConstantData(graph, cst_path, debugger);
  337. }
  338. }
  339. void E2eDump::DumpConstantData(const session::KernelGraph *graph, const std::string &cst_dump_path,
  340. const Debugger *debugger) {
  341. // Dump constant to npy file
  342. MS_EXCEPTION_IF_NULL(graph);
  343. auto &dump_json_parser = DumpJsonParser::GetInstance();
  344. MS_LOG(INFO) << "DumpConstants. Current iteration is " << dump_json_parser.cur_dump_iter();
  345. MS_LOG(INFO) << "Current graph id is " << graph->graph_id();
  346. if (!dump_json_parser.OutputNeedDump()) {
  347. return;
  348. }
  349. const auto value_nodes = graph->graph_value_nodes();
  350. for (auto &item : value_nodes) {
  351. DumpSingleAnfNode(item, VALUE_NODE_OUTPUT_INDEX, cst_dump_path, false, debugger);
  352. }
  353. }
  354. void E2eDump::UpdateIterDumpSetup(const session::KernelGraph *graph, bool sink_mode) {
  355. uint32_t graph_id = graph->graph_id();
  356. auto &dump_json_parser = DumpJsonParser::GetInstance();
  357. if (IsDeviceTargetGPU()) {
  358. if (starting_graph_id == INT32_MAX) {
  359. starting_graph_id = graph_id;
  360. } else if (starting_graph_id == graph_id && !MsContext::GetInstance()->get_param<bool>(MS_CTX_ENABLE_MINDRT)) {
  361. // Update dump iter for mindrt runtime is done using UpdateIterGPUDump().
  362. // Update dump iter for GPU old runtime.
  363. dump_json_parser.UpdateDumpIter();
  364. }
  365. return;
  366. }
  367. // If device target is Ascend
  368. if (sink_mode && graph->IsDatasetGraph()) {
  369. MS_LOG(INFO) << "No need to update iteration for dataset graph.";
  370. return;
  371. }
  372. // In multi network scripts, dump iter is equal to the number of networks that have been executed so far.
  373. dump_json_parser.UpdateDumpIter();
  374. }
  375. void E2eDump::DumpSetup(const session::KernelGraph *graph) {
  376. auto &dump_json_parser = DumpJsonParser::GetInstance();
  377. bool sink_mode = (ConfigManager::GetInstance().dataset_mode() || E2eDump::isDatasetGraph(graph));
  378. if (dump_json_parser.async_dump_enabled() || dump_json_parser.e2e_dump_enabled()) {
  379. UpdateIterDumpSetup(graph, sink_mode);
  380. }
  381. }
  382. void E2eDump::UpdateIterMindRTDump() {
  383. // update dump iter for GPU and kernel by kernel ascend dump.
  384. DumpJsonParser::GetInstance().UpdateDumpIter();
  385. }
  386. void E2eDump::DumpRunIter(const KernelGraphPtr &graph, uint32_t rank_id) {
  387. auto &json_parser = DumpJsonParser::GetInstance();
  388. if (!(json_parser.async_dump_enabled() || json_parser.e2e_dump_enabled())) {
  389. return;
  390. }
  391. bool sink_mode = (ConfigManager::GetInstance().dataset_mode() || graph->IsDatasetGraph());
  392. auto iter_num = SizeToInt(LongToSize(ConfigManager::GetInstance().iter_num()));
  393. if (graph->IsDatasetGraph()) {
  394. MS_LOG(INFO) << "graph: " << graph->graph_id() << " is dataset graph, not creating graph history file.";
  395. return;
  396. }
  397. std::string execution_order_path = json_parser.path() + "/rank_" + std::to_string(rank_id) + "/execution_order/";
  398. std::string file_name_to_check =
  399. execution_order_path + "/ms_global_execution_order_graph_" + std::to_string(graph->graph_id()) + ".csv";
  400. auto real_path = Common::CreatePrefixPath(file_name_to_check);
  401. if (!real_path.has_value()) {
  402. MS_LOG(WARNING) << "Check file path: " << file_name_to_check << " failed.";
  403. return;
  404. }
  405. std::string file_name = real_path.value();
  406. ChangeFileMode(file_name, S_IWUSR);
  407. std::ofstream fout(file_name, std::ofstream::app);
  408. if (!fout.is_open()) {
  409. MS_LOG(WARNING) << "Open file for saving graph global execution order failed.";
  410. return;
  411. }
  412. if (sink_mode && json_parser.async_dump_enabled()) {
  413. // for async dump when sink_mode = true, cur_dump_iter() = current_epoch
  414. // dump history for all iterations in the epoch
  415. Debugger::GetInstance()->UpdateGraphIterMap(graph->graph_id(), iter_num);
  416. auto graph_iter_map = Debugger::GetInstance()->GetGraphIterMap();
  417. auto step_per_epoch = graph_iter_map[graph->graph_id()];
  418. for (int i = 0; i < step_per_epoch; i++) {
  419. auto step = (json_parser.cur_dump_iter() * step_per_epoch) + i;
  420. fout << (std::to_string(step) + "\n");
  421. }
  422. } else {
  423. fout << std::to_string(json_parser.cur_dump_iter()) + "\n";
  424. }
  425. fout.close();
  426. ChangeFileMode(file_name, S_IRUSR);
  427. }
  428. void E2eDump::DumpData(const session::KernelGraph *graph, uint32_t rank_id, const Debugger *debugger) {
  429. MS_EXCEPTION_IF_NULL(graph);
  430. bool success = false;
  431. auto &dump_json_parser = DumpJsonParser::GetInstance();
  432. uint32_t graph_id = graph->graph_id();
  433. if (dump_json_parser.GetIterDumpFlag()) {
  434. MS_LOG(INFO) << "Start e2e dump. Current iteration is " << dump_json_parser.cur_dump_iter();
  435. MS_LOG(INFO) << "Current graph id is " << graph_id;
  436. std::string dump_path = GenerateDumpPath(graph_id, rank_id);
  437. std::string cst_path = GenerateDumpPath(graph_id, rank_id, true);
  438. if (dump_json_parser.IsStatisticDump()) {
  439. TensorStatDump::OpenStatisticsFile(dump_path);
  440. }
  441. DumpInput(graph, dump_path, debugger);
  442. DumpOutput(graph, dump_path, debugger);
  443. DumpParameters(graph, dump_path, debugger);
  444. if (IsDeviceTargetGPU() && dump_json_parser.e2e_dump_enabled()) {
  445. DumpConstantData(graph, cst_path, debugger);
  446. }
  447. if (dump_json_parser.IsStatisticDump()) {
  448. CsvWriter::GetInstance().CloseFile();
  449. }
  450. success = true;
  451. }
  452. if (success) {
  453. MS_LOG(DEBUG) << "E2eDump Dump Data completed!";
  454. } else {
  455. MS_LOG(DEBUG) << "E2eDump Dump has not occurred!";
  456. }
  457. }
  458. bool E2eDump::DumpSingleNodeData(const CNodePtr &node, uint32_t graph_id, uint32_t rank_id, const Debugger *debugger) {
  459. bool success = false;
  460. auto &dump_json_parser = DumpJsonParser::GetInstance();
  461. if (dump_json_parser.DumpEnabledForIter()) {
  462. std::string dump_path = GenerateDumpPath(graph_id, rank_id);
  463. DumpInputSingleNode(node, dump_path, debugger);
  464. DumpOutputSingleNode(node, dump_path, debugger);
  465. success = true;
  466. }
  467. return success;
  468. }
  469. bool E2eDump::DumpParametersData(const session::KernelGraph *graph, uint32_t rank_id, const Debugger *debugger) {
  470. bool success = false;
  471. uint32_t graph_id = graph->graph_id();
  472. auto &dump_json_parser = DumpJsonParser::GetInstance();
  473. if (dump_json_parser.DumpEnabledForIter()) {
  474. MS_LOG(INFO) << "DumpParameters. Current iteration is " << dump_json_parser.cur_dump_iter();
  475. MS_LOG(INFO) << "Current graph id is " << graph_id;
  476. std::string dump_path = GenerateDumpPath(graph_id, rank_id);
  477. DumpParameters(graph, dump_path, debugger);
  478. success = true;
  479. }
  480. return success;
  481. }
  482. bool E2eDump::isDatasetGraph(const session::KernelGraph *graph) {
  483. // check if there is GetNext or InitDataSetQueue node
  484. const auto &nodes = graph->execution_order();
  485. for (const auto &node : nodes) {
  486. auto node_name = AnfAlgo::GetCNodeName(node);
  487. if (node_name == prim::kPrimGetNext->name() || node_name == prim::kPrimInitDataSetQueue->name()) {
  488. return true;
  489. }
  490. }
  491. return false;
  492. }
  493. bool E2eDump::DumpDirExists(const std::string &dump_path) {
  494. DIR *dir = opendir(dump_path.c_str());
  495. if (dir != nullptr) {
  496. MS_LOG(INFO) << "Dump dir " << dump_path << " exists";
  497. if (closedir(dir) == -1) {
  498. MS_LOG(WARNING) << "Dump dir " << dump_path << " close failed!";
  499. }
  500. return true;
  501. }
  502. return false;
  503. }
  504. #ifdef ENABLE_D
  505. void E2eDump::DumpTensorToFile(const std::string &dump_path, const debugger::dump::DumpData &dump_data,
  506. char *data_ptr) {
  507. // dump input tensors
  508. std::vector<debugger::dump::OpInput> input_tensors(dump_data.input().begin(), dump_data.input().end());
  509. uint64_t offset = 0;
  510. for (uint32_t slot = 0; slot < input_tensors.size(); slot++) {
  511. auto in_tensor = input_tensors[slot];
  512. auto succ = ConvertFormatForTensorAndDump(dump_path, in_tensor, data_ptr + offset, "input", slot);
  513. if (!succ) {
  514. MS_LOG(INFO) << "Failed to convert format for tensor " << dump_path << ".input." << slot;
  515. }
  516. offset += in_tensor.size();
  517. }
  518. // dump output tensors
  519. std::vector<debugger::dump::OpOutput> output_tensors(dump_data.output().begin(), dump_data.output().end());
  520. for (uint32_t slot = 0; slot < output_tensors.size(); slot++) {
  521. auto out_tensor = output_tensors[slot];
  522. auto succ = ConvertFormatForTensorAndDump(dump_path, out_tensor, data_ptr + offset, "output", slot);
  523. if (!succ) {
  524. MS_LOG(INFO) << "Failed to convert format for tensor " << dump_path << ".output." << slot;
  525. }
  526. offset += out_tensor.size();
  527. }
  528. }
  529. template <typename T>
  530. bool DumpTensorStatsIfNeeded(const std::string &dump_path, const T &tensor, char *data_ptr, const std::string &io,
  531. uint32_t slot, const ShapeVector &shape, TypeId type) {
  532. // dump_path: dump_dir/op_type.op_name.task_id.stream_id.timestamp
  533. if (!DumpJsonParser::GetInstance().IsStatisticDump()) {
  534. return true;
  535. }
  536. size_t pos = dump_path.rfind("/");
  537. std::string file_name = dump_path.substr(pos + 1);
  538. size_t first_dot = file_name.find(".");
  539. size_t fourth_dot = file_name.rfind(".");
  540. size_t third_dot = file_name.rfind(".", fourth_dot - 1);
  541. size_t second_dot = file_name.rfind(".", third_dot - 1);
  542. if (first_dot == std::string::npos || second_dot == std::string::npos || third_dot == std::string::npos ||
  543. first_dot == second_dot) {
  544. MS_LOG(ERROR) << "Dump path " << dump_path << " received is not well formed";
  545. return false;
  546. }
  547. std::string op_type = file_name.substr(0, first_dot);
  548. std::string op_name = file_name.substr(first_dot + 1, second_dot - first_dot - 1);
  549. std::string task_id = file_name.substr(second_dot + 1, third_dot - second_dot - 1);
  550. std::string stream_id = file_name.substr(third_dot + 1, fourth_dot - third_dot - 1);
  551. std::string timestamp = file_name.substr(fourth_dot + 1);
  552. TensorStatDump stat_dump(op_type, op_name, task_id, stream_id, timestamp, io, slot, slot);
  553. std::shared_ptr<TensorData> data = std::make_shared<TensorData>();
  554. if (type <= TypeId::kNumberTypeBegin || type >= TypeId::kNumberTypeComplex64) {
  555. MS_LOG(ERROR) << "Data type of operator " << file_name << " is not supported by statistic dump";
  556. return false;
  557. }
  558. data->SetType(type);
  559. data->SetByteSize((size_t)tensor.size());
  560. data->SetShape(shape);
  561. data->SetDataPtr(data_ptr);
  562. return stat_dump.DumpTensorStatsToFile(dump_path.substr(0, pos), data);
  563. }
  564. template <typename T>
  565. bool E2eDump::ConvertFormatForTensorAndDump(std::string dump_path, const T &tensor, char *data_ptr,
  566. const std::string &io, uint32_t slot) {
  567. // dump_path: dump_dir/op_type.op_name.task_id.stream_id.timestamp
  568. std::ostringstream dump_path_ss;
  569. dump_path_ss << dump_path << "." << io << "." << slot << ".";
  570. std::string dump_path_slot = dump_path_ss.str();
  571. // get format
  572. auto iter_fmt = kFormatToStringMap.find(tensor.format());
  573. if (iter_fmt == kFormatToStringMap.end()) {
  574. MS_LOG(INFO) << "Unsupported tensor format for tensor " << dump_path << ": unknown(" << tensor.format() << ")";
  575. return false;
  576. }
  577. std::string device_format = iter_fmt->second;
  578. // get data type
  579. auto iter_dtype = kDataTypetoMSTypeMap.find(tensor.data_type());
  580. if (iter_dtype == kDataTypetoMSTypeMap.end()) {
  581. MS_LOG(INFO) << "Unsupported data type for tensor " << dump_path << ": unknown(" << tensor.data_type() << ")";
  582. return false;
  583. }
  584. auto src_type = iter_dtype->second;
  585. // get host shape
  586. std::vector<size_t> device_shape;
  587. (void)std::copy(tensor.shape().dim().begin(), tensor.shape().dim().end(), std::back_inserter(device_shape));
  588. ShapeVector shape_d;
  589. (void)std::transform(device_shape.begin(), device_shape.end(), std::back_inserter(shape_d), SizeToLong);
  590. std::vector<size_t> host_shape;
  591. (void)std::copy(tensor.original_shape().dim().begin(), tensor.original_shape().dim().end(),
  592. std::back_inserter(host_shape));
  593. ShapeVector shape_to;
  594. (void)std::transform(host_shape.begin(), host_shape.end(), std::back_inserter(shape_to), SizeToLong);
  595. size_t data_size = (size_t)tensor.size();
  596. bool trans_success = false;
  597. auto trans_buf = std::vector<uint8_t>(data_size);
  598. // convert format to host format. It can be either NCHW or ND (non 4-dimemsions).
  599. const uint8_t kNumFourDim = 4;
  600. std::string host_format;
  601. if (host_shape.size() == kNumFourDim) {
  602. host_format = kOpFormat_NCHW;
  603. } else {
  604. host_format = kOpFormat_ND;
  605. }
  606. if (device_format != host_format) {
  607. auto iter = kSuppTransFormatPair.find(std::make_pair(device_format, host_format));
  608. if (iter == kSuppTransFormatPair.end()) {
  609. MS_LOG(INFO) << "Do not support convert from format " << device_format << " to " << host_format << " for tensor "
  610. << dump_path_slot;
  611. } else {
  612. const trans::FormatArgs format_args{data_ptr, data_size, host_format, device_format, shape_to, shape_d, src_type};
  613. auto group = tensor.sub_format() > 1 ? tensor.sub_format() : 1;
  614. trans_success = trans::TransFormatFromDeviceToHost(format_args, trans_buf.data(), group);
  615. if (!trans_success) {
  616. MS_LOG(ERROR) << "Trans format failed.";
  617. }
  618. }
  619. }
  620. // dump tensor data into npy file
  621. bool dump_success = true;
  622. if (trans_success) {
  623. dump_success = DumpTensorStatsIfNeeded(dump_path, tensor, reinterpret_cast<char *>(trans_buf.data()), io, slot,
  624. shape_to, src_type);
  625. if (DumpJsonParser::GetInstance().IsTensorDump()) {
  626. dump_path_slot += host_format;
  627. dump_success =
  628. DumpJsonParser::DumpToFile(dump_path_slot, trans_buf.data(), data_size, shape_to, src_type) && dump_success;
  629. }
  630. } else {
  631. dump_success = DumpTensorStatsIfNeeded(dump_path, tensor, data_ptr, io, slot, shape_to, src_type);
  632. if (DumpJsonParser::GetInstance().IsTensorDump()) {
  633. dump_path_slot += device_format;
  634. dump_success =
  635. DumpJsonParser::DumpToFile(dump_path_slot, data_ptr, data_size, shape_to, src_type) && dump_success;
  636. }
  637. }
  638. return dump_success;
  639. }
  640. uint64_t UnpackUint64Value(char *ptr) {
  641. #if defined(__APPLE__)
  642. return *reinterpret_cast<const uint64_t *>(ptr);
  643. #else
  644. return le16toh(*reinterpret_cast<const uint64_t *>(ptr));
  645. #endif
  646. }
  647. std::string IntToHexString(const uint64_t value) {
  648. std::stringstream ss;
  649. ss << "0x" << std::hex << value;
  650. return ss.str();
  651. }
  652. nlohmann::json E2eDump::ParseOverflowInfo(char *data_ptr) {
  653. uint32_t index = 0;
  654. uint64_t model_id = UnpackUint64Value(data_ptr + index);
  655. index += kUint64Size;
  656. uint64_t stream_id = UnpackUint64Value(data_ptr + index);
  657. index += kUint64Size;
  658. uint64_t task_id = UnpackUint64Value(data_ptr + index);
  659. index += kUint64Size;
  660. uint64_t task_type = UnpackUint64Value(data_ptr + index);
  661. index += kUint64Size;
  662. uint64_t pc_start = UnpackUint64Value(data_ptr + index);
  663. index += kUint64Size;
  664. uint64_t para_base = UnpackUint64Value(data_ptr + index);
  665. nlohmann::json overflow_info;
  666. overflow_info["model_id"] = model_id;
  667. overflow_info["stream_id"] = stream_id;
  668. overflow_info["task_id"] = task_id;
  669. overflow_info["task_type"] = task_type;
  670. overflow_info["pc_start"] = IntToHexString(pc_start);
  671. overflow_info["para_base"] = IntToHexString(para_base);
  672. return overflow_info;
  673. }
  674. void E2eDump::DumpOpDebugToFile(const std::string &dump_path, const debugger::dump::DumpData &dump_data,
  675. char *data_ptr) {
  676. std::string out_path = dump_path + ".output.";
  677. std::vector<debugger::dump::OpOutput> op_debug(dump_data.output().begin(), dump_data.output().end());
  678. for (uint32_t slot = 0; slot < op_debug.size(); slot++) {
  679. uint32_t index = 0;
  680. // parse DHA Atomic Add info
  681. nlohmann::json dha_atomic_add_info = ParseOverflowInfo(data_ptr + index);
  682. index += kDhaAtomicAddInfoSize;
  683. // parse L2 Atomic Add info
  684. nlohmann::json l2_atomic_add_info = ParseOverflowInfo(data_ptr + index);
  685. index += kL2AtomicAddInfoSize;
  686. // parse AICore info
  687. nlohmann::json ai_core_info = ParseOverflowInfo(data_ptr + index);
  688. index += kAiCoreInfoSize;
  689. // parse DHA Atomic Add status
  690. dha_atomic_add_info["status"] = UnpackUint64Value(data_ptr + index);
  691. index += kDhaAtomicAddStatusSize;
  692. // parse L2 Atomic Add status
  693. l2_atomic_add_info["status"] = UnpackUint64Value(data_ptr + index);
  694. index += kL2AtomicAddStatusSize;
  695. // parse AICore status
  696. uint64_t kernel_code = UnpackUint64Value(data_ptr + index);
  697. index += kUint64Size;
  698. uint64_t block_idx = UnpackUint64Value(data_ptr + index);
  699. index += kUint64Size;
  700. uint64_t status = UnpackUint64Value(data_ptr + index);
  701. ai_core_info["kernel_code"] = IntToHexString(kernel_code);
  702. ai_core_info["block_idx"] = block_idx;
  703. ai_core_info["status"] = status;
  704. nlohmann::json opdebug_data;
  705. opdebug_data["DHA Atomic Add"] = dha_atomic_add_info;
  706. opdebug_data["L2 Atomic Add"] = l2_atomic_add_info;
  707. opdebug_data["AI Core"] = ai_core_info;
  708. // save json to file
  709. DumpToFile(out_path + std::to_string(slot) + ".json", opdebug_data.dump());
  710. }
  711. }
  712. #endif // ENABLE_D
  713. } // namespace mindspore