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

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