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
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
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
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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 "framework/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, "[Stop][Model] failed. model id:%u", model_id);
  32. }
  33. ret = model_manager->Unload(model_id);
  34. if (ret != SUCCESS) {
  35. GELOGE(ret, "[Unload][Model] 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", GetContext().DeviceId(), rt_ret);
  47. GELOGE(RT_FAILED, "[Call][RtSetDevice] failed, device_id:%u, ret:0x%X", GetContext().DeviceId(), rt_ret);
  48. return RT_FAILED;
  49. }
  50. if (ge_root_model_ptr == nullptr) {
  51. REPORT_INNER_ERROR("E19999", "Check param ge_root_model_ptr nullptr, check invalid");
  52. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[LoadGraph][Check][Param] GE load graph model_ptr is nullptr.");
  53. return GE_GRAPH_PARAM_NULLPTR;
  54. }
  55. auto model_manager = ModelManager::GetInstance();
  56. GE_CHECK_NOTNULL(model_manager);
  57. Status ret = model_manager->LoadModelOnline(model_id, ge_root_model_ptr, listener);
  58. if (ret != SUCCESS) {
  59. GELOGE(ret, "[Load][Model] Online failed. ret = %u, model_id:%u", ret, model_id);
  60. rt_ret = rtDeviceReset(GetContext().DeviceId());
  61. if (rt_ret != RT_ERROR_NONE) {
  62. REPORT_CALL_ERROR("E19999", "Call rtDeviceReset failed, device_id:%u, ret:0x%X",
  63. GetContext().DeviceId(), rt_ret);
  64. GELOGE(RT_FAILED, "[Call][RtDeviceReset] failed, device_id:%u, ret:0x%X", GetContext().DeviceId(), rt_ret);
  65. }
  66. return ret;
  67. }
  68. if (ge_root_model_ptr->IsSpecificStream()) {
  69. GELOGI("No need to start a new thread to run model in specific scene.");
  70. rt_ret = rtDeviceReset(GetContext().DeviceId());
  71. if (rt_ret != RT_ERROR_NONE) {
  72. REPORT_CALL_ERROR("E19999", "Call rtDeviceReset failed, device_id:%u, ret:0x%X",
  73. GetContext().DeviceId(), rt_ret);
  74. GELOGE(RT_FAILED, "[Call][RtDeviceReset] failed, device_id:%u, ret:0x%X", GetContext().DeviceId(), rt_ret);
  75. }
  76. return SUCCESS;
  77. }
  78. ret = model_manager->Start(model_id);
  79. if (ret != SUCCESS) {
  80. if (model_manager->Unload(model_id) != SUCCESS) {
  81. GELOGE(ret, "[Unload][Model] failed while trying to unload after a failed start, model_id:%u.", model_id);
  82. }
  83. rt_ret = rtDeviceReset(GetContext().DeviceId());
  84. if (rt_ret != RT_ERROR_NONE) {
  85. REPORT_CALL_ERROR("E19999", "Call rtDeviceReset failed, device_id:%u, ret:0x%X",
  86. GetContext().DeviceId(), rt_ret);
  87. GELOGE(RT_FAILED, "[Call][RtDeviceReset] failed, device_id:%u, ret:0x%X", GetContext().DeviceId(), rt_ret);
  88. }
  89. GELOGE(ret, "[Start][Model] failed, model_id:%u.", model_id);
  90. return ret;
  91. }
  92. rt_ret = rtDeviceReset(GetContext().DeviceId());
  93. if (rt_ret != RT_ERROR_NONE) {
  94. REPORT_CALL_ERROR("E19999", "Call rtDeviceReset failed, device_id:%u, ret:0x%X",
  95. GetContext().DeviceId(), rt_ret);
  96. GELOGE(RT_FAILED, "[Call][RtDeviceReset] failed, device_id:%u, ret:0x%X", GetContext().DeviceId(), rt_ret);
  97. return RT_FAILED;
  98. }
  99. GELOGI("Load model online success, model_id:%u.", model_id);
  100. return SUCCESS;
  101. }
  102. Status GraphLoader::GetMaxUsedMemory(uint32_t model_id, uint64_t &max_size) {
  103. auto model_manager = ModelManager::GetInstance();
  104. GE_CHECK_NOTNULL(model_manager);
  105. Status ret = model_manager->GetMaxUsedMemory(model_id, max_size);
  106. if (ret != SUCCESS) {
  107. GELOGE(ret, "[Call][GetMaxUsedMemory] failed, model_id:%u.", model_id);
  108. return ret;
  109. }
  110. return SUCCESS;
  111. }
  112. Status GraphLoader::LoadDataFromFile(const std::string &path, int32_t priority, ModelData &model_data) {
  113. if (!CheckInputPathValid(path, "model_file")) {
  114. GELOGE(ACL_ERROR_GE_EXEC_MODEL_PATH_INVALID, "[Check][Param] model path is invalid:%s", path.c_str());
  115. return ACL_ERROR_GE_EXEC_MODEL_PATH_INVALID;
  116. }
  117. GELOGI("Load model begin, model path is: %s", path.c_str());
  118. Status ret = ModelParserBase::LoadFromFile(path.c_str(), priority, model_data);
  119. if (ret != SUCCESS) {
  120. GELOGE(ret, "[Call][LoadFromFile] failed. ret = %u, path:%s", ret, path.c_str());
  121. if (model_data.model_data != nullptr) {
  122. delete[] static_cast<char *>(model_data.model_data);
  123. model_data.model_data = nullptr;
  124. }
  125. }
  126. return ret;
  127. }
  128. Status GraphLoader::CommandHandle(const Command &command) {
  129. try {
  130. auto model_manager = ModelManager::GetInstance();
  131. GE_CHECK_NOTNULL(model_manager);
  132. Status ret = model_manager->HandleCommand(command);
  133. if (ret != SUCCESS) {
  134. GELOGE(ret, "[Handle][Command] failed, module_index:%lu.", command.module_index);
  135. return ret;
  136. }
  137. } catch (std::bad_alloc &) {
  138. REPORT_INNER_ERROR("E19999", "Bad memory allocation occur");
  139. GELOGE(ACL_ERROR_GE_MEMORY_ALLOCATION, "[Handle][Command] failed, "
  140. "bad memory allocation occur, module_index:%lu.", command.module_index);
  141. return ACL_ERROR_GE_MEMORY_ALLOCATION;
  142. } catch (...) {
  143. REPORT_INNER_ERROR("E19999", "Some exceptions occur");
  144. GELOGE(FAILED, "[Handle][Command] failed, some exceptions occur, module_index:%lu.", command.module_index);
  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][RtSetDevice] failed, device_id:%u, ret:0x%X", GetContext().DeviceId(), 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][RtMemGetInfo] 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][RtDeviceReset] failed, device_id:%u, ret:0x%X", GetContext().DeviceId(), 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][AicpuKernel] failed, session_id:%lu, model_id:%u, sub_model_id:%u.",
  248. session_id, model_id, sub_model_id);
  249. return ret;
  250. }
  251. return SUCCESS;
  252. }
  253. Status GraphLoader::DestroyAicpuSessionForInfer(uint32_t model_id) {
  254. auto model_manager = ModelManager::GetInstance();
  255. GE_CHECK_NOTNULL(model_manager);
  256. Status ret = model_manager->DestroyAicpuSessionForInfer(model_id);
  257. if (ret != SUCCESS) {
  258. GELOGE(ret, "[Call][DestroyAicpuSessionForInfer] failed, model_id:%u.", model_id);
  259. return ret;
  260. }
  261. return SUCCESS;
  262. }
  263. } // namespace ge

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