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 15 kB

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

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