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 25 kB

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

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