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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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,
  143. "[LoadMultiModelOnline] Load multi model failed, graph %s has invalid deploy attr %s",
  144. root_graph->GetName().c_str(),
  145. ATTR_NAME_DEPLOY_INFO.c_str());
  146. return FAILED;
  147. }
  148. auto thread_instances_size = deploy_info.size();
  149. auto device_id_fission_from = GetContext().DeviceId();
  150. GELOGI("Graph %s need to load model %zu times, and fission from device %u.",
  151. root_graph->GetName().c_str(), thread_instances_size,
  152. device_id_fission_from);
  153. ThreadPool executor(thread_instances_size);
  154. std::vector<std::future<Status>> vector_future;
  155. GE_TIMESTAMP_START(LoadModelOnline);
  156. for (size_t i = 0; i < thread_instances_size; ++i) {
  157. auto thread_instance = deploy_info[i];
  158. std::string device_type;
  159. ModelIdInfo model_id_info;
  160. //TODO: listener要区分同步异步
  161. std::shared_ptr<ModelListener> listener;
  162. if (is_async) {
  163. listener = MakeShared<RunAsyncListener>();
  164. GE_CHECK_NOTNULL(listener);
  165. } else {
  166. }
  167. int64_t device_id_fissioned = kInvalidDieId;
  168. if (!ge::AttrUtils::GetInt(thread_instance, kAttrDeviceId, device_id_fissioned)
  169. || device_id_fissioned == kInvalidDieId) {
  170. REPORT_CALL_ERROR("E19999", "graph %s has invalid deploy attr %s", root_graph->GetName().c_str(),
  171. ATTR_NAME_DEPLOY_INFO.c_str());
  172. GELOGE(GRAPH_FAILED, "[LoadMultiModelOnline] graph %s has invalid deploy attr %s", root_graph->GetName().c_str(),
  173. ATTR_NAME_DEPLOY_INFO.c_str());
  174. return GRAPH_FAILED;
  175. };
  176. if (ge::AttrUtils::GetStr(thread_instance, kAttrDeviceType, device_type) && device_type == kMultiMode) {
  177. std::future<Status> f = executor.commit(GraphLoader::LoadModelOnline, model_id_info.model_id, ge_root_model,
  178. listener, device_id_fission_from, device_id_fissioned);
  179. if (!f.valid()) {
  180. GELOGE(FAILED, "[Call][Commit] failed, Future is invalid");
  181. return FAILED;
  182. }
  183. vector_future.emplace_back(std::move(f));
  184. } else {
  185. std::future<Status> f = executor.commit(GraphLoader::LoadModelOnline, model_id_info.model_id, ge_root_model,
  186. listener, device_id_fissioned, kInvalidDieId);
  187. if (!f.valid()) {
  188. GELOGE(FAILED, "[Call][Commit] failed, Future is invalid");
  189. return FAILED;
  190. }
  191. vector_future.emplace_back(std::move(f));
  192. }
  193. }
  194. GE_TIMESTAMP_EVENT_END(LoadModelOnline, "GraphLoader::LoadModelOnline");
  195. for (size_t i = 0; i < vector_future.size(); ++i) {
  196. Status ret_status = vector_future[i].get();
  197. if (ret_status != SUCCESS) {
  198. REPORT_CALL_ERROR("E19999", " Load multi model %zu failed", i);
  199. GELOGE(ret_status, "[LoadMultiModelOnline] Load multi model failed", i);
  200. return ret_status;
  201. }
  202. }
  203. return SUCCESS;
  204. }
  205. Status GraphLoader::GetMaxUsedMemory(uint32_t model_id, uint64_t &max_size) {
  206. auto model_manager = ModelManager::GetInstance();
  207. GE_CHECK_NOTNULL(model_manager);
  208. Status ret = model_manager->GetMaxUsedMemory(model_id, max_size);
  209. if (ret != SUCCESS) {
  210. GELOGE(ret, "[Call][GetMaxUsedMemory] failed, model_id:%u.", model_id);
  211. return ret;
  212. }
  213. return SUCCESS;
  214. }
  215. Status GraphLoader::LoadDataFromFile(const std::string &path, int32_t priority, ModelData &model_data) {
  216. if (!CheckInputPathValid(path, "model_file")) {
  217. GELOGE(ACL_ERROR_GE_EXEC_MODEL_PATH_INVALID, "[Check][Param] model path is invalid:%s", path.c_str());
  218. return ACL_ERROR_GE_EXEC_MODEL_PATH_INVALID;
  219. }
  220. GELOGI("Load model begin, model path is: %s", path.c_str());
  221. Status ret = ModelParserBase::LoadFromFile(path.c_str(), priority, model_data);
  222. if (ret != SUCCESS) {
  223. GELOGE(ret, "[Call][LoadFromFile] failed. ret = %u, path:%s", ret, path.c_str());
  224. if (model_data.model_data != nullptr) {
  225. delete[] static_cast<char *>(model_data.model_data);
  226. model_data.model_data = nullptr;
  227. }
  228. }
  229. return ret;
  230. }
  231. Status GraphLoader::CommandHandle(const Command &command) {
  232. try {
  233. auto model_manager = ModelManager::GetInstance();
  234. GE_CHECK_NOTNULL(model_manager);
  235. Status ret = model_manager->HandleCommand(command);
  236. if (ret != SUCCESS) {
  237. GELOGE(ret, "[Handle][Command] failed, module_index:%lu.", command.module_index);
  238. return ret;
  239. }
  240. } catch (std::bad_alloc &) {
  241. REPORT_INNER_ERROR("E19999", "Bad memory allocation occur");
  242. GELOGE(ACL_ERROR_GE_MEMORY_ALLOCATION, "[Handle][Command] failed, "
  243. "bad memory allocation occur, module_index:%lu.", command.module_index);
  244. return ACL_ERROR_GE_MEMORY_ALLOCATION;
  245. } catch (...) {
  246. REPORT_INNER_ERROR("E19999", "Some exceptions occur");
  247. GELOGE(FAILED, "[Handle][Command] failed, some exceptions occur, module_index:%lu.", command.module_index);
  248. return FAILED;
  249. }
  250. return SUCCESS;
  251. }
  252. Status GraphLoader::LoadModelFromData(uint32_t &model_id, const ModelData &model_data, void *dev_ptr,
  253. size_t mem_size, void *weight_ptr, size_t weight_size) {
  254. GELOGI("Load model begin, model_id:%u.", model_id);
  255. // For ACL, Open Device from App.
  256. auto model_manager = ModelManager::GetInstance();
  257. GE_CHECK_NOTNULL(model_manager);
  258. Status ret = model_manager->LoadModelOffline(
  259. model_id, model_data, nullptr, dev_ptr, mem_size, weight_ptr, weight_size);
  260. if (ret != SUCCESS) {
  261. GELOGE(ret, "[Load][Model] failed, model_id:%u.", model_id);
  262. return ret;
  263. }
  264. GELOGI("Load model success, model_id:%u.", model_id);
  265. return SUCCESS;
  266. }
  267. ///
  268. /// @ingroup ge
  269. /// @brief Load task list from ModelData with queue.
  270. /// @param [out] model_id: model id allocate from manager.
  271. /// @param [in] model_data: Model data load from offline model.
  272. /// @param [in] input_queue_ids: input queue ids create from user.
  273. /// @param [in] output_queue_ids: input queue ids create from user.
  274. /// @return: 0 for success / others for fail
  275. ///
  276. Status GraphLoader::LoadModelWithQ(uint32_t &model_id, const ModelData &model_data,
  277. const std::vector<uint32_t> &input_queue_ids,
  278. const std::vector<uint32_t> &output_queue_ids) {
  279. GELOGI("Load model with queue begin, model_id:%u.", model_id);
  280. // For ACL, Open Device from App.
  281. auto model_manager = ModelManager::GetInstance();
  282. GE_CHECK_NOTNULL(model_manager);
  283. Status ret = model_manager->LoadModelWithQ(model_id, model_data, input_queue_ids, output_queue_ids);
  284. if (ret != SUCCESS) {
  285. GELOGE(ret, "[Load][Model] with queue failed, model_id:%u.", model_id);
  286. return ret;
  287. }
  288. GELOGI("Load model with queue success, model_id:%u.", model_id);
  289. return SUCCESS;
  290. }
  291. ///
  292. /// @ingroup domi_ome
  293. /// @brief execute model
  294. /// @param [in] model_id model id
  295. /// @param [in] stream stream to execute model on
  296. /// @param [in] async_mode is asynchronize mode.
  297. /// @param [in] input_data model input data
  298. /// @param [in] input_desc description of model input data
  299. /// @param [out] output_data model output data
  300. /// @param [out] output_desc description of model output data
  301. ///
  302. Status GraphLoader::ExecuteModel(uint32_t model_id, rtStream_t stream, bool async_mode, const InputData &input_data,
  303. const std::vector<GeTensorDesc> &input_desc, OutputData &output_data,
  304. std::vector<GeTensorDesc> &output_desc) {
  305. auto model_manager = ModelManager::GetInstance();
  306. GE_CHECK_NOTNULL(model_manager);
  307. Status ret = model_manager->ExecuteModel(model_id, stream, async_mode,
  308. input_data, input_desc, output_data, output_desc);
  309. if (ret != SUCCESS) {
  310. GELOGE(ret, "[Execute][Model] failed, model_id:%u.", model_id);
  311. return ret;
  312. }
  313. GELOGD("Execute model success, model_id:%u.", model_id);
  314. return SUCCESS;
  315. }
  316. Status GraphLoader::GetMemoryInfo(int64_t &free) {
  317. rtError_t rt_ret = rtSetDevice(GetContext().DeviceId());
  318. if (rt_ret != RT_ERROR_NONE) {
  319. REPORT_CALL_ERROR("E19999", "Call rtSetDevice failed, device_id:%u, ret:0x%X",
  320. GetContext().DeviceId(), rt_ret);
  321. GELOGE(RT_FAILED, "[Call][RtSetDevice] failed, device_id:%u, ret:0x%X", GetContext().DeviceId(), rt_ret);
  322. return RT_FAILED;
  323. }
  324. size_t total_mem = 0;
  325. size_t free_mem = 0;
  326. rt_ret = rtMemGetInfo(&free_mem, &total_mem);
  327. if (rt_ret != RT_ERROR_NONE) {
  328. REPORT_CALL_ERROR("E19999", "Call rtMemGetInfo failed, ret:0x%X", rt_ret);
  329. GELOGE(RT_FAILED, "[Call][RtMemGetInfo] failed, ret:0x%X", rt_ret);
  330. return RT_FAILED;
  331. }
  332. rt_ret = rtDeviceReset(GetContext().DeviceId());
  333. if (rt_ret != RT_ERROR_NONE) {
  334. REPORT_CALL_ERROR("E19999", "Call rtDeviceReset failed, device_id:%u, ret:0x%X",
  335. GetContext().DeviceId(), rt_ret);
  336. GELOGE(RT_FAILED, "[Call][RtDeviceReset] failed, device_id:%u, ret:0x%X", GetContext().DeviceId(), rt_ret);
  337. return RT_FAILED;
  338. }
  339. // Add small page memory size
  340. free = static_cast<int64_t>(free_mem + VarManager::Instance(GetContext().SessionId())->GetUseMaxMemorySize() -
  341. total_mem);
  342. GELOGI("GetMemoryInfo free[%zu], total[%zu], return free[%ld]", free_mem, total_mem, free);
  343. return SUCCESS;
  344. }
  345. Status GraphLoader::DestroyAicpuKernel(uint64_t session_id, uint32_t model_id, uint32_t sub_model_id) {
  346. auto model_manager = ModelManager::GetInstance();
  347. GE_CHECK_NOTNULL(model_manager);
  348. Status ret = model_manager->DestroyAicpuKernel(session_id, model_id, sub_model_id);
  349. if (ret != SUCCESS) {
  350. GELOGE(ret, "[Destroy][AicpuKernel] failed, session_id:%lu, model_id:%u, sub_model_id:%u.",
  351. session_id, model_id, sub_model_id);
  352. return ret;
  353. }
  354. return SUCCESS;
  355. }
  356. Status GraphLoader::DestroyAicpuSessionForInfer(uint32_t model_id) {
  357. auto model_manager = ModelManager::GetInstance();
  358. GE_CHECK_NOTNULL(model_manager);
  359. Status ret = model_manager->DestroyAicpuSessionForInfer(model_id);
  360. if (ret != SUCCESS) {
  361. GELOGE(ret, "[Call][DestroyAicpuSessionForInfer] failed, model_id:%u.", model_id);
  362. return ret;
  363. }
  364. return SUCCESS;
  365. }
  366. } // namespace ge

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