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

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