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.

graph_loader.cc 12 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
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 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
5 years ago
4 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
4 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
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 "graph/load/graph_loader.h"
  17. #include <string>
  18. #include <vector>
  19. #include "common/helper/model_helper.h"
  20. #include "common/model_parser/model_parser.h"
  21. #include "graph/ge_context.h"
  22. #include "graph/load/model_manager/model_manager.h"
  23. #include "graph/manager/graph_var_manager.h"
  24. #include "omm/csa_interact.h"
  25. namespace ge {
  26. Status GraphLoader::UnloadModel(uint32_t model_id) {
  27. auto model_manager = ModelManager::GetInstance();
  28. GE_CHECK_NOTNULL(model_manager);
  29. GELOGI("UnLoad model begin, model id:%u.", model_id);
  30. Status ret = model_manager->Stop(model_id);
  31. if (ret != SUCCESS) {
  32. GELOGE(ret, "UnloadModel: Stop failed. model id:%u", model_id);
  33. }
  34. ret = model_manager->Unload(model_id);
  35. if (ret != SUCCESS) {
  36. GELOGE(ret, "UnloadModel: Unload failed. model id:%u", model_id);
  37. CsaInteract::GetInstance().WriteErrorCode(ret, ERROR_MODULE_FMK, JOBSUBSTATE_GRAPH_UNLOAD);
  38. return ret;
  39. }
  40. GELOGI("UnLoad model success, model id:%u.", model_id);
  41. return SUCCESS;
  42. }
  43. Status GraphLoader::LoadModelOnline(uint32_t &model_id, const std::shared_ptr<ge::GeRootModel> &ge_root_model_ptr,
  44. const std::shared_ptr<ModelListener> &listener) {
  45. GELOGI("Load model online begin.");
  46. rtError_t rt_ret = rtSetDevice(GetContext().DeviceId());
  47. if (rt_ret != RT_ERROR_NONE) {
  48. REPORT_CALL_ERROR("E19999", "Call rtSetDevice failed, device_id:%u, ret:0x%X",
  49. GetContext().DeviceId(), rt_ret);
  50. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", rt_ret);
  51. CsaInteract::GetInstance().WriteErrorCode(rt_ret, ERROR_MODULE_RUNTIME, JOBSUBSTATE_GRAPH_LOAD);
  52. return RT_FAILED;
  53. }
  54. if (ge_root_model_ptr == nullptr) {
  55. REPORT_INNER_ERROR("E19999", "Check param ge_root_model_ptr nullptr, check invalid");
  56. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[LoadGraph] GE load graph model_ptr is nullptr.");
  57. return GE_GRAPH_PARAM_NULLPTR;
  58. }
  59. auto model_manager = ModelManager::GetInstance();
  60. GE_CHECK_NOTNULL(model_manager);
  61. Status ret = model_manager->LoadModelOnline(model_id, ge_root_model_ptr, listener);
  62. if (ret != SUCCESS) {
  63. GELOGE(ret, "LoadModel: Load failed. ret = %u", ret);
  64. CsaInteract::GetInstance().WriteErrorCode(ret, ERROR_MODULE_FMK, JOBSUBSTATE_GRAPH_LOAD);
  65. rt_ret = rtDeviceReset(GetContext().DeviceId());
  66. if (rt_ret != RT_ERROR_NONE) {
  67. REPORT_CALL_ERROR("E19999", "Call rtDeviceReset failed, device_id:%u, ret:0x%X",
  68. GetContext().DeviceId(), rt_ret);
  69. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", rt_ret);
  70. }
  71. return ret;
  72. }
  73. ret = model_manager->Start(model_id);
  74. if (ret != SUCCESS) {
  75. if (model_manager->Unload(model_id) != SUCCESS) {
  76. GELOGE(ret, "LoadModel: Unload failed while trying to unload after a failed start.");
  77. }
  78. rt_ret = rtDeviceReset(GetContext().DeviceId());
  79. if (rt_ret != RT_ERROR_NONE) {
  80. REPORT_CALL_ERROR("E19999", "Call rtDeviceReset failed, device_id:%u, ret:0x%X",
  81. GetContext().DeviceId(), rt_ret);
  82. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", rt_ret);
  83. }
  84. GELOGE(ret, "LoadModel: Start failed.");
  85. CsaInteract::GetInstance().WriteErrorCode(ret, ERROR_MODULE_FMK, JOBSUBSTATE_GRAPH_EXEC);
  86. return ret;
  87. }
  88. rt_ret = rtDeviceReset(GetContext().DeviceId());
  89. if (rt_ret != RT_ERROR_NONE) {
  90. REPORT_CALL_ERROR("E19999", "Call rtDeviceReset failed, device_id:%u, ret:0x%X",
  91. GetContext().DeviceId(), rt_ret);
  92. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", rt_ret);
  93. return RT_FAILED;
  94. }
  95. GELOGI("Load model online success, model_id:%u.", model_id);
  96. return SUCCESS;
  97. }
  98. Status GraphLoader::GetMaxUsedMemory(uint32_t model_id, uint64_t &max_size) {
  99. auto model_manager = ModelManager::GetInstance();
  100. GE_CHECK_NOTNULL(model_manager);
  101. Status ret = model_manager->GetMaxUsedMemory(model_id, max_size);
  102. if (ret != SUCCESS) {
  103. GELOGE(ret, "GetMaxUsedMemory: GetMaxUsedMemory failed.");
  104. return ret;
  105. }
  106. return SUCCESS;
  107. }
  108. Status GraphLoader::LoadDataFromFile(const std::string &path, const std::string &key_path, int32_t priority,
  109. ModelData &model_data) {
  110. if (!CheckInputPathValid(path)) {
  111. GELOGE(ACL_ERROR_GE_EXEC_MODEL_PATH_INVALID, "model path is invalid: %s", path.c_str());
  112. return ACL_ERROR_GE_EXEC_MODEL_PATH_INVALID;
  113. }
  114. GELOGI("Load model begin, model path is: %s", path.c_str());
  115. if (!key_path.empty() && !CheckInputPathValid(key_path)) {
  116. REPORT_INNER_ERROR("E19999", "Param key_path:%s empty or invalid",
  117. key_path.c_str());
  118. GELOGE(ACL_ERROR_GE_PARAM_INVALID, "decrypt_key path is invalid: %s", key_path.c_str());
  119. return ACL_ERROR_GE_PARAM_INVALID;
  120. }
  121. Status ret = ModelParserBase::LoadFromFile(path.c_str(), key_path.c_str(), priority, model_data);
  122. if (ret != SUCCESS) {
  123. GELOGE(ret, "LoadModelFromFile: Load failed. ret = %u", ret);
  124. if (model_data.model_data != nullptr) {
  125. delete[] static_cast<char *>(model_data.model_data);
  126. model_data.model_data = nullptr;
  127. }
  128. }
  129. return ret;
  130. }
  131. Status GraphLoader::CommandHandle(const Command &command) {
  132. try {
  133. auto model_manager = ModelManager::GetInstance();
  134. GE_CHECK_NOTNULL(model_manager);
  135. Status ret = model_manager->HandleCommand(command);
  136. if (ret != SUCCESS) {
  137. GELOGE(ret, "CommandHandle: Command Handle failed.");
  138. return ret;
  139. }
  140. } catch (std::bad_alloc &) {
  141. REPORT_INNER_ERROR("E19999", "Bad memory allocation occur");
  142. GELOGE(ACL_ERROR_GE_MEMORY_ALLOCATION, "Command handle failed, bad memory allocation occur !");
  143. return ACL_ERROR_GE_MEMORY_ALLOCATION;
  144. } catch (...) {
  145. REPORT_INNER_ERROR("E19999", "Some exceptions occur");
  146. GELOGE(FAILED, "Command handle failed, some exceptions occur !");
  147. return FAILED;
  148. }
  149. return SUCCESS;
  150. }
  151. Status GraphLoader::LoadModelFromData(uint32_t &model_id, const ModelData &model_data, void *dev_ptr,
  152. size_t mem_size, void *weight_ptr, size_t weight_size) {
  153. GELOGI("Load model begin, model_id:%u.", model_id);
  154. // For ACL, Open Device from App.
  155. auto model_manager = ModelManager::GetInstance();
  156. GE_CHECK_NOTNULL(model_manager);
  157. Status ret = model_manager->LoadModelOffline(
  158. model_id, model_data, nullptr, dev_ptr, mem_size, weight_ptr, weight_size);
  159. if (ret != SUCCESS) {
  160. GELOGE(ret, "Load model failed, model_id:%u.", model_id);
  161. return ret;
  162. }
  163. GELOGI("Load model success, model_id:%u.", model_id);
  164. return SUCCESS;
  165. }
  166. ///
  167. /// @ingroup ge
  168. /// @brief Load task list from ModelData with queue.
  169. /// @param [out] model_id: model id allocate from manager.
  170. /// @param [in] model_data: Model data load from offline model.
  171. /// @param [in] input_queue_ids: input queue ids create from user.
  172. /// @param [in] output_queue_ids: input queue ids create from user.
  173. /// @return: 0 for success / others for fail
  174. ///
  175. Status GraphLoader::LoadModelWithQ(uint32_t &model_id, const ModelData &model_data,
  176. const std::vector<uint32_t> &input_queue_ids,
  177. const std::vector<uint32_t> &output_queue_ids) {
  178. GELOGI("Load model with queue begin, model_id:%u.", model_id);
  179. // For ACL, Open Device from App.
  180. auto model_manager = ModelManager::GetInstance();
  181. GE_CHECK_NOTNULL(model_manager);
  182. Status ret = model_manager->LoadModelWithQ(model_id, model_data, input_queue_ids, output_queue_ids);
  183. if (ret != SUCCESS) {
  184. GELOGE(ret, "Load model with queue failed, model_id:%u.", model_id);
  185. return ret;
  186. }
  187. GELOGI("Load model with queue success, model_id:%u.", model_id);
  188. return SUCCESS;
  189. }
  190. ///
  191. /// @ingroup domi_ome
  192. /// @brief execute model
  193. /// @param [in] model_id model id
  194. /// @param [in] stream stream to execute model on
  195. /// @param [in] async_mode is asynchronize mode.
  196. /// @param [in] input_data model input data
  197. /// @param [in] input_desc description of model input data
  198. /// @param [out] output_data model output data
  199. /// @param [out] output_desc description of model output data
  200. ///
  201. Status GraphLoader::ExecuteModel(uint32_t model_id, rtStream_t stream, bool async_mode, const InputData &input_data,
  202. const std::vector<GeTensorDesc> &input_desc, OutputData &output_data,
  203. std::vector<GeTensorDesc> &output_desc) {
  204. auto model_manager = ModelManager::GetInstance();
  205. GE_CHECK_NOTNULL(model_manager);
  206. Status ret = model_manager->ExecuteModel(model_id, stream, async_mode,
  207. input_data, input_desc, output_data, output_desc);
  208. if (ret != SUCCESS) {
  209. GELOGE(ret, "Execute model failed, model_id:%u.", model_id);
  210. return ret;
  211. }
  212. GELOGD("Execute model success, model_id:%u.", model_id);
  213. return SUCCESS;
  214. }
  215. Status GraphLoader::GetMemoryInfo(int64_t &free) {
  216. rtError_t rt_ret = rtSetDevice(GetContext().DeviceId());
  217. if (rt_ret != RT_ERROR_NONE) {
  218. REPORT_CALL_ERROR("E19999", "Call rtSetDevice failed, device_id:%u, ret:0x%X",
  219. GetContext().DeviceId(), rt_ret);
  220. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", rt_ret);
  221. CsaInteract::GetInstance().WriteErrorCode(rt_ret, ERROR_MODULE_RUNTIME, JOBSUBSTATE_GRAPH_LOAD);
  222. return RT_FAILED;
  223. }
  224. size_t total_mem = 0;
  225. size_t free_mem = 0;
  226. rt_ret = rtMemGetInfo(&free_mem, &total_mem);
  227. if (rt_ret != RT_ERROR_NONE) {
  228. REPORT_CALL_ERROR("E19999", "Call rtMemGetInfo failed, ret:0x%X", rt_ret);
  229. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", rt_ret);
  230. return RT_FAILED;
  231. }
  232. rt_ret = rtDeviceReset(GetContext().DeviceId());
  233. if (rt_ret != RT_ERROR_NONE) {
  234. REPORT_CALL_ERROR("E19999", "Call rtDeviceReset failed, device_id:%u, ret:0x%X",
  235. GetContext().DeviceId(), rt_ret);
  236. GELOGE(RT_FAILED, "Call rt api failed, ret: 0x%X", rt_ret);
  237. return RT_FAILED;
  238. }
  239. // Add small page memory size
  240. free = static_cast<int64_t>(free_mem + VarManager::Instance(GetContext().SessionId())->GetUseMaxMemorySize() -
  241. total_mem);
  242. GELOGI("GetMemoryInfo free[%zu], total[%zu], return free[%ld]", free_mem, total_mem, free);
  243. return SUCCESS;
  244. }
  245. Status GraphLoader::DestroyAicpuKernel(uint64_t session_id, uint32_t model_id, uint32_t sub_model_id) {
  246. auto model_manager = ModelManager::GetInstance();
  247. GE_CHECK_NOTNULL(model_manager);
  248. Status ret = model_manager->DestroyAicpuKernel(session_id, model_id, sub_model_id);
  249. if (ret != SUCCESS) {
  250. GELOGE(ret, "Destroy aicpu kernel failed.");
  251. return ret;
  252. }
  253. return SUCCESS;
  254. }
  255. Status GraphLoader::DestroyAicpuSessionForInfer(uint32_t model_id) {
  256. auto model_manager = ModelManager::GetInstance();
  257. GE_CHECK_NOTNULL(model_manager);
  258. Status ret = model_manager->DestroyAicpuSessionForInfer(model_id);
  259. if (ret != SUCCESS) {
  260. GELOGE(ret, "Destroy aicpu serrion for infer failed.");
  261. return ret;
  262. }
  263. return SUCCESS;
  264. }
  265. } // namespace ge

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