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 16 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
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
5 years ago
4 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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/utils/tensor_adapter.h"
  33. #include "runtime/mem.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, "option %s=%s is invalid", OPTION_EXEC_DISABLE_REUSED_MEMORY, iter->second.c_str());
  46. return FAILED;
  47. }
  48. }
  49. return SUCCESS;
  50. }
  51. }
  52. static std::mutex mutex_; // BuildGraph and RunGraph use
  53. bool InnerSession::is_dump_server_inited_ = false;
  54. InnerSession::InnerSession(uint64_t session_id, const std::map<string, string> &options)
  55. : init_flag_(false), session_id_(session_id), options_(options) {}
  56. Status InnerSession::Initialize() {
  57. if (init_flag_) {
  58. GELOGW("[InnerSession:%lu] session already initialize.", session_id_);
  59. return SUCCESS;
  60. }
  61. // If the global options and the session options are duplicated, the session options is preferred.
  62. auto all_options = options_;
  63. all_options.insert(GetMutableGlobalOptions().begin(), GetMutableGlobalOptions().end());
  64. Status ret = CheckReuseMemoryOption(all_options);
  65. if (ret != SUCCESS) {
  66. GELOGE(ret, "[InnerSession:%lu] check reuse memory option failed.", session_id_);
  67. return ret;
  68. }
  69. UpdateThreadContext(std::map<std::string, std::string>{});
  70. // session device id set here
  71. std::string str_session_device_id;
  72. if (GetContext().GetOption("ge.session_device_id", str_session_device_id) == SUCCESS) {
  73. GELOGI("Option session device id has set, value is %s.", str_session_device_id.c_str());
  74. uint32_t session_device_id = 0;
  75. try {
  76. session_device_id = static_cast<uint32_t>(std::stoi(str_session_device_id.c_str()));
  77. // session device id has priority
  78. GetContext().SetCtxDeviceId(session_device_id);
  79. } catch (std::invalid_argument &) {
  80. GELOGW("session device id %s transform to int failed.", str_session_device_id.c_str());
  81. } catch (std::out_of_range &) {
  82. GELOGW("session device id %s transform to int failed.", str_session_device_id.c_str());
  83. }
  84. }
  85. GE_CHK_RT_RET(rtSetDevice(GetContext().DeviceId()));
  86. DumpProperties dump_properties;
  87. dump_properties.InitByOptions();
  88. GE_CHK_STATUS_RET(AddDumpProperties(dump_properties), "Add dump properties failed");
  89. ret = graph_manager_.Initialize(options_);
  90. if (ret != SUCCESS) {
  91. GELOGE(ret, "[InnerSession:%lu] initialize failed.", session_id_);
  92. GE_CHK_STATUS(RemoveDumpProperties(), "Remove dump properties failed");
  93. return ret;
  94. }
  95. ret = VarManager::Instance(session_id_)->SetMemoryMallocSize(all_options);
  96. if (ret != SUCCESS) {
  97. GELOGE(ret, "failed to set malloc size");
  98. (void)graph_manager_.Finalize();
  99. GE_CHK_STATUS(RemoveDumpProperties(), "Remove dump properties failed");
  100. GE_CHK_RT(rtDeviceReset(static_cast<int32_t>(GetContext().DeviceId())));
  101. return ret;
  102. }
  103. int32_t version = static_cast<int32_t>(SessionVersion::ClOUD_VERSION);
  104. const int DEFAULT_DEVICE_ID = 0;
  105. const int DEFAULT_JOB_ID = 0;
  106. ret = VarManager::Instance(session_id_)->Init(version, session_id_, DEFAULT_DEVICE_ID, DEFAULT_JOB_ID);
  107. if (ret != SUCCESS) {
  108. GELOGE(ret, "failed to init session instance");
  109. GE_CHK_STATUS(RemoveDumpProperties(), "Remove dump properties failed");
  110. }
  111. init_flag_ = true;
  112. return SUCCESS;
  113. }
  114. Status InnerSession::Finalize() {
  115. std::lock_guard<std::mutex> lock(resource_mutex_);
  116. if (!init_flag_) {
  117. GELOGW("[InnerSession:%lu] session does not initialize.", session_id_);
  118. return SUCCESS;
  119. }
  120. UpdateThreadContext(std::map<std::string, std::string>{});
  121. Status ret = graph_manager_.Finalize();
  122. if (ret != SUCCESS) {
  123. // Subsequent code execution is required, so no return is required
  124. GELOGE(ret, "[InnerSession:%lu] finalize failed.", session_id_);
  125. }
  126. ModelManager::GetInstance()->DestroyAicpuSession(session_id_);
  127. init_flag_ = false;
  128. // release var memory
  129. GELOGI("VarManager free var memory.");
  130. (void)VarManager::Instance(session_id_)->FreeVarMemory();
  131. // release analyzer saved info(Session Level)
  132. Analyzer::GetInstance()->DestroySessionJsonObject(session_id_);
  133. GE_CHK_RT(rtDeviceReset(static_cast<int32_t>(GetContext().DeviceId())));
  134. GE_CHK_STATUS_RET(RemoveDumpProperties(), "Remove dump properties failed");
  135. return ret;
  136. }
  137. Status InnerSession::GetVariable(const std::string &name, Tensor &val) {
  138. UpdateThreadContext(std::map<std::string, std::string>{});
  139. return graph_manager_.GetVariable(name, val);
  140. }
  141. Status InnerSession::AddGraph(uint32_t graph_id, const Graph &graph) {
  142. std::map<std::string, std::string> options;
  143. return AddGraph(graph_id, graph, options);
  144. }
  145. Status InnerSession::AddGraph(uint32_t graph_id, const Graph &graph,
  146. const std::map<std::string, std::string> &options) {
  147. std::lock_guard<std::mutex> lock(resource_mutex_);
  148. if (!init_flag_) {
  149. GELOGE(GE_SESS_INIT_FAILED, "[InnerSession:%lu] initialize failed.", session_id_);
  150. return GE_SESS_INIT_FAILED;
  151. }
  152. UpdateThreadContext(options);
  153. Status ret = graph_manager_.AddGraph(graph_id, graph, options, domi::GetContext());
  154. if (ret != SUCCESS) {
  155. GELOGE(ret, "[InnerSession:%lu] add graph %u failed.", session_id_, graph_id);
  156. return ret;
  157. }
  158. GELOGI("[InnerSession:%lu] add graph success, graph_id=%u.", session_id_, graph_id);
  159. return SUCCESS;
  160. }
  161. Status InnerSession::AddGraphWithCopy(uint32_t graph_id, const Graph &graph,
  162. const std::map<std::string, std::string> &options) {
  163. std::lock_guard<std::mutex> lock(resource_mutex_);
  164. if (!init_flag_) {
  165. GELOGE(GE_SESS_INIT_FAILED, "[InnerSession:%lu] initialize failed.", session_id_);
  166. return GE_SESS_INIT_FAILED;
  167. }
  168. UpdateThreadContext(options);
  169. Status ret = graph_manager_.AddGraphWithCopy(graph_id, graph, options, domi::GetContext());
  170. if (ret != SUCCESS) {
  171. GELOGE(ret, "[InnerSession:%lu] add graph %u failed.", session_id_, graph_id);
  172. return ret;
  173. }
  174. GELOGI("[InnerSession:%lu] add graph success, graph_id=%u.", session_id_, graph_id);
  175. return SUCCESS;
  176. }
  177. Status InnerSession::RunGraph(uint32_t graph_id, const std::vector<Tensor> &inputs, std::vector<Tensor> &outputs) {
  178. GELOGI("[InnerSession:%lu] run graph on session, graph_id=%u.", session_id_, graph_id);
  179. if (mutex_.try_lock()) {
  180. std::lock_guard<std::mutex> lock(mutex_, std::adopt_lock);
  181. if (!init_flag_) {
  182. GELOGE(GE_SESS_INIT_FAILED, "[InnerSession:%lu] initialize failed.", session_id_);
  183. return GE_SESS_INIT_FAILED;
  184. }
  185. UpdateThreadContext(graph_id);
  186. vector<GeTensor> geInputs;
  187. for (auto &item : inputs) {
  188. geInputs.push_back(TensorAdapter::AsGeTensor(item));
  189. }
  190. vector<GeTensor> geOutputs;
  191. Status ret = graph_manager_.RunGraph(graph_id, geInputs, geOutputs, session_id_);
  192. domi::GetContext().out_nodes_map.clear();
  193. domi::GetContext().user_out_nodes.clear();
  194. if (ret != SUCCESS) {
  195. GELOGE(ret, "[InnerSession:%lu] run graph failed, graph_id=%u.", session_id_, graph_id);
  196. return ret;
  197. }
  198. outputs.clear();
  199. for (auto &item : geOutputs) {
  200. outputs.push_back(TensorAdapter::AsTensor(item));
  201. }
  202. GELOGI("[InnerSession:%lu] run graph success, graph_id=%u.", session_id_, graph_id);
  203. return SUCCESS;
  204. } else {
  205. GELOGE(GE_SESS_ALREADY_RUNNING, "[InnerSession:%lu] run graph failed, graph_id=%u.", session_id_, graph_id);
  206. return GE_SESS_ALREADY_RUNNING;
  207. }
  208. }
  209. Status InnerSession::RemoveGraph(uint32_t graph_id) {
  210. std::lock_guard<std::mutex> lock(resource_mutex_);
  211. if (!init_flag_) {
  212. GELOGE(GE_SESS_INIT_FAILED, "[InnerSession:%lu] initialize failed.", session_id_);
  213. return GE_SESS_INIT_FAILED;
  214. }
  215. UpdateThreadContext(graph_id);
  216. Status ret = graph_manager_.RemoveGraph(graph_id);
  217. if (ret != SUCCESS) {
  218. GELOGE(ret, "[InnerSession:%lu] remove graph failed, graph_id=%u.", session_id_, graph_id);
  219. return ret;
  220. }
  221. GELOGI("[InnerSession:%lu] remove graph success, graph_id=%u.", session_id_, graph_id);
  222. return SUCCESS;
  223. }
  224. Status InnerSession::RegisterCallBackFunc(
  225. const std::string &key,
  226. const std::function<Status(uint32_t, const std::map<std::string, ge::Tensor> &)> &callback) {
  227. std::lock_guard<std::mutex> lock(resource_mutex_);
  228. if (!init_flag_) {
  229. GELOGE(GE_SESS_INIT_FAILED, "[InnerSession:%lu] initialize failed.", session_id_);
  230. return GE_SESS_INIT_FAILED;
  231. }
  232. UpdateThreadContext(std::map<std::string, std::string>{});
  233. Status ret = graph_manager_.RegisterCallBackFunc(key, callback);
  234. if (ret != SUCCESS) {
  235. GELOGE(ret, "[InnerSession:%lu] register %s callback function failed.", session_id_, key.c_str());
  236. return ret;
  237. }
  238. GELOGI("[InnerSession:%lu] register %s callback function success.", session_id_, key.c_str());
  239. return SUCCESS;
  240. }
  241. Status InnerSession::RegisterCallBackFunc(
  242. const std::string &key,
  243. const std::function<Status(uint32_t, const std::map<AscendString, ge::Tensor> &)> &callback) {
  244. std::lock_guard<std::mutex> lock(resource_mutex_);
  245. if (!init_flag_) {
  246. GELOGE(GE_SESS_INIT_FAILED, "[InnerSession:%lu] initialize failed.", session_id_);
  247. return GE_SESS_INIT_FAILED;
  248. }
  249. UpdateThreadContext(std::map<std::string, std::string>{});
  250. Status ret = graph_manager_.RegisterCallBackFunc(key, callback);
  251. if (ret != SUCCESS) {
  252. GELOGE(ret, "[InnerSession:%lu] register %s callback function failed.", session_id_, key.c_str());
  253. return ret;
  254. }
  255. GELOGI("[InnerSession:%lu] register %s callback function success.", session_id_, key.c_str());
  256. return SUCCESS;
  257. }
  258. Status InnerSession::BuildGraph(uint32_t graph_id, const std::vector<InputTensorInfo> &inputs) {
  259. UpdateThreadContext(graph_id);
  260. GELOGI("[InnerSession:%lu] build graph on session, graph_id=%u.", session_id_, graph_id);
  261. std::vector<ge::GeTensor> ge_inputs;
  262. for (auto const &input : inputs) {
  263. std::vector<int64_t> input_dims;
  264. std::transform(input.dims.begin(), input.dims.end(), std::back_inserter(input_dims),
  265. [](int64_t x) -> int64_t { return x; });
  266. GeShape input_shape(input_dims);
  267. GeTensorDesc input_tensor_desc;
  268. input_tensor_desc.SetShape(input_shape);
  269. input_tensor_desc.SetDataType(static_cast<ge::DataType>(input.data_type));
  270. ge_inputs.emplace_back(input_tensor_desc);
  271. }
  272. GeRootModelPtr ge_root_model = nullptr;
  273. Status ret = graph_manager_.BuildGraph(graph_id, ge_inputs, ge_root_model, session_id_, true);
  274. if (ret != SUCCESS) {
  275. GELOGE(ret, "[InnerSession:%lu] build graph failed, graph_id=%u.", session_id_, graph_id);
  276. return ret;
  277. }
  278. GELOGI("[InnerSession:%lu] build graph success, graph_id=%u.", session_id_, graph_id);
  279. return ret;
  280. }
  281. Status InnerSession::RunGraphAsync(uint32_t graph_id, const std::vector<InputTensorInfo> &inputs,
  282. RunAsyncCallback callback) {
  283. UpdateThreadContext(graph_id);
  284. GELOGI("[InnerSession:%lu] run graph on session, graph_id=%u.", session_id_, graph_id);
  285. Status ret = graph_manager_.RunGraphAsync(graph_id, inputs, session_id_, callback);
  286. if (ret != SUCCESS) {
  287. GELOGE(ret, "[InnerSession:%lu] run graph failed, graph_id=%u.", session_id_, graph_id);
  288. return ret;
  289. }
  290. GELOGI("[InnerSession:%lu] run graph success, graph_id=%u.", session_id_, graph_id);
  291. return ret;
  292. }
  293. const GraphManager &InnerSession::getGraphManagerObj() const { return graph_manager_; }
  294. void InnerSession::UpdateThreadContext(const std::map<std::string, std::string> &options) {
  295. GetThreadLocalContext().SetGlobalOption(GetMutableGlobalOptions());
  296. GetThreadLocalContext().SetSessionOption(options_);
  297. GetThreadLocalContext().SetGraphOption(options);
  298. GetContext().SetSessionId(session_id_);
  299. SetRtSocVersion();
  300. }
  301. void InnerSession::UpdateThreadContext(uint32_t graph_id) {
  302. auto options = graph_manager_.GetGraphOptions(graph_id);
  303. if (options == nullptr) {
  304. GELOGW("graph level options is null.");
  305. UpdateThreadContext(std::map<std::string, std::string>{});
  306. } else {
  307. UpdateThreadContext(*options);
  308. }
  309. }
  310. bool InnerSession::IsGraphNeedRebuild(uint32_t graph_id) {
  311. UpdateThreadContext(graph_id);
  312. return graph_manager_.IsGraphNeedRebuild(graph_id);
  313. }
  314. Status InnerSession::GetAllVariables(std::map<std::string, GeTensorDesc> &all_variables) {
  315. return VarManager::Instance(session_id_)->GetAllVariables(all_variables);
  316. }
  317. Status InnerSession::GenCheckPointGraph(const std::map<std::string, GeTensorDesc> &all_variables, Graph &graph) {
  318. return graph_manager_.GenCheckPointGraph(all_variables, graph);
  319. }
  320. Status InnerSession::SaveVariables(const Graph &graph, const std::vector<std::string> &var_names,
  321. const std::vector<Tensor> &outputs, std::vector<Tensor> &var_values) {
  322. return graph_manager_.SaveVariables(graph, var_names, outputs, var_values);
  323. }
  324. Status InnerSession::AddDumpProperties(const DumpProperties &dump_properties) {
  325. if (!is_dump_server_inited_) {
  326. if (dump_properties.IsDumpOpen() || dump_properties.IsOpDebugOpen()) {
  327. GE_IF_BOOL_EXEC(AdxDataDumpServerInit() != kDumpStatus, GELOGE(PARAM_INVALID, "Data dump server init failed");
  328. return PARAM_INVALID)
  329. GELOGI("Init adx data dump server success");
  330. is_dump_server_inited_ = true;
  331. }
  332. }
  333. DumpManager::GetInstance().AddDumpProperties(session_id_, dump_properties);
  334. return SUCCESS;
  335. }
  336. Status InnerSession::RemoveDumpProperties() {
  337. DumpManager::GetInstance().RemoveDumpProperties(session_id_);
  338. if (is_dump_server_inited_ && DumpManager::GetInstance().GetDumpPropertiesMap().empty()) {
  339. GE_IF_BOOL_EXEC(AdxDataDumpServerUnInit() != kDumpStatus, GELOGE(PARAM_INVALID, "Data dump server uninit failed");
  340. return PARAM_INVALID)
  341. GELOGI("UnInit adx data dump server success");
  342. is_dump_server_inited_ = false;
  343. }
  344. return SUCCESS;
  345. }
  346. void InnerSession::SetRtSocVersion() {
  347. const auto &global_options = GetMutableGlobalOptions();
  348. auto it = global_options.find(ge::SOC_VERSION);
  349. if (it != global_options.end()) {
  350. const char *soc_version = it->second.c_str();
  351. rtError_t rt_ret = rtSetSocVersion(soc_version);
  352. if (rt_ret != RT_ERROR_NONE) {
  353. GELOGW("Set soc version %s failed. ret:0x%X", soc_version, rt_ret);
  354. }
  355. GELOGI("Set soc version %s success.", soc_version);
  356. }
  357. }
  358. } // namespace ge

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