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.

data_dumper.cc 43 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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 "graph/load/model_manager/data_dumper.h"
  17. #include <cstdlib>
  18. #include <ctime>
  19. #include <map>
  20. #include <utility>
  21. #include <vector>
  22. #include "common/debug/memory_dumper.h"
  23. #include "common/properties_manager.h"
  24. #include "common/util.h"
  25. #include "framework/common/debug/ge_log.h"
  26. #include "framework/common/util.h"
  27. #include "graph/anchor.h"
  28. #include "graph/debug/ge_attr_define.h"
  29. #include "graph/load/model_manager/model_utils.h"
  30. #include "graph/manager/util/debug.h"
  31. #include "graph/utils/attr_utils.h"
  32. #include "graph/utils/tensor_utils.h"
  33. #include "proto/dump_task.pb.h"
  34. #include "proto/ge_ir.pb.h"
  35. #include "proto/op_mapping_info.pb.h"
  36. #include "runtime/base.h"
  37. #include "runtime/mem.h"
  38. namespace {
  39. const uint32_t kAicpuLoadFlag = 1;
  40. const uint32_t kAicpuUnloadFlag = 0;
  41. const int64_t kOpDebugSize = 2048;
  42. const int64_t kOpDebugShape = 2048;
  43. const int8_t kDecimal = 10;
  44. const uint32_t kAddrLen = sizeof(void *);
  45. const char *const kDumpOutput = "output";
  46. const char *const kDumpInput = "input";
  47. const char *const kDumpAll = "all";
  48. // parse for format like nodename:input:index
  49. static bool ParseNameIndex(const std::string &node_name_index, std::string &node_name, std::string &input_or_output,
  50. size_t &index) {
  51. auto sep = node_name_index.rfind(':');
  52. if (sep == std::string::npos) {
  53. return false;
  54. }
  55. auto index_str = node_name_index.substr(sep + 1);
  56. index = static_cast<size_t>(std::strtol(index_str.c_str(), nullptr, kDecimal));
  57. auto node_name_without_index = node_name_index.substr(0, sep);
  58. sep = node_name_without_index.rfind(':');
  59. if (sep == std::string::npos) {
  60. return false;
  61. }
  62. node_name = node_name_without_index.substr(0, sep);
  63. input_or_output = node_name_without_index.substr(sep + 1);
  64. return !(input_or_output != kDumpInput && input_or_output != kDumpOutput);
  65. }
  66. static bool IsTensorDescWithSkipDumpAddrType(bool has_mem_type_attr, vector<int64_t> v_memory_type, size_t i) {
  67. return has_mem_type_attr && (v_memory_type[i] == RT_MEMORY_L1);
  68. }
  69. static uint64_t GetNowTime() {
  70. uint64_t ret = 0;
  71. mmTimeval tv;
  72. if (mmGetTimeOfDay(&tv, nullptr) == 0) {
  73. ret = tv.tv_sec * 1000000ULL + tv.tv_usec;
  74. }
  75. return ret;
  76. }
  77. static void ReplaceStringElem(std::string &str) {
  78. for_each(str.begin(), str.end(), [](char &ch) {
  79. if ((ch == ' ') || (ch == '.') || (ch == '/') || (ch == '\\')) {
  80. ch = '_';
  81. }
  82. });
  83. }
  84. } // namespace
  85. static int32_t GetIrDataType(ge::DataType data_type) {
  86. static const std::map<ge::DataType, ge::proto::DataType> data_type_map = {
  87. {ge::DT_UNDEFINED, ge::proto::DT_UNDEFINED},
  88. {ge::DT_FLOAT, ge::proto::DT_FLOAT},
  89. {ge::DT_FLOAT16, ge::proto::DT_FLOAT16},
  90. {ge::DT_INT8, ge::proto::DT_INT8},
  91. {ge::DT_UINT8, ge::proto::DT_UINT8},
  92. {ge::DT_INT16, ge::proto::DT_INT16},
  93. {ge::DT_UINT16, ge::proto::DT_UINT16},
  94. {ge::DT_INT32, ge::proto::DT_INT32},
  95. {ge::DT_INT64, ge::proto::DT_INT64},
  96. {ge::DT_UINT32, ge::proto::DT_UINT32},
  97. {ge::DT_UINT64, ge::proto::DT_UINT64},
  98. {ge::DT_BOOL, ge::proto::DT_BOOL},
  99. {ge::DT_DOUBLE, ge::proto::DT_DOUBLE},
  100. {ge::DT_DUAL, ge::proto::DT_DUAL},
  101. {ge::DT_DUAL_SUB_INT8, ge::proto::DT_DUAL_SUB_INT8},
  102. {ge::DT_DUAL_SUB_UINT8, ge::proto::DT_DUAL_SUB_UINT8},
  103. {ge::DT_COMPLEX64, ge::proto::DT_COMPLEX64},
  104. {ge::DT_COMPLEX128, ge::proto::DT_COMPLEX128},
  105. {ge::DT_QINT8, ge::proto::DT_QINT8},
  106. {ge::DT_QINT16, ge::proto::DT_QINT16},
  107. {ge::DT_QINT32, ge::proto::DT_QINT32},
  108. {ge::DT_QUINT8, ge::proto::DT_QUINT8},
  109. {ge::DT_QUINT16, ge::proto::DT_QUINT16},
  110. {ge::DT_RESOURCE, ge::proto::DT_RESOURCE},
  111. {ge::DT_STRING_REF, ge::proto::DT_STRING_REF},
  112. {ge::DT_STRING, ge::proto::DT_STRING},
  113. {ge::DT_VARIANT, ge::proto::DT_VARIANT},
  114. };
  115. auto iter = data_type_map.find(data_type);
  116. if (iter == data_type_map.end()) {
  117. return static_cast<int32_t>(ge::proto::DT_UNDEFINED);
  118. }
  119. return static_cast<int32_t>(iter->second);
  120. }
  121. namespace ge {
  122. DataDumper::~DataDumper() {
  123. ReleaseDevMem(&dev_mem_load_);
  124. ReleaseDevMem(&dev_mem_unload_);
  125. }
  126. void DataDumper::ReleaseDevMem(void **ptr) noexcept {
  127. if (ptr == nullptr) {
  128. return;
  129. }
  130. if (*ptr != nullptr) {
  131. rtError_t rt_ret = rtFree(*ptr);
  132. if (rt_ret != RT_ERROR_NONE) {
  133. GELOGE(RT_FAILED, "Call rtFree failed, ret: 0x%X", rt_ret);
  134. }
  135. *ptr = nullptr;
  136. }
  137. }
  138. void DataDumper::SetLoopAddr(void *global_step, void *loop_per_iter, void *loop_cond) {
  139. global_step_ = reinterpret_cast<uintptr_t>(global_step);
  140. loop_per_iter_ = reinterpret_cast<uintptr_t>(loop_per_iter);
  141. loop_cond_ = reinterpret_cast<uintptr_t>(loop_cond);
  142. }
  143. void DataDumper::SaveDumpInput(const std::shared_ptr<Node> &node) {
  144. if (node != nullptr) {
  145. auto input_op_desc = node->GetOpDesc();
  146. if (input_op_desc == nullptr) {
  147. GELOGE(PARAM_INVALID, "input op desc is null.");
  148. return;
  149. }
  150. for (auto &out_data_anchor : node->GetAllOutDataAnchors()) {
  151. for (auto &dst_in_data_anchor : out_data_anchor->GetPeerInDataAnchors()) {
  152. ge::NodePtr dst_node = dst_in_data_anchor->GetOwnerNode();
  153. auto op_desc = dst_node->GetOpDesc();
  154. if (op_desc == nullptr) {
  155. GELOGE(PARAM_INVALID, "input op desc is null.");
  156. return;
  157. }
  158. input_map_.insert(
  159. {op_desc->GetName(), {input_op_desc, dst_in_data_anchor->GetIdx(), out_data_anchor->GetIdx()}});
  160. }
  161. }
  162. }
  163. }
  164. void DataDumper::SaveEndGraphId(uint32_t task_id, uint32_t stream_id) {
  165. end_graph_task_id_ = task_id;
  166. end_graph_stream_id_ = stream_id;
  167. }
  168. void DataDumper::SaveOpDebugId(uint32_t task_id, uint32_t stream_id, void *op_debug_addr, bool is_op_debug) {
  169. op_debug_task_id_ = task_id;
  170. op_debug_stream_id_ = stream_id;
  171. op_debug_addr_ = op_debug_addr;
  172. is_op_debug_ = is_op_debug;
  173. }
  174. void DataDumper::SaveDumpOpInfo(const RuntimeParam &model_param, const OpDescPtr &op, uint32_t task_id,
  175. uint32_t stream_id) {
  176. GELOGD("Start SaveDumpOpInfo of task_id: %u, stream_id: %u", task_id, stream_id);
  177. OpDescInfo op_desc_info;
  178. op_desc_info.op_name = op->GetName();
  179. op_desc_info.op_type = op->GetType();
  180. op_desc_info.task_id = task_id;
  181. op_desc_info.stream_id = stream_id;
  182. for (size_t i = 0; i < op->GetAllInputsSize(); ++i) {
  183. GeTensorDescPtr input_tensor_desc = op->MutableInputDesc(i);
  184. if (input_tensor_desc == nullptr) {
  185. continue;
  186. }
  187. op_desc_info.input_format.emplace_back(input_tensor_desc->GetFormat());
  188. op_desc_info.input_shape.emplace_back(input_tensor_desc->GetShape().GetDims());
  189. op_desc_info.input_data_type.emplace_back(input_tensor_desc->GetDataType());
  190. int64_t input_size = 0;
  191. if (TensorUtils::GetTensorSizeInBytes(*input_tensor_desc, input_size) != SUCCESS) {
  192. GELOGW("Get input size failed");
  193. return;
  194. }
  195. GELOGD("Save dump op info, the input size is %ld", input_size);
  196. op_desc_info.input_size.emplace_back(input_size);
  197. }
  198. for (size_t j = 0; j < op->GetOutputsSize(); ++j) {
  199. GeTensorDescPtr output_tensor_desc = op->MutableOutputDesc(j);
  200. if (output_tensor_desc == nullptr) {
  201. continue;
  202. }
  203. op_desc_info.output_format.emplace_back(output_tensor_desc->GetFormat());
  204. op_desc_info.output_shape.emplace_back(output_tensor_desc->GetShape().GetDims());
  205. op_desc_info.output_data_type.emplace_back(output_tensor_desc->GetDataType());
  206. int64_t output_size = 0;
  207. if (TensorUtils::GetTensorSizeInBytes(*output_tensor_desc, output_size) != SUCCESS) {
  208. GELOGW("Get input size failed");
  209. return;
  210. }
  211. GELOGD("Save dump op info, the output size is %ld", output_size);
  212. op_desc_info.output_size.emplace_back(output_size);
  213. }
  214. op_desc_info.input_addrs = ModelUtils::GetInputDataAddrs(model_param, op);
  215. op_desc_info.output_addrs = ModelUtils::GetOutputDataAddrs(model_param, op);
  216. op_desc_info_.emplace_back(op_desc_info);
  217. }
  218. bool DataDumper::GetOpDescInfo(uint32_t stream_id, uint32_t task_id, OpDescInfo &op_desc_info) const {
  219. GELOGI("There are %zu op need to dump.", op_desc_info_.size());
  220. for (size_t index = 0; index < op_desc_info_.size(); ++index) {
  221. OpDescInfo dump_op_info = op_desc_info_.at(index);
  222. if (dump_op_info.task_id == task_id && dump_op_info.stream_id == stream_id) {
  223. GELOGI("find exception op of task_id: %u, stream_id: %u.", task_id, stream_id);
  224. op_desc_info = dump_op_info;
  225. return true;
  226. }
  227. }
  228. return false;
  229. }
  230. void DataDumper::SaveDumpTask(uint32_t task_id, uint32_t stream_id, const std::shared_ptr<OpDesc> &op_desc,
  231. uintptr_t args) {
  232. if (op_desc == nullptr) {
  233. GELOGE(PARAM_INVALID, "Opdesc is nullptr");
  234. return;
  235. }
  236. GELOGI("Save dump task %s, task id: %u, stream id: %u", op_desc->GetName().c_str(), task_id, stream_id);
  237. op_list_.push_back({task_id, stream_id, op_desc, args, true});
  238. for (auto iter = input_map_.equal_range(op_desc->GetName()); iter.first != iter.second; ++iter.first) {
  239. InnerInputMapping &inner_input_mapping = iter.first->second;
  240. auto &data_op = inner_input_mapping.data_op;
  241. if (data_op == nullptr) {
  242. GELOGE(PARAM_INVALID, "data_op is null.");
  243. return;
  244. }
  245. auto input_tensor = op_desc->GetInputDescPtr(inner_input_mapping.input_anchor_index);
  246. if (input_tensor == nullptr) {
  247. GELOGE(PARAM_INVALID, "input_tensor is null, index: %d, size: %zu.", inner_input_mapping.input_anchor_index,
  248. op_desc->GetInputsSize());
  249. return;
  250. }
  251. int64_t data_size = 0;
  252. if (AttrUtils::GetInt(input_tensor, ATTR_NAME_INPUT_ORIGIN_SIZE, data_size)) {
  253. GELOGI("Get aipp data size according to attr is %ld", data_size);
  254. } else if (TensorUtils::GetTensorSizeInBytes(*input_tensor, data_size) != SUCCESS) {
  255. GELOGE(PARAM_INVALID, "Get input size filed");
  256. return;
  257. }
  258. GELOGI("Save input dump task %s, id: %u,stream id :%u,data size :%ld", data_op->GetName().c_str(), task_id,
  259. stream_id, data_size);
  260. op_list_.push_back({task_id, stream_id, data_op, args, false, inner_input_mapping.input_anchor_index,
  261. inner_input_mapping.output_anchor_index, input_tensor->GetShape().GetDims(), data_size});
  262. }
  263. }
  264. static void SetOpMappingLoopAddr(uintptr_t step_id, uintptr_t loop_per_iter, uintptr_t loop_cond,
  265. aicpu::dump::OpMappingInfo &op_mapping_info) {
  266. if (step_id != 0) {
  267. GELOGI("step_id exists.");
  268. op_mapping_info.set_step_id_addr(static_cast<uint64_t>(step_id));
  269. }
  270. if (loop_per_iter != 0) {
  271. GELOGI("loop_per_iter exists.");
  272. op_mapping_info.set_iterations_per_loop_addr(static_cast<uint64_t>(loop_per_iter));
  273. }
  274. if (loop_cond != 0) {
  275. GELOGI("loop_cond exists.");
  276. op_mapping_info.set_loop_cond_addr(static_cast<uint64_t>(loop_cond));
  277. }
  278. }
  279. Status DataDumper::GenerateOutput(aicpu::dump::Output &output, const OpDesc::Vistor<GeTensorDesc> &tensor_descs,
  280. const uintptr_t &addr, size_t index) {
  281. output.set_data_type(static_cast<int32_t>(GetIrDataType(tensor_descs.at(index).GetDataType())));
  282. output.set_format(static_cast<int32_t>(tensor_descs.at(index).GetFormat()));
  283. for (auto dim : tensor_descs.at(index).GetShape().GetDims()) {
  284. output.mutable_shape()->add_dim(dim);
  285. }
  286. for (auto dim : tensor_descs.at(index).GetOriginShape().GetDims()) {
  287. output.mutable_origin_shape()->add_dim(dim);
  288. }
  289. int64_t output_size = 0;
  290. if (TensorUtils::GetTensorSizeInBytes(tensor_descs.at(index), output_size) != SUCCESS) {
  291. REPORT_CALL_ERROR("E19999", "Get tensor size fail");
  292. GELOGE(PARAM_INVALID, "Get output size filed");
  293. return PARAM_INVALID;
  294. }
  295. GELOGD("Get output size in dump is %ld", output_size);
  296. std::string origin_name;
  297. int32_t origin_output_index = -1;
  298. (void)AttrUtils::GetStr(&tensor_descs.at(index), ATTR_NAME_DATA_DUMP_ORIGIN_NAME, origin_name);
  299. (void)AttrUtils::GetInt(&tensor_descs.at(index), ATTR_NAME_DATA_DUMP_ORIGIN_OUTPUT_INDEX, origin_output_index);
  300. output.set_size(output_size);
  301. output.set_original_name(origin_name);
  302. output.set_original_output_index(origin_output_index);
  303. output.set_original_output_format(static_cast<int32_t>(tensor_descs.at(index).GetOriginFormat()));
  304. output.set_original_output_data_type(static_cast<int32_t>(tensor_descs.at(index).GetOriginDataType()));
  305. output.set_address(static_cast<uint64_t>(addr));
  306. return SUCCESS;
  307. }
  308. Status DataDumper::DumpRefOutput(const DataDumper::InnerDumpInfo &inner_dump_info, aicpu::dump::Output &output,
  309. size_t i, const std::string &node_name_index) {
  310. std::string dump_op_name;
  311. std::string input_or_output;
  312. size_t index;
  313. // parser and find which node's input or output tensor desc is chosen for dump info
  314. if (!ParseNameIndex(node_name_index, dump_op_name, input_or_output, index)) {
  315. GELOGE(PARAM_INVALID, "Op [%s] output desc[%zu] with invalid ATTR_DATA_DUMP_REF attr[%s].",
  316. inner_dump_info.op->GetName().c_str(), i, node_name_index.c_str());
  317. return PARAM_INVALID;
  318. }
  319. GE_CHECK_NOTNULL(compute_graph_);
  320. auto replace_node = compute_graph_->FindNode(dump_op_name);
  321. GE_RT_PARAM_INVALID_WITH_LOG_IF_TRUE(replace_node == nullptr,
  322. "Op [%s] output desc[%zu] with invalid ATTR_DATA_DUMP_REF attr[%s],"
  323. " cannot find redirect node[%s].",
  324. inner_dump_info.op->GetName().c_str(), i, node_name_index.c_str(),
  325. dump_op_name.c_str());
  326. auto replace_opdesc = replace_node->GetOpDesc();
  327. GE_CHECK_NOTNULL(replace_opdesc);
  328. auto iter = ref_info_.find(replace_opdesc);
  329. GE_RT_PARAM_INVALID_WITH_LOG_IF_TRUE(iter == ref_info_.end(),
  330. "Op [%s] output desc[%zu] cannot find any saved redirect node[%s]'s info.",
  331. inner_dump_info.op->GetName().c_str(), i, replace_opdesc->GetName().c_str());
  332. GE_CHECK_NOTNULL(iter->second);
  333. auto addr = reinterpret_cast<uintptr_t>(iter->second);
  334. if (input_or_output == kDumpInput) {
  335. const auto &replace_input_descs = replace_opdesc->GetAllInputsDesc();
  336. addr += kAddrLen * index;
  337. GE_CHK_STATUS_RET(GenerateOutput(output, replace_input_descs, addr, index), "Generate output failed");
  338. } else if (input_or_output == kDumpOutput) {
  339. const auto &replace_output_descs = replace_opdesc->GetAllOutputsDesc();
  340. const auto replace_input_size = replace_opdesc->GetAllInputsDesc().size();
  341. addr += (index + replace_input_size) * kAddrLen;
  342. GE_CHK_STATUS_RET(GenerateOutput(output, replace_output_descs, addr, index), "Generate output failed");
  343. }
  344. GELOGD("Op [%s] output desc[%zu] dump info is replaced by node[%s] [%s] tensor_desc [%zu]",
  345. inner_dump_info.op->GetName().c_str(), i, dump_op_name.c_str(), input_or_output.c_str(), index);
  346. return SUCCESS;
  347. }
  348. Status DataDumper::DumpOutputWithTask(const InnerDumpInfo &inner_dump_info, aicpu::dump::Task &task) {
  349. const auto &output_descs = inner_dump_info.op->GetAllOutputsDesc();
  350. const std::vector<void *> output_addrs = ModelUtils::GetOutputDataAddrs(*runtime_param_, inner_dump_info.op);
  351. if (output_descs.size() != output_addrs.size()) {
  352. REPORT_INNER_ERROR("E19999", "output_desc size:%zu != output addr size:%zu in op:%s(%s)",
  353. output_descs.size(), output_addrs.size(),
  354. inner_dump_info.op->GetName().c_str(), inner_dump_info.op->GetType().c_str());
  355. GELOGE(PARAM_INVALID, "Invalid output desc addrs size %zu, op %s has %zu output desc.", output_addrs.size(),
  356. inner_dump_info.op->GetName().c_str(), output_descs.size());
  357. return PARAM_INVALID;
  358. }
  359. std::vector<int64_t> v_memory_type;
  360. bool has_mem_type_attr = ge::AttrUtils::GetListInt(inner_dump_info.op, ATTR_NAME_OUTPUT_MEM_TYPE_LIST, v_memory_type);
  361. GE_RT_PARAM_INVALID_WITH_LOG_IF_TRUE(has_mem_type_attr && (v_memory_type.size() != output_descs.size()),
  362. "DumpOutputWithTask[%s], output size[%zu], output memory type size[%zu]",
  363. inner_dump_info.op->GetName().c_str(), output_descs.size(),
  364. v_memory_type.size());
  365. for (size_t i = 0; i < output_descs.size(); ++i) {
  366. aicpu::dump::Output output;
  367. std::string node_name_index;
  368. const auto &output_desc = output_descs.at(i);
  369. // check dump output tensor desc is redirected by attr ATTR_DATA_DUMP_REF
  370. if (AttrUtils::GetStr(&output_desc, ATTR_DATA_DUMP_REF, node_name_index)) {
  371. GE_CHK_STATUS_RET(DumpRefOutput(inner_dump_info, output, i, node_name_index), "DumpRefOutput failed");
  372. task.mutable_output()->Add(std::move(output));
  373. } else {
  374. if (IsTensorDescWithSkipDumpAddrType(has_mem_type_attr, v_memory_type, i)) {
  375. GELOGI("[L1Fusion] DumpOutputWithTask[%s] output[%zu] is l1 addr.", inner_dump_info.op->GetName().c_str(), i);
  376. int64_t output_size = 0;
  377. if (TensorUtils::GetTensorSizeInBytes(output_descs.at(i), output_size) != SUCCESS) {
  378. REPORT_CALL_ERROR("E19999", "Get output tensor size fail in op:%s(%s), index:%zu",
  379. inner_dump_info.op->GetName().c_str(), inner_dump_info.op->GetType().c_str(), i);
  380. GELOGE(PARAM_INVALID, "Get output size failed.");
  381. return PARAM_INVALID;
  382. }
  383. GELOGI("Get output size of l1_fusion_dump is %ld", output_size);
  384. GenerateOpBuffer(output_size, task);
  385. } else {
  386. const auto input_size = inner_dump_info.op->GetInputsSize();
  387. auto addr = inner_dump_info.args + (i + input_size) * kAddrLen;
  388. GE_CHK_STATUS_RET(GenerateOutput(output, output_descs, addr, i), "Generate output failed");
  389. task.mutable_output()->Add(std::move(output));
  390. }
  391. }
  392. }
  393. return SUCCESS;
  394. }
  395. Status DataDumper::DumpOutput(const InnerDumpInfo &inner_dump_info, aicpu::dump::Task &task) {
  396. GELOGI("Start dump output");
  397. if (inner_dump_info.is_task) {
  398. // tbe or aicpu op, these ops are with task
  399. return DumpOutputWithTask(inner_dump_info, task);
  400. }
  401. // else data, const or variable op
  402. aicpu::dump::Output output;
  403. auto output_tensor = inner_dump_info.op->GetOutputDescPtr(inner_dump_info.output_anchor_index);
  404. const std::vector<void *> output_addrs = ModelUtils::GetOutputDataAddrs(*runtime_param_, inner_dump_info.op);
  405. if (output_tensor == nullptr) {
  406. REPORT_INNER_ERROR("E19999", "output_desc tensor is nullptr in op:%s(%s), index:%u, "
  407. "check invalid",
  408. inner_dump_info.op->GetName().c_str(), inner_dump_info.op->GetType().c_str(),
  409. inner_dump_info.output_anchor_index);
  410. GELOGE(PARAM_INVALID, "output_tensor is null, index: %d, size: %zu.", inner_dump_info.output_anchor_index,
  411. inner_dump_info.op->GetOutputsSize());
  412. return PARAM_INVALID;
  413. }
  414. output.set_data_type(static_cast<int32_t>(GetIrDataType(output_tensor->GetDataType())));
  415. output.set_format(static_cast<int32_t>(output_tensor->GetFormat()));
  416. for (auto dim : inner_dump_info.dims) {
  417. output.mutable_shape()->add_dim(dim);
  418. }
  419. std::string origin_name;
  420. int32_t origin_output_index = -1;
  421. (void)AttrUtils::GetStr(output_tensor, ATTR_NAME_DATA_DUMP_ORIGIN_NAME, origin_name);
  422. (void)AttrUtils::GetInt(output_tensor, ATTR_NAME_DATA_DUMP_ORIGIN_OUTPUT_INDEX, origin_output_index);
  423. output.set_size(inner_dump_info.data_size);
  424. output.set_original_name(origin_name);
  425. output.set_original_output_index(origin_output_index);
  426. output.set_original_output_format(static_cast<int32_t>(output_tensor->GetOriginFormat()));
  427. output.set_original_output_data_type(static_cast<int32_t>(output_tensor->GetOriginDataType()));
  428. // due to lhisi virtual addr bug, cannot use args now
  429. if (inner_dump_info.output_anchor_index >= static_cast<int>(output_addrs.size())) {
  430. REPORT_INNER_ERROR("E19999", "output_anchor_index:%u >= output addr size:%zu in op:%s(%s), "
  431. "check invalid", inner_dump_info.output_anchor_index, output_addrs.size(),
  432. inner_dump_info.op->GetName().c_str(), inner_dump_info.op->GetType().c_str());
  433. GELOGE(FAILED, "Index is out of range.");
  434. return FAILED;
  435. }
  436. auto data_addr = inner_dump_info.args + kAddrLen * static_cast<uint32_t>(inner_dump_info.input_anchor_index);
  437. output.set_address(static_cast<uint64_t>(data_addr));
  438. task.mutable_output()->Add(std::move(output));
  439. return SUCCESS;
  440. }
  441. Status DataDumper::GenerateInput(aicpu::dump::Input &input, const OpDesc::Vistor<GeTensorDesc> &tensor_descs,
  442. const uintptr_t &addr, size_t index) {
  443. input.set_data_type(static_cast<int32_t>(GetIrDataType(tensor_descs.at(index).GetDataType())));
  444. input.set_format(static_cast<int32_t>(tensor_descs.at(index).GetFormat()));
  445. for (auto dim : tensor_descs.at(index).GetShape().GetDims()) {
  446. input.mutable_shape()->add_dim(dim);
  447. }
  448. for (auto dim : tensor_descs.at(index).GetOriginShape().GetDims()) {
  449. input.mutable_origin_shape()->add_dim(dim);
  450. }
  451. int64_t input_size = 0;
  452. if (AttrUtils::GetInt(tensor_descs.at(index), ATTR_NAME_INPUT_ORIGIN_SIZE, input_size)) {
  453. GELOGI("Get aipp input size according to attr is %ld", input_size);
  454. } else if (TensorUtils::GetTensorSizeInBytes(tensor_descs.at(index), input_size) != SUCCESS) {
  455. REPORT_CALL_ERROR("E19999", "Get tensor size fail");
  456. GELOGE(PARAM_INVALID, "Get input size filed");
  457. return PARAM_INVALID;
  458. }
  459. GELOGD("Get input size in dump is %ld", input_size);
  460. input.set_size(input_size);
  461. input.set_address(static_cast<uint64_t>(addr));
  462. return SUCCESS;
  463. }
  464. Status DataDumper::DumpRefInput(const DataDumper::InnerDumpInfo &inner_dump_info, aicpu::dump::Input &input, size_t i,
  465. const std::string &node_name_index) {
  466. std::string dump_op_name;
  467. std::string input_or_output;
  468. size_t index;
  469. // parser and find which node's input or output tensor desc is chosen for dump info
  470. if (!ParseNameIndex(node_name_index, dump_op_name, input_or_output, index)) {
  471. GELOGE(PARAM_INVALID, "Op [%s] input desc[%zu] with invalid ATTR_DATA_DUMP_REF attr[%s].",
  472. inner_dump_info.op->GetName().c_str(), i, node_name_index.c_str());
  473. return PARAM_INVALID;
  474. }
  475. GE_CHECK_NOTNULL(compute_graph_);
  476. auto replace_node = compute_graph_->FindNode(dump_op_name);
  477. GE_RT_PARAM_INVALID_WITH_LOG_IF_TRUE(replace_node == nullptr,
  478. "Op [%s] input desc[%zu] with invalid ATTR_DATA_DUMP_REF attr[%s],"
  479. " cannot find redirect node[%s].",
  480. inner_dump_info.op->GetName().c_str(), i, node_name_index.c_str(),
  481. dump_op_name.c_str());
  482. auto replace_opdesc = replace_node->GetOpDesc();
  483. GE_CHECK_NOTNULL(replace_opdesc);
  484. auto iter = ref_info_.find(replace_opdesc);
  485. GE_RT_PARAM_INVALID_WITH_LOG_IF_TRUE(iter == ref_info_.end(),
  486. "Op [%s] input desc[%zu] cannot find any saved redirect node[%s]'s info.",
  487. inner_dump_info.op->GetName().c_str(), i, replace_opdesc->GetName().c_str());
  488. GE_CHECK_NOTNULL(iter->second);
  489. auto addr = reinterpret_cast<uintptr_t>(iter->second);
  490. if (input_or_output == kDumpInput) {
  491. const auto &replace_input_descs = replace_opdesc->GetAllInputsDesc();
  492. addr += kAddrLen * index;
  493. GE_CHK_STATUS_RET(GenerateInput(input, replace_input_descs, addr, index), "Generate input failed");
  494. } else if (input_or_output == kDumpOutput) {
  495. const auto &replace_output_descs = replace_opdesc->GetAllOutputsDesc();
  496. const auto replace_input_size = replace_opdesc->GetAllInputsDesc().size();
  497. addr += (index + replace_input_size) * kAddrLen;
  498. GE_CHK_STATUS_RET(GenerateInput(input, replace_output_descs, addr, index), "Generate input failed");
  499. }
  500. GELOGD("Op [%s] input desc[%zu] dump info is replaced by node[%s] [%s] tensor_desc [%zu]",
  501. inner_dump_info.op->GetName().c_str(), i, dump_op_name.c_str(), input_or_output.c_str(), index);
  502. return SUCCESS;
  503. }
  504. Status DataDumper::DumpInput(const InnerDumpInfo &inner_dump_info, aicpu::dump::Task &task) {
  505. GELOGI("Start dump input");
  506. const auto &input_descs = inner_dump_info.op->GetAllInputsDesc();
  507. const std::vector<void *> input_addrs = ModelUtils::GetInputDataAddrs(*runtime_param_, inner_dump_info.op);
  508. if (input_descs.size() != input_addrs.size()) {
  509. REPORT_INNER_ERROR("E19999", "input_desc size:%zu != input addr size:%zu in op:%s(%s)",
  510. input_descs.size(), input_addrs.size(),
  511. inner_dump_info.op->GetName().c_str(), inner_dump_info.op->GetType().c_str());
  512. GELOGE(PARAM_INVALID, "Invalid input desc addrs size %zu, op %s has %zu input desc.", input_addrs.size(),
  513. inner_dump_info.op->GetName().c_str(), input_descs.size());
  514. return PARAM_INVALID;
  515. }
  516. std::vector<int64_t> v_memory_type;
  517. bool has_mem_type_attr = ge::AttrUtils::GetListInt(inner_dump_info.op, ATTR_NAME_INPUT_MEM_TYPE_LIST, v_memory_type);
  518. GE_RT_PARAM_INVALID_WITH_LOG_IF_TRUE(has_mem_type_attr && (v_memory_type.size() != input_descs.size()),
  519. "DumpInput[%s], input size[%zu], input memory type size[%zu]",
  520. inner_dump_info.op->GetName().c_str(), input_descs.size(), v_memory_type.size());
  521. for (size_t i = 0; i < input_descs.size(); ++i) {
  522. aicpu::dump::Input input;
  523. std::string node_name_index;
  524. // check dump input tensor desc is redirected by attr ATTR_DATA_DUMP_REF
  525. if (AttrUtils::GetStr(&input_descs.at(i), ATTR_DATA_DUMP_REF, node_name_index)) {
  526. GE_CHK_STATUS_RET(DumpRefInput(inner_dump_info, input, i, node_name_index), "DumpRefInput failed");
  527. task.mutable_input()->Add(std::move(input));
  528. // normal dump without attr
  529. } else {
  530. if (IsTensorDescWithSkipDumpAddrType(has_mem_type_attr, v_memory_type, i)) {
  531. GELOGI("[L1Fusion] DumpInput[%s] input[%zu] is l1 addr", inner_dump_info.op->GetName().c_str(), i);
  532. int64_t input_size = 0;
  533. if (AttrUtils::GetInt(input_descs.at(i), ATTR_NAME_INPUT_ORIGIN_SIZE, input_size)) {
  534. GELOGI("Get aipp input size according to attr is %ld", input_size);
  535. } else if (TensorUtils::GetTensorSizeInBytes(input_descs.at(i), input_size) != SUCCESS) {
  536. REPORT_CALL_ERROR("E19999", "Get input tensor size fail in op:%s(%s), index:%zu",
  537. inner_dump_info.op->GetName().c_str(), inner_dump_info.op->GetType().c_str(), i);
  538. GELOGE(PARAM_INVALID, "Get input size failed.");
  539. return PARAM_INVALID;
  540. }
  541. GELOGI("Get input size of l1_fusion_dump is %ld", input_size);
  542. GenerateOpBuffer(input_size, task);
  543. } else {
  544. auto addr = inner_dump_info.args + kAddrLen * i;
  545. GE_CHK_STATUS_RET(GenerateInput(input, input_descs, addr, i), "Generate input failed");
  546. task.mutable_input()->Add(std::move(input));
  547. }
  548. }
  549. }
  550. return SUCCESS;
  551. }
  552. void DataDumper::GenerateOpBuffer(const int64_t &size, aicpu::dump::Task &task) {
  553. aicpu::dump::OpBuffer op_buffer;
  554. op_buffer.set_buffer_type(aicpu::dump::BufferType::L1);
  555. op_buffer.set_address(reinterpret_cast<uintptr_t>(l1_fusion_addr_));
  556. op_buffer.set_size(size);
  557. task.mutable_buffer()->Add(std::move(op_buffer));
  558. }
  559. Status DataDumper::ExecuteLoadDumpInfo(aicpu::dump::OpMappingInfo &op_mapping_info) {
  560. std::string proto_str;
  561. size_t proto_size = op_mapping_info.ByteSizeLong();
  562. bool ret = op_mapping_info.SerializeToString(&proto_str);
  563. if (!ret || proto_size == 0) {
  564. REPORT_INNER_ERROR("E19999", "Serialize proto to string fail");
  565. GELOGE(PARAM_INVALID, "Protobuf SerializeToString failed, proto size %zu.", proto_size);
  566. return PARAM_INVALID;
  567. }
  568. if (dev_mem_load_ != nullptr) {
  569. GELOGW("dev_mem_load_ has been used.");
  570. ReleaseDevMem(&dev_mem_load_);
  571. }
  572. rtError_t rt_ret = rtMalloc(&dev_mem_load_, proto_size, RT_MEMORY_HBM);
  573. if (rt_ret != RT_ERROR_NONE) {
  574. REPORT_CALL_ERROR("E19999", "Call rtMalloc failed, size:%zu, ret:0x%X",
  575. proto_size, rt_ret);
  576. GELOGE(RT_FAILED, "Call rtMalloc failed, ret: 0x%X", rt_ret);
  577. return RT_ERROR_TO_GE_STATUS(rt_ret);
  578. }
  579. GE_PRINT_DYNAMIC_MEMORY(rtMalloc, "load dump information.", proto_size)
  580. rt_ret = rtMemcpy(dev_mem_load_, proto_size, proto_str.c_str(), proto_size, RT_MEMCPY_HOST_TO_DEVICE);
  581. if (rt_ret != RT_ERROR_NONE) {
  582. REPORT_CALL_ERROR("E19999", "Call rtMemcpy failed, size:%zu, ret:0x%X",
  583. proto_size, rt_ret);
  584. GELOGE(RT_FAILED, "Call rtMemcpy failed, ret: 0x%X", rt_ret);
  585. return RT_ERROR_TO_GE_STATUS(rt_ret);
  586. }
  587. rt_ret = rtDatadumpInfoLoad(dev_mem_load_, proto_size);
  588. if (rt_ret != RT_ERROR_NONE) {
  589. REPORT_CALL_ERROR("E19999", "Call rtDatadumpInfoLoad failed, ret:0x%X", rt_ret);
  590. GELOGE(RT_FAILED, "Call rtDatadumpInfoLoad failed, ret: 0x%X", rt_ret);
  591. return RT_ERROR_TO_GE_STATUS(rt_ret);
  592. }
  593. load_flag_ = true;
  594. GELOGI("LoadDumpInfo success, proto size is: %zu.", proto_size);
  595. return SUCCESS;
  596. }
  597. Status DataDumper::ExecuteUnLoadDumpInfo(aicpu::dump::OpMappingInfo &op_mapping_info) {
  598. std::string proto_str;
  599. size_t proto_size = op_mapping_info.ByteSizeLong();
  600. bool ret = op_mapping_info.SerializeToString(&proto_str);
  601. if (!ret || proto_size == 0) {
  602. REPORT_INNER_ERROR("E19999", "Serialize proto to string fail");
  603. GELOGE(PARAM_INVALID, "Protobuf SerializeToString failed, proto size %zu.", proto_size);
  604. return PARAM_INVALID;
  605. }
  606. if (dev_mem_unload_ != nullptr) {
  607. GELOGW("dev_mem_unload_ has been used.");
  608. ReleaseDevMem(&dev_mem_unload_);
  609. }
  610. rtError_t rt_ret = rtMalloc(&dev_mem_unload_, proto_size, RT_MEMORY_HBM);
  611. if (rt_ret != RT_ERROR_NONE) {
  612. REPORT_CALL_ERROR("E19999", "Call rtMalloc failed, size:%zu, ret:0x%X",
  613. proto_size, rt_ret);
  614. GELOGE(RT_FAILED, "Call rtMalloc failed, ret: 0x%X", rt_ret);
  615. return RT_ERROR_TO_GE_STATUS(rt_ret);
  616. }
  617. GE_PRINT_DYNAMIC_MEMORY(rtMalloc, "unload dump information.", proto_size)
  618. rt_ret = rtMemcpy(dev_mem_unload_, proto_size, proto_str.c_str(), proto_size, RT_MEMCPY_HOST_TO_DEVICE);
  619. if (rt_ret != RT_ERROR_NONE) {
  620. REPORT_CALL_ERROR("E19999", "Call rtMemcpy failed, size:%zu, ret:0x%X",
  621. proto_size, rt_ret);
  622. GELOGE(RT_FAILED, "Call rtMemcpy failed, ret: 0x%X", rt_ret);
  623. return RT_ERROR_TO_GE_STATUS(rt_ret);
  624. }
  625. rt_ret = rtDatadumpInfoLoad(dev_mem_unload_, proto_size);
  626. if (rt_ret != RT_ERROR_NONE) {
  627. REPORT_CALL_ERROR("E19999", "Call rtDatadumpInfoLoad failed, ret:0x%X", rt_ret);
  628. GELOGE(RT_FAILED, "Call rtDatadumpInfoLoad failed, ret: 0x%X", rt_ret);
  629. return RT_ERROR_TO_GE_STATUS(rt_ret);
  630. }
  631. load_flag_ = false;
  632. GELOGI("UnloadDumpInfo success, proto size is: %zu.", proto_size);
  633. return SUCCESS;
  634. }
  635. Status DataDumper::LoadDumpInfo() {
  636. std::string dump_list_key;
  637. PrintCheckLog(dump_list_key);
  638. if (op_list_.empty()) {
  639. GELOGD("op_list_ is empty");
  640. }
  641. aicpu::dump::OpMappingInfo op_mapping_info;
  642. auto dump_path = dump_properties_.GetDumpPath() + std::to_string(device_id_) + "/";
  643. op_mapping_info.set_dump_path(dump_path);
  644. op_mapping_info.set_model_name(dump_list_key);
  645. op_mapping_info.set_model_id(model_id_);
  646. op_mapping_info.set_flag(kAicpuLoadFlag);
  647. op_mapping_info.set_dump_step(dump_properties_.GetDumpStep());
  648. SetOpMappingLoopAddr(global_step_, loop_per_iter_, loop_cond_, op_mapping_info);
  649. auto ret = BuildTaskInfo(op_mapping_info);
  650. if (ret != SUCCESS) {
  651. GELOGE(ret, "Build task info failed");
  652. return ret;
  653. }
  654. SetEndGraphIdToAicpu(end_graph_task_id_, end_graph_stream_id_, op_mapping_info);
  655. SetOpDebugIdToAicpu(op_debug_task_id_, op_debug_stream_id_, op_debug_addr_, op_mapping_info);
  656. if (!op_list_.empty() || is_op_debug_ || is_end_graph_) {
  657. auto ret = ExecuteLoadDumpInfo(op_mapping_info);
  658. if (ret != SUCCESS) {
  659. GELOGE(ret, "Execute load dump info failed");
  660. return ret;
  661. }
  662. }
  663. return SUCCESS;
  664. }
  665. Status DataDumper::BuildTaskInfo(aicpu::dump::OpMappingInfo &op_mapping_info) {
  666. for (const auto &op_iter : op_list_) {
  667. auto op_desc = op_iter.op;
  668. GELOGD("Op %s in model begin to add task in op_mapping_info", op_desc->GetName().c_str());
  669. aicpu::dump::Task task;
  670. task.set_end_graph(false);
  671. task.set_task_id(op_iter.task_id);
  672. task.set_stream_id(op_iter.stream_id);
  673. task.mutable_op()->set_op_name(op_desc->GetName());
  674. task.mutable_op()->set_op_type(op_desc->GetType());
  675. if (dump_properties_.GetDumpMode() == kDumpOutput) {
  676. Status ret = DumpOutput(op_iter, task);
  677. if (ret != SUCCESS) {
  678. GELOGE(ret, "Dump output failed");
  679. return ret;
  680. }
  681. op_mapping_info.mutable_task()->Add(std::move(task));
  682. continue;
  683. }
  684. if (dump_properties_.GetDumpMode() == kDumpInput) {
  685. if (op_iter.is_task) {
  686. Status ret = DumpInput(op_iter, task);
  687. if (ret != SUCCESS) {
  688. GELOGE(ret, "Dump input failed");
  689. return ret;
  690. }
  691. }
  692. op_mapping_info.mutable_task()->Add(std::move(task));
  693. continue;
  694. }
  695. if (dump_properties_.GetDumpMode() == kDumpAll || is_op_debug_) {
  696. auto ret = DumpOutput(op_iter, task);
  697. if (ret != SUCCESS) {
  698. GELOGE(ret, "Dump output failed when in dumping all");
  699. return ret;
  700. }
  701. if (op_iter.is_task) {
  702. ret = DumpInput(op_iter, task);
  703. if (ret != SUCCESS) {
  704. GELOGE(ret, "Dump input failed when in dumping all");
  705. return ret;
  706. }
  707. }
  708. op_mapping_info.mutable_task()->Add(std::move(task));
  709. continue;
  710. }
  711. }
  712. return SUCCESS;
  713. }
  714. void DataDumper::SetEndGraphIdToAicpu(uint32_t task_id, uint32_t stream_id,
  715. aicpu::dump::OpMappingInfo &op_mapping_info) {
  716. if (dump_properties_.GetDumpMode() == kDumpOutput || dump_properties_.GetDumpMode() == kDumpInput ||
  717. dump_properties_.GetDumpMode() == kDumpAll) {
  718. aicpu::dump::Task task;
  719. task.set_end_graph(true);
  720. task.set_task_id(end_graph_task_id_);
  721. task.set_stream_id(end_graph_stream_id_);
  722. task.mutable_op()->set_op_name(NODE_NAME_END_GRAPH);
  723. task.mutable_op()->set_op_type(ENDGRAPH);
  724. op_mapping_info.mutable_task()->Add(std::move(task));
  725. is_end_graph_ = true;
  726. if (op_mapping_info.model_name_param_case() == aicpu::dump::OpMappingInfo::kModelName) {
  727. GELOGI("Add end_graph_info to aicpu, model_name is %s, task_id is %u, stream_id is %u",
  728. op_mapping_info.model_name().c_str(), end_graph_task_id_, end_graph_stream_id_);
  729. return;
  730. }
  731. GELOGI("Add end_graph_info to aicpu, task_id is %u, stream_id is %u", end_graph_task_id_, end_graph_stream_id_);
  732. }
  733. }
  734. void DataDumper::SetOpDebugIdToAicpu(uint32_t task_id, uint32_t stream_id, void *op_debug_addr,
  735. aicpu::dump::OpMappingInfo &op_mapping_info) {
  736. if (is_op_debug_) {
  737. GELOGI("add op_debug_info to aicpu, task_id is %u, stream_id is %u", task_id, stream_id);
  738. aicpu::dump::Task task;
  739. task.set_end_graph(false);
  740. task.set_task_id(task_id);
  741. task.set_stream_id(stream_id);
  742. task.mutable_op()->set_op_name(NODE_NAME_OP_DEBUG);
  743. task.mutable_op()->set_op_type(OP_TYPE_OP_DEBUG);
  744. // set output
  745. aicpu::dump::Output output;
  746. output.set_data_type(DT_UINT8);
  747. output.set_format(FORMAT_ND);
  748. output.mutable_shape()->add_dim(kOpDebugShape);
  749. output.set_original_name(NODE_NAME_OP_DEBUG);
  750. output.set_original_output_index(0);
  751. output.set_original_output_format(FORMAT_ND);
  752. output.set_original_output_data_type(DT_UINT8);
  753. // due to lhisi virtual addr bug, cannot use args now
  754. output.set_address(static_cast<uint64_t>(reinterpret_cast<uintptr_t>(op_debug_addr)));
  755. output.set_size(kOpDebugSize);
  756. task.mutable_output()->Add(std::move(output));
  757. op_mapping_info.mutable_task()->Add(std::move(task));
  758. }
  759. }
  760. Status DataDumper::UnloadDumpInfo() {
  761. if (!load_flag_) {
  762. load_flag_ = false;
  763. return SUCCESS;
  764. }
  765. GELOGI("UnloadDumpInfo start.");
  766. aicpu::dump::OpMappingInfo op_mapping_info;
  767. op_mapping_info.set_model_id(model_id_);
  768. op_mapping_info.set_flag(kAicpuUnloadFlag);
  769. for (const auto &op_iter : op_list_) {
  770. aicpu::dump::Task task;
  771. task.set_task_id(op_iter.task_id);
  772. task.set_stream_id(op_iter.stream_id);
  773. op_mapping_info.mutable_task()->Add(std::move(task));
  774. }
  775. auto ret = ExecuteUnLoadDumpInfo(op_mapping_info);
  776. if (ret != SUCCESS) {
  777. GELOGE(ret, "Execute unload dump info failed");
  778. return ret;
  779. }
  780. return SUCCESS;
  781. }
  782. void DataDumper::DumpShrink() {
  783. compute_graph_.reset();
  784. input_map_.clear();
  785. ref_info_.clear();
  786. }
  787. void DataDumper::PrintCheckLog(string &dump_list_key) {
  788. std::set<std::string> model_list = dump_properties_.GetAllDumpModel();
  789. if (model_list.empty()) {
  790. return;
  791. }
  792. bool not_find_by_omname = model_list.find(om_name_) == model_list.end();
  793. bool not_find_by_modelname = model_list.find(model_name_) == model_list.end();
  794. dump_list_key = not_find_by_omname ? model_name_ : om_name_;
  795. GELOGI("%zu op need dump in known shape model %s.", op_list_.size(), dump_list_key.c_str());
  796. if (model_list.find(DUMP_ALL_MODEL) == model_list.end()) {
  797. if (not_find_by_omname && not_find_by_modelname) {
  798. std::string model_list_str;
  799. for (auto &model : model_list) {
  800. model_list_str += "[" + model + "].";
  801. }
  802. GELOGW("Model %s will not be set to dump, dump list: %s", dump_list_key.c_str(), model_list_str.c_str());
  803. return;
  804. }
  805. }
  806. std::set<std::string> config_dump_op_list = dump_properties_.GetPropertyValue(dump_list_key);
  807. std::set<std::string> dump_op_list;
  808. for (auto &inner_dump_info : op_list_) {
  809. // oplist value OpDescPtr is not nullptr
  810. dump_op_list.insert(inner_dump_info.op->GetName());
  811. }
  812. for (auto &dump_op : config_dump_op_list) {
  813. if (dump_op_list.find(dump_op) == dump_op_list.end()) {
  814. GELOGW("Op %s set to dump but not exist in model %s or not a valid op.", dump_op.c_str(), dump_list_key.c_str());
  815. }
  816. }
  817. }
  818. Status DataDumper::DumpExceptionInput(const OpDescInfo &op_desc_info, const string &dump_file) {
  819. GELOGI("Start to dump exception input");
  820. for (size_t i = 0; i < op_desc_info.input_addrs.size(); i++) {
  821. if (Debug::DumpDevMem(dump_file.data(), op_desc_info.input_addrs.at(i), op_desc_info.input_size.at(i)) != SUCCESS) {
  822. GELOGE(PARAM_INVALID, "Dump the %zu input data failed", i);
  823. return PARAM_INVALID;
  824. }
  825. }
  826. return SUCCESS;
  827. }
  828. Status DataDumper::DumpExceptionOutput(const OpDescInfo &op_desc_info, const string &dump_file) {
  829. GELOGI("Start to dump exception output");
  830. for (size_t i = 0; i < op_desc_info.output_addrs.size(); i++) {
  831. if (Debug::DumpDevMem(dump_file.data(), op_desc_info.output_addrs.at(i), op_desc_info.output_size.at(i)) !=
  832. SUCCESS) {
  833. GELOGE(PARAM_INVALID, "Dump the %zu input data failed", i);
  834. return PARAM_INVALID;
  835. }
  836. }
  837. return SUCCESS;
  838. }
  839. Status DataDumper::DumpExceptionInfo(const std::vector<rtExceptionInfo> exception_infos) {
  840. GELOGI("Start to dump exception info");
  841. for (const rtExceptionInfo &iter : exception_infos) {
  842. OpDescInfo op_desc_info;
  843. if (GetOpDescInfo(iter.streamid, iter.taskid, op_desc_info)) {
  844. toolkit::dumpdata::DumpData dump_data;
  845. dump_data.set_version("2.0");
  846. dump_data.set_dump_time(GetNowTime());
  847. dump_data.set_op_name(op_desc_info.op_name);
  848. for (size_t i = 0; i < op_desc_info.input_format.size(); ++i) {
  849. toolkit::dumpdata::OpInput input;
  850. input.set_data_type(toolkit::dumpdata::OutputDataType(GetIrDataType(op_desc_info.input_data_type[i])));
  851. input.set_format(toolkit::dumpdata::OutputFormat(op_desc_info.input_format[i]));
  852. for (auto dim : op_desc_info.input_shape[i]) {
  853. input.mutable_shape()->add_dim(dim);
  854. }
  855. input.set_size(op_desc_info.input_size[i]);
  856. GELOGI("The input size int exception is %ld", op_desc_info.input_size[i]);
  857. dump_data.mutable_input()->Add(std::move(input));
  858. }
  859. for (size_t j = 0; j < op_desc_info.output_format.size(); ++j) {
  860. toolkit::dumpdata::OpOutput output;
  861. output.set_data_type(toolkit::dumpdata::OutputDataType(GetIrDataType(op_desc_info.output_data_type[j])));
  862. output.set_format(toolkit::dumpdata::OutputFormat(op_desc_info.output_format[j]));
  863. for (auto dim : op_desc_info.output_shape[j]) {
  864. output.mutable_shape()->add_dim(dim);
  865. }
  866. output.set_size(op_desc_info.output_size[j]);
  867. GELOGI("The output size int exception is %ld", op_desc_info.output_size[j]);
  868. dump_data.mutable_output()->Add(std::move(output));
  869. }
  870. uint64_t now_time = GetNowTime();
  871. std::string op_name = op_desc_info.op_name;
  872. std::string op_type = op_desc_info.op_type;
  873. ReplaceStringElem(op_name);
  874. ReplaceStringElem(op_type);
  875. string dump_file_path =
  876. "./" + op_type + "." + op_name + "." + std::to_string(op_desc_info.task_id) + "." + std::to_string(now_time);
  877. GELOGI("The exception dump file path is %s", dump_file_path.c_str());
  878. uint64_t proto_size = dump_data.ByteSizeLong();
  879. std::unique_ptr<char[]> proto_msg(new (std::nothrow) char[proto_size]);
  880. bool ret = dump_data.SerializeToArray(proto_msg.get(), proto_size);
  881. if (!ret || proto_size == 0) {
  882. REPORT_INNER_ERROR("E19999", "Serialize proto to string fail");
  883. GELOGE(PARAM_INVALID, "Dump data proto serialize failed");
  884. return PARAM_INVALID;
  885. }
  886. GE_CHK_STATUS_RET(MemoryDumper::DumpToFile(dump_file_path.c_str(), &proto_size, sizeof(uint64_t)),
  887. "Failed to dump proto size");
  888. GE_CHK_STATUS_RET(MemoryDumper::DumpToFile(dump_file_path.c_str(), proto_msg.get(), proto_size),
  889. "Failed to dump proto msg");
  890. if (DumpExceptionInput(op_desc_info, dump_file_path) != SUCCESS) {
  891. GELOGE(PARAM_INVALID, "Dump exception input failed");
  892. return PARAM_INVALID;
  893. }
  894. if (DumpExceptionOutput(op_desc_info, dump_file_path) != SUCCESS) {
  895. GELOGE(PARAM_INVALID, "Dump exception output failed");
  896. return PARAM_INVALID;
  897. }
  898. GELOGI("Dump exception info SUCCESS");
  899. } else {
  900. GELOGE(PARAM_INVALID, "Get op desc info failed,task id:%u,stream id:%u", iter.taskid, iter.streamid);
  901. return PARAM_INVALID;
  902. }
  903. }
  904. return SUCCESS;
  905. }
  906. } // namespace ge

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示