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.

session_manager.cc 18 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
4 years ago
5 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
4 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
4 years ago
5 years ago
5 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
5 years ago
5 years ago
5 years ago
5 years ago
4 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
5 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
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 "session/session_manager.h"
  17. #include <memory>
  18. #include <utility>
  19. #include "common/ge/ge_util.h"
  20. #include "framework/common/debug/ge_log.h"
  21. #include "graph/ge_context.h"
  22. #include "graph/load/model_manager/model_manager.h"
  23. #include "graph/manager/util/rt_context_util.h"
  24. using std::map;
  25. using std::string;
  26. using std::vector;
  27. namespace ge {
  28. Status SessionManager::Initialize(const std::map<std::string, std::string> &options) {
  29. if (init_flag_) {
  30. GELOGW("Session Manager has been initialized.");
  31. return SUCCESS;
  32. }
  33. init_flag_ = true;
  34. return SUCCESS;
  35. }
  36. Status SessionManager::Finalize() {
  37. if (!init_flag_) {
  38. GELOGW("Session Manager has not been initialized.");
  39. return SUCCESS;
  40. }
  41. std::lock_guard<std::mutex> lock(mutex_);
  42. for (auto iter = session_manager_map_.begin(); iter != session_manager_map_.end(); ++iter) {
  43. (void)iter->second->Finalize();
  44. }
  45. session_manager_map_.clear();
  46. init_flag_ = false;
  47. return SUCCESS;
  48. }
  49. Status SessionManager::SetRtContext(SessionId session_id, rtContext_t rt_context) {
  50. GELOGI("set rt_context RT_CTX_NORMAL_MODE, device id:%u.", GetContext().DeviceId());
  51. GE_CHK_RT_RET(rtCtxCreate(&rt_context, RT_CTX_NORMAL_MODE, static_cast<int32_t>(GetContext().DeviceId())));
  52. GE_CHK_RT_RET(rtCtxSetCurrent(rt_context));
  53. RtContextUtil::GetInstance().AddRtContext(session_id, rt_context);
  54. return SUCCESS;
  55. }
  56. Status SessionManager::CreateSession(const std::map<std::string, std::string> &options, SessionId &session_id) {
  57. if (!init_flag_) {
  58. GELOGE(GE_SESSION_MANAGER_NOT_INIT, "[Create][Session]fail for Session manager is not initialized.");
  59. REPORT_INNER_ERROR("E19999", "CreateSession fail for Session manager is not initialized.");
  60. return GE_SESSION_MANAGER_NOT_INIT;
  61. }
  62. SessionId next_session_id = 0;
  63. std::lock_guard<std::mutex> lock(mutex_);
  64. Status nextSessionIdRet = GetNextSessionId(next_session_id);
  65. if (nextSessionIdRet != SUCCESS) {
  66. return nextSessionIdRet;
  67. }
  68. SessionPtr sessionPtr = MakeShared<InnerSession>(next_session_id, options);
  69. if (sessionPtr == nullptr) {
  70. return MEMALLOC_FAILED;
  71. }
  72. Status ret = sessionPtr->Initialize();
  73. if (ret != SUCCESS) {
  74. return ret;
  75. }
  76. (void)session_manager_map_.emplace(std::pair<SessionId, SessionPtr>(next_session_id, sessionPtr));
  77. session_id = next_session_id;
  78. // create a context
  79. ret = SetRtContext(session_id, rtContext_t());
  80. return ret;
  81. }
  82. Status SessionManager::DestroySession(SessionId session_id) {
  83. if (!init_flag_) {
  84. GELOGE(GE_SESSION_MANAGER_NOT_INIT,
  85. "[Destroy][Session]fail for Session manager is not initialized, session_id:%lu.", session_id);
  86. REPORT_INNER_ERROR("E19999", "DestroySession fail for Session manager is not initialized, session_id:%lu.",
  87. session_id);
  88. return GE_SESSION_MANAGER_NOT_INIT;
  89. }
  90. std::lock_guard<std::mutex> lock(mutex_);
  91. std::map<SessionId, SessionPtr>::iterator it = session_manager_map_.find(session_id);
  92. if (it == session_manager_map_.end()) {
  93. return GE_SESSION_NOT_EXIST;
  94. }
  95. if (ModelManager::GetInstance() != nullptr) {
  96. ModelManager::GetInstance()->DestroyAicpuSession(session_id);
  97. }
  98. // Unified destruct rt_context
  99. RtContextUtil::GetInstance().DestroyRtContexts(session_id);
  100. SessionPtr innerSession = it->second;
  101. Status ret = innerSession->Finalize();
  102. if (ret != SUCCESS) {
  103. return ret;
  104. }
  105. (void)session_manager_map_.erase(session_id);
  106. return ret;
  107. }
  108. Status SessionManager::GetVariable(SessionId session_id, const std::string &name, Tensor &val) {
  109. if (!init_flag_) {
  110. GELOGE(GE_SESSION_MANAGER_NOT_INIT,
  111. "[Get][Variable]fail for Session manager is not initialized, session_id:%lu, input_name:%s.",
  112. session_id, name.c_str());
  113. REPORT_INNER_ERROR("E19999",
  114. "GetVariable fail for Session manager is not initialized, session_id:%lu, input_name:%s.",
  115. session_id, name.c_str());
  116. return GE_SESSION_MANAGER_NOT_INIT;
  117. }
  118. SessionPtr innerSession = nullptr;
  119. {
  120. std::lock_guard<std::mutex> lock(mutex_);
  121. std::map<SessionId, SessionPtr>::iterator it = session_manager_map_.find(session_id);
  122. if (it == session_manager_map_.end()) {
  123. return GE_SESSION_NOT_EXIST;
  124. } else {
  125. innerSession = it->second;
  126. }
  127. }
  128. return innerSession->GetVariable(name, val);
  129. }
  130. Status SessionManager::AddGraph(SessionId session_id, uint32_t graph_id, const Graph &graph) {
  131. std::map<std::string, std::string> options;
  132. return AddGraph(session_id, graph_id, graph, options);
  133. }
  134. Status SessionManager::AddGraph(SessionId session_id, uint32_t graph_id, const Graph &graph,
  135. const std::map<std::string, std::string> &options) {
  136. if (!init_flag_) {
  137. GELOGE(GE_SESSION_MANAGER_NOT_INIT,
  138. "[Add][Graph]fail for Session manager is not initialized, session_id:%lu, graph_id:%u.",
  139. session_id, graph_id);
  140. REPORT_INNER_ERROR("E19999", "AddGraph fail for Session manager is not initialized, session_id:%lu, graph_id:%u.",
  141. session_id, graph_id);
  142. return GE_SESSION_MANAGER_NOT_INIT;
  143. }
  144. SessionPtr innerSession = nullptr;
  145. {
  146. std::lock_guard<std::mutex> lock(mutex_);
  147. std::map<SessionId, SessionPtr>::iterator it = session_manager_map_.find(session_id);
  148. if (it == session_manager_map_.end()) {
  149. return GE_SESSION_NOT_EXIST;
  150. } else {
  151. innerSession = it->second;
  152. }
  153. auto compute_graph = GraphUtils::GetComputeGraph(graph);
  154. GE_CHECK_NOTNULL(compute_graph);
  155. std::string session_graph_id = std::to_string(session_id) + "_" + std::to_string(graph_id);
  156. if (!AttrUtils::SetStr(*compute_graph, ATTR_NAME_SESSION_GRAPH_ID, session_graph_id)) {
  157. GELOGW("Set graph session_graph_id attr failed.");
  158. } else {
  159. GELOGD("Set graph session_graph_id attr to [%s]", session_graph_id.c_str());
  160. }
  161. for (auto graph : compute_graph->GetAllSubgraphs()) {
  162. AttrUtils::SetStr(*graph, ATTR_NAME_SESSION_GRAPH_ID, session_graph_id);
  163. }
  164. }
  165. return innerSession->AddGraph(graph_id, graph, options);
  166. }
  167. Status SessionManager::AddGraphWithCopy(SessionId session_id, uint32_t graph_id, const Graph &graph,
  168. const std::map<std::string, std::string> &options) {
  169. if (!init_flag_) {
  170. GELOGE(GE_SESSION_MANAGER_NOT_INIT,
  171. "[Add][GraphWithCopy]fail for Session manager is not initialized, session_id:%lu, graph_id:%u.",
  172. session_id, graph_id);
  173. REPORT_INNER_ERROR("E19999",
  174. "AddGraphWithCopy fail for Session manager is not initialized, session_id:%lu, graph_id:%u",
  175. session_id, graph_id);
  176. return GE_SESSION_MANAGER_NOT_INIT;
  177. }
  178. SessionPtr innerSession = nullptr;
  179. {
  180. std::lock_guard<std::mutex> lock(mutex_);
  181. std::map<SessionId, SessionPtr>::iterator it = session_manager_map_.find(session_id);
  182. if (it == session_manager_map_.end()) {
  183. return GE_SESSION_NOT_EXIST;
  184. } else {
  185. innerSession = it->second;
  186. }
  187. auto compute_graph = GraphUtils::GetComputeGraph(graph);
  188. GE_CHECK_NOTNULL(compute_graph);
  189. std::string session_graph_id = std::to_string(session_id) + "_" + std::to_string(graph_id);
  190. if (!AttrUtils::SetStr(*compute_graph, ATTR_NAME_SESSION_GRAPH_ID, session_graph_id)) {
  191. GELOGW("Set graph session_graph_id attr failed.");
  192. } else {
  193. GELOGD("Set graph session_graph_id attr to [%s]", session_graph_id.c_str());
  194. }
  195. for (auto graph : compute_graph->GetAllSubgraphs()) {
  196. AttrUtils::SetStr(*graph, ATTR_NAME_SESSION_GRAPH_ID, session_graph_id);
  197. }
  198. }
  199. return innerSession->AddGraphWithCopy(graph_id, graph, options);
  200. }
  201. Status SessionManager::RunGraph(SessionId session_id, uint32_t graph_id, const std::vector<Tensor> &inputs,
  202. std::vector<Tensor> &outputs) {
  203. if (!init_flag_) {
  204. GELOGE(GE_SESSION_MANAGER_NOT_INIT,
  205. "[Run][Graph]fail for Session manager is not initialized, session_id:%lu, graph_id:%u.",
  206. session_id, graph_id);
  207. REPORT_INNER_ERROR("E19999",
  208. "RunGraph fail for Session manager is not initialized, session_id:%lu, graph_id:%u.",
  209. session_id, graph_id);
  210. return GE_SESSION_MANAGER_NOT_INIT;
  211. }
  212. SessionPtr innerSession = nullptr;
  213. {
  214. std::lock_guard<std::mutex> lock(mutex_);
  215. std::map<SessionId, SessionPtr>::iterator it = session_manager_map_.find(session_id);
  216. if (it == session_manager_map_.end()) {
  217. return GE_SESSION_NOT_EXIST;
  218. } else {
  219. innerSession = it->second;
  220. }
  221. }
  222. return innerSession->RunGraph(graph_id, inputs, outputs);
  223. }
  224. Status SessionManager::RemoveGraph(SessionId session_id, uint32_t graph_id) {
  225. if (!init_flag_) {
  226. GELOGE(GE_SESSION_MANAGER_NOT_INIT,
  227. "[Remove][Graph]fail for Session manager is not initialized, session_id:%lu graph_id:%u.",
  228. session_id, graph_id);
  229. REPORT_INNER_ERROR("E19999",
  230. "RemoveGraph fail for Session manager is not initialized, session_id:%lu graph_id:%u.",
  231. session_id, graph_id);
  232. return GE_SESSION_MANAGER_NOT_INIT;
  233. }
  234. SessionPtr innerSession = nullptr;
  235. {
  236. std::lock_guard<std::mutex> lock(mutex_);
  237. std::map<SessionId, SessionPtr>::iterator it = session_manager_map_.find(session_id);
  238. if (it == session_manager_map_.end()) {
  239. return GE_SESSION_NOT_EXIST;
  240. } else {
  241. innerSession = it->second;
  242. }
  243. }
  244. return innerSession->RemoveGraph(graph_id);
  245. }
  246. bool SessionManager::HasSession(SessionId session_id) {
  247. if (!init_flag_) {
  248. GELOGE(GE_SESSION_MANAGER_NOT_INIT,
  249. "[Has][Session]fail for Session manager is not initialized, session_id:%lu.", session_id);
  250. REPORT_INNER_ERROR("E19999",
  251. "HasSession fail for Session manager is not initialized, session_id:%lu.", session_id);
  252. return false;
  253. }
  254. return session_manager_map_.find(session_id) != session_manager_map_.end();
  255. }
  256. Status SessionManager::GetNextSessionId(SessionId &next_session_id) {
  257. if (!init_flag_) {
  258. GELOGE(GE_SESSION_MANAGER_NOT_INIT, "[Get][NextSessionId]fail for Session manager is not initialized.");
  259. REPORT_INNER_ERROR("E19999", "GetNextSessionId fail for Session manager is not initialized.");
  260. return GE_SESSION_MANAGER_NOT_INIT;
  261. }
  262. static SessionId session_id = 0;
  263. next_session_id = session_id++;
  264. return SUCCESS;
  265. }
  266. Status SessionManager::RegisterCallBackFunc(
  267. SessionId session_id, const std::string &key,
  268. const std::function<Status(uint32_t, const std::map<std::string, ge::Tensor> &)> &callback) {
  269. if (!init_flag_) {
  270. GELOGE(GE_SESSION_MANAGER_NOT_INIT,
  271. "[Register][CallBackFunc]fail for Session manager is not initialized, session_id:%lu, input_key:%s.",
  272. session_id, key.c_str());
  273. REPORT_INNER_ERROR("E19999", "RegisterCallBackFunc fail for Session manager is not initialized,"
  274. "session_id:%lu, input_key:%s.", session_id, key.c_str());
  275. return GE_SESSION_MANAGER_NOT_INIT;
  276. }
  277. SessionPtr innerSession = nullptr;
  278. {
  279. std::lock_guard<std::mutex> lock(mutex_);
  280. std::map<SessionId, SessionPtr>::iterator it = session_manager_map_.find(session_id);
  281. if (it == session_manager_map_.end()) {
  282. return GE_SESSION_NOT_EXIST;
  283. } else {
  284. innerSession = it->second;
  285. }
  286. }
  287. return innerSession->RegisterCallBackFunc(key, callback);
  288. }
  289. Status SessionManager::RegisterCallBackFunc(
  290. SessionId session_id, const std::string &key,
  291. const std::function<Status(uint32_t, const std::map<AscendString, ge::Tensor> &)> &callback) {
  292. if (!init_flag_) {
  293. GELOGE(GE_SESSION_MANAGER_NOT_INIT,
  294. "[Register][CallBackFunc]fail for Session manager is not initialized, session_id:%lu, input_key:%s.",
  295. session_id, key.c_str());
  296. REPORT_INNER_ERROR("E19999", "RegisterCallBackFunc fail for Session manager is not initialized,"
  297. "session_id:%lu, input_key:%s.", session_id, key.c_str());
  298. return GE_SESSION_MANAGER_NOT_INIT;
  299. }
  300. SessionPtr innerSession = nullptr;
  301. {
  302. std::lock_guard<std::mutex> lock(mutex_);
  303. std::map<SessionId, SessionPtr>::iterator it = session_manager_map_.find(session_id);
  304. if (it == session_manager_map_.end()) {
  305. return GE_SESSION_NOT_EXIST;
  306. } else {
  307. innerSession = it->second;
  308. }
  309. }
  310. return innerSession->RegisterCallBackFunc(key, callback);
  311. }
  312. Status SessionManager::BuildGraph(SessionId session_id, uint32_t graph_id, const std::vector<InputTensorInfo> &inputs) {
  313. if (!init_flag_) {
  314. GELOGE(GE_SESSION_MANAGER_NOT_INIT, "[Build][Graph]fail for Session manager is not initialized,"
  315. "session_id:%lu, graph_id:%u.", session_id, graph_id);
  316. REPORT_INNER_ERROR("E19999", "BuildGraph fail for Session manager is not initialized,"
  317. "session_id:%lu, graph_id:%u.", session_id, graph_id);
  318. return GE_SESSION_MANAGER_NOT_INIT;
  319. }
  320. SessionPtr innerSession = nullptr;
  321. {
  322. std::lock_guard<std::mutex> lock(mutex_);
  323. std::map<SessionId, SessionPtr>::iterator it = session_manager_map_.find(session_id);
  324. if (it == session_manager_map_.end()) {
  325. return GE_SESSION_NOT_EXIST;
  326. } else {
  327. innerSession = it->second;
  328. }
  329. }
  330. return innerSession->BuildGraph(graph_id, inputs);
  331. }
  332. Status SessionManager::RunGraphAsync(SessionId session_id, uint32_t graph_id,
  333. const std::vector<InputTensorInfo> &inputs, RunAsyncCallback callback) {
  334. if (!init_flag_) {
  335. GELOGE(GE_SESSION_MANAGER_NOT_INIT,
  336. "[AsyncRun][Graph]fail for Session manager is not initialized, session_id:%lu, graph_id:%u.",
  337. session_id, graph_id);
  338. REPORT_INNER_ERROR("E19999",
  339. "RunGraphAsync fail for Session manager is not initialized, session_id:%lu, graph_id:%u.",
  340. session_id, graph_id);
  341. return GE_SESSION_MANAGER_NOT_INIT;
  342. }
  343. SessionPtr innerSession = nullptr;
  344. {
  345. std::lock_guard<std::mutex> lock(mutex_);
  346. std::map<SessionId, SessionPtr>::iterator it = session_manager_map_.find(session_id);
  347. if (it == session_manager_map_.end()) {
  348. return GE_SESSION_NOT_EXIST;
  349. } else {
  350. innerSession = it->second;
  351. }
  352. }
  353. return innerSession->RunGraphAsync(graph_id, inputs, callback);
  354. }
  355. Status SessionManager::GetVariables(SessionId session_id, const std::vector<std::string> &var_names,
  356. std::vector<Tensor> &var_values) {
  357. // step 0: init session manager
  358. if (!init_flag_) {
  359. GELOGE(GE_SESSION_MANAGER_NOT_INIT,
  360. "[Get][Variables]fail for Session manager is not initialized, session_id:%lu", session_id);
  361. REPORT_INNER_ERROR("E19999",
  362. "GetVariables fail for Session manager is not initialized, session_id:%lu", session_id);
  363. return GE_SESSION_MANAGER_NOT_INIT;
  364. }
  365. SessionPtr innerSession = nullptr;
  366. {
  367. std::lock_guard<std::mutex> lock(mutex_);
  368. std::map<SessionId, SessionPtr>::iterator it = session_manager_map_.find(session_id);
  369. if (it == session_manager_map_.end()) {
  370. return GE_SESSION_NOT_EXIST;
  371. } else {
  372. innerSession = it->second;
  373. }
  374. }
  375. // step 1: get all variable
  376. std::map<std::string, GeTensorDesc> all_variables;
  377. Status ret = innerSession->GetAllVariables(all_variables);
  378. if (ret != SUCCESS) {
  379. GELOGE(FAILED, "[Get][AllVariables]failed.");
  380. return FAILED;
  381. }
  382. // srep 2: create check point graph
  383. Graph graph = Graph("checkpoint");
  384. ret = innerSession->GenCheckPointGraph(all_variables, graph);
  385. if (ret != SUCCESS) {
  386. GELOGE(FAILED, "[GenCheck][PointGraph] failed.");
  387. return FAILED;
  388. }
  389. // step 3: run check point graph
  390. uint32_t graph_id = GetCurrentSecondTimestap();
  391. ret = AddGraph(session_id, graph_id, graph);
  392. if (ret != SUCCESS) {
  393. GELOGE(FAILED, "[Add][Graph] failed.");
  394. return FAILED;
  395. }
  396. vector<Tensor> inputs;
  397. vector<Tensor> outputs;
  398. ret = RunGraph(session_id, graph_id, inputs, outputs);
  399. if (ret != SUCCESS) {
  400. GELOGE(FAILED, "[Run][Graph] failed.");
  401. return FAILED;
  402. }
  403. // step 4: save variables
  404. ret = innerSession->SaveVariables(graph, var_names, outputs, var_values);
  405. GELOGD("[SessionManager] outputs size is [%zu], var values size is [%zu].", outputs.size(), var_values.size());
  406. if (ret != SUCCESS) {
  407. GELOGE(FAILED, "[Save][Variables] failed.");
  408. return FAILED;
  409. }
  410. // step 5: remove graph
  411. ret = innerSession->RemoveGraph(graph_id);
  412. if (ret != SUCCESS) {
  413. GELOGE(FAILED, "[Remove][Graph] failed.");
  414. return FAILED;
  415. }
  416. return ret;
  417. }
  418. bool SessionManager::IsGraphNeedRebuild(SessionId session_id, uint32_t graph_id) {
  419. if (!init_flag_) {
  420. GELOGE(GE_SESSION_MANAGER_NOT_INIT,
  421. "[Check][GraphNeedRebuild]fail for Session manager is not initialized, session_id:%lu, graph_id:%u.",
  422. session_id, graph_id);
  423. REPORT_INNER_ERROR("E19999",
  424. "IsGraphNeedRebuild fail for Session manager is not initialized, session_id:%lu, graph_id:%u.",
  425. session_id, graph_id);
  426. return true;
  427. }
  428. SessionPtr innerSession = nullptr;
  429. {
  430. std::lock_guard<std::mutex> lock(mutex_);
  431. auto it = session_manager_map_.find(session_id);
  432. if (it == session_manager_map_.end()) {
  433. GELOGE(GE_SESSION_NOT_EXIST, "[Find][InnerSession] fail for %lu does not exists", session_id);
  434. REPORT_INNER_ERROR("E19999",
  435. "IsGraphNeedRebuild fail for InnerSession is not exists, session_id:%lu, graph_id:%u.",
  436. session_id, graph_id);
  437. return true;
  438. } else {
  439. innerSession = it->second;
  440. }
  441. }
  442. return innerSession->IsGraphNeedRebuild(graph_id);
  443. }
  444. } // namespace ge

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