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

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