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

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