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.

memcpy_addr_async_pass.cc 20 kB

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
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
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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/passes/memcpy_addr_async_pass.h"
  17. #include "common/ge/ge_util.h"
  18. #include "framework/common/debug/log.h"
  19. #include "graph/utils/node_utils.h"
  20. #include "graph/utils/op_desc_utils.h"
  21. #include "graph/utils/tensor_utils.h"
  22. namespace ge {
  23. Status MemcpyAddrAsyncPass::Run(ComputeGraphPtr graph) {
  24. GE_CHECK_NOTNULL(graph);
  25. if (graph->GetGraphUnknownFlag()) {
  26. for (const auto &node : graph->GetAllNodes()) {
  27. if (node->GetType() == STREAMSWITCH) {
  28. auto sub_graph = node->GetOwnerComputeGraph();
  29. if (sub_graph != nullptr && !sub_graph->GetGraphUnknownFlag()) {
  30. GE_CHK_STATUS_RET(AddMemcpyAsyncNode(node), "Add memcpyasync node failed in known subgraph.");
  31. }
  32. }
  33. }
  34. GELOGD("Graph[%s] is unknown graph, skip.", graph->GetName().c_str());
  35. return SUCCESS;
  36. }
  37. int64_t value = 0;
  38. rtError_t rt_ret = rtGetRtCapability(FEATURE_TYPE_MEMCPY, MEMCPY_INFO_SUPPORT_ZEROCOPY, &value);
  39. if (rt_ret != RT_ERROR_NONE) {
  40. REPORT_CALL_ERROR("E19999", "Call rtGetRtCapability failed, ret = 0x%X",
  41. rt_ret);
  42. GELOGE(RT_FAILED, "rtGetRtCapability failed, error=0x%x.", rt_ret);
  43. return RT_FAILED;
  44. }
  45. for (auto &node : graph->GetAllNodes()) {
  46. auto op_desc = node->GetOpDesc();
  47. GE_IF_BOOL_EXEC(op_desc == nullptr, continue);
  48. if (op_desc->GetType() == STREAMSWITCHN || op_desc->GetType() == STREAMMERGE) {
  49. Status ret = AddMemcpyAddrAsyncNode(graph, node);
  50. if (ret != SUCCESS) {
  51. GELOGE(ret, "AddMemcpyAddrAsyncNode failed.");
  52. return ret;
  53. }
  54. }
  55. // handle data->netoutput, const->netoutput in root graph, use mem_addr_async to improve performance
  56. if (op_desc->GetType() == NETOUTPUT) {
  57. // check this netoutput is on root graph
  58. if (node->GetOwnerComputeGraph()->GetParentNode() == nullptr) {
  59. Status ret = InsertMemAddrAsyncNodeBeforeNetoutput(node->GetOwnerComputeGraph(), node);
  60. if (ret != SUCCESS) {
  61. GELOGE(ret, "AddMemcpyAddrAsyncNode failed.");
  62. return ret;
  63. }
  64. }
  65. }
  66. }
  67. return SUCCESS;
  68. }
  69. Status MemcpyAddrAsyncPass::AddMemcpyAsyncNode(const NodePtr &node) {
  70. GE_CHECK_NOTNULL(node);
  71. GELOGI("Start add memcpyasync node in front of node %s", node->GetName().c_str());
  72. known_sub_graph_ = true;
  73. auto sub_graph = node->GetOwnerComputeGraph();
  74. for (InDataAnchorPtr &in_data_anchor : node->GetAllInDataAnchors()) {
  75. OutDataAnchorPtr peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  76. GE_IF_BOOL_EXEC(peer_out_anchor == nullptr, continue);
  77. auto memcpy_async_node = CreateMemcpyAddrAsyncNode(sub_graph, peer_out_anchor, node);
  78. if (memcpy_async_node == nullptr) {
  79. GELOGE(INTERNAL_ERROR, "Create memcpyasync node failed.");
  80. return INTERNAL_ERROR;
  81. }
  82. Status ret = InsertMemcpyAddrAsyncNode(peer_out_anchor, in_data_anchor, memcpy_async_node);
  83. if (ret != SUCCESS) {
  84. GELOGE(ret, "Insert memcpyasync node failed.");
  85. return ret;
  86. }
  87. }
  88. return SUCCESS;
  89. }
  90. Status MemcpyAddrAsyncPass::AddMemcpyAddrAsyncNode(const ComputeGraphPtr &graph, const NodePtr &node) {
  91. GELOGI("Start AddMemcpyAddrAsyncNode for %s.", node->GetName().c_str());
  92. for (InDataAnchorPtr &in_data_anchor : node->GetAllInDataAnchors()) {
  93. OutDataAnchorPtr peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  94. GE_IF_BOOL_EXEC(peer_out_anchor == nullptr, continue);
  95. NodePtr in_node = peer_out_anchor->GetOwnerNode();
  96. if (in_node->GetType() == DATA) {
  97. ComputeGraphPtr owner_graph = in_node->GetOwnerComputeGraph();
  98. GE_CHECK_NOTNULL(owner_graph);
  99. // Data is in parent_graph
  100. if (owner_graph->GetParentGraph() == nullptr) {
  101. GELOGI("Need to insert MemcpyAddrAsync directly when data in parent graph.");
  102. NodePtr memcpy_addr_async_node = CreateMemcpyAddrAsyncNode(graph, peer_out_anchor, node);
  103. GE_IF_BOOL_EXEC(memcpy_addr_async_node == nullptr, GELOGE(INTERNAL_ERROR, "CreateMemcpyAddrAsyncNode failed.");
  104. return INTERNAL_ERROR);
  105. Status ret = InsertMemcpyAddrAsyncNode(peer_out_anchor, in_data_anchor, memcpy_addr_async_node);
  106. GE_IF_BOOL_EXEC(ret != SUCCESS, GELOGE(ret, "InsertMemcpyAddrAsyncNode failed."); return ret);
  107. } else {
  108. uint32_t parent_index = 0;
  109. if (!AttrUtils::GetInt(in_node->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  110. REPORT_CALL_ERROR("E19999", "Get Attr:%s from op:%s(%s) failed",
  111. ATTR_NAME_PARENT_NODE_INDEX.c_str(),
  112. in_node->GetName().c_str(), in_node->GetType().c_str());
  113. GELOGE(INTERNAL_ERROR, "Failed to get parent index of %s", in_node->GetName().c_str());
  114. return INTERNAL_ERROR;
  115. }
  116. // Data is in sub_graph
  117. GELOGI("Need to find data in parent graph, then insert MemcpyAddrAsync.");
  118. NodePtr parent_node = owner_graph->GetParentNode();
  119. user_data_for_known_ = in_node;
  120. out_of_user_data_for_known_ = node;
  121. peer_out_anchor_for_known_ = peer_out_anchor;
  122. in_anchor_for_known_ = in_data_anchor;
  123. FindUserData(parent_node, parent_index);
  124. if (find_user_data_) {
  125. GELOGI("Insert memcpy_addr_async for non_dynamic.");
  126. GE_CHECK_NOTNULL(peer_out_anchor_);
  127. NodePtr memcpy_addr_async_node = CreateMemcpyAddrAsyncNode(graph, peer_out_anchor_, out_of_user_data_);
  128. GE_IF_BOOL_EXEC(memcpy_addr_async_node == nullptr,
  129. GELOGE(INTERNAL_ERROR, "CreateMemcpyAddrAsyncNode failed.");
  130. return INTERNAL_ERROR);
  131. Status ret = InsertMemcpyAddrAsyncNode(peer_out_anchor_, in_anchor_, memcpy_addr_async_node);
  132. GE_IF_BOOL_EXEC(ret != SUCCESS, GELOGE(ret, "InsertMemcpyAddrAsyncNode failed."); return ret);
  133. }
  134. if (find_user_data_for_known_) {
  135. GELOGI("Insert memcpy_addr_async for known graph.");
  136. auto sub_graph = user_data_for_known_->GetOwnerComputeGraph();
  137. NodePtr memcpy_addr_async_node =
  138. CreateMemcpyAddrAsyncNode(sub_graph, peer_out_anchor_for_known_, out_of_user_data_for_known_);
  139. GE_IF_BOOL_EXEC(memcpy_addr_async_node == nullptr,
  140. GELOGE(INTERNAL_ERROR, "CreateMemcpyAddrAsyncNode for known failed.");
  141. return INTERNAL_ERROR);
  142. Status ret =
  143. InsertMemcpyAddrAsyncNode(peer_out_anchor_for_known_, in_anchor_for_known_, memcpy_addr_async_node);
  144. GE_IF_BOOL_EXEC(ret != SUCCESS, GELOGE(ret, "InsertMemcpyAddrAsyncNode for known failed."); return ret);
  145. }
  146. }
  147. }
  148. }
  149. return SUCCESS;
  150. }
  151. void MemcpyAddrAsyncPass::FindUserDataForKnown(const NodePtr &parent_node, uint32_t &parent_index) {
  152. GELOGI("Start FindUserDataForKnown of %s.", parent_node->GetName().c_str());
  153. if (user_data_for_known_->GetOpDesc() == nullptr) {
  154. GELOGI("Cannot get op_desc of %s.", user_data_for_known_->GetName().c_str());
  155. return;
  156. }
  157. string src_var_name;
  158. if (ge::AttrUtils::GetStr(user_data_for_known_->GetOpDesc(), REF_VAR_SRC_VAR_NAME, src_var_name)) {
  159. GELOGI("The data in known graph is variable, no need to insert memcpy_addr_async.");
  160. find_user_data_for_known_ = false;
  161. return;
  162. } else {
  163. find_user_data_for_known_ = true;
  164. }
  165. }
  166. void MemcpyAddrAsyncPass::FindUserDataForNonDynamic(const ge::NodePtr &parent_node, uint32_t &parent_index) {
  167. GELOGI("Start to FindUserDataForNonDynamic of %s.", parent_node->GetName().c_str());
  168. InDataAnchorPtr in_data_anchor = parent_node->GetInDataAnchor(parent_index);
  169. OutDataAnchorPtr out_anchor = in_data_anchor->GetPeerOutAnchor();
  170. GE_IF_BOOL_EXEC(out_anchor == nullptr,
  171. REPORT_INNER_ERROR("E19999", "Index:%u in data node of op:%s(%s) not exist, check invalid",
  172. parent_index,
  173. parent_node->GetName().c_str(), parent_node->GetType().c_str());
  174. GELOGE(INTERNAL_ERROR, "Cannot find out_anchor of %s.", parent_node->GetName().c_str());
  175. return);
  176. NodePtr in_node = out_anchor->GetOwnerNode();
  177. GELOGI("in_node of parent_node is %s.", in_node->GetName().c_str());
  178. if (in_node->GetType() == DATA) {
  179. if (in_node->GetOwnerComputeGraph()->GetParentGraph() != nullptr) {
  180. // DATA is in sub graph again, update user_data of known firstly
  181. user_data_for_known_ = in_node;
  182. out_of_user_data_for_known_ = parent_node;
  183. peer_out_anchor_for_known_ = out_anchor;
  184. in_anchor_for_known_ = in_data_anchor;
  185. NodePtr pre_in_node = in_node->GetOwnerComputeGraph()->GetParentNode();
  186. if (!AttrUtils::GetInt(in_node->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  187. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed",
  188. ATTR_NAME_PARENT_NODE_INDEX.c_str(),
  189. in_node->GetName().c_str(), in_node->GetType().c_str());
  190. GELOGE(INTERNAL_ERROR, "Failed to refresh parent index of %s", in_node->GetName().c_str());
  191. return;
  192. }
  193. FindUserData(pre_in_node, parent_index);
  194. } else {
  195. // DATA is in parent graph and not has input
  196. user_data_ = in_node;
  197. out_of_user_data_ = parent_node;
  198. peer_out_anchor_ = out_anchor;
  199. in_anchor_ = in_data_anchor;
  200. find_user_data_ = true;
  201. GELOGI("%s connect with %s, will insert memcpyaddr.", user_data_->GetName().c_str(),
  202. out_of_user_data_->GetName().c_str());
  203. }
  204. } else if (in_node->GetType() == IF || in_node->GetType() == WHILE || in_node->GetType() == CASE) {
  205. if (!AttrUtils::GetInt(parent_node->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  206. REPORT_CALL_ERROR("E19999", "Get Attr:%s to op:%s(%s) failed",
  207. ATTR_NAME_PARENT_NODE_INDEX.c_str(),
  208. parent_node->GetName().c_str(), parent_node->GetType().c_str());
  209. GELOGE(INTERNAL_ERROR, "Failed to refresh parent index of %s", in_node->GetName().c_str());
  210. return;
  211. }
  212. FindUserData(in_node, parent_index);
  213. } else {
  214. GELOGI("%s connect with %s, which is not user_data.", parent_node->GetName().c_str(), in_node->GetName().c_str());
  215. find_user_data_ = false;
  216. }
  217. }
  218. void MemcpyAddrAsyncPass::FindUserData(const NodePtr &parent_node, uint32_t &parent_index) {
  219. auto parent_op_desc = parent_node->GetOpDesc();
  220. if (parent_op_desc == nullptr) {
  221. GELOGI("Cannot get op_desc of %s.", parent_node->GetName().c_str());
  222. return;
  223. }
  224. bool is_unknown_shape = false;
  225. if (parent_node->GetType() == PARTITIONEDCALL &&
  226. AttrUtils::GetBool(parent_op_desc, ATTR_NAME_IS_UNKNOWN_SHAPE, is_unknown_shape) && !is_unknown_shape) {
  227. FindUserDataForKnown(parent_node, parent_index);
  228. } else {
  229. FindUserDataForNonDynamic(parent_node, parent_index);
  230. }
  231. }
  232. NodePtr MemcpyAddrAsyncPass::CreateMemcpyAddrAsyncNode(const ComputeGraphPtr &graph,
  233. const OutDataAnchorPtr &out_data_anchor,
  234. const NodePtr &out_of_user_data) {
  235. GELOGD("Start CreateMemcpyAddrAsyncNode.");
  236. static uint32_t new_node_index = 0;
  237. OpDescPtr pre_op_desc = out_data_anchor->GetOwnerNode()->GetOpDesc();
  238. GE_CHK_BOOL_EXEC(pre_op_desc != nullptr,
  239. REPORT_INNER_ERROR("E19999", "OpDesc in node is nullptr, check invalid");
  240. return nullptr, "Op_desc of pre node is invalid.");
  241. OpDescPtr op_desc = nullptr;
  242. if (known_sub_graph_) { // insert memcpyasync node when known sub graph
  243. string node_name = pre_op_desc->GetName() + "_" + MEMCPYASYNC + "_" + std::to_string(new_node_index++);
  244. op_desc = MakeShared<OpDesc>(node_name, MEMCPYASYNC);
  245. } else {
  246. string node_name = pre_op_desc->GetName() + "_" + MEMCPYADDRASYNC + "_" + std::to_string(new_node_index++);
  247. op_desc = MakeShared<OpDesc>(node_name, MEMCPYADDRASYNC);
  248. }
  249. GE_CHECK_NOTNULL_EXEC(op_desc,
  250. REPORT_CALL_ERROR("E19999", "New OpDesc failed");
  251. return nullptr);
  252. if (op_desc->AddInputDesc(pre_op_desc->GetOutputDesc(out_data_anchor->GetIdx())) != GRAPH_SUCCESS) {
  253. REPORT_CALL_ERROR("E19999", "Add input desc to op:%s(%s) failed",
  254. pre_op_desc->GetName().c_str(), pre_op_desc->GetType().c_str());
  255. GELOGE(INTERNAL_ERROR, "Add memcpy_addr_async input desc failed.");
  256. return nullptr;
  257. }
  258. if (op_desc->AddOutputDesc(pre_op_desc->GetOutputDesc(out_data_anchor->GetIdx())) != GRAPH_SUCCESS) {
  259. REPORT_CALL_ERROR("E19999", "Add output desc to op:%s(%s) failed",
  260. pre_op_desc->GetName().c_str(), pre_op_desc->GetType().c_str());
  261. GELOGE(INTERNAL_ERROR, "Add memcpy_addr_async output desc failed.");
  262. return nullptr;
  263. }
  264. string stream_label;
  265. if (AttrUtils::GetStr(out_of_user_data->GetOpDesc(), ATTR_NAME_STREAM_LABEL, stream_label)) {
  266. (void)AttrUtils::SetStr(op_desc, ATTR_NAME_STREAM_LABEL, stream_label);
  267. GELOGD("Node %s set stream label: %s", op_desc->GetName().c_str(), stream_label.c_str());
  268. }
  269. bool rts_label_node = false;
  270. if (AttrUtils::GetBool(out_of_user_data->GetOpDesc(), ATTR_NAME_RTS_LABEL_NODE, rts_label_node)) {
  271. (void)AttrUtils::SetBool(op_desc, ATTR_NAME_RTS_LABEL_NODE, rts_label_node);
  272. GELOGD("Node %s set rts label node attribute", op_desc->GetName().c_str());
  273. }
  274. bool labeled_input = false;
  275. (void)ge::AttrUtils::GetBool(out_of_user_data->GetOpDesc(), ATTR_NAME_NODE_CONNECT_INPUT, labeled_input);
  276. if (labeled_input) {
  277. if (!ge::AttrUtils::SetBool(out_of_user_data->GetOpDesc(), ATTR_NAME_NODE_CONNECT_INPUT, false)) {
  278. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed",
  279. ATTR_NAME_NODE_CONNECT_INPUT.c_str(),
  280. out_of_user_data->GetName().c_str(), out_of_user_data->GetType().c_str());
  281. GELOGE(FAILED, "Failed to unset attr %s for node %s.", ATTR_NAME_NODE_CONNECT_INPUT.c_str(),
  282. out_of_user_data->GetName().c_str());
  283. return nullptr;
  284. }
  285. if (!ge::AttrUtils::SetBool(op_desc, ATTR_NAME_NODE_CONNECT_INPUT, true)) {
  286. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed",
  287. ATTR_NAME_NODE_CONNECT_INPUT.c_str(),
  288. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  289. GELOGE(FAILED, "Failed to set attr %s for node %s.", ATTR_NAME_NODE_CONNECT_INPUT.c_str(),
  290. op_desc->GetName().c_str());
  291. return nullptr;
  292. }
  293. }
  294. NodePtr memcpy_addr_async_node = graph->AddNode(op_desc);
  295. GE_CHECK_NOTNULL_EXEC(memcpy_addr_async_node,
  296. REPORT_CALL_ERROR("E19999", "Add node:%s(%s) to graph:%s failed",
  297. op_desc->GetName().c_str(), op_desc->GetType().c_str(),
  298. graph->GetName().c_str());
  299. return nullptr);
  300. return memcpy_addr_async_node;
  301. }
  302. Status MemcpyAddrAsyncPass::InsertMemcpyAddrAsyncNode(const OutDataAnchorPtr &out_anchor,
  303. const InDataAnchorPtr &in_anchor, const NodePtr &node) {
  304. // insert memcpy_addr of each user_data and out_of_user_data
  305. if (GraphUtils::RemoveEdge(out_anchor, in_anchor) != GRAPH_SUCCESS) {
  306. REPORT_CALL_ERROR("E19999", "Remove edge between op:%s(%s)(index:%d) and op:%s(%s)(index:%d) failed",
  307. out_anchor->GetOwnerNode()->GetName().c_str(), out_anchor->GetOwnerNode()->GetType().c_str(),
  308. out_anchor->GetIdx(),
  309. in_anchor->GetOwnerNode()->GetName().c_str(), in_anchor->GetOwnerNode()->GetType().c_str(),
  310. in_anchor->GetIdx());
  311. GELOGE(INTERNAL_ERROR, "Remove edge of %s and %s failed.", out_anchor->GetOwnerNode()->GetName().c_str(),
  312. in_anchor->GetOwnerNode()->GetName().c_str());
  313. return INTERNAL_ERROR;
  314. }
  315. if (GraphUtils::AddEdge(out_anchor, node->GetInDataAnchor(0)) != GRAPH_SUCCESS) {
  316. REPORT_CALL_ERROR("E19999", "Remove edge between op:%s(%s)(index:%d) and op:%s(%s)(index:0) failed",
  317. out_anchor->GetOwnerNode()->GetName().c_str(), out_anchor->GetOwnerNode()->GetType().c_str(),
  318. out_anchor->GetIdx(),
  319. node->GetName().c_str(), node->GetType().c_str());
  320. GELOGE(INTERNAL_ERROR, "Add edge of %s and %s failed.", out_anchor->GetOwnerNode()->GetName().c_str(),
  321. node->GetName().c_str());
  322. return INTERNAL_ERROR;
  323. }
  324. if (GraphUtils::AddEdge(node->GetOutDataAnchor(0), in_anchor) != GRAPH_SUCCESS) {
  325. REPORT_CALL_ERROR("E19999", "Remove edge between op:%s(%s)(index:0) and op:%s(%s)(index:%d) failed",
  326. node->GetName().c_str(), node->GetType().c_str(),
  327. in_anchor->GetOwnerNode()->GetName().c_str(), in_anchor->GetOwnerNode()->GetType().c_str(),
  328. in_anchor->GetIdx());
  329. GELOGE(INTERNAL_ERROR, "Add edge of %s and %s failed.", node->GetName().c_str(),
  330. in_anchor->GetOwnerNode()->GetName().c_str());
  331. return INTERNAL_ERROR;
  332. }
  333. return SUCCESS;
  334. }
  335. Status MemcpyAddrAsyncPass::InsertMemAddrAsyncNodeBeforeNetoutput(const ComputeGraphPtr &graph, const NodePtr &node) {
  336. GELOGD("Start AddMemcpyAddrAsyncNode for %s.", node->GetName().c_str());
  337. for (const auto &in_data_anchor : node->GetAllInDataAnchors()) {
  338. auto in_node = NodeUtils::GetInDataNodeByIndex(*node, in_data_anchor->GetIdx());
  339. GE_CHECK_NOTNULL(in_node);
  340. auto peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  341. if ((in_node->GetType() != CONSTANT) &&
  342. (in_node->GetType() != CONSTANTOP) &&
  343. (in_node->GetType() != DATA)) {
  344. continue;
  345. }
  346. auto desc = in_node->GetOpDesc();
  347. GE_CHECK_NOTNULL(desc);
  348. if (IsEmptyTenor(desc->GetOutputDesc(peer_out_anchor->GetIdx()).GetShape())) {
  349. continue;
  350. }
  351. GELOGI("Need to insert MemcpyAddrAsync before netoutput on parent graph.");
  352. NodePtr memcpy_addr_async_node = CreateMemcpyAddrAsyncNode(graph, peer_out_anchor, in_node);
  353. GE_IF_BOOL_EXEC(memcpy_addr_async_node == nullptr, GELOGE(INTERNAL_ERROR, "CreateMemcpyAddrAsyncNode failed.");
  354. return INTERNAL_ERROR);
  355. Status ret = InsertMemcpyAddrAsyncNode(peer_out_anchor, in_data_anchor, memcpy_addr_async_node);
  356. GE_IF_BOOL_EXEC(ret != SUCCESS, GELOGE(ret, "InsertMemcpyAddrAsyncNode failed."); return ret);
  357. GELOGI("Insert mem_addr_async node %s success between %s and %s.", memcpy_addr_async_node->GetName().c_str(),
  358. in_node->GetName().c_str(), node->GetName().c_str());
  359. // if src node is const, need to update attr and offset here because this pass process is after offset set.
  360. if ((in_node->GetType() == CONSTANT) || (in_node->GetType() == CONSTANTOP)) {
  361. NodeUtils::UpdateIsInputConst(memcpy_addr_async_node);
  362. auto output_desc = node->GetOpDesc();
  363. GE_CHECK_NOTNULL(output_desc);
  364. auto output_tensor_desc = output_desc->MutableInputDesc(static_cast<uint32_t>(in_data_anchor->GetIdx()));
  365. int64_t data_offset = 0;
  366. (void)TensorUtils::GetDataOffset(*output_tensor_desc, data_offset);
  367. auto input_tensor = memcpy_addr_async_node->GetOpDesc()->MutableInputDesc(0);
  368. GELOGI("Need update const Offset %ld to op [%s]", data_offset, memcpy_addr_async_node->GetName().c_str());
  369. TensorUtils::SetDataOffset(*input_tensor, data_offset);
  370. TensorUtils::SetDataOffset(*output_tensor_desc, 0);
  371. }
  372. }
  373. NodeUtils::UpdateIsInputConst(node);
  374. return SUCCESS;
  375. }
  376. bool MemcpyAddrAsyncPass::IsEmptyTenor(const GeShape &shape) const {
  377. for (const auto dim : shape.GetDims()) {
  378. if (dim == 0) {
  379. return true;
  380. }
  381. }
  382. return false;
  383. }
  384. } // namespace ge

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