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.

iterator_op_pass.cc 19 kB

5 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
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 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
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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/iterator_op_pass.h"
  17. #include <memory>
  18. #include <sstream>
  19. #include <string>
  20. #include <vector>
  21. #include "common/debug/log.h"
  22. #include "framework/common/debug/ge_log.h"
  23. #include "common/ge/ge_util.h"
  24. #include "framework/common/debug/ge_log.h"
  25. #include "graph/anchor.h"
  26. #include "graph/common/omg_util.h"
  27. #include "graph/graph.h"
  28. #include "graph/node.h"
  29. #include "graph/passes/pass_utils.h"
  30. #include "graph/utils/graph_utils.h"
  31. #include "runtime/mem.h"
  32. #include "graph/manager/graph_var_manager.h"
  33. #include "graph/ge_context.h"
  34. #include "graph/manager/util/rt_context_util.h"
  35. namespace ge {
  36. const char *const kGetNext = "GetNext";
  37. const int kMaxIterationsPerLoop = INT32_MAX - 1;
  38. Status IteratorOpPass::Run(ge::ComputeGraphPtr graph) {
  39. GELOGD("GetNextOpPass begin");
  40. GE_CHECK_NOTNULL(graph);
  41. if (!PassUtils::IsNeedTrainIteFlowCtrl(graph)) {
  42. return SUCCESS;
  43. }
  44. std::string type;
  45. for (ge::NodePtr &node : graph->GetDirectNode()) {
  46. GE_CHK_STATUS_RET(GetOriginalType(node, type));
  47. auto op_desc = node->GetOpDesc();
  48. GE_CHECK_NOTNULL(op_desc);
  49. const string op_type = op_desc->GetType();
  50. if (type == "IteratorV2" || type == "Iterator" || op_type == kGetNext) {
  51. ge::NodePtr memcpy_async_node = InsertMemcpyAsyncNode(node, graph);
  52. GE_CHECK_NOTNULL(memcpy_async_node);
  53. auto status = SetCycleEvent(memcpy_async_node);
  54. if (status != ge::SUCCESS) {
  55. REPORT_CALL_ERROR("E19999", "Set cycle event to op:%s(%s) failed",
  56. memcpy_async_node->GetName().c_str(), memcpy_async_node->GetType().c_str());
  57. GELOGE(status, "Set cycle event failed.");
  58. return status;
  59. }
  60. status = SetStreamLabel(memcpy_async_node, memcpy_async_node->GetName());
  61. if (status != ge::SUCCESS) {
  62. REPORT_CALL_ERROR("E19999", "Set stream label:%s to op:%s(%s) failed",
  63. memcpy_async_node->GetName().c_str(), memcpy_async_node->GetName().c_str(),
  64. memcpy_async_node->GetType().c_str());
  65. GELOGE(status, "set stream label failed.");
  66. return status;
  67. }
  68. status = SetStreamLabel(node, node->GetName());
  69. if (status != ge::SUCCESS) {
  70. REPORT_CALL_ERROR("E19999", "Set stream label:%s to op:%s(%s) failed",
  71. node->GetName().c_str(), node->GetName().c_str(), node->GetType().c_str());
  72. GELOGE(status, "set stream label failed.");
  73. return status;
  74. }
  75. GELOGI("Set independent loop for iterator node success");
  76. int64_t loop_per_iter = 0;
  77. ge::GeTensorDesc ge_tensor_desc;
  78. status = VarManager::Instance(graph->GetSessionID())->GetCurVarDesc(NODE_NAME_FLOWCTRL_LOOP_PER_ITER,
  79. ge_tensor_desc);
  80. GE_IF_BOOL_EXEC(status != SUCCESS, GELOGW("Fail to Get var_desc of NODE_NAME_FLOWCTRL_LOOP_PER_ITER failed.");
  81. continue);
  82. Status ret;
  83. ret = SetRtContext(graph->GetSessionID(), graph->GetGraphID(), rtContext_t(), RT_CTX_NORMAL_MODE);
  84. // EOS will not be considered if ret is not SUCCESS.
  85. GE_IF_BOOL_EXEC(ret != SUCCESS, GELOGW("Set rt context RT_CTX_NORMAL_MODE failed."); continue);
  86. status = GetVariableValue(graph->GetSessionID(), ge_tensor_desc, NODE_NAME_FLOWCTRL_LOOP_PER_ITER,
  87. &loop_per_iter);
  88. ret = SetRtContext(graph->GetSessionID(), graph->GetGraphID(), rtContext_t(), RT_CTX_GEN_MODE);
  89. // The following process will be affected if ret is not SUCCESS.
  90. GE_IF_BOOL_EXEC(ret != SUCCESS, GELOGE(ret, "Set rt context RT_CTX_GEN_MODE failed."); return ret);
  91. GE_IF_BOOL_EXEC(status != SUCCESS, GELOGW("Get variable value of NODE_NAME_FLOWCTRL_LOOP_PER_ITER failed.");
  92. continue);
  93. GELOGI("The value of NODE_NAME_FLOWCTRL_LOOP_PER_ITER is %ld", loop_per_iter);
  94. if (loop_per_iter == kMaxIterationsPerLoop) {
  95. ge::NodePtr end_of_sequence_node = InsertEndOfSequenceNode(node, memcpy_async_node, graph);
  96. GE_CHECK_NOTNULL(end_of_sequence_node);
  97. status = SetStreamLabel(end_of_sequence_node, end_of_sequence_node->GetName());
  98. if (status != ge::SUCCESS) {
  99. REPORT_CALL_ERROR("E19999", "Set stream label:%s to op:%s(%s) failed",
  100. end_of_sequence_node->GetName().c_str(), end_of_sequence_node->GetName().c_str(),
  101. end_of_sequence_node->GetType().c_str());
  102. GELOGE(status, "set stream label failed.");
  103. return status;
  104. }
  105. GELOGI("Insert EndOfSequence node success.");
  106. }
  107. }
  108. GELOGI("GetNextOpPass end");
  109. }
  110. GELOGD("GetNextOpPass end");
  111. return SUCCESS;
  112. }
  113. Status IteratorOpPass::GetVariableValue(uint64_t session_id, const ge::GeTensorDesc &tensor_desc,
  114. const std::string &var_name, void *dest) {
  115. // base_addr
  116. uint8_t *var_mem_base = VarManager::Instance(session_id)->GetVarMemoryBase(RT_MEMORY_HBM);
  117. GE_CHECK_NOTNULL(var_mem_base);
  118. // offset + logic_base
  119. uint8_t *dev_ptr = nullptr;
  120. auto status = VarManager::Instance(session_id)->GetVarAddr(var_name, tensor_desc, &dev_ptr);
  121. if (status != ge::SUCCESS) {
  122. REPORT_CALL_ERROR("E19999", "Get Var add by name:%s failed, session_id:%lu",
  123. var_name.c_str(), session_id);
  124. GELOGE(status, "Get variable %s address failed.", var_name.c_str());
  125. return status;
  126. }
  127. int64_t offset = static_cast<int64_t>(reinterpret_cast<intptr_t>(dev_ptr));
  128. // logic_base_addr
  129. auto logic_var_base = VarManager::Instance(session_id)->GetVarMemLogicBase();
  130. // devcice_addr
  131. uint8_t *variable_addr = static_cast<uint8_t *>(var_mem_base + offset - logic_var_base);
  132. GE_CHK_RT_RET(rtMemcpy(dest, sizeof(int64_t), variable_addr, sizeof(int64_t), RT_MEMCPY_DEVICE_TO_HOST));
  133. return SUCCESS;
  134. }
  135. ///
  136. /// @brief insert EndOfSequence after GetNext
  137. ///
  138. /// @param pre_node
  139. /// @param graph
  140. /// @return ge::NodePtr
  141. ///
  142. ge::NodePtr IteratorOpPass::InsertEndOfSequenceNode(const ge::NodePtr &pre_node, const ge::NodePtr &memcpy_node,
  143. const ge::ComputeGraphPtr &graph) {
  144. GELOGI("Start to insert EndOfSequence node.");
  145. GE_CHK_BOOL_EXEC(pre_node != nullptr, GELOGW("Pre node is null."); return nullptr);
  146. GE_CHK_BOOL_EXEC(graph != nullptr, GELOGW("graph is null."); return nullptr);
  147. ge::OpDescPtr end_of_seq_op_desc = CreateEndOfSequenceOp(pre_node);
  148. GE_CHK_BOOL_EXEC(end_of_seq_op_desc != nullptr, GELOGW("Create EndOfSequence op fail."); return nullptr);
  149. ge::NodePtr end_of_seq_node = graph->AddNode(end_of_seq_op_desc);
  150. GE_CHK_BOOL_EXEC(end_of_seq_node != nullptr, return nullptr, "Insert EndOfSequence node fail.");
  151. // getnext(data) --> EOS
  152. GE_CHK_BOOL_EXEC(pre_node->GetAllOutDataAnchorsSize() != 0, GELOGW("Pre node has no output."); return nullptr);
  153. auto out_anchor = pre_node->GetOutDataAnchor(0);
  154. ge::graphStatus status;
  155. status = GraphUtils::AddEdge(out_anchor, end_of_seq_node->GetInDataAnchor(0));
  156. GE_CHK_BOOL_EXEC(status == GRAPH_SUCCESS,
  157. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(index:0) and op:%s(%s)(index:0) failed",
  158. pre_node->GetName().c_str(), pre_node->GetType().c_str(),
  159. end_of_seq_node->GetName().c_str(), end_of_seq_node->GetType().c_str());
  160. return nullptr,
  161. "Graph add EndOfSequence op input edge fail, dst node: %s.",
  162. end_of_seq_node->GetName().c_str());
  163. // EOS(control) --> subsequent of memcpy
  164. OutControlAnchorPtr out_ctrl_anchor = end_of_seq_node->GetOutControlAnchor();
  165. GE_CHK_BOOL_EXEC(out_ctrl_anchor != nullptr, GELOGW("out_ctrl_anchor is null."); return nullptr);
  166. // add ctrl edge
  167. for (const auto &out_node : memcpy_node->GetOutNodes()) {
  168. auto in_ctrl_anchor = out_node->GetInControlAnchor();
  169. if (in_ctrl_anchor == nullptr) {
  170. continue;
  171. }
  172. status = GraphUtils::AddEdge(out_ctrl_anchor, in_ctrl_anchor);
  173. GE_CHK_BOOL_EXEC(status == GRAPH_SUCCESS,
  174. REPORT_CALL_ERROR("E19999", "Add control edge between op:%s(%s) and op:%s(%s) failed",
  175. end_of_seq_node->GetName().c_str(), end_of_seq_node->GetType().c_str(),
  176. out_node->GetName().c_str(), out_node->GetType().c_str());
  177. return nullptr,
  178. "Graph add EndOfSequence op out ctrl edge fail, dst node: %s.",
  179. out_node->GetName().c_str());
  180. GELOGI("Graph add EndOfSequence op out ctrl edge, dst node: %s.",
  181. out_node->GetName().c_str());
  182. }
  183. return end_of_seq_node;
  184. }
  185. ///
  186. /// @brief create EndOfSequence
  187. ///
  188. /// @param pre_node
  189. /// @return ge::OpDescPtr
  190. ///
  191. ge::OpDescPtr IteratorOpPass::CreateEndOfSequenceOp(const ge::NodePtr &pre_node) {
  192. GELOGI("Start to create endOfSequence op.");
  193. GE_CHK_BOOL_EXEC(pre_node != nullptr,
  194. REPORT_INNER_ERROR("E19999", "Param pre_node is nullptr, check invalid");
  195. return nullptr, "Input param invalid.");
  196. string node_name = pre_node->GetName() + "_EndOfSequence";
  197. ge::OpDescPtr op_desc = MakeShared<OpDesc>(node_name, ENDOFSEQUENCE);
  198. if (op_desc == nullptr) {
  199. REPORT_CALL_ERROR("E19999", "New OpDesc failed");
  200. GELOGE(FAILED, "MakeShared fail.");
  201. return op_desc;
  202. }
  203. ge::OpDescPtr pre_node_op_desc = pre_node->GetOpDesc();
  204. GE_CHK_BOOL_EXEC(pre_node_op_desc != nullptr,
  205. REPORT_INNER_ERROR("E19999", "OpDesc in node is nullptr, check invalid");
  206. return nullptr, "OpDesc of pre_node is invalid.");
  207. GELOGI("Create EndOfSequence op:%s.", op_desc->GetName().c_str());
  208. GE_CHK_BOOL_EXEC(op_desc->AddInputDesc(pre_node_op_desc->GetOutputDesc(0)) == GRAPH_SUCCESS,
  209. REPORT_CALL_ERROR("E19999", "Add input desc to op:%s(%s) failed",
  210. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  211. return nullptr,
  212. "Create EndOfSequence op:add input desc fail.");
  213. return op_desc;
  214. }
  215. ///
  216. /// @brief insert memcpy after GetNext
  217. ///
  218. /// @param pre_node
  219. /// @param graph
  220. /// @return ge::NodePtr
  221. ///
  222. ge::NodePtr IteratorOpPass::InsertMemcpyAsyncNode(const ge::NodePtr &pre_node, const ge::ComputeGraphPtr &graph) {
  223. GE_CHK_BOOL_EXEC(pre_node != nullptr, GELOGW("Pre node is null."); return nullptr);
  224. GE_CHK_BOOL_EXEC(graph != nullptr, GELOGW("graph is null."); return nullptr);
  225. ge::OpDescPtr memcpy_async_op_desc = CreateMemcpyAsyncOp(pre_node);
  226. GE_CHK_BOOL_EXEC(memcpy_async_op_desc != nullptr, GELOGW("Create memcpyAsync op fail."); return nullptr);
  227. ge::NodePtr memcpy_async_node = graph->AddNode(memcpy_async_op_desc);
  228. GE_CHK_BOOL_EXEC(memcpy_async_node != nullptr,
  229. REPORT_CALL_ERROR("E19999", "Add node:%s(%s) to graph:%s failed",
  230. memcpy_async_op_desc->GetName().c_str(), memcpy_async_op_desc->GetType().c_str(),
  231. graph->GetName().c_str());
  232. return nullptr, "Insert mencpy node fail.");
  233. // Data out
  234. for (auto &out_anchor : pre_node->GetAllOutDataAnchors()) {
  235. if (out_anchor == nullptr) {
  236. continue;
  237. }
  238. ge::graphStatus status;
  239. GELOGI("Graph add memcpyAsync op in edge, index:%d.", out_anchor->GetIdx());
  240. for (auto &peer_in_anchor : out_anchor->GetPeerInDataAnchors()) {
  241. GE_IF_BOOL_EXEC(peer_in_anchor == nullptr, GELOGW("peer_in_anchor is nullptr"); return nullptr);
  242. status = GraphUtils::RemoveEdge(out_anchor, peer_in_anchor);
  243. GE_CHK_BOOL_EXEC(status == GRAPH_SUCCESS,
  244. REPORT_CALL_ERROR(
  245. "E19999", "Remove edge between op:%s(%s)(index:%d) and op:%s(%s)(index:%d) failed",
  246. pre_node->GetName().c_str(), pre_node->GetType().c_str(), out_anchor->GetIdx(),
  247. peer_in_anchor->GetOwnerNode()->GetName().c_str(),
  248. peer_in_anchor->GetOwnerNode()->GetType().c_str(),
  249. peer_in_anchor->GetIdx());
  250. return nullptr, "Remove edge failed, index:%d.", out_anchor->GetIdx());
  251. status = GraphUtils::AddEdge(memcpy_async_node->GetOutDataAnchor(out_anchor->GetIdx()), peer_in_anchor);
  252. GE_CHK_BOOL_EXEC(status == GRAPH_SUCCESS,
  253. REPORT_CALL_ERROR(
  254. "E19999", "Add edge between op:%s(%s)(index:%d) and op:%s(%s)(index:%d) failed",
  255. memcpy_async_node->GetName().c_str(), memcpy_async_node->GetType().c_str(),
  256. out_anchor->GetIdx(),
  257. peer_in_anchor->GetOwnerNode()->GetName().c_str(),
  258. peer_in_anchor->GetOwnerNode()->GetType().c_str(),
  259. peer_in_anchor->GetIdx());
  260. return nullptr,
  261. "Graph add memcpyAsync op out edge fail, src index:%d, dst index:%d, dst node: %s.",
  262. out_anchor->GetIdx(), peer_in_anchor->GetIdx(),
  263. peer_in_anchor->GetOwnerNode()->GetName().c_str());
  264. GELOGI("Graph add memcpyAsync op out edge, src index:%d, dst index:%d, dst node: %s.", out_anchor->GetIdx(),
  265. peer_in_anchor->GetIdx(), peer_in_anchor->GetOwnerNode()->GetName().c_str());
  266. }
  267. status = GraphUtils::AddEdge(out_anchor, memcpy_async_node->GetInDataAnchor(out_anchor->GetIdx()));
  268. GE_CHK_BOOL_EXEC(status == GRAPH_SUCCESS,
  269. REPORT_CALL_ERROR(
  270. "E19999", "Add edge between op:%s(%s)(index:%d) and op:%s(%s)(index:%d) failed",
  271. pre_node->GetName().c_str(), pre_node->GetType().c_str(), out_anchor->GetIdx(),
  272. memcpy_async_node->GetName().c_str(), memcpy_async_node->GetType().c_str(),
  273. out_anchor->GetIdx());
  274. return nullptr, "Graph add memcpyAsync op in edge fail, index:%d.",
  275. out_anchor->GetIdx());
  276. }
  277. // Control out
  278. OutControlAnchorPtr out_ctrl_anchor = pre_node->GetOutControlAnchor();
  279. GE_IF_BOOL_EXEC(out_ctrl_anchor != nullptr,
  280. for (auto &peer_in_ctrl_anchor : out_ctrl_anchor->GetPeerInControlAnchors()) {
  281. ge::graphStatus status = GraphUtils::RemoveEdge(out_ctrl_anchor, peer_in_ctrl_anchor);
  282. GE_CHK_BOOL_EXEC(status == GRAPH_SUCCESS,
  283. REPORT_CALL_ERROR(
  284. "E19999", "Remove control edge between op:%s(%s) and op:%s(%s) failed",
  285. pre_node->GetName().c_str(), pre_node->GetType().c_str(),
  286. peer_in_ctrl_anchor->GetOwnerNode()->GetName().c_str(),
  287. peer_in_ctrl_anchor->GetOwnerNode()->GetType().c_str());
  288. return nullptr, "Remove edge failed, dst node: %s.",
  289. peer_in_ctrl_anchor->GetOwnerNode()->GetName().c_str());
  290. status = GraphUtils::AddEdge(memcpy_async_node->GetOutControlAnchor(), peer_in_ctrl_anchor);
  291. GE_CHK_BOOL_EXEC(status == GRAPH_SUCCESS,
  292. REPORT_CALL_ERROR(
  293. "E19999", "Add control edge between op:%s(%s) and op:%s(%s) failed",
  294. memcpy_async_node->GetName().c_str(), memcpy_async_node->GetType().c_str(),
  295. peer_in_ctrl_anchor->GetOwnerNode()->GetName().c_str(),
  296. peer_in_ctrl_anchor->GetOwnerNode()->GetType().c_str());
  297. return nullptr,
  298. "Graph add memcpyAsync op out ctrl edge fail, dst node: %s.",
  299. peer_in_ctrl_anchor->GetOwnerNode()->GetName().c_str());
  300. GELOGI("Graph add memcpyAsync op out ctrl edge, dst node: %s.",
  301. peer_in_ctrl_anchor->GetOwnerNode()->GetName().c_str());
  302. });
  303. GELOGI("Insert memcpyAsync op success.");
  304. return memcpy_async_node;
  305. }
  306. ///
  307. /// @brief create memcpy
  308. ///
  309. /// @param pre_node
  310. /// @return ge::OpDescPtr
  311. ///
  312. ge::OpDescPtr IteratorOpPass::CreateMemcpyAsyncOp(const ge::NodePtr &pre_node) {
  313. GE_CHK_BOOL_EXEC(pre_node != nullptr, return nullptr, "Input param invalid.");
  314. string node_name = pre_node->GetName() + "_MemcpyAsync";
  315. ge::OpDescPtr op_desc = MakeShared<OpDesc>(node_name.c_str(), MEMCPYASYNC);
  316. if (op_desc == nullptr) {
  317. REPORT_CALL_ERROR("E19999", "New OpDesc failed");
  318. GELOGE(FAILED, "MakeShared fail.");
  319. return op_desc;
  320. }
  321. GELOGI("Create memcpyAsync op:%s.", op_desc->GetName().c_str());
  322. ge::OpDescPtr pre_node_op_desc = pre_node->GetOpDesc();
  323. GE_CHK_BOOL_EXEC(pre_node_op_desc != nullptr,
  324. REPORT_INNER_ERROR("E19999", "OpDesc in node is nullptr, check invalid");
  325. return nullptr, "OpDesc of pre_node is invalid.");
  326. size_t out_size = pre_node_op_desc->GetOutputsSize();
  327. GELOGI("Create memcpyAsync op, pre_node out_size: %zu.", out_size);
  328. for (size_t i = 0; i < out_size; i++) {
  329. GE_CHK_BOOL_EXEC(op_desc->AddInputDesc(pre_node_op_desc->GetOutputDesc(i)) == GRAPH_SUCCESS,
  330. REPORT_CALL_ERROR("E19999", "Add input desc to op:%s(%s) failed",
  331. pre_node_op_desc->GetName().c_str(), pre_node_op_desc->GetType().c_str());
  332. return nullptr,
  333. "Create memcpyAsync op:add input desc fail.");
  334. GE_CHK_BOOL_EXEC(op_desc->AddOutputDesc(pre_node_op_desc->GetOutputDesc(i)) == GRAPH_SUCCESS,
  335. REPORT_CALL_ERROR("E19999", "Add output desc to op:%s(%s) failed",
  336. pre_node_op_desc->GetName().c_str(), pre_node_op_desc->GetType().c_str());
  337. return nullptr,
  338. "Create memcpyAsync op:add output desc fail.");
  339. }
  340. return op_desc;
  341. }
  342. Status IteratorOpPass::SetRtContext(uint64_t session_id, uint32_t graph_id, rtContext_t rt_context, rtCtxMode_t mode) {
  343. GELOGI("set rt_context, session id: %lu, graph id: %u, mode %d, device id:%u.", session_id,
  344. graph_id, static_cast<int>(mode), ge::GetContext().DeviceId());
  345. GE_CHK_RT_RET(rtCtxCreate(&rt_context, mode, ge::GetContext().DeviceId()));
  346. GE_CHK_RT_RET(rtCtxSetCurrent(rt_context));
  347. RtContextUtil::GetInstance().AddRtContext(session_id, graph_id, rt_context);
  348. return SUCCESS;
  349. }
  350. } // namespace ge

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