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 14 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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::RunGraph(uint32_t graph_id, const std::vector<Tensor> &inputs, std::vector<Tensor> &outputs) {
  146. GELOGI("[InnerSession:%lu] run graph on session, graph_id=%u.", session_id_, graph_id);
  147. if (mutex_.try_lock()) {
  148. std::lock_guard<std::mutex> lock(mutex_, std::adopt_lock);
  149. if (!init_flag_) {
  150. GELOGE(GE_SESS_INIT_FAILED, "[InnerSession:%lu] initialize failed.", session_id_);
  151. return GE_SESS_INIT_FAILED;
  152. }
  153. UpdateThreadContext(graph_id);
  154. vector<GeTensor> geInputs;
  155. for (auto &item : inputs) {
  156. geInputs.push_back(TensorAdapter::AsGeTensor(item));
  157. }
  158. vector<GeTensor> geOutputs;
  159. Status ret = graph_manager_.RunGraph(graph_id, geInputs, geOutputs, session_id_);
  160. domi::GetContext().out_nodes_map.clear();
  161. domi::GetContext().user_out_nodes.clear();
  162. if (ret != SUCCESS) {
  163. GELOGE(ret, "[InnerSession:%lu] run graph failed, graph_id=%u.", session_id_, graph_id);
  164. return ret;
  165. }
  166. outputs.clear();
  167. for (auto &item : geOutputs) {
  168. outputs.push_back(TensorAdapter::AsTensor(item));
  169. }
  170. GELOGI("[InnerSession:%lu] run graph success, graph_id=%u.", session_id_, graph_id);
  171. return SUCCESS;
  172. } else {
  173. GELOGE(GE_SESS_ALREADY_RUNNING, "[InnerSession:%lu] run graph failed, graph_id=%u.", session_id_, graph_id);
  174. return GE_SESS_ALREADY_RUNNING;
  175. }
  176. }
  177. Status InnerSession::RemoveGraph(uint32_t graph_id) {
  178. std::lock_guard<std::mutex> lock(resource_mutex_);
  179. if (!init_flag_) {
  180. GELOGE(GE_SESS_INIT_FAILED, "[InnerSession:%lu] initialize failed.", session_id_);
  181. return GE_SESS_INIT_FAILED;
  182. }
  183. UpdateThreadContext(graph_id);
  184. Status ret = graph_manager_.RemoveGraph(graph_id);
  185. if (ret != SUCCESS) {
  186. GELOGE(ret, "[InnerSession:%lu] remove graph failed, graph_id=%u.", session_id_, graph_id);
  187. return ret;
  188. }
  189. GELOGI("[InnerSession:%lu] remove graph success, graph_id=%u.", session_id_, graph_id);
  190. return SUCCESS;
  191. }
  192. Status InnerSession::RegisterCallBackFunc(
  193. const std::string &key,
  194. const std::function<Status(uint32_t, const std::map<std::string, ge::Tensor> &)> &callback) {
  195. std::lock_guard<std::mutex> lock(resource_mutex_);
  196. if (!init_flag_) {
  197. GELOGE(GE_SESS_INIT_FAILED, "[InnerSession:%lu] initialize failed.", session_id_);
  198. return GE_SESS_INIT_FAILED;
  199. }
  200. UpdateThreadContext(std::map<std::string, std::string>{});
  201. Status ret = graph_manager_.RegisterCallBackFunc(key, callback);
  202. if (ret != SUCCESS) {
  203. GELOGE(ret, "[InnerSession:%lu] register %s callback function failed.", session_id_, key.c_str());
  204. return ret;
  205. }
  206. GELOGI("[InnerSession:%lu] register %s callback function success.", session_id_, key.c_str());
  207. return SUCCESS;
  208. }
  209. Status InnerSession::RegisterCallBackFunc(
  210. const std::string &key,
  211. const std::function<Status(uint32_t, const std::map<ge::AscendString, ge::Tensor> &)> &callback) {
  212. std::lock_guard<std::mutex> lock(resource_mutex_);
  213. if (!init_flag_) {
  214. GELOGE(GE_SESS_INIT_FAILED, "[InnerSession:%lu] initialize failed.", session_id_);
  215. return GE_SESS_INIT_FAILED;
  216. }
  217. UpdateThreadContext(std::map<std::string, std::string>{});
  218. Status ret = graph_manager_.RegisterCallBackFunc(key, callback);
  219. if (ret != SUCCESS) {
  220. GELOGE(ret, "[InnerSession:%lu] register %s callback function failed.", session_id_, key.c_str());
  221. return ret;
  222. }
  223. GELOGI("[InnerSession:%lu] register %s callback function success.", session_id_, key.c_str());
  224. return SUCCESS;
  225. }
  226. Status InnerSession::BuildGraph(uint32_t graph_id, const std::vector<InputTensorInfo> &inputs) {
  227. UpdateThreadContext(graph_id);
  228. GELOGI("[InnerSession:%lu] build graph on session, graph_id=%u.", session_id_, graph_id);
  229. std::vector<ge::GeTensor> ge_inputs;
  230. for (auto const &input : inputs) {
  231. std::vector<int64_t> input_dims;
  232. std::transform(input.dims.begin(), input.dims.end(), std::back_inserter(input_dims),
  233. [](int64_t x) -> int64_t { return x; });
  234. GeShape input_shape(input_dims);
  235. GeTensorDesc input_tensor_desc;
  236. input_tensor_desc.SetShape(input_shape);
  237. input_tensor_desc.SetDataType(static_cast<ge::DataType>(input.data_type));
  238. ge_inputs.emplace_back(input_tensor_desc);
  239. }
  240. GeRootModelPtr ge_root_model = nullptr;
  241. Status ret = graph_manager_.BuildGraph(graph_id, ge_inputs, ge_root_model, session_id_, true);
  242. if (ret != SUCCESS) {
  243. GELOGE(ret, "[InnerSession:%lu] build graph failed, graph_id=%u.", session_id_, graph_id);
  244. return ret;
  245. }
  246. GELOGI("[InnerSession:%lu] build graph success, graph_id=%u.", session_id_, graph_id);
  247. return ret;
  248. }
  249. Status InnerSession::RunGraphAsync(uint32_t graph_id, const std::vector<InputTensorInfo> &inputs,
  250. RunAsyncCallback callback) {
  251. UpdateThreadContext(graph_id);
  252. GELOGI("[InnerSession:%lu] run graph on session, graph_id=%u.", session_id_, graph_id);
  253. Status ret = graph_manager_.RunGraphAsync(graph_id, inputs, session_id_, callback);
  254. if (ret != SUCCESS) {
  255. GELOGE(ret, "[InnerSession:%lu] run graph failed, graph_id=%u.", session_id_, graph_id);
  256. return ret;
  257. }
  258. GELOGI("[InnerSession:%lu] run graph success, graph_id=%u.", session_id_, graph_id);
  259. return ret;
  260. }
  261. const GraphManager &InnerSession::getGraphManagerObj() const { return graph_manager_; }
  262. void InnerSession::UpdateThreadContext(const std::map<std::string, std::string> &options) {
  263. GetThreadLocalContext().SetGlobalOption(GetMutableGlobalOptions());
  264. GetThreadLocalContext().SetSessionOption(options_);
  265. GetThreadLocalContext().SetGraphOption(options);
  266. GetContext().SetSessionId(session_id_);
  267. SetRtSocVersion();
  268. }
  269. void InnerSession::UpdateThreadContext(uint32_t graph_id) {
  270. auto options = graph_manager_.GetGraphOptions(graph_id);
  271. if (options == nullptr) {
  272. GELOGW("graph level options is null.");
  273. UpdateThreadContext(std::map<std::string, std::string>{});
  274. } else {
  275. UpdateThreadContext(*options);
  276. }
  277. }
  278. bool InnerSession::IsGraphNeedRebuild(uint32_t graph_id) {
  279. UpdateThreadContext(graph_id);
  280. return graph_manager_.IsGraphNeedRebuild(graph_id);
  281. }
  282. Status InnerSession::GetAllVariables(std::map<std::string, GeTensorDesc> &all_variables) {
  283. return VarManager::Instance(session_id_)->GetAllVariables(all_variables);
  284. }
  285. Status InnerSession::GenCheckPointGraph(const std::map<std::string, GeTensorDesc> &all_variables, Graph &graph) {
  286. return graph_manager_.GenCheckPointGraph(all_variables, graph);
  287. }
  288. Status InnerSession::SaveVariables(const Graph &graph, const std::vector<std::string> &var_names,
  289. const std::vector<Tensor> &outputs, std::vector<Tensor> &var_values) {
  290. return graph_manager_.SaveVariables(graph, var_names, outputs, var_values);
  291. }
  292. Status InnerSession::AddDumpProperties(const DumpProperties &dump_properties) {
  293. if (!is_dump_server_inited_) {
  294. if (dump_properties.IsDumpOpen() || dump_properties.IsOpDebugOpen()) {
  295. GE_IF_BOOL_EXEC(AdxDataDumpServerInit() != kDumpStatus, GELOGE(PARAM_INVALID, "Data dump server init failed");
  296. return PARAM_INVALID)
  297. GELOGI("Init adx data dump server success");
  298. is_dump_server_inited_ = true;
  299. }
  300. }
  301. PropertiesManager::Instance().AddDumpProperties(session_id_, dump_properties);
  302. return SUCCESS;
  303. }
  304. Status InnerSession::RemoveDumpProperties() {
  305. PropertiesManager::Instance().RemoveDumpProperties(session_id_);
  306. if (is_dump_server_inited_ && PropertiesManager::Instance().GetDumpPropertiesMap().empty()) {
  307. GE_IF_BOOL_EXEC(AdxDataDumpServerUnInit() != kDumpStatus, GELOGE(PARAM_INVALID, "Data dump server uninit failed");
  308. return PARAM_INVALID)
  309. GELOGI("UnInit adx data dump server success");
  310. is_dump_server_inited_ = false;
  311. }
  312. return SUCCESS;
  313. }
  314. void InnerSession::SetRtSocVersion() {
  315. const auto &global_options = GetMutableGlobalOptions();
  316. auto it = global_options.find(ge::SOC_VERSION);
  317. if (it != global_options.end()) {
  318. const char *soc_version = it->second.c_str();
  319. rtError_t rt_ret = rtSetSocVersion(soc_version);
  320. if (rt_ret != RT_ERROR_NONE) {
  321. GELOGW("Set soc version %s failed. ret:0x%X", soc_version, rt_ret);
  322. }
  323. GELOGI("Set soc version %s success.", soc_version);
  324. }
  325. }
  326. } // namespace ge

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