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

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