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

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