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

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