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
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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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::RunGraphWithStreamAsync(SessionId session_id,
  225. uint32_t graph_id,
  226. rtStream_t stream,
  227. const std::vector<Tensor> &inputs,
  228. std::vector<Tensor> &outputs) {
  229. if (!init_flag_) {
  230. GELOGE(GE_SESSION_MANAGER_NOT_INIT,
  231. "[RunWithStream][Graph]Session manager is not initialized,"
  232. "session id = %lu, graph id = %u, stream = %p.", session_id, graph_id, stream);
  233. REPORT_INNER_ERROR("E19999",
  234. "RunGraphWithStreamAsync fail for Session manager is not initialized,"
  235. "session id = %lu, graph id = %u, stream = %p.", session_id, graph_id, stream);
  236. return GE_SESSION_MANAGER_NOT_INIT;
  237. }
  238. SessionPtr innerSession = nullptr;
  239. {
  240. std::lock_guard<std::mutex> lock(mutex_);
  241. std::map<SessionId, SessionPtr>::iterator it = session_manager_map_.find(session_id);
  242. if (it == session_manager_map_.end()) {
  243. return GE_SESSION_NOT_EXIST;
  244. } else {
  245. innerSession = it->second;
  246. }
  247. }
  248. return innerSession->RunGraphWithStreamAsync(graph_id, stream, inputs, outputs);
  249. }
  250. Status SessionManager::RemoveGraph(SessionId session_id, uint32_t graph_id) {
  251. if (!init_flag_) {
  252. GELOGE(GE_SESSION_MANAGER_NOT_INIT,
  253. "[Remove][Graph]fail for Session manager is not initialized, session_id:%lu graph_id:%u.",
  254. session_id, graph_id);
  255. REPORT_INNER_ERROR("E19999",
  256. "RemoveGraph fail for Session manager is not initialized, session_id:%lu graph_id:%u.",
  257. session_id, graph_id);
  258. return GE_SESSION_MANAGER_NOT_INIT;
  259. }
  260. SessionPtr innerSession = nullptr;
  261. {
  262. std::lock_guard<std::mutex> lock(mutex_);
  263. std::map<SessionId, SessionPtr>::iterator it = session_manager_map_.find(session_id);
  264. if (it == session_manager_map_.end()) {
  265. return GE_SESSION_NOT_EXIST;
  266. } else {
  267. innerSession = it->second;
  268. }
  269. }
  270. return innerSession->RemoveGraph(graph_id);
  271. }
  272. bool SessionManager::HasSession(SessionId session_id) {
  273. if (!init_flag_) {
  274. GELOGE(GE_SESSION_MANAGER_NOT_INIT,
  275. "[Has][Session]fail for Session manager is not initialized, session_id:%lu.", session_id);
  276. REPORT_INNER_ERROR("E19999",
  277. "HasSession fail for Session manager is not initialized, session_id:%lu.", session_id);
  278. return false;
  279. }
  280. return session_manager_map_.find(session_id) != session_manager_map_.end();
  281. }
  282. Status SessionManager::GetNextSessionId(SessionId &next_session_id) {
  283. if (!init_flag_) {
  284. GELOGE(GE_SESSION_MANAGER_NOT_INIT, "[Get][NextSessionId]fail for Session manager is not initialized.");
  285. REPORT_INNER_ERROR("E19999", "GetNextSessionId fail for Session manager is not initialized.");
  286. return GE_SESSION_MANAGER_NOT_INIT;
  287. }
  288. static SessionId session_id = 0;
  289. next_session_id = session_id++;
  290. return SUCCESS;
  291. }
  292. Status SessionManager::RegisterCallBackFunc(
  293. SessionId session_id, const std::string &key,
  294. const std::function<Status(uint32_t, const std::map<std::string, ge::Tensor> &)> &callback) {
  295. if (!init_flag_) {
  296. GELOGE(GE_SESSION_MANAGER_NOT_INIT,
  297. "[Register][CallBackFunc]fail for Session manager is not initialized, session_id:%lu, input_key:%s.",
  298. session_id, key.c_str());
  299. REPORT_INNER_ERROR("E19999", "RegisterCallBackFunc fail for Session manager is not initialized,"
  300. "session_id:%lu, input_key:%s.", session_id, key.c_str());
  301. return GE_SESSION_MANAGER_NOT_INIT;
  302. }
  303. SessionPtr innerSession = nullptr;
  304. {
  305. std::lock_guard<std::mutex> lock(mutex_);
  306. std::map<SessionId, SessionPtr>::iterator it = session_manager_map_.find(session_id);
  307. if (it == session_manager_map_.end()) {
  308. return GE_SESSION_NOT_EXIST;
  309. } else {
  310. innerSession = it->second;
  311. }
  312. }
  313. return innerSession->RegisterCallBackFunc(key, callback);
  314. }
  315. Status SessionManager::RegisterCallBackFunc(
  316. SessionId session_id, const std::string &key,
  317. const std::function<Status(uint32_t, const std::map<AscendString, ge::Tensor> &)> &callback) {
  318. if (!init_flag_) {
  319. GELOGE(GE_SESSION_MANAGER_NOT_INIT,
  320. "[Register][CallBackFunc]fail for Session manager is not initialized, session_id:%lu, input_key:%s.",
  321. session_id, key.c_str());
  322. REPORT_INNER_ERROR("E19999", "RegisterCallBackFunc fail for Session manager is not initialized,"
  323. "session_id:%lu, input_key:%s.", session_id, key.c_str());
  324. return GE_SESSION_MANAGER_NOT_INIT;
  325. }
  326. SessionPtr innerSession = nullptr;
  327. {
  328. std::lock_guard<std::mutex> lock(mutex_);
  329. std::map<SessionId, SessionPtr>::iterator it = session_manager_map_.find(session_id);
  330. if (it == session_manager_map_.end()) {
  331. return GE_SESSION_NOT_EXIST;
  332. } else {
  333. innerSession = it->second;
  334. }
  335. }
  336. return innerSession->RegisterCallBackFunc(key, callback);
  337. }
  338. Status SessionManager::BuildGraph(SessionId session_id, uint32_t graph_id, const std::vector<InputTensorInfo> &inputs) {
  339. if (!init_flag_) {
  340. GELOGE(GE_SESSION_MANAGER_NOT_INIT, "[Build][Graph]fail for Session manager is not initialized,"
  341. "session_id:%lu, graph_id:%u.", session_id, graph_id);
  342. REPORT_INNER_ERROR("E19999", "BuildGraph fail for Session manager is not initialized,"
  343. "session_id:%lu, graph_id:%u.", session_id, graph_id);
  344. return GE_SESSION_MANAGER_NOT_INIT;
  345. }
  346. SessionPtr innerSession = nullptr;
  347. {
  348. std::lock_guard<std::mutex> lock(mutex_);
  349. std::map<SessionId, SessionPtr>::iterator it = session_manager_map_.find(session_id);
  350. if (it == session_manager_map_.end()) {
  351. return GE_SESSION_NOT_EXIST;
  352. } else {
  353. innerSession = it->second;
  354. }
  355. }
  356. return innerSession->BuildGraph(graph_id, inputs);
  357. }
  358. Status SessionManager::BuildGraph(SessionId session_id, uint32_t graph_id, const std::vector<ge::Tensor> &inputs) {
  359. if (!init_flag_) {
  360. GELOGE(GE_SESSION_MANAGER_NOT_INIT, "[Build][Graph]fail for Session manager is not initialized,"
  361. "session_id:%lu, graph_id:%u.", session_id, graph_id);
  362. REPORT_INNER_ERROR("E19999", "BuildGraph fail for Session manager is not initialized,"
  363. "session_id:%lu, graph_id:%u.", session_id, graph_id);
  364. return GE_SESSION_MANAGER_NOT_INIT;
  365. }
  366. SessionPtr innerSession = nullptr;
  367. {
  368. std::lock_guard<std::mutex> lock(mutex_);
  369. std::map<SessionId, SessionPtr>::iterator it = session_manager_map_.find(session_id);
  370. if (it == session_manager_map_.end()) {
  371. return GE_SESSION_NOT_EXIST;
  372. } else {
  373. innerSession = it->second;
  374. }
  375. }
  376. return innerSession->BuildGraph(graph_id, inputs);
  377. }
  378. Status SessionManager::RunGraphAsync(SessionId session_id, uint32_t graph_id,
  379. const std::vector<ge::Tensor> &inputs, RunAsyncCallback callback) {
  380. if (!init_flag_) {
  381. GELOGE(GE_SESSION_MANAGER_NOT_INIT,
  382. "[AsyncRun][Graph]fail for Session manager is not initialized, session_id:%lu, graph_id:%u.",
  383. session_id, graph_id);
  384. REPORT_INNER_ERROR("E19999",
  385. "RunGraphAsync fail for Session manager is not initialized, session_id:%lu, graph_id:%u.",
  386. session_id, graph_id);
  387. return GE_SESSION_MANAGER_NOT_INIT;
  388. }
  389. SessionPtr innerSession = nullptr;
  390. {
  391. std::lock_guard<std::mutex> lock(mutex_);
  392. std::map<SessionId, SessionPtr>::iterator it = session_manager_map_.find(session_id);
  393. if (it == session_manager_map_.end()) {
  394. return GE_SESSION_NOT_EXIST;
  395. } else {
  396. innerSession = it->second;
  397. }
  398. }
  399. return innerSession->RunGraphAsync(graph_id, inputs, callback);
  400. }
  401. Status SessionManager::GetVariables(SessionId session_id, const std::vector<std::string> &var_names,
  402. std::vector<Tensor> &var_values) {
  403. // step 0: init session manager
  404. if (!init_flag_) {
  405. GELOGE(GE_SESSION_MANAGER_NOT_INIT,
  406. "[Get][Variables]fail for Session manager is not initialized, session_id:%lu", session_id);
  407. REPORT_INNER_ERROR("E19999",
  408. "GetVariables fail for Session manager is not initialized, session_id:%lu", session_id);
  409. return GE_SESSION_MANAGER_NOT_INIT;
  410. }
  411. SessionPtr innerSession = nullptr;
  412. {
  413. std::lock_guard<std::mutex> lock(mutex_);
  414. std::map<SessionId, SessionPtr>::iterator it = session_manager_map_.find(session_id);
  415. if (it == session_manager_map_.end()) {
  416. return GE_SESSION_NOT_EXIST;
  417. } else {
  418. innerSession = it->second;
  419. }
  420. }
  421. // step 1: get all variable
  422. std::map<std::string, GeTensorDesc> all_variables;
  423. Status ret = innerSession->GetAllVariables(all_variables);
  424. if (ret != SUCCESS) {
  425. GELOGE(FAILED, "[Get][AllVariables]failed.");
  426. return FAILED;
  427. }
  428. // srep 2: create check point graph
  429. Graph graph = Graph("checkpoint");
  430. ret = innerSession->GenCheckPointGraph(all_variables, graph);
  431. if (ret != SUCCESS) {
  432. GELOGE(FAILED, "[GenCheck][PointGraph] failed.");
  433. return FAILED;
  434. }
  435. // step 3: run check point graph
  436. uint32_t graph_id = GetCurrentSecondTimestap();
  437. ret = AddGraph(session_id, graph_id, graph);
  438. if (ret != SUCCESS) {
  439. GELOGE(FAILED, "[Add][Graph] failed.");
  440. return FAILED;
  441. }
  442. vector<Tensor> inputs;
  443. vector<Tensor> outputs;
  444. ret = RunGraph(session_id, graph_id, inputs, outputs);
  445. if (ret != SUCCESS) {
  446. GELOGE(FAILED, "[Run][Graph] failed.");
  447. return FAILED;
  448. }
  449. // step 4: save variables
  450. ret = innerSession->SaveVariables(graph, var_names, outputs, var_values);
  451. GELOGD("[SessionManager] outputs size is [%zu], var values size is [%zu].", outputs.size(), var_values.size());
  452. if (ret != SUCCESS) {
  453. GELOGE(FAILED, "[Save][Variables] failed.");
  454. return FAILED;
  455. }
  456. // step 5: remove graph
  457. ret = innerSession->RemoveGraph(graph_id);
  458. if (ret != SUCCESS) {
  459. GELOGE(FAILED, "[Remove][Graph] failed.");
  460. return FAILED;
  461. }
  462. return ret;
  463. }
  464. bool SessionManager::IsGraphNeedRebuild(SessionId session_id, uint32_t graph_id) {
  465. if (!init_flag_) {
  466. GELOGE(GE_SESSION_MANAGER_NOT_INIT,
  467. "[Check][GraphNeedRebuild]fail for Session manager is not initialized, session_id:%lu, graph_id:%u.",
  468. session_id, graph_id);
  469. REPORT_INNER_ERROR("E19999",
  470. "IsGraphNeedRebuild fail for Session manager is not initialized, session_id:%lu, graph_id:%u.",
  471. session_id, graph_id);
  472. return true;
  473. }
  474. SessionPtr innerSession = nullptr;
  475. {
  476. std::lock_guard<std::mutex> lock(mutex_);
  477. auto it = session_manager_map_.find(session_id);
  478. if (it == session_manager_map_.end()) {
  479. GELOGE(GE_SESSION_NOT_EXIST, "[Find][InnerSession] fail for %lu does not exists", session_id);
  480. REPORT_INNER_ERROR("E19999",
  481. "IsGraphNeedRebuild fail for InnerSession is not exists, session_id:%lu, graph_id:%u.",
  482. session_id, graph_id);
  483. return true;
  484. } else {
  485. innerSession = it->second;
  486. }
  487. }
  488. return innerSession->IsGraphNeedRebuild(graph_id);
  489. }
  490. } // namespace ge

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