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

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