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.

inner_session.cc 23 kB

5 years ago
5 years ago
4 years ago
5 years ago
4 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
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
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
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
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 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
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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/inner_session.h"
  17. #include <map>
  18. #include <memory>
  19. #include <vector>
  20. #include "analyzer/analyzer.h"
  21. #include "adx_datadump_server.h"
  22. #include "common/dump/dump_properties.h"
  23. #include "common/dump/dump_manager.h"
  24. #include "common/util.h"
  25. #include "framework/common/debug/ge_log.h"
  26. #include "graph/ge_context.h"
  27. #include "graph/ge_global_options.h"
  28. #include "graph/ge_local_context.h"
  29. #include "graph/common/local_context.h"
  30. #include "graph/load/model_manager/model_manager.h"
  31. #include "graph/manager/graph_var_manager.h"
  32. #include "graph/manager/graph_mem_manager.h"
  33. #include "graph/utils/tensor_adapter.h"
  34. #include "runtime/mem.h"
  35. namespace ge {
  36. namespace {
  37. const int32_t kDumpStatus = 0;
  38. Status CheckReuseMemoryOption(const std::map<string, string> &options) {
  39. auto iter = options.find(OPTION_EXEC_DISABLE_REUSED_MEMORY);
  40. if (iter != options.end()) {
  41. if (iter->second == "0") {
  42. GELOGD("%s=0, reuse memory is open", OPTION_EXEC_DISABLE_REUSED_MEMORY);
  43. } else if (iter->second == "1") {
  44. GELOGD("%s=1, reuse memory is close", OPTION_EXEC_DISABLE_REUSED_MEMORY);
  45. } else {
  46. GELOGE(PARAM_INVALID, "[CheckReuse][MemoryOption]option %s=%s is invalid",
  47. OPTION_EXEC_DISABLE_REUSED_MEMORY, iter->second.c_str());
  48. REPORT_INNER_ERROR("E19999", "CheckReuseMemoryOption failed because option %s=%s is invalid.",
  49. OPTION_EXEC_DISABLE_REUSED_MEMORY, iter->second.c_str());
  50. return FAILED;
  51. }
  52. }
  53. return SUCCESS;
  54. }
  55. }
  56. static std::mutex mutex_; // BuildGraph and RunGraph use
  57. bool InnerSession::is_dump_server_inited_ = false;
  58. InnerSession::InnerSession(uint64_t session_id, const std::map<string, string> &options)
  59. : init_flag_(false), session_id_(session_id), options_(options) {}
  60. Status InnerSession::Initialize() {
  61. if (init_flag_) {
  62. GELOGW("[InnerSession:%lu] session already initialize.", session_id_);
  63. return SUCCESS;
  64. }
  65. // If the global options and the session options are duplicated, the session options is preferred.
  66. auto all_options = options_;
  67. all_options.insert(GetMutableGlobalOptions().begin(), GetMutableGlobalOptions().end());
  68. Status ret = CheckReuseMemoryOption(all_options);
  69. if (ret != SUCCESS) {
  70. GELOGE(ret, "[CheckReuse][MemoryOption] failed, [InnerSession:%lu].", session_id_);
  71. REPORT_CALL_ERROR("E19999", "CheckReuseMemoryOption failed, InnerSession=%lu.", session_id_);
  72. return ret;
  73. }
  74. UpdateThreadContext(std::map<std::string, std::string>{});
  75. // session device id set here
  76. std::string str_session_device_id;
  77. if (GetContext().GetOption("ge.session_device_id", str_session_device_id) == SUCCESS) {
  78. GELOGI("Option session device id has set, value is %s.", str_session_device_id.c_str());
  79. uint32_t session_device_id = 0;
  80. try {
  81. session_device_id = static_cast<uint32_t>(std::stoi(str_session_device_id.c_str()));
  82. // session device id has priority
  83. GetContext().SetCtxDeviceId(session_device_id);
  84. } catch (std::invalid_argument &) {
  85. GELOGW("session device id %s transform to int failed.", str_session_device_id.c_str());
  86. } catch (std::out_of_range &) {
  87. GELOGW("session device id %s transform to int failed.", str_session_device_id.c_str());
  88. }
  89. }
  90. GE_CHK_RT_RET(rtSetDevice(GetContext().DeviceId()));
  91. DumpProperties dump_properties;
  92. dump_properties.InitByOptions();
  93. GE_CHK_STATUS_RET(AddDumpProperties(dump_properties), "[Add][DumpProperties] failed.");
  94. ret = graph_manager_.Initialize(options_);
  95. if (ret != SUCCESS) {
  96. GELOGE(ret, "[Init][GraphManager] failed, InnerSession:%lu.", session_id_);
  97. REPORT_CALL_ERROR("E19999", "GraphManager initialize failed, InnerSession:%lu.", session_id_);
  98. GE_CHK_STATUS(RemoveDumpProperties(), "[Remove][DumpProperties] failed.");
  99. return ret;
  100. }
  101. ret = VarManager::Instance(session_id_)->SetMemoryMallocSize(all_options);
  102. if (ret != SUCCESS) {
  103. GELOGE(ret, "[Set][MemoryMallocSize] failed.");
  104. REPORT_CALL_ERROR("E19999", "VarManager SetMemoryMallocSize failed, InnerSession:%lu.", session_id_);
  105. (void)graph_manager_.Finalize();
  106. GE_CHK_STATUS(RemoveDumpProperties(), "[Remove][DumpProperties] failed.");
  107. GE_CHK_RT(rtDeviceReset(static_cast<int32_t>(GetContext().DeviceId())));
  108. return ret;
  109. }
  110. int32_t version = static_cast<int32_t>(SessionVersion::ClOUD_VERSION);
  111. const int DEFAULT_DEVICE_ID = 0;
  112. const int DEFAULT_JOB_ID = 0;
  113. ret = VarManager::Instance(session_id_)->Init(version, session_id_, DEFAULT_DEVICE_ID, DEFAULT_JOB_ID);
  114. if (ret != SUCCESS) {
  115. GELOGE(ret, "[Init][VarManager] failed.");
  116. REPORT_CALL_ERROR("E19999", "VarManager init failed, InnerSession:%lu.", session_id_);
  117. GE_CHK_STATUS(RemoveDumpProperties(), "[Remove][DumpProperties] failed.");
  118. }
  119. init_flag_ = true;
  120. return SUCCESS;
  121. }
  122. Status InnerSession::Finalize() {
  123. std::lock_guard<std::mutex> lock(resource_mutex_);
  124. if (!init_flag_) {
  125. GELOGW("[InnerSession:%lu] session does not initialize.", session_id_);
  126. return SUCCESS;
  127. }
  128. UpdateThreadContext(std::map<std::string, std::string>{});
  129. Status ret = graph_manager_.Finalize();
  130. if (ret != SUCCESS) {
  131. // Subsequent code execution is required, so no return is required
  132. GELOGE(ret, "[Finalize][GraphManager] failed, InnerSession:%lu.", session_id_);
  133. REPORT_CALL_ERROR("E19999", "GraphManager Finalize failed, InnerSession:%lu.", session_id_);
  134. }
  135. ModelManager::GetInstance()->DestroyAicpuSession(session_id_);
  136. init_flag_ = false;
  137. // release var memory
  138. GELOGI("VarManager free var memory.");
  139. (void)VarManager::Instance(session_id_)->FreeVarMemory();
  140. for (auto memory_type : MemManager::Instance().GetAllMemoryType()) {
  141. (void)MemManager::Instance().SessionScopeMemInstance(memory_type).Free(session_id_);
  142. }
  143. // release analyzer saved info(Session Level)
  144. Analyzer::GetInstance()->DestroySessionJsonObject(session_id_);
  145. GE_CHK_RT(rtDeviceReset(static_cast<int32_t>(GetContext().DeviceId())));
  146. GE_CHK_STATUS_RET(RemoveDumpProperties(), "[Remove][DumpProperties] failed.");
  147. return ret;
  148. }
  149. Status InnerSession::GetVariable(const std::string &name, Tensor &val) {
  150. UpdateThreadContext(std::map<std::string, std::string>{});
  151. return graph_manager_.GetVariable(name, val);
  152. }
  153. Status InnerSession::AddGraph(uint32_t graph_id, const Graph &graph) {
  154. std::map<std::string, std::string> options;
  155. return AddGraph(graph_id, graph, options);
  156. }
  157. Status InnerSession::AddGraph(uint32_t graph_id, const Graph &graph,
  158. const std::map<std::string, std::string> &options) {
  159. std::lock_guard<std::mutex> lock(resource_mutex_);
  160. if (!init_flag_) {
  161. GELOGE(GE_SESS_INIT_FAILED, "[Add][Graph] failed because GraphManager not init, InnerSession:%lu, graph_id:%u.",
  162. session_id_, graph_id);
  163. REPORT_INNER_ERROR("E19999", "AddGraph failed because GraphManager not init, InnerSession:%lu, graph_id:%u.",
  164. session_id_, graph_id);
  165. return GE_SESS_INIT_FAILED;
  166. }
  167. UpdateThreadContext(options);
  168. Status ret = graph_manager_.AddGraph(graph_id, graph, options, domi::GetContext());
  169. if (ret != SUCCESS) {
  170. GELOGE(ret, "[Add][Graph] failed, InnerSession:%lu graphid: %u.", session_id_, graph_id);
  171. REPORT_CALL_ERROR("E19999", "GraphManager AddGraph failed, InnerSession:%lu graphid: %u.", session_id_, graph_id);
  172. return ret;
  173. }
  174. GELOGI("[InnerSession:%lu] add graph success, graph_id=%u.", session_id_, graph_id);
  175. return SUCCESS;
  176. }
  177. Status InnerSession::AddGraphWithCopy(uint32_t graph_id, const Graph &graph,
  178. const std::map<std::string, std::string> &options) {
  179. std::lock_guard<std::mutex> lock(resource_mutex_);
  180. if (!init_flag_) {
  181. GELOGE(GE_SESS_INIT_FAILED, "[Add][Graph] failed because GraphManager not init, InnerSession:%lu, graph_id:%u.",
  182. session_id_, graph_id);
  183. REPORT_INNER_ERROR("E19999",
  184. "AddGraphWithCopy failed because GraphManager not init, InnerSession:%lu, graph_id:%u.",
  185. session_id_, graph_id);
  186. return GE_SESS_INIT_FAILED;
  187. }
  188. UpdateThreadContext(options);
  189. Status ret = graph_manager_.AddGraphWithCopy(graph_id, graph, options, domi::GetContext());
  190. if (ret != SUCCESS) {
  191. GELOGE(ret, "[Add][Graph] failed, InnerSession:%lu graphid: %u.", session_id_, graph_id);
  192. REPORT_CALL_ERROR("E19999",
  193. "GraphManager AddGraphWithCopy failed, InnerSession:%lu graphid: %u.", session_id_, graph_id);
  194. return ret;
  195. }
  196. GELOGI("[InnerSession:%lu] add graph success, graph_id=%u.", session_id_, graph_id);
  197. return SUCCESS;
  198. }
  199. Status InnerSession::RunGraph(uint32_t graph_id, const std::vector<Tensor> &inputs, std::vector<Tensor> &outputs) {
  200. GELOGI("[InnerSession:%lu] run graph on session, graph_id=%u.", session_id_, graph_id);
  201. if (mutex_.try_lock()) {
  202. std::lock_guard<std::mutex> lock(mutex_, std::adopt_lock);
  203. if (!init_flag_) {
  204. GELOGE(GE_SESS_INIT_FAILED, "[Run][Graph]failed because GraphManager not Init, InnerSession:%lu, graph_id:%u.",
  205. session_id_, graph_id);
  206. REPORT_INNER_ERROR("E19999", "RunGraph failed because GraphManager not Init, InnerSession:%lu, graph_id:%u.",
  207. session_id_, graph_id);
  208. return GE_SESS_INIT_FAILED;
  209. }
  210. UpdateThreadContext(graph_id);
  211. vector<GeTensor> geInputs;
  212. for (auto &item : inputs) {
  213. geInputs.push_back(TensorAdapter::AsGeTensor(item));
  214. }
  215. vector<GeTensor> geOutputs;
  216. Status ret = graph_manager_.RunGraph(graph_id, geInputs, geOutputs, session_id_);
  217. domi::GetContext().out_nodes_map.clear();
  218. domi::GetContext().user_out_nodes.clear();
  219. if (ret != SUCCESS) {
  220. GELOGE(ret, "[Run][Graph]failed, InnerSession:%lu graph_id=%u.", session_id_, graph_id);
  221. REPORT_CALL_ERROR("E19999",
  222. "GraphManager RunGraph failed, InnerSession:%lu graph_id=%u.", session_id_, graph_id);
  223. return ret;
  224. }
  225. outputs.clear();
  226. for (auto &item : geOutputs) {
  227. outputs.push_back(TensorAdapter::AsTensor(item));
  228. }
  229. GELOGI("[InnerSession:%lu] run graph success, graph_id=%u.", session_id_, graph_id);
  230. return SUCCESS;
  231. } else {
  232. GELOGE(GE_SESS_ALREADY_RUNNING, "[Run][Graph]failed, InnerSession:%lu, graph_id=%u.", session_id_, graph_id);
  233. REPORT_INNER_ERROR("E19999",
  234. "RunGraph failed because mutex try_lock false, InnerSession:%lu, graph_id=%u.",
  235. session_id_, graph_id);
  236. return GE_SESS_ALREADY_RUNNING;
  237. }
  238. }
  239. Status InnerSession::RunGraphWithStreamAsync(uint32_t graph_id, rtStream_t stream,
  240. const std::vector<Tensor> &inputs, std::vector<Tensor> &outputs) {
  241. GELOGI("Run graph with stream, session id = %lu, graph id = %u, stream = %p.",
  242. session_id_, graph_id, stream);
  243. if (mutex_.try_lock()) {
  244. std::lock_guard<std::mutex> lock(mutex_, std::adopt_lock);
  245. if (!init_flag_) {
  246. GELOGE(GE_SESS_INIT_FAILED, "[Run][GraphWithStream]failed because GraphManager not Init,"
  247. "session id = %lu, graph id = %u, stream = %p.", session_id_, graph_id, stream);
  248. REPORT_INNER_ERROR("E19999", "RunGraphWithStreamAsync failed because GraphManager not Init,"
  249. "session id = %lu, graph id = %u, stream = %p.", session_id_, graph_id, stream);
  250. return GE_SESS_INIT_FAILED;
  251. }
  252. UpdateThreadContext(graph_id);
  253. vector<GeTensor> ge_inputs;
  254. for (auto &item : inputs) {
  255. ge_inputs.emplace_back(TensorAdapter::AsGeTensor(item));
  256. }
  257. vector<GeTensor> ge_outputs;
  258. for (auto &item : outputs) {
  259. ge_outputs.emplace_back(TensorAdapter::AsGeTensor(item));
  260. }
  261. Status ret = graph_manager_.RunGraphWithStreamAsync(graph_id, stream, session_id_, ge_inputs, ge_outputs);
  262. domi::GetContext().out_nodes_map.clear();
  263. domi::GetContext().user_out_nodes.clear();
  264. if (ret != SUCCESS) {
  265. GELOGE(ret, "[Run][GraphWithStreamAsync]failed,"
  266. "session id = %lu, graph id = %u, stream = %p.", session_id_, graph_id, stream);
  267. REPORT_CALL_ERROR("E19999", "GraphManager RunGrapWithStreamhAsync failed,"
  268. "session id = %lu, graph id = %u, stream = %p.", session_id_, graph_id, stream);
  269. return ret;
  270. }
  271. GELOGI("Run graph with stream success, session id = %lu, graph id = %u, stream = %p.",
  272. session_id_, graph_id, stream);
  273. return SUCCESS;
  274. } else {
  275. GELOGE(GE_SESS_ALREADY_RUNNING, "[Run][GraphWithStreamAsync]failed because mutex try_lock false,"
  276. "session id = %lu, graph id = %u, stream = %p.", session_id_, graph_id, stream);
  277. REPORT_INNER_ERROR("E19999", "[Run][GraphWithStreamAsync]failed failed because mutex try_lock false,"
  278. "session id = %lu, graph id = %u, stream = %p.", session_id_, graph_id, stream);
  279. return GE_SESS_ALREADY_RUNNING;
  280. }
  281. }
  282. Status InnerSession::RemoveGraph(uint32_t graph_id) {
  283. std::lock_guard<std::mutex> lock(resource_mutex_);
  284. if (!init_flag_) {
  285. GELOGE(GE_SESS_INIT_FAILED,
  286. "[Remove][Graph] failed because GraphManager not init, InnerSession:%lu, graph_id=%u.",
  287. session_id_, graph_id);
  288. REPORT_INNER_ERROR("E19999",
  289. "RemoveGraph failed, because GraphManager not init, InnerSession:%lu, graph_id=%u.",
  290. session_id_, graph_id);
  291. return GE_SESS_INIT_FAILED;
  292. }
  293. UpdateThreadContext(graph_id);
  294. Status ret = graph_manager_.RemoveGraph(graph_id);
  295. if (ret != SUCCESS) {
  296. GELOGE(ret, "[Remove][Graph] failed, InnerSession:%lu, graph_id=%u.", session_id_, graph_id);
  297. REPORT_CALL_ERROR("E19999",
  298. "GraphManager RemoveGraph failed, InnerSession:%lu, graph_id=%u.", session_id_, graph_id);
  299. return ret;
  300. }
  301. GELOGI("[InnerSession:%lu] remove graph success, graph_id=%u.", session_id_, graph_id);
  302. return SUCCESS;
  303. }
  304. Status InnerSession::RegisterCallBackFunc(
  305. const std::string &key,
  306. const std::function<Status(uint32_t, const std::map<std::string, ge::Tensor> &)> &callback) {
  307. std::lock_guard<std::mutex> lock(resource_mutex_);
  308. if (!init_flag_) {
  309. GELOGE(GE_SESS_INIT_FAILED,
  310. "[Register][CallBackFunc] failed because GraphManager not initialize, InnerSession:%lu.", session_id_);
  311. REPORT_INNER_ERROR("E19999",
  312. "RegisterCallBackFunc failed because GraphManager not init, InnerSession:%lu.", session_id_);
  313. return GE_SESS_INIT_FAILED;
  314. }
  315. UpdateThreadContext(std::map<std::string, std::string>{});
  316. Status ret = graph_manager_.RegisterCallBackFunc(key, callback);
  317. if (ret != SUCCESS) {
  318. GELOGE(ret, "[Register][CallBackFunc] failed, InnerSession:%lu register %s.", session_id_, key.c_str());
  319. REPORT_CALL_ERROR("E19999",
  320. "GraphManager RegisterCallBackFunc failed, InnerSession:%lu register %s.",
  321. session_id_, key.c_str());
  322. return ret;
  323. }
  324. GELOGI("[InnerSession:%lu] register %s callback function success.", session_id_, key.c_str());
  325. return SUCCESS;
  326. }
  327. Status InnerSession::RegisterCallBackFunc(
  328. const std::string &key,
  329. const std::function<Status(uint32_t, const std::map<AscendString, ge::Tensor> &)> &callback) {
  330. std::lock_guard<std::mutex> lock(resource_mutex_);
  331. if (!init_flag_) {
  332. GELOGE(GE_SESS_INIT_FAILED,
  333. "[Register][CallBackFunc]failed because GraphManager not initialize, InnerSession:%lu.", session_id_);
  334. REPORT_INNER_ERROR("E19999",
  335. "RegisterCallBackFunc failed because GraphManager not initialize, InnerSession:%lu.",
  336. session_id_);
  337. return GE_SESS_INIT_FAILED;
  338. }
  339. UpdateThreadContext(std::map<std::string, std::string>{});
  340. Status ret = graph_manager_.RegisterCallBackFunc(key, callback);
  341. if (ret != SUCCESS) {
  342. GELOGE(ret, "[Register][CallBackFunc] failed, InnerSession:%lu register %s.", session_id_, key.c_str());
  343. REPORT_CALL_ERROR("E19999",
  344. "GraphManager RegisterCallBackFunc failed, InnerSession:%lu register %s.",
  345. session_id_, key.c_str());
  346. return ret;
  347. }
  348. GELOGI("[InnerSession:%lu] register %s callback function success.", session_id_, key.c_str());
  349. return SUCCESS;
  350. }
  351. Status InnerSession::BuildGraph(uint32_t graph_id, const std::vector<InputTensorInfo> &inputs) {
  352. UpdateThreadContext(graph_id);
  353. GELOGI("[InnerSession:%lu] build graph on session, graph_id=%u.", session_id_, graph_id);
  354. std::vector<ge::GeTensor> ge_inputs;
  355. for (auto const &input : inputs) {
  356. std::vector<int64_t> input_dims;
  357. std::transform(input.dims.begin(), input.dims.end(), std::back_inserter(input_dims),
  358. [](int64_t x) -> int64_t { return x; });
  359. GeShape input_shape(input_dims);
  360. GeTensorDesc input_tensor_desc;
  361. input_tensor_desc.SetShape(input_shape);
  362. input_tensor_desc.SetDataType(static_cast<ge::DataType>(input.data_type));
  363. ge_inputs.emplace_back(input_tensor_desc);
  364. }
  365. GeRootModelPtr ge_root_model = nullptr;
  366. Status ret = graph_manager_.BuildGraph(graph_id, ge_inputs, ge_root_model, session_id_, true);
  367. if (ret != SUCCESS) {
  368. GELOGE(ret, "[Build][Graph] failed, InnerSession:%lu graph_id=%u.", session_id_, graph_id);
  369. REPORT_CALL_ERROR("E19999",
  370. "GraphManager BuildGraph failed, InnerSession:%lu graph_id=%u.", session_id_, graph_id);
  371. return ret;
  372. }
  373. GELOGI("[InnerSession:%lu] build graph success, graph_id=%u.", session_id_, graph_id);
  374. return ret;
  375. }
  376. Status InnerSession::BuildGraph(uint32_t graph_id, const std::vector<ge::Tensor> &inputs) {
  377. UpdateThreadContext(graph_id);
  378. GELOGI("[InnerSession:%lu] build graph on session, graph_id=%u.", session_id_, graph_id);
  379. std::vector<ge::GeTensor> ge_inputs;
  380. for (const auto &input : inputs) {
  381. ge_inputs.emplace_back(TensorAdapter::AsGeTensor(input));
  382. }
  383. GeRootModelPtr ge_root_model = nullptr;
  384. Status ret = graph_manager_.BuildGraph(graph_id, ge_inputs, ge_root_model, session_id_, true);
  385. if (ret != SUCCESS) {
  386. GELOGE(ret, "[Build][Graph] failed, InnerSession:%lu graph_id=%u.", session_id_, graph_id);
  387. REPORT_CALL_ERROR("E19999",
  388. "GraphManager BuildGraph failed, InnerSession:%lu graph_id=%u.", session_id_, graph_id);
  389. return ret;
  390. }
  391. GELOGI("[InnerSession:%lu] build graph success, graph_id=%u.", session_id_, graph_id);
  392. return ret;
  393. }
  394. Status InnerSession::RunGraphAsync(uint32_t graph_id, const std::vector<ge::Tensor> &inputs,
  395. RunAsyncCallback callback) {
  396. UpdateThreadContext(graph_id);
  397. GELOGI("[InnerSession:%lu] run graph on session, graph_id=%u.", session_id_, graph_id);
  398. Status ret = graph_manager_.RunGraphAsync(graph_id, inputs, session_id_, callback);
  399. if (ret != SUCCESS) {
  400. GELOGE(ret, "[Run][GraphAsync]failed, InnerSession:%lu graph_id=%u.", session_id_, graph_id);
  401. REPORT_CALL_ERROR("E19999",
  402. "GraphManager RunGraphAsync failed, InnerSession:%lu graph_id=%u.", session_id_, graph_id);
  403. return ret;
  404. }
  405. GELOGI("[InnerSession:%lu] run graph success, graph_id=%u.", session_id_, graph_id);
  406. return ret;
  407. }
  408. const GraphManager &InnerSession::getGraphManagerObj() const { return graph_manager_; }
  409. void InnerSession::UpdateThreadContext(const std::map<std::string, std::string> &options) {
  410. GetThreadLocalContext().SetGlobalOption(GetMutableGlobalOptions());
  411. GetThreadLocalContext().SetSessionOption(options_);
  412. GetThreadLocalContext().SetGraphOption(options);
  413. GetContext().SetSessionId(session_id_);
  414. SetRtSocVersion();
  415. }
  416. void InnerSession::UpdateThreadContext(uint32_t graph_id) {
  417. auto options = graph_manager_.GetGraphOptions(graph_id);
  418. if (options == nullptr) {
  419. GELOGW("graph level options is null.");
  420. UpdateThreadContext(std::map<std::string, std::string>{});
  421. } else {
  422. UpdateThreadContext(*options);
  423. }
  424. }
  425. bool InnerSession::IsGraphNeedRebuild(uint32_t graph_id) {
  426. UpdateThreadContext(graph_id);
  427. return graph_manager_.IsGraphNeedRebuild(graph_id);
  428. }
  429. Status InnerSession::GetAllVariables(std::map<std::string, GeTensorDesc> &all_variables) {
  430. return VarManager::Instance(session_id_)->GetAllVariables(all_variables);
  431. }
  432. Status InnerSession::GenCheckPointGraph(const std::map<std::string, GeTensorDesc> &all_variables, Graph &graph) {
  433. return graph_manager_.GenCheckPointGraph(all_variables, graph);
  434. }
  435. Status InnerSession::SaveVariables(const Graph &graph, const std::vector<std::string> &var_names,
  436. const std::vector<Tensor> &outputs, std::vector<Tensor> &var_values) {
  437. return graph_manager_.SaveVariables(graph, var_names, outputs, var_values);
  438. }
  439. Status InnerSession::AddDumpProperties(const DumpProperties &dump_properties) {
  440. if (!is_dump_server_inited_) {
  441. if (dump_properties.IsDumpOpen() || dump_properties.IsOpDebugOpen()) {
  442. GE_IF_BOOL_EXEC(AdxDataDumpServerInit() != kDumpStatus,
  443. GELOGE(PARAM_INVALID, "[Init][AdxDataDumpServer] failed, session_id:%lu.", session_id_);
  444. return PARAM_INVALID)
  445. GELOGI("Init adx data dump server success");
  446. is_dump_server_inited_ = true;
  447. }
  448. }
  449. DumpManager::GetInstance().AddDumpProperties(session_id_, dump_properties);
  450. return SUCCESS;
  451. }
  452. Status InnerSession::RemoveDumpProperties() {
  453. DumpManager::GetInstance().RemoveDumpProperties(session_id_);
  454. if (is_dump_server_inited_ && DumpManager::GetInstance().GetDumpPropertiesMap().empty()) {
  455. GE_IF_BOOL_EXEC(AdxDataDumpServerUnInit() != kDumpStatus,
  456. GELOGE(PARAM_INVALID, "[UnInit][AdxDataDumpServer] failed, session_id:%lu.", session_id_);
  457. REPORT_INNER_ERROR("E19999", "RemoveDumpProperties failed because AdxDataDumpServerUnInit failed,"
  458. "session_id:%lu", session_id_);
  459. return PARAM_INVALID)
  460. GELOGI("UnInit adx data dump server success");
  461. is_dump_server_inited_ = false;
  462. }
  463. return SUCCESS;
  464. }
  465. void InnerSession::SetRtSocVersion() {
  466. const auto &global_options = GetMutableGlobalOptions();
  467. auto it = global_options.find(ge::SOC_VERSION);
  468. if (it != global_options.end()) {
  469. const char *soc_version = it->second.c_str();
  470. rtError_t rt_ret = rtSetSocVersion(soc_version);
  471. if (rt_ret != RT_ERROR_NONE) {
  472. GELOGW("Set soc version %s failed. ret:0x%X", soc_version, rt_ret);
  473. }
  474. GELOGI("Set soc version %s success.", soc_version);
  475. }
  476. }
  477. } // namespace ge

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