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 16 kB

5 years ago
5 years ago
4 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
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 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
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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 <thread>
  20. #include "framework/common/helper/model_helper.h"
  21. #include "common/model_parser/model_parser.h"
  22. #include "graph/ge_context.h"
  23. #include "graph/load/model_manager/model_manager.h"
  24. #include "graph/manager/graph_var_manager.h"
  25. #include "graph/debug/ge_attr_define.h"
  26. #include "common/thread_pool.h"
  27. namespace ge {
  28. namespace {
  29. //deploy info
  30. const char *const kAttrDeviceType = "_device_type";
  31. const char *const kAttrDeviceId = "_device_id";
  32. const char *const kAttrGraphName = "_graph_name";
  33. const char *const kAttrGraphInputs = "_graph_inputs";
  34. }
  35. Status GraphLoader::UnloadModel(uint32_t model_id) {
  36. auto model_manager = ModelManager::GetInstance();
  37. GE_CHECK_NOTNULL(model_manager);
  38. GELOGI("UnLoad model begin, model id:%u.", model_id);
  39. Status ret = model_manager->Stop(model_id);
  40. if (ret != SUCCESS) {
  41. GELOGE(ret, "[Stop][Model] failed. model id:%u", model_id);
  42. }
  43. ret = model_manager->Unload(model_id);
  44. if (ret != SUCCESS) {
  45. GELOGE(ret, "[Unload][Model] failed. model id:%u", model_id);
  46. return ret;
  47. }
  48. GELOGI("UnLoad model success, model id:%u.", model_id);
  49. return SUCCESS;
  50. }
  51. Status GraphLoader::SetDevice(uint32_t device_id, int64_t die_id) {
  52. if (device_id != kInvalidDeviceId && die_id != kInvalidDieId) {
  53. rtError_t rt_ret = rtSetDevice(device_id, kMultiMode);
  54. if (rt_ret != RT_ERROR_NONE) {
  55. REPORT_CALL_ERROR("E19999", "Call rtSetDevice failed, device_id:%u, ret:0x%X", device_id, rt_ret);
  56. GELOGE(RT_FAILED, "[Call][rtSetDevice] failed, device_id:%u, ret:0x%X", device_id, rt_ret);
  57. return RT_FAILED;
  58. }
  59. rt_ret = rtSetDieId(die_id);
  60. if (rt_ret != RT_ERROR_NONE) {
  61. REPORT_CALL_ERROR("E19999", "Call rtSetDieId failed, device_id:%u, ret:0x%X", die_id, rt_ret);
  62. GELOGE(RT_FAILED, "[Call][RtSetDevice] rtSetDieId, device_id:%u, ret:0x%X", die_id, rt_ret);
  63. return RT_FAILED;
  64. }
  65. } else if (device_id != kInvalidDeviceId && die_id == kInvalidDieId) {
  66. rtError_t rt_ret = rtSetDevice(device_id);
  67. if (rt_ret != RT_ERROR_NONE) {
  68. REPORT_CALL_ERROR("E19999", "Call rtSetDevice failed, device_id:%u, ret:0x%X", device_id, rt_ret);
  69. GELOGE(RT_FAILED, "[Call][RtSetDevice] failed, device_id:%u, ret:0x%X", device_id, rt_ret);
  70. return RT_FAILED;
  71. }
  72. } else {
  73. REPORT_CALL_ERROR("E19999", "Call SetDevice failed, device_id:%u, die_id:%ld", device_id, die_id);
  74. GELOGE(RT_FAILED, "[Call][SetDevice] failed, device_id:%u, die_id:%ld", device_id, die_id);
  75. return RT_FAILED;
  76. }
  77. return SUCCESS;
  78. }
  79. Status GraphLoader::ResetDevice(uint32_t device_id, int64_t die_id) {
  80. if (die_id != kInvalidDieId) {
  81. rtError_t rt_ret = rtDieReset(die_id);
  82. if (rt_ret != RT_ERROR_NONE) {
  83. REPORT_CALL_ERROR("E19999", "Call rtSetDevice failed, device_id:%u, ret:0x%X", die_id, rt_ret);
  84. GELOGE(RT_FAILED, "[Call][RtSetDevice] failed, device_id:%u, ret:0x%X", die_id, rt_ret);
  85. return RT_FAILED;
  86. }
  87. } else {
  88. rtError_t rt_ret = rtDeviceReset(device_id);
  89. if (rt_ret != RT_ERROR_NONE) {
  90. REPORT_CALL_ERROR("E19999", "Call rtSetDevice failed, device_id:%u, ret:0x%X", device_id, rt_ret);
  91. GELOGE(RT_FAILED, "[Call][RtSetDevice] failed, device_id:%u, ret:0x%X", device_id, rt_ret);
  92. return RT_FAILED;
  93. }
  94. }
  95. return SUCCESS;
  96. }
  97. Status GraphLoader::LoadModelOnline(uint32_t &model_id,
  98. const std::shared_ptr<ge::GeRootModel> &ge_root_model_ptr,
  99. const std::shared_ptr<ModelListener> &listener,
  100. uint32_t device_id,
  101. int64_t die_id) {
  102. GELOGI("Load model online begin.");
  103. if (ge_root_model_ptr == nullptr) {
  104. REPORT_INNER_ERROR("E19999", "Check param ge_root_model_ptr nullptr, check invalid");
  105. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[LoadGraph][Check][Param] GE load graph model_ptr is nullptr.");
  106. return GE_GRAPH_PARAM_NULLPTR;
  107. }
  108. if (SetDevice(device_id, die_id) != SUCCESS) {
  109. REPORT_CALL_ERROR("E19999", "Call SetDevice failed, device_id:%u", device_id);
  110. GELOGE(RT_FAILED, "[Call][SetDevice] failed, device_id:%u", device_id);
  111. return RT_FAILED;
  112. }
  113. GE_MAKE_GUARD(reset_device, [&] { GE_CHK_RT(ResetDevice(device_id, die_id)); });
  114. auto model_manager = ModelManager::GetInstance();
  115. GE_CHECK_NOTNULL(model_manager);
  116. Status ret = model_manager->LoadModelOnline(model_id, ge_root_model_ptr, listener,device_id, die_id);
  117. if (ret != SUCCESS) {
  118. GELOGE(ret, "[Load][Model] Online failed. ret = %u, model_id:%u", ret, model_id);
  119. return ret;
  120. }
  121. ge_root_model_ptr->SetModelId(model_id);
  122. if (ge_root_model_ptr->IsSpecificStream()) {
  123. GELOGI("No need to start a new thread to run model in specific scene.");
  124. return SUCCESS;
  125. }
  126. ret = model_manager->Start(model_id);
  127. if (ret != SUCCESS) {
  128. if (model_manager->Unload(model_id) != SUCCESS) {
  129. GELOGE(ret, "[Unload][Model] failed while trying to unload after a failed start, model_id:%u.", model_id);
  130. }
  131. GELOGE(ret, "[Start][Model] failed, model_id:%u.", model_id);
  132. return ret;
  133. }
  134. GELOGI("Load model online success, model_id:%u.", model_id);
  135. return SUCCESS;
  136. }
  137. Status GraphLoader::LoadMultiModelOnline(const std::shared_ptr<ge::GeRootModel> &ge_root_model, bool is_async) {
  138. // get deploy number of model instance
  139. auto root_graph = ge_root_model->GetRootGraph();
  140. vector<GeAttrValue::NAMED_ATTRS> deploy_info;
  141. if (!ge::AttrUtils::GetListNamedAttrs(root_graph, ATTR_NAME_DEPLOY_INFO, deploy_info) || deploy_info.empty()) {
  142. GELOGE(FAILED, "[LoadMultiModelOnline] Load multi model failed, graph %s has invalid deploy attr %s",
  143. root_graph->GetName().c_str(), ATTR_NAME_DEPLOY_INFO.c_str());
  144. return FAILED;
  145. }
  146. auto thread_instances_size = deploy_info.size();
  147. auto device_id_fission_from = GetContext().DeviceId();
  148. GELOGI("Graph %s need to load model %zu times, and fission from device %u.", root_graph->GetName().c_str(),
  149. thread_instances_size, device_id_fission_from);
  150. ThreadPool executor(thread_instances_size);
  151. std::vector<std::future<Status>> vector_future;
  152. GE_TIMESTAMP_START(LoadModelOnline);
  153. for (size_t i = 0; i < thread_instances_size; ++i) {
  154. auto thread_instance = deploy_info[i];
  155. std::string device_type;
  156. ModelIdInfo model_id_info;
  157. std::shared_ptr<ModelListener> listener;
  158. if (is_async) {
  159. listener = MakeShared<RunAsyncListener>();
  160. GE_CHECK_NOTNULL(listener);
  161. } else {
  162. // TODO: GraphModelListener for sync
  163. }
  164. int64_t device_id_fissioned = kInvalidDieId;
  165. if (!ge::AttrUtils::GetInt(thread_instance, kAttrDeviceId, device_id_fissioned) ||
  166. device_id_fissioned == kInvalidDieId) {
  167. REPORT_CALL_ERROR("E19999", "graph %s has invalid deploy attr %s", root_graph->GetName().c_str(),
  168. ATTR_NAME_DEPLOY_INFO.c_str());
  169. GELOGE(GRAPH_FAILED, "[LoadMultiModelOnline] graph %s has invalid deploy attr %s", root_graph->GetName().c_str(),
  170. ATTR_NAME_DEPLOY_INFO.c_str());
  171. return GRAPH_FAILED;
  172. };
  173. if (ge::AttrUtils::GetStr(thread_instance, kAttrDeviceType, device_type) && device_type == kMultiMode) {
  174. std::future<Status> f = executor.commit(GraphLoader::LoadModelOnline, model_id_info.model_id, ge_root_model,
  175. listener, device_id_fission_from, device_id_fissioned);
  176. if (!f.valid()) {
  177. GELOGE(FAILED, "[Call][Commit] failed, Future is invalid");
  178. return FAILED;
  179. }
  180. vector_future.emplace_back(std::move(f));
  181. } else {
  182. std::future<Status> f = executor.commit(GraphLoader::LoadModelOnline, model_id_info.model_id, ge_root_model,
  183. listener, device_id_fissioned, kInvalidDieId);
  184. if (!f.valid()) {
  185. GELOGE(FAILED, "[Call][Commit] failed, Future is invalid");
  186. return FAILED;
  187. }
  188. vector_future.emplace_back(std::move(f));
  189. }
  190. }
  191. GE_TIMESTAMP_EVENT_END(LoadModelOnline, "GraphLoader::LoadModelOnline");
  192. for (size_t i = 0; i < vector_future.size(); ++i) {
  193. Status ret_status = vector_future[i].get();
  194. if (ret_status != SUCCESS) {
  195. REPORT_CALL_ERROR("E19999", " Load multi model %zu failed", i);
  196. GELOGE(ret_status, "[LoadMultiModelOnline] Load multi model failed", i);
  197. return ret_status;
  198. }
  199. }
  200. return SUCCESS;
  201. }
  202. Status GraphLoader::GetMaxUsedMemory(uint32_t model_id, uint64_t &max_size) {
  203. auto model_manager = ModelManager::GetInstance();
  204. GE_CHECK_NOTNULL(model_manager);
  205. Status ret = model_manager->GetMaxUsedMemory(model_id, max_size);
  206. if (ret != SUCCESS) {
  207. GELOGE(ret, "[Call][GetMaxUsedMemory] failed, model_id:%u.", model_id);
  208. return ret;
  209. }
  210. return SUCCESS;
  211. }
  212. Status GraphLoader::LoadDataFromFile(const std::string &path, int32_t priority, ModelData &model_data) {
  213. if (!CheckInputPathValid(path, "model_file")) {
  214. GELOGE(ACL_ERROR_GE_EXEC_MODEL_PATH_INVALID, "[Check][Param] model path is invalid:%s", path.c_str());
  215. return ACL_ERROR_GE_EXEC_MODEL_PATH_INVALID;
  216. }
  217. GELOGI("Load model begin, model path is: %s", path.c_str());
  218. Status ret = ModelParserBase::LoadFromFile(path.c_str(), priority, model_data);
  219. if (ret != SUCCESS) {
  220. GELOGE(ret, "[Call][LoadFromFile] failed. ret = %u, path:%s", ret, path.c_str());
  221. if (model_data.model_data != nullptr) {
  222. delete[] static_cast<char *>(model_data.model_data);
  223. model_data.model_data = nullptr;
  224. }
  225. }
  226. return ret;
  227. }
  228. Status GraphLoader::CommandHandle(const Command &command) {
  229. try {
  230. auto model_manager = ModelManager::GetInstance();
  231. GE_CHECK_NOTNULL(model_manager);
  232. Status ret = model_manager->HandleCommand(command);
  233. if (ret != SUCCESS) {
  234. GELOGE(ret, "[Handle][Command] failed, module_index:%lu.", command.module_index);
  235. return ret;
  236. }
  237. } catch (std::bad_alloc &) {
  238. REPORT_INNER_ERROR("E19999", "Bad memory allocation occur");
  239. GELOGE(ACL_ERROR_GE_MEMORY_ALLOCATION, "[Handle][Command] failed, "
  240. "bad memory allocation occur, module_index:%lu.", command.module_index);
  241. return ACL_ERROR_GE_MEMORY_ALLOCATION;
  242. } catch (...) {
  243. REPORT_INNER_ERROR("E19999", "Some exceptions occur");
  244. GELOGE(FAILED, "[Handle][Command] failed, some exceptions occur, module_index:%lu.", command.module_index);
  245. return FAILED;
  246. }
  247. return SUCCESS;
  248. }
  249. Status GraphLoader::LoadModelFromData(uint32_t &model_id, const ModelData &model_data, void *dev_ptr,
  250. size_t mem_size, void *weight_ptr, size_t weight_size) {
  251. GELOGI("Load model begin, model_id:%u.", model_id);
  252. // For ACL, Open Device from App.
  253. auto model_manager = ModelManager::GetInstance();
  254. GE_CHECK_NOTNULL(model_manager);
  255. Status ret = model_manager->LoadModelOffline(
  256. model_id, model_data, nullptr, dev_ptr, mem_size, weight_ptr, weight_size);
  257. if (ret != SUCCESS) {
  258. GELOGE(ret, "[Load][Model] failed, model_id:%u.", model_id);
  259. return ret;
  260. }
  261. GELOGI("Load model success, model_id:%u.", model_id);
  262. return SUCCESS;
  263. }
  264. ///
  265. /// @ingroup ge
  266. /// @brief Load task list from ModelData with queue.
  267. /// @param [out] model_id: model id allocate from manager.
  268. /// @param [in] model_data: Model data load from offline model.
  269. /// @param [in] input_queue_ids: input queue ids create from user.
  270. /// @param [in] output_queue_ids: input queue ids create from user.
  271. /// @return: 0 for success / others for fail
  272. ///
  273. Status GraphLoader::LoadModelWithQ(uint32_t &model_id, const ModelData &model_data,
  274. const std::vector<uint32_t> &input_queue_ids,
  275. const std::vector<uint32_t> &output_queue_ids) {
  276. GELOGI("Load model with queue begin, model_id:%u.", model_id);
  277. // For ACL, Open Device from App.
  278. auto model_manager = ModelManager::GetInstance();
  279. GE_CHECK_NOTNULL(model_manager);
  280. Status ret = model_manager->LoadModelWithQ(model_id, model_data, input_queue_ids, output_queue_ids);
  281. if (ret != SUCCESS) {
  282. GELOGE(ret, "[Load][Model] with queue failed, model_id:%u.", model_id);
  283. return ret;
  284. }
  285. GELOGI("Load model with queue success, model_id:%u.", model_id);
  286. return SUCCESS;
  287. }
  288. ///
  289. /// @ingroup domi_ome
  290. /// @brief execute model
  291. /// @param [in] model_id model id
  292. /// @param [in] stream stream to execute model on
  293. /// @param [in] async_mode is asynchronize mode.
  294. /// @param [in] input_data model input data
  295. /// @param [in] input_desc description of model input data
  296. /// @param [out] output_data model output data
  297. /// @param [out] output_desc description of model output data
  298. ///
  299. Status GraphLoader::ExecuteModel(uint32_t model_id, rtStream_t stream, bool async_mode, const InputData &input_data,
  300. const std::vector<GeTensorDesc> &input_desc, OutputData &output_data,
  301. std::vector<GeTensorDesc> &output_desc) {
  302. auto model_manager = ModelManager::GetInstance();
  303. GE_CHECK_NOTNULL(model_manager);
  304. Status ret = model_manager->ExecuteModel(model_id, stream, async_mode,
  305. input_data, input_desc, output_data, output_desc);
  306. if (ret != SUCCESS) {
  307. GELOGE(ret, "[Execute][Model] failed, model_id:%u.", model_id);
  308. return ret;
  309. }
  310. GELOGD("Execute model success, model_id:%u.", model_id);
  311. return SUCCESS;
  312. }
  313. Status GraphLoader::GetMemoryInfo(int64_t &free) {
  314. rtError_t rt_ret = rtSetDevice(GetContext().DeviceId());
  315. if (rt_ret != RT_ERROR_NONE) {
  316. REPORT_CALL_ERROR("E19999", "Call rtSetDevice failed, device_id:%u, ret:0x%X",
  317. GetContext().DeviceId(), rt_ret);
  318. GELOGE(RT_FAILED, "[Call][RtSetDevice] failed, device_id:%u, ret:0x%X", GetContext().DeviceId(), rt_ret);
  319. return RT_FAILED;
  320. }
  321. size_t total_mem = 0;
  322. size_t free_mem = 0;
  323. rt_ret = rtMemGetInfo(&free_mem, &total_mem);
  324. if (rt_ret != RT_ERROR_NONE) {
  325. REPORT_CALL_ERROR("E19999", "Call rtMemGetInfo failed, ret:0x%X", rt_ret);
  326. GELOGE(RT_FAILED, "[Call][RtMemGetInfo] failed, ret:0x%X", rt_ret);
  327. return RT_FAILED;
  328. }
  329. rt_ret = rtDeviceReset(GetContext().DeviceId());
  330. if (rt_ret != RT_ERROR_NONE) {
  331. REPORT_CALL_ERROR("E19999", "Call rtDeviceReset failed, device_id:%u, ret:0x%X",
  332. GetContext().DeviceId(), rt_ret);
  333. GELOGE(RT_FAILED, "[Call][RtDeviceReset] failed, device_id:%u, ret:0x%X", GetContext().DeviceId(), rt_ret);
  334. return RT_FAILED;
  335. }
  336. // Add small page memory size
  337. free = static_cast<int64_t>(free_mem + VarManager::Instance(GetContext().SessionId())->GetUseMaxMemorySize() -
  338. total_mem);
  339. GELOGI("GetMemoryInfo free[%zu], total[%zu], return free[%ld]", free_mem, total_mem, free);
  340. return SUCCESS;
  341. }
  342. Status GraphLoader::DestroyAicpuKernel(uint64_t session_id, uint32_t model_id, uint32_t sub_model_id) {
  343. auto model_manager = ModelManager::GetInstance();
  344. GE_CHECK_NOTNULL(model_manager);
  345. Status ret = model_manager->DestroyAicpuKernel(session_id, model_id, sub_model_id);
  346. if (ret != SUCCESS) {
  347. GELOGE(ret, "[Destroy][AicpuKernel] failed, session_id:%lu, model_id:%u, sub_model_id:%u.",
  348. session_id, model_id, sub_model_id);
  349. return ret;
  350. }
  351. return SUCCESS;
  352. }
  353. Status GraphLoader::DestroyAicpuSessionForInfer(uint32_t model_id) {
  354. auto model_manager = ModelManager::GetInstance();
  355. GE_CHECK_NOTNULL(model_manager);
  356. Status ret = model_manager->DestroyAicpuSessionForInfer(model_id);
  357. if (ret != SUCCESS) {
  358. GELOGE(ret, "[Call][DestroyAicpuSessionForInfer] failed, model_id:%u.", model_id);
  359. return ret;
  360. }
  361. return SUCCESS;
  362. }
  363. } // namespace ge

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