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 37 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. /**
  2. * Copyright 2020-2022 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/common/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. /*
  98. * Feature group: Dump.
  99. * Target device group: GPU.
  100. * Runtime category: Old runtime, MindRT.
  101. * Description: This function is for dumping tensor in memory to disk in GPU machine.
  102. */
  103. void E2eDump::DumpGPUMemToFile(const std::string &file_path, const std::string &original_kernel_name,
  104. const device::DeviceAddress &addr, const ShapeVector &int_shapes,
  105. const TypeId &host_type, const TypeId &device_type, bool trans_flag, size_t slot,
  106. const Debugger *debugger) {
  107. #ifdef ENABLE_DEBUGGER
  108. auto format = kOpFormat_DEFAULT;
  109. MS_EXCEPTION_IF_NULL(debugger);
  110. auto ret = debugger->DumpTensorToFile(original_kernel_name, trans_flag, file_path, format, int_shapes, host_type,
  111. device_type, addr.format(), slot);
  112. if (!ret) {
  113. MS_LOG(INFO) << "DumpTensorToFile Failed: flag:" << trans_flag << ", path:" << file_path
  114. << ", host_format:" << format;
  115. }
  116. #endif
  117. }
  118. void E2eDump::DumpOutput(const session::KernelGraph *graph, const std::string &dump_path, const Debugger *debugger) {
  119. MS_EXCEPTION_IF_NULL(graph);
  120. auto &dump_json_parser = DumpJsonParser::GetInstance();
  121. if (!dump_json_parser.OutputNeedDump()) {
  122. return;
  123. }
  124. MS_LOG(INFO) << "Start e2e dump output";
  125. bool trans_flag = dump_json_parser.trans_flag();
  126. const auto &apply_kernels = graph->execution_order();
  127. for (const auto &node : apply_kernels) {
  128. MS_EXCEPTION_IF_NULL(node);
  129. std::string kernel_name = GetKernelNodeName(node);
  130. if (!dump_json_parser.NeedDump(kernel_name)) {
  131. continue;
  132. }
  133. DumpJsonParser::GetInstance().MatchKernel(kernel_name);
  134. DumpOutputImpl(node, trans_flag, dump_path, &kernel_name, debugger);
  135. }
  136. }
  137. void E2eDump::DumpOutputSingleNode(const CNodePtr &node, const std::string &dump_path, const Debugger *debugger) {
  138. auto &dump_json_parser = DumpJsonParser::GetInstance();
  139. if (!dump_json_parser.OutputNeedDump()) {
  140. return;
  141. }
  142. bool trans_flag = dump_json_parser.trans_flag();
  143. MS_EXCEPTION_IF_NULL(node);
  144. std::string kernel_name = GetKernelNodeName(node);
  145. if (!dump_json_parser.NeedDump(kernel_name)) {
  146. return;
  147. }
  148. DumpJsonParser::GetInstance().MatchKernel(kernel_name);
  149. DumpOutputImpl(node, trans_flag, dump_path, &kernel_name, debugger);
  150. }
  151. void E2eDump::DumpOutputImpl(const CNodePtr &node, bool trans_flag, const std::string &dump_path,
  152. std::string *kernel_name, const Debugger *debugger) {
  153. MS_EXCEPTION_IF_NULL(node);
  154. GetFileKernelName(NOT_NULL(kernel_name));
  155. auto output_size = AnfAlgo::GetOutputTensorNum(node);
  156. for (size_t j = 0; j < output_size; ++j) {
  157. if (!AnfAlgo::OutputAddrExist(node, j)) {
  158. continue;
  159. }
  160. auto addr = AnfAlgo::GetOutputAddr(node, j);
  161. MS_EXCEPTION_IF_NULL(addr);
  162. ShapeVector int_shapes;
  163. GetDumpIntShape(node, j, NOT_NULL(&int_shapes), trans_flag);
  164. auto type = AnfAlgo::GetOutputInferDataType(node, j);
  165. auto device_type = AnfAlgo::GetOutputDeviceDataType(node, j);
  166. std::string op_type = AnfAlgo::GetCNodeName(node);
  167. std::string op_name = GetOpNameWithoutScope(*kernel_name);
  168. uint32_t task_id = 0;
  169. uint32_t stream_id = 0;
  170. uint64_t timestamp = GetTimeStamp();
  171. std::string file_path = dump_path + '/' + op_type + '.' + op_name + '.' + std::to_string(task_id) + '.' +
  172. std::to_string(stream_id) + '.' + std::to_string(timestamp) + ".output." +
  173. std::to_string(j);
  174. if (IsDeviceTargetGPU()) {
  175. if (DumpJsonParser::GetInstance().IsStatisticDump()) {
  176. TensorStatDump stat_dump(op_type, op_name, task_id, stream_id, timestamp, false, j, j);
  177. stat_dump.DumpTensorStatsToFile(GetKernelNodeName(node), dump_path, debugger);
  178. }
  179. if (DumpJsonParser::GetInstance().IsTensorDump()) {
  180. DumpGPUMemToFile(file_path, GetKernelNodeName(node), *addr, int_shapes, type, device_type, trans_flag, j,
  181. debugger);
  182. }
  183. } else {
  184. DumpMemToFile(file_path, *addr, int_shapes, type, trans_flag);
  185. }
  186. }
  187. }
  188. void E2eDump::DumpInput(const session::KernelGraph *graph, const std::string &dump_path, const Debugger *debugger) {
  189. MS_EXCEPTION_IF_NULL(graph);
  190. auto &dump_json_parser = DumpJsonParser::GetInstance();
  191. if (!dump_json_parser.InputNeedDump()) {
  192. return;
  193. }
  194. MS_LOG(INFO) << "Start e2e dump input";
  195. bool trans_flag = dump_json_parser.trans_flag();
  196. const auto &apply_kernels = graph->execution_order();
  197. for (const auto &node : apply_kernels) {
  198. MS_EXCEPTION_IF_NULL(node);
  199. std::string kernel_name = GetKernelNodeName(node);
  200. if (!dump_json_parser.NeedDump(kernel_name)) {
  201. continue;
  202. }
  203. DumpJsonParser::GetInstance().MatchKernel(kernel_name);
  204. DumpInputImpl(node, trans_flag, dump_path, &kernel_name, debugger);
  205. }
  206. }
  207. void E2eDump::DumpInputSingleNode(const CNodePtr &node, const std::string &dump_path, const Debugger *debugger) {
  208. auto &dump_json_parser = DumpJsonParser::GetInstance();
  209. if (!dump_json_parser.InputNeedDump()) {
  210. return;
  211. }
  212. bool trans_flag = dump_json_parser.trans_flag();
  213. MS_EXCEPTION_IF_NULL(node);
  214. std::string kernel_name = GetKernelNodeName(node);
  215. if (!dump_json_parser.NeedDump(kernel_name)) {
  216. return;
  217. }
  218. DumpJsonParser::GetInstance().MatchKernel(kernel_name);
  219. DumpInputImpl(node, trans_flag, dump_path, &kernel_name, debugger);
  220. }
  221. void E2eDump::DumpInputImpl(const CNodePtr &node, bool trans_flag, const std::string &dump_path,
  222. std::string *kernel_name, const Debugger *debugger) {
  223. MS_EXCEPTION_IF_NULL(node);
  224. GetFileKernelName(NOT_NULL(kernel_name));
  225. auto input_size = AnfAlgo::GetInputTensorNum(node);
  226. for (size_t j = 0; j < input_size; ++j) {
  227. auto kernel_with_index = AnfAlgo::GetPrevNodeOutput(node, j);
  228. auto input = kernel_with_index.first;
  229. auto index = kernel_with_index.second;
  230. if (!AnfAlgo::OutputAddrExist(input, index)) {
  231. continue;
  232. }
  233. auto addr = AnfAlgo::GetOutputAddr(input, index);
  234. MS_EXCEPTION_IF_NULL(addr);
  235. std::string tensor_name = GetKernelNodeName(node);
  236. size_t slot = j;
  237. if (IsDeviceTargetGPU()) {
  238. auto input_kernel = node->input(j + 1);
  239. std::string input_kernel_name = GetKernelNodeName(input_kernel);
  240. tensor_name = input_kernel_name;
  241. slot = 0;
  242. }
  243. ShapeVector int_shapes;
  244. GetDumpIntShape(input, index, NOT_NULL(&int_shapes), trans_flag);
  245. auto type = AnfAlgo::GetOutputInferDataType(input, index);
  246. auto device_type = AnfAlgo::GetOutputDeviceDataType(input, index);
  247. std::string op_type = AnfAlgo::GetCNodeName(node);
  248. std::string op_name = GetOpNameWithoutScope(*kernel_name);
  249. uint64_t timestamp = GetTimeStamp();
  250. uint32_t task_id = 0;
  251. uint32_t stream_id = 0;
  252. std::string file_path = dump_path + '/' + op_type + '.' + op_name + '.' + std::to_string(task_id) + '.' +
  253. std::to_string(stream_id) + '.' + std::to_string(timestamp) + ".input." + std::to_string(j);
  254. MS_EXCEPTION_IF_NULL(addr);
  255. if (IsDeviceTargetGPU()) {
  256. if (DumpJsonParser::GetInstance().IsStatisticDump()) {
  257. TensorStatDump stat_dump(op_type, op_name, task_id, stream_id, timestamp, true, j, slot);
  258. stat_dump.DumpTensorStatsToFile(tensor_name, dump_path, debugger);
  259. }
  260. if (DumpJsonParser::GetInstance().IsTensorDump()) {
  261. DumpGPUMemToFile(file_path, tensor_name, *addr, int_shapes, type, device_type, trans_flag, slot, debugger);
  262. }
  263. } else {
  264. DumpMemToFile(file_path, *addr, int_shapes, type, trans_flag);
  265. }
  266. }
  267. }
  268. void E2eDump::DumpSingleAnfNode(const AnfNodePtr &anf_node, const size_t output_index, const std::string &dump_path,
  269. bool trans_flag, const Debugger *debugger) {
  270. MS_EXCEPTION_IF_NULL(anf_node);
  271. auto &dump_json_parser = DumpJsonParser::GetInstance();
  272. if ((!anf_node->isa<Parameter>() && !anf_node->isa<ValueNode>()) || IsValueNode<StringImm>(anf_node)) {
  273. return;
  274. }
  275. std::string node_name = GetKernelNodeName(anf_node);
  276. if (!dump_json_parser.NeedDump(node_name)) {
  277. return;
  278. }
  279. DumpJsonParser::GetInstance().MatchKernel(node_name);
  280. GetFileKernelName(NOT_NULL(&node_name));
  281. std::string dump_name = node_name;
  282. const std::string cst_prefix = "Default--";
  283. if (anf_node->isa<ValueNode>()) {
  284. if (dump_name.find(cst_prefix) == std::string::npos) {
  285. MS_LOG(INFO) << "Incorrect constant format: " << dump_name;
  286. return;
  287. }
  288. dump_name = node_name.substr(cst_prefix.length());
  289. trans_flag = false;
  290. }
  291. // check if output address exists, if not, return;
  292. if (!AnfAlgo::OutputAddrExist(anf_node, output_index)) {
  293. return;
  294. }
  295. auto addr = AnfAlgo::GetOutputAddr(anf_node, output_index);
  296. MS_EXCEPTION_IF_NULL(addr);
  297. ShapeVector int_shapes;
  298. GetDumpIntShape(anf_node, output_index, NOT_NULL(&int_shapes), trans_flag);
  299. auto type = AnfAlgo::GetOutputInferDataType(anf_node, output_index);
  300. auto device_type = AnfAlgo::GetOutputDeviceDataType(anf_node, output_index);
  301. uint64_t timestamp = GetTimeStamp();
  302. uint32_t task_id = 0;
  303. uint32_t stream_id = 0;
  304. std::string file_path = dump_path + "/Parameter." + dump_name + '.' + std::to_string(task_id) + '.' +
  305. std::to_string(stream_id) + '.' + std::to_string(timestamp) + ".output.0";
  306. if (IsDeviceTargetGPU()) {
  307. if (dump_json_parser.IsStatisticDump()) {
  308. TensorStatDump stat_dump("Parameter", dump_name, task_id, stream_id, timestamp, false, 0, 0);
  309. stat_dump.DumpTensorStatsToFile(node_name, dump_path, debugger);
  310. }
  311. if (dump_json_parser.IsTensorDump()) {
  312. DumpGPUMemToFile(file_path, node_name, *addr, int_shapes, type, device_type, trans_flag, 0, debugger);
  313. }
  314. } else {
  315. DumpMemToFile(file_path, *addr, int_shapes, type, trans_flag);
  316. }
  317. }
  318. /*
  319. * Feature group: Dump.
  320. * Target device group: Ascend, GPU.
  321. * Runtime category: MindRT.
  322. * Description: This function is similar to DumpSingleAnfNode function but it is only for dumping parameters in mindRT.
  323. * This function uses GetParameterInfo to get dump info for the parameter node.
  324. */
  325. void E2eDump::DumpSingleParameterNode(const AnfNodePtr &anf_node, const std::string &dump_path, bool trans_flag,
  326. const Debugger *debugger) {
  327. MS_EXCEPTION_IF_NULL(anf_node);
  328. auto &dump_json_parser = DumpJsonParser::GetInstance();
  329. std::string node_name = GetKernelNodeName(anf_node);
  330. if (!anf_node->isa<Parameter>() || !dump_json_parser.NeedDump(node_name)) {
  331. return;
  332. }
  333. DumpJsonParser::GetInstance().MatchKernel(node_name);
  334. GetFileKernelName(NOT_NULL(&node_name));
  335. ShapeVector int_shapes;
  336. TypeId type;
  337. TypeId device_type;
  338. auto addr = GetParameterInfo(anf_node, NOT_NULL(&int_shapes), NOT_NULL(&type), NOT_NULL(&device_type));
  339. if (addr == nullptr) {
  340. MS_LOG(DEBUG) << "Skip node: " << node_name << ". Parameter data is not available for mindRT.";
  341. return;
  342. }
  343. uint64_t timestamp = GetTimeStamp();
  344. uint32_t task_id = 0;
  345. uint32_t stream_id = 0;
  346. std::string file_path = dump_path + "/Parameter." + node_name + '.' + std::to_string(task_id) + '.' +
  347. std::to_string(stream_id) + '.' + std::to_string(timestamp) + ".output.0";
  348. if (IsDeviceTargetGPU()) {
  349. if (dump_json_parser.IsStatisticDump()) {
  350. TensorStatDump stat_dump("Parameter", node_name, task_id, stream_id, timestamp, false, 0, 0);
  351. stat_dump.DumpTensorStatsToFile(node_name, dump_path, debugger);
  352. }
  353. if (dump_json_parser.IsTensorDump()) {
  354. DumpGPUMemToFile(file_path, node_name, *addr, int_shapes, type, device_type, trans_flag, 0, debugger);
  355. }
  356. } else {
  357. DumpMemToFile(file_path, *addr, int_shapes, type, trans_flag);
  358. }
  359. }
  360. void E2eDump::DumpParameters(const session::KernelGraph *graph, const std::string &dump_path,
  361. const Debugger *debugger) {
  362. MS_EXCEPTION_IF_NULL(graph);
  363. auto &dump_json_parser = DumpJsonParser::GetInstance();
  364. if (!dump_json_parser.OutputNeedDump()) {
  365. return;
  366. }
  367. MS_LOG(INFO) << "Start e2e dump parameters";
  368. bool trans_flag = dump_json_parser.trans_flag();
  369. // dump parameters
  370. const auto &parameters = graph->inputs();
  371. for (auto &item : parameters) {
  372. DumpSingleAnfNode(item, PARAMETER_OUTPUT_INDEX, dump_path, trans_flag, debugger);
  373. }
  374. }
  375. void E2eDump::DumpConstantData(const session::KernelGraph *graph, uint32_t rank_id, const Debugger *debugger) {
  376. MS_EXCEPTION_IF_NULL(graph);
  377. auto &dump_json_parser = DumpJsonParser::GetInstance();
  378. if (!IsDeviceTargetGPU() || !dump_json_parser.e2e_dump_enabled()) {
  379. return;
  380. }
  381. uint32_t graph_id = graph->graph_id();
  382. std::string cst_path = GenerateDumpPath(graph_id, rank_id, true);
  383. if (!Common::FileExists(cst_path)) {
  384. DumpConstantData(graph, cst_path, debugger);
  385. }
  386. }
  387. void E2eDump::DumpConstantData(const session::KernelGraph *graph, const std::string &cst_dump_path,
  388. const Debugger *debugger) {
  389. // Dump constant to npy file
  390. MS_EXCEPTION_IF_NULL(graph);
  391. auto &dump_json_parser = DumpJsonParser::GetInstance();
  392. MS_LOG(INFO) << "DumpConstants. Current iteration is " << dump_json_parser.cur_dump_iter();
  393. MS_LOG(INFO) << "Current graph id is " << graph->graph_id();
  394. if (!dump_json_parser.OutputNeedDump()) {
  395. return;
  396. }
  397. const auto value_nodes = graph->graph_value_nodes();
  398. for (auto &item : value_nodes) {
  399. DumpSingleAnfNode(item, VALUE_NODE_OUTPUT_INDEX, cst_dump_path, false, debugger);
  400. }
  401. }
  402. /*
  403. * Feature group: Dump.
  404. * Target device group: Ascend, GPU.
  405. * Runtime category: Old runtime.
  406. * Description: This function is for updating dump iteration for GPU and ascend old runtime.
  407. */
  408. void E2eDump::UpdateIterOldRTDump(const session::KernelGraph *graph) {
  409. MS_EXCEPTION_IF_NULL(graph);
  410. auto &dump_json_parser = DumpJsonParser::GetInstance();
  411. uint32_t graph_id = graph->graph_id();
  412. if (IsDeviceTargetGPU()) {
  413. if (starting_graph_id == INT32_MAX) {
  414. starting_graph_id = graph_id;
  415. } else if (starting_graph_id == graph_id && !MsContext::GetInstance()->get_param<bool>(MS_CTX_ENABLE_MINDRT)) {
  416. // Update dump iter for mindrt runtime is done using UpdateIterGPUDump().
  417. // Update dump iter for GPU old runtime.
  418. dump_json_parser.UpdateDumpIter();
  419. }
  420. return;
  421. }
  422. // If device target is Ascend
  423. if (graph->IsDatasetGraph()) {
  424. MS_LOG(INFO) << "No need to update iteration for dataset graph.";
  425. return;
  426. }
  427. // In multi network scripts, dump iter is equal to the number of networks that have been executed so far.
  428. dump_json_parser.UpdateDumpIter();
  429. }
  430. /*
  431. * Feature group: Dump.
  432. * Target device group: Ascend, GPU.
  433. * Runtime category: MindRT.
  434. * Description: This function is for updating dump iteration for GPU and ascend MindRT dump. Please note that dump with
  435. * dataset_sink_mode = True is not supported for GPU.
  436. */
  437. void E2eDump::UpdateIterMindRTDump() {
  438. auto debugger = Debugger::GetInstance();
  439. // Dataset graph is always the first graph in the list when dataset_sink_mode is true.
  440. auto graph = (debugger->GetStepGraphPtrList())[0];
  441. auto context = MsContext::GetInstance();
  442. MS_EXCEPTION_IF_NULL(context);
  443. if (context->get_param<std::string>(MS_CTX_DEVICE_TARGET) == kAscendDevice && graph->IsDatasetGraph()) {
  444. MS_LOG(INFO) << "No need to update iteration for dataset graph.";
  445. return;
  446. }
  447. // update dump iter for GPU and kernel by kernel ascend dump.
  448. DumpJsonParser::GetInstance().UpdateDumpIter();
  449. }
  450. /*
  451. * Feature group: Dump.
  452. * Target device group: Ascend, GPU.
  453. * Runtime category: Old runtime, MindRT.
  454. * Description: Generates graph history files (dumping all the iteration numbers in which the graph was executed) for
  455. * the given graph and rank_id. If dataset_sink_mode is true for async dump in ascend, this function is called once per
  456. * each epoch and dumps all the iterations in the epoch to the graph history file.
  457. */
  458. void E2eDump::DumpRunIter(const KernelGraphPtr &graph, uint32_t rank_id) {
  459. auto &json_parser = DumpJsonParser::GetInstance();
  460. if (!(json_parser.async_dump_enabled() || json_parser.e2e_dump_enabled())) {
  461. return;
  462. }
  463. bool sink_mode = (ConfigManager::GetInstance().dataset_mode() || graph->IsDatasetGraph());
  464. auto iter_num = SizeToInt(LongToSize(ConfigManager::GetInstance().iter_num()));
  465. if (graph->IsDatasetGraph()) {
  466. MS_LOG(INFO) << "graph: " << graph->graph_id() << " is dataset graph, not creating graph history file.";
  467. return;
  468. }
  469. std::string execution_order_path = json_parser.path() + "/rank_" + std::to_string(rank_id) + "/execution_order/";
  470. std::string file_name_to_check =
  471. execution_order_path + "/ms_global_execution_order_graph_" + std::to_string(graph->graph_id()) + ".csv";
  472. auto real_path = Common::CreatePrefixPath(file_name_to_check);
  473. if (!real_path.has_value()) {
  474. MS_LOG(WARNING) << "Check file path: " << file_name_to_check << " failed.";
  475. return;
  476. }
  477. std::string file_name = real_path.value();
  478. ChangeFileMode(file_name, S_IWUSR);
  479. std::ofstream fout(file_name, std::ofstream::app);
  480. if (!fout.is_open()) {
  481. MS_LOG(WARNING) << "Open file for saving graph global execution order failed.";
  482. return;
  483. }
  484. if (sink_mode && json_parser.async_dump_enabled() && !Debugger::GetInstance()->GetAscendKernelByKernelFlag()) {
  485. // for async dump when sink_mode = true, cur_dump_iter() = current_epoch
  486. // dump history for all iterations in the epoch
  487. Debugger::GetInstance()->UpdateGraphIterMap(graph->graph_id(), iter_num);
  488. auto graph_iter_map = Debugger::GetInstance()->GetGraphIterMap();
  489. auto step_per_epoch = graph_iter_map[graph->graph_id()];
  490. for (int i = 0; i < step_per_epoch; i++) {
  491. auto step = (json_parser.cur_dump_iter() * step_per_epoch) + i;
  492. fout << (std::to_string(step) + "\n");
  493. }
  494. } else {
  495. fout << std::to_string(json_parser.cur_dump_iter()) + "\n";
  496. }
  497. fout.close();
  498. ChangeFileMode(file_name, S_IRUSR);
  499. }
  500. /*
  501. * Feature group: Dump.
  502. * Target device group: Ascend, GPU.
  503. * Runtime category: Old runtime, MindRT.
  504. * Description: This function is for dumping the whole graph. It is used for old runtime in GPU and Ascend and
  505. * super-kernel mindRT in Ascend.
  506. */
  507. void E2eDump::DumpData(const session::KernelGraph *graph, uint32_t rank_id, const Debugger *debugger) {
  508. MS_EXCEPTION_IF_NULL(graph);
  509. bool success = false;
  510. auto &dump_json_parser = DumpJsonParser::GetInstance();
  511. uint32_t graph_id = graph->graph_id();
  512. if (!dump_json_parser.e2e_dump_enabled()) {
  513. return;
  514. }
  515. if (dump_json_parser.GetIterDumpFlag()) {
  516. MS_LOG(INFO) << "Start e2e dump. Current iteration is " << dump_json_parser.cur_dump_iter();
  517. MS_LOG(INFO) << "Current graph id is " << graph_id;
  518. std::string dump_path = GenerateDumpPath(graph_id, rank_id);
  519. if (dump_json_parser.IsStatisticDump()) {
  520. TensorStatDump::OpenStatisticsFile(dump_path);
  521. }
  522. DumpInput(graph, dump_path, debugger);
  523. DumpOutput(graph, dump_path, debugger);
  524. if (!MsContext::GetInstance()->get_param<bool>(MS_CTX_ENABLE_MINDRT)) {
  525. // Dump parameters for old runtime. For mindRT it is done in PostExecuteGraphDebugger.
  526. DumpParameters(graph, dump_path, debugger);
  527. // DumpConstantData for GPU old runtime.
  528. DumpConstantData(graph, rank_id, debugger);
  529. }
  530. if (dump_json_parser.IsStatisticDump()) {
  531. CsvWriter::GetInstance().CloseFile();
  532. }
  533. success = true;
  534. }
  535. if (success) {
  536. MS_LOG(DEBUG) << "E2eDump Dump Data completed!";
  537. } else {
  538. MS_LOG(DEBUG) << "E2eDump Dump has not occurred!";
  539. }
  540. }
  541. /*
  542. * Feature group: Dump.
  543. * Target device group: Ascend, GPU.
  544. * Runtime category: MindRT.
  545. * Description: This function is for dumping a single node. It is used for mindrt in GPU and Ascend kernel-by-kernel.
  546. */
  547. bool E2eDump::DumpSingleNodeData(const CNodePtr &node, uint32_t graph_id, uint32_t rank_id, const Debugger *debugger) {
  548. bool success = false;
  549. auto &dump_json_parser = DumpJsonParser::GetInstance();
  550. if (dump_json_parser.DumpEnabledForIter()) {
  551. std::string dump_path = GenerateDumpPath(graph_id, rank_id);
  552. DumpInputSingleNode(node, dump_path, debugger);
  553. DumpOutputSingleNode(node, dump_path, debugger);
  554. success = true;
  555. }
  556. return success;
  557. }
  558. /*
  559. * Feature group: Dump.
  560. * Target device group: Ascend, GPU.
  561. * Runtime category: MindRT.
  562. * Description: This function is for dumping all the parameters in the current root graph for GPU, Ascend superkernel
  563. * (e2e dump) and Ascend kernel-by-kernel (e2e and async dump).
  564. */
  565. void E2eDump::DumpParametersData(uint32_t rank_id, const Debugger *debugger) {
  566. uint32_t root_graph_id = debugger->GetCurrentRootGraphId();
  567. auto &dump_json_parser = DumpJsonParser::GetInstance();
  568. if (dump_json_parser.async_dump_enabled() && !debugger->GetAscendKernelByKernelFlag()) {
  569. // Dump parameters for mindRT in async dump only for kernel by kernel mode.
  570. return;
  571. }
  572. if (dump_json_parser.DumpEnabledForIter()) {
  573. MS_LOG(INFO) << "DumpParameters. Current iteration is " << dump_json_parser.cur_dump_iter();
  574. MS_LOG(INFO) << "Current root graph id is " << root_graph_id;
  575. std::string dump_path = GenerateDumpPath(root_graph_id, rank_id);
  576. bool trans_flag = dump_json_parser.trans_flag();
  577. for (auto &item : debugger->GetParametersMindRT()) {
  578. DumpSingleParameterNode(item, dump_path, trans_flag, debugger);
  579. }
  580. }
  581. }
  582. #ifdef ENABLE_D
  583. /*
  584. * Feature group: Dump.
  585. * Target device group: Ascend.
  586. * Runtime category: Old runtime, MindRT.
  587. * Description: This function is for ascend A+M dump only. It parses and converts each slot of tensor in DumpData object
  588. * and dump the tensor data in npy file or statistic data in csv file.
  589. */
  590. void E2eDump::DumpTensorToFile(const std::string &dump_path, const debugger::dump::DumpData &dump_data,
  591. char *data_ptr) {
  592. // dump input tensors
  593. std::vector<debugger::dump::OpInput> input_tensors(dump_data.input().begin(), dump_data.input().end());
  594. uint64_t offset = 0;
  595. for (uint32_t slot = 0; slot < input_tensors.size(); slot++) {
  596. auto in_tensor = input_tensors[slot];
  597. auto succ = ConvertFormatForTensorAndDump(dump_path, in_tensor, data_ptr + offset, "input", slot);
  598. if (!succ) {
  599. MS_LOG(INFO) << "Failed to convert format for tensor " << dump_path << ".input." << slot;
  600. }
  601. offset += in_tensor.size();
  602. }
  603. // dump output tensors
  604. std::vector<debugger::dump::OpOutput> output_tensors(dump_data.output().begin(), dump_data.output().end());
  605. for (uint32_t slot = 0; slot < output_tensors.size(); slot++) {
  606. auto out_tensor = output_tensors[slot];
  607. auto succ = ConvertFormatForTensorAndDump(dump_path, out_tensor, data_ptr + offset, "output", slot);
  608. if (!succ) {
  609. MS_LOG(INFO) << "Failed to convert format for tensor " << dump_path << ".output." << slot;
  610. }
  611. offset += out_tensor.size();
  612. }
  613. }
  614. /*
  615. * Feature group: Dump.
  616. * Target device group: Ascend.
  617. * Runtime category: Old runtime, MindRT.
  618. * Description: It serves for A+M dump. Save statistic of the tensor data into dump path as configured.
  619. */
  620. template <typename T>
  621. bool DumpTensorStatsIfNeeded(const std::string &dump_path, const T &tensor, char *data_ptr, const std::string &io,
  622. uint32_t slot, const ShapeVector &shape, TypeId type) {
  623. // dump_path: dump_dir/op_type.op_name.task_id.stream_id.timestamp
  624. if (!DumpJsonParser::GetInstance().IsStatisticDump()) {
  625. return true;
  626. }
  627. size_t pos = dump_path.rfind("/");
  628. std::string file_name = dump_path.substr(pos + 1);
  629. size_t first_dot = file_name.find(".");
  630. size_t fourth_dot = file_name.rfind(".");
  631. size_t third_dot = file_name.rfind(".", fourth_dot - 1);
  632. size_t second_dot = file_name.rfind(".", third_dot - 1);
  633. if (first_dot == std::string::npos || second_dot == std::string::npos || third_dot == std::string::npos ||
  634. first_dot == second_dot) {
  635. MS_LOG(ERROR) << "Dump path " << dump_path << " received is not well formed";
  636. return false;
  637. }
  638. std::string op_type = file_name.substr(0, first_dot);
  639. std::string op_name = file_name.substr(first_dot + 1, second_dot - first_dot - 1);
  640. std::string task_id = file_name.substr(second_dot + 1, third_dot - second_dot - 1);
  641. std::string stream_id = file_name.substr(third_dot + 1, fourth_dot - third_dot - 1);
  642. std::string timestamp = file_name.substr(fourth_dot + 1);
  643. TensorStatDump stat_dump(op_type, op_name, task_id, stream_id, timestamp, io, slot, slot);
  644. std::shared_ptr<TensorData> data = std::make_shared<TensorData>();
  645. if (type <= TypeId::kNumberTypeBegin || type >= TypeId::kNumberTypeComplex64) {
  646. MS_LOG(ERROR) << "Data type of operator " << file_name << " is not supported by statistic dump";
  647. return false;
  648. }
  649. data->SetType(type);
  650. data->SetByteSize((size_t)tensor.size());
  651. data->SetShape(shape);
  652. data->SetDataPtr(data_ptr);
  653. return stat_dump.DumpTensorStatsToFile(dump_path.substr(0, pos), data);
  654. }
  655. /*
  656. * Feature group: Dump.
  657. * Target device group: Ascend.
  658. * Runtime category: Old runtime, MindRT.
  659. * Description: It serves for A+M dump. Parse each attributes in Dumpdata proto object from device format to mindspore
  660. * supported format and save tensor data or statistic as configured.
  661. */
  662. template <typename T>
  663. bool E2eDump::ConvertFormatForTensorAndDump(std::string dump_path, const T &tensor, char *data_ptr,
  664. const std::string &io, uint32_t slot) {
  665. // dump_path: dump_dir/op_type.op_name.task_id.stream_id.timestamp
  666. std::ostringstream dump_path_ss;
  667. dump_path_ss << dump_path << "." << io << "." << slot << ".";
  668. std::string dump_path_slot = dump_path_ss.str();
  669. // get format
  670. auto iter_fmt = kFormatToStringMap.find(tensor.format());
  671. if (iter_fmt == kFormatToStringMap.end()) {
  672. MS_LOG(INFO) << "Unsupported tensor format for tensor " << dump_path << ": unknown(" << tensor.format() << ")";
  673. return false;
  674. }
  675. std::string device_format = iter_fmt->second;
  676. // get data type
  677. auto iter_dtype = kDataTypetoMSTypeMap.find(tensor.data_type());
  678. if (iter_dtype == kDataTypetoMSTypeMap.end()) {
  679. MS_LOG(INFO) << "Unsupported data type for tensor " << dump_path << ": unknown(" << tensor.data_type() << ")";
  680. return false;
  681. }
  682. auto src_type = iter_dtype->second;
  683. // get host shape
  684. std::vector<size_t> device_shape;
  685. (void)std::copy(tensor.shape().dim().begin(), tensor.shape().dim().end(), std::back_inserter(device_shape));
  686. ShapeVector shape_d;
  687. (void)std::transform(device_shape.begin(), device_shape.end(), std::back_inserter(shape_d), SizeToLong);
  688. std::vector<size_t> host_shape;
  689. (void)std::copy(tensor.original_shape().dim().begin(), tensor.original_shape().dim().end(),
  690. std::back_inserter(host_shape));
  691. ShapeVector shape_to;
  692. (void)std::transform(host_shape.begin(), host_shape.end(), std::back_inserter(shape_to), SizeToLong);
  693. size_t data_size = (size_t)tensor.size();
  694. bool trans_success = false;
  695. auto trans_buf = std::vector<uint8_t>(data_size);
  696. // convert format to host format. It can be either NCHW or ND (non 4-dimemsions).
  697. const uint8_t kNumFourDim = 4;
  698. std::string host_format;
  699. if (host_shape.size() == kNumFourDim) {
  700. host_format = kOpFormat_NCHW;
  701. } else {
  702. host_format = kOpFormat_ND;
  703. }
  704. if (device_format != host_format) {
  705. auto iter = kSuppTransFormatPair.find(std::make_pair(device_format, host_format));
  706. if (iter == kSuppTransFormatPair.end()) {
  707. MS_LOG(INFO) << "Do not support convert from format " << device_format << " to " << host_format << " for tensor "
  708. << dump_path_slot;
  709. } else {
  710. const trans::FormatArgs format_args{data_ptr, data_size, host_format, device_format, shape_to, shape_d, src_type};
  711. auto group = tensor.sub_format() > 1 ? tensor.sub_format() : 1;
  712. trans_success = trans::TransFormatFromDeviceToHost(format_args, trans_buf.data(), group);
  713. if (!trans_success) {
  714. MS_LOG(ERROR) << "Trans format failed.";
  715. }
  716. }
  717. }
  718. // dump tensor data into npy file
  719. bool dump_success = true;
  720. if (trans_success) {
  721. dump_success = DumpTensorStatsIfNeeded(dump_path, tensor, reinterpret_cast<char *>(trans_buf.data()), io, slot,
  722. shape_to, src_type);
  723. if (DumpJsonParser::GetInstance().IsTensorDump()) {
  724. dump_path_slot += host_format;
  725. dump_success =
  726. DumpJsonParser::DumpToFile(dump_path_slot, trans_buf.data(), data_size, shape_to, src_type) && dump_success;
  727. }
  728. } else {
  729. dump_success = DumpTensorStatsIfNeeded(dump_path, tensor, data_ptr, io, slot, shape_to, src_type);
  730. if (DumpJsonParser::GetInstance().IsTensorDump()) {
  731. dump_path_slot += device_format;
  732. dump_success =
  733. DumpJsonParser::DumpToFile(dump_path_slot, data_ptr, data_size, shape_to, src_type) && dump_success;
  734. }
  735. }
  736. return dump_success;
  737. }
  738. uint64_t UnpackUint64Value(char *ptr) {
  739. #if defined(__APPLE__)
  740. return *reinterpret_cast<const uint64_t *>(ptr);
  741. #else
  742. return le16toh(*reinterpret_cast<const uint64_t *>(ptr));
  743. #endif
  744. }
  745. std::string IntToHexString(const uint64_t value) {
  746. std::stringstream ss;
  747. ss << "0x" << std::hex << value;
  748. return ss.str();
  749. }
  750. nlohmann::json E2eDump::ParseOverflowInfo(char *data_ptr) {
  751. uint32_t index = 0;
  752. uint64_t model_id = UnpackUint64Value(data_ptr + index);
  753. index += kUint64Size;
  754. uint64_t stream_id = UnpackUint64Value(data_ptr + index);
  755. index += kUint64Size;
  756. uint64_t task_id = UnpackUint64Value(data_ptr + index);
  757. index += kUint64Size;
  758. uint64_t task_type = UnpackUint64Value(data_ptr + index);
  759. index += kUint64Size;
  760. uint64_t pc_start = UnpackUint64Value(data_ptr + index);
  761. index += kUint64Size;
  762. uint64_t para_base = UnpackUint64Value(data_ptr + index);
  763. nlohmann::json overflow_info;
  764. overflow_info["model_id"] = model_id;
  765. overflow_info["stream_id"] = stream_id;
  766. overflow_info["task_id"] = task_id;
  767. overflow_info["task_type"] = task_type;
  768. overflow_info["pc_start"] = IntToHexString(pc_start);
  769. overflow_info["para_base"] = IntToHexString(para_base);
  770. return overflow_info;
  771. }
  772. /*
  773. * Feature group: Dump.
  774. * Target device group: Ascend.
  775. * Runtime category: Old runtime, MindRT.
  776. * Description: This function is for Ascend A+M dump. It parses and dump op overflow info in json file.
  777. */
  778. void E2eDump::DumpOpDebugToFile(const std::string &dump_path, const debugger::dump::DumpData &dump_data,
  779. char *data_ptr) {
  780. std::string out_path = dump_path + ".output.";
  781. std::vector<debugger::dump::OpOutput> op_debug(dump_data.output().begin(), dump_data.output().end());
  782. for (uint32_t slot = 0; slot < op_debug.size(); slot++) {
  783. uint32_t index = 0;
  784. // parse DHA Atomic Add info
  785. nlohmann::json dha_atomic_add_info = ParseOverflowInfo(data_ptr + index);
  786. index += kDhaAtomicAddInfoSize;
  787. // parse L2 Atomic Add info
  788. nlohmann::json l2_atomic_add_info = ParseOverflowInfo(data_ptr + index);
  789. index += kL2AtomicAddInfoSize;
  790. // parse AICore info
  791. nlohmann::json ai_core_info = ParseOverflowInfo(data_ptr + index);
  792. index += kAiCoreInfoSize;
  793. // parse DHA Atomic Add status
  794. dha_atomic_add_info["status"] = UnpackUint64Value(data_ptr + index);
  795. index += kDhaAtomicAddStatusSize;
  796. // parse L2 Atomic Add status
  797. l2_atomic_add_info["status"] = UnpackUint64Value(data_ptr + index);
  798. index += kL2AtomicAddStatusSize;
  799. // parse AICore status
  800. uint64_t kernel_code = UnpackUint64Value(data_ptr + index);
  801. index += kUint64Size;
  802. uint64_t block_idx = UnpackUint64Value(data_ptr + index);
  803. index += kUint64Size;
  804. uint64_t status = UnpackUint64Value(data_ptr + index);
  805. ai_core_info["kernel_code"] = IntToHexString(kernel_code);
  806. ai_core_info["block_idx"] = block_idx;
  807. ai_core_info["status"] = status;
  808. nlohmann::json opdebug_data;
  809. opdebug_data["DHA Atomic Add"] = dha_atomic_add_info;
  810. opdebug_data["L2 Atomic Add"] = l2_atomic_add_info;
  811. opdebug_data["AI Core"] = ai_core_info;
  812. // save json to file
  813. DumpToFile(out_path + std::to_string(slot) + ".json", opdebug_data.dump());
  814. }
  815. }
  816. #endif // ENABLE_D
  817. } // namespace mindspore