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

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