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.

model_manager.cc 69 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 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
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
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
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 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
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
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
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
5 years ago
5 years ago
5 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
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 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
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
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 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
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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717
  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/model_manager/model_manager.h"
  17. #include <string>
  18. #include "aicpu/aicpu_schedule/aicpu_op_type_list.h"
  19. #include "common/model_parser/model_parser.h"
  20. #include "common/dump/dump_manager.h"
  21. #include "common/l2_cache_optimize.h"
  22. #include "common/profiling/profiling_manager.h"
  23. #include "graph/common/ge_call_wrapper.h"
  24. #include "graph/load/model_manager/davinci_model.h"
  25. #include "model/ge_root_model.h"
  26. #include "common/formats/utils/formats_trans_utils.h"
  27. namespace ge {
  28. thread_local uint32_t device_count = 0;
  29. namespace {
  30. const int kCmdParSize = 2;
  31. const int kDumpCmdPairSize = 2;
  32. const std::size_t kProfCmdParaMaxSize = 1000;
  33. const std::size_t kProfStartCmdParaSize = 2;
  34. const std::string kCmdTypeDump = "dump";
  35. const std::string kCmdTypeProfInit = "prof_init";
  36. const std::string kCmdTypeProfFinalize = "prof_finalize";
  37. const std::string kCmdTypeProfStart = "prof_start";
  38. const std::string kCmdTypeProfStop = "prof_stop";
  39. const std::string kCmdTypeProfModelSubscribe = "prof_model_subscribe";
  40. const std::string kCmdTypeProfModelUnsubscribe = "prof_model_cancel_subscribe";
  41. const char *const kBatchLoadBuf = "batchLoadsoFrombuf";
  42. const char *const kDeleteCustOp = "deleteCustOp";
  43. const int kTimeSpecNano = 1000000000;
  44. const int kTimeSpecMiro = 1000000;
  45. const int kOpNameMaxSize = 100;
  46. const uint64_t kInferSessionId = 0;
  47. #pragma pack(push, 1)
  48. struct CustAicpuSoBuf {
  49. uint64_t kernelSoBuf;
  50. uint32_t kernelSoBufLen;
  51. uint64_t kernelSoName;
  52. uint32_t kernelSoNameLen;
  53. };
  54. struct BatchLoadOpFromBufArgs {
  55. uint32_t soNum;
  56. uint64_t args;
  57. };
  58. #pragma pack(pop)
  59. } // namespace
  60. DumpProperties ModelManager::dump_properties_;
  61. std::mutex ModelManager::exeception_infos_mutex_;
  62. std::shared_ptr<ModelManager> ModelManager::GetInstance() {
  63. static const std::shared_ptr<ModelManager> instance_ptr =
  64. shared_ptr<ModelManager>(new (std::nothrow) ModelManager(), ModelManager::FinalizeForPtr);
  65. return instance_ptr;
  66. }
  67. ModelManager::ModelManager() {
  68. max_model_id_ = 0;
  69. session_id_bias_ = 0;
  70. }
  71. Status ModelManager::KernelLaunchEx(aicpu::FWKAdapter::FWKOperateType op_type, uint64_t session_id, uint32_t model_id,
  72. uint32_t sub_model_id) {
  73. STR_FWK_OP_KERNEL param_base = {};
  74. void *devicebase = nullptr;
  75. void *aicpu_kernel_addr = nullptr;
  76. const uint32_t kKernelType = 0;
  77. param_base.fwkKernelType = kKernelType;
  78. param_base.fwkKernelBase.fwk_kernel.opType = op_type;
  79. param_base.fwkKernelBase.fwk_kernel.sessionID = session_id;
  80. if (op_type == aicpu::FWKAdapter::FWKOperateType::FWK_ADPT_KERNEL_DESTROY) {
  81. std::vector<uint64_t> v_aicpu_kernel;
  82. std::string model_key = std::to_string(session_id) + "_" + std::to_string(model_id) + "_" +
  83. std::to_string(sub_model_id);
  84. std::lock_guard<std::recursive_mutex> lock(map_mutex_);
  85. auto iter = model_aicpu_kernel_.find(model_key);
  86. if (iter != model_aicpu_kernel_.end()) {
  87. GELOGD("kernel destroy session_id %lu, model_id %u, sub_model_id %u..", session_id, model_id, sub_model_id);
  88. v_aicpu_kernel = model_aicpu_kernel_.at(model_key);
  89. // Insert size of aicpu kernel vector in the first element
  90. v_aicpu_kernel.insert(v_aicpu_kernel.begin(), v_aicpu_kernel.size());
  91. auto kernel_size = sizeof(uint64_t) * (v_aicpu_kernel.size());
  92. rtError_t rt_ret = rtMalloc(&aicpu_kernel_addr, kernel_size, RT_MEMORY_HBM);
  93. GE_IF_BOOL_EXEC(rt_ret != RT_ERROR_NONE, GELOGE(RT_FAILED, "rtMalloc error, ret: 0x%X", rt_ret);
  94. return RT_ERROR_TO_GE_STATUS(rt_ret);)
  95. rt_ret = rtMemcpy(aicpu_kernel_addr, kernel_size, v_aicpu_kernel.data(), kernel_size, RT_MEMCPY_HOST_TO_DEVICE);
  96. GE_IF_BOOL_EXEC(rt_ret != RT_ERROR_NONE, GELOGE(RT_FAILED, "rtMemcpy to input_output_addr_ error: 0x%X", rt_ret);
  97. GE_CHK_RT(rtFree(aicpu_kernel_addr)); return RT_ERROR_TO_GE_STATUS(rt_ret);)
  98. uint64_t kernel_id_addr = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(aicpu_kernel_addr));
  99. param_base.fwkKernelBase.fwk_kernel.kernelID = kernel_id_addr;
  100. // In the scene of loading once and running many times, the kernel needs to be destroyed many times,
  101. // and connot be removed from kernel map.
  102. }
  103. }
  104. rtError_t rt_ret = rtMalloc(&(devicebase), sizeof(STR_FWK_OP_KERNEL), RT_MEMORY_HBM);
  105. if (rt_ret != RT_ERROR_NONE) {
  106. GELOGE(RT_FAILED, "malloc device memory failed. ret: 0x%X", rt_ret);
  107. GE_IF_BOOL_EXEC(aicpu_kernel_addr != nullptr, GE_CHK_RT(rtFree(aicpu_kernel_addr)));
  108. return RT_ERROR_TO_GE_STATUS(rt_ret);
  109. }
  110. rt_ret =
  111. rtMemcpy(devicebase, sizeof(STR_FWK_OP_KERNEL), &param_base, sizeof(STR_FWK_OP_KERNEL), RT_MEMCPY_HOST_TO_DEVICE);
  112. if (rt_ret != RT_ERROR_NONE) {
  113. GELOGE(RT_FAILED, "memory copy to device failed. ret: 0x%X", rt_ret);
  114. GE_IF_BOOL_EXEC(aicpu_kernel_addr != nullptr, GE_CHK_RT(rtFree(aicpu_kernel_addr)));
  115. GE_CHK_RT(rtFree(devicebase));
  116. return RT_ERROR_TO_GE_STATUS(rt_ret);
  117. }
  118. rtStream_t stream = nullptr;
  119. rt_ret = rtStreamCreate(&stream, 0);
  120. if (rt_ret != RT_ERROR_NONE) {
  121. GELOGE(RT_FAILED, "create stream failed. ret: 0x%X", rt_ret);
  122. GE_IF_BOOL_EXEC(aicpu_kernel_addr != nullptr, GE_CHK_RT(rtFree(aicpu_kernel_addr)));
  123. GE_CHK_RT(rtFree(devicebase));
  124. return RT_ERROR_TO_GE_STATUS(rt_ret);
  125. }
  126. rt_ret = rtKernelLaunchEx(devicebase, sizeof(STR_FWK_OP_KERNEL), 0, stream);
  127. if (rt_ret != RT_ERROR_NONE) {
  128. GELOGE(RT_FAILED, "rtKernelLaunchEx failed. ret: 0x%X", rt_ret);
  129. GE_IF_BOOL_EXEC(aicpu_kernel_addr != nullptr, GE_CHK_RT(rtFree(aicpu_kernel_addr)));
  130. GE_CHK_RT(rtFree(devicebase));
  131. GE_CHK_RT(rtStreamDestroy(stream));
  132. return RT_ERROR_TO_GE_STATUS(rt_ret);
  133. }
  134. rt_ret = rtStreamSynchronize(stream);
  135. if (rt_ret != RT_ERROR_NONE) {
  136. GELOGE(RT_FAILED, "rtStreamSynchronize failed. ret: 0x%X", rt_ret);
  137. GE_IF_BOOL_EXEC(aicpu_kernel_addr != nullptr, GE_CHK_RT(rtFree(aicpu_kernel_addr)));
  138. GE_CHK_RT(rtFree(devicebase));
  139. GE_CHK_RT(rtStreamDestroy(stream));
  140. return RT_ERROR_TO_GE_STATUS(rt_ret);
  141. }
  142. if (aicpu_kernel_addr != nullptr) {
  143. rt_ret = rtFree(aicpu_kernel_addr);
  144. if (rt_ret != RT_ERROR_NONE) {
  145. GELOGE(RT_FAILED, "free memory failed. ret: 0x%X", rt_ret);
  146. GE_CHK_RT(rtFree(devicebase));
  147. GE_CHK_RT(rtStreamDestroy(stream));
  148. return RT_ERROR_TO_GE_STATUS(rt_ret);
  149. }
  150. }
  151. rt_ret = rtFree(devicebase);
  152. if (rt_ret != RT_ERROR_NONE) {
  153. GELOGE(RT_FAILED, "free memory failed. ret: 0x%X", rt_ret);
  154. GE_CHK_RT(rtStreamDestroy(stream));
  155. return RT_ERROR_TO_GE_STATUS(rt_ret);
  156. }
  157. rt_ret = rtStreamDestroy(stream);
  158. if (rt_ret != RT_ERROR_NONE) {
  159. GELOGE(RT_FAILED, "rtStreamDestroy failed. ret: 0x%X", rt_ret);
  160. return RT_ERROR_TO_GE_STATUS(rt_ret);
  161. }
  162. return SUCCESS;
  163. }
  164. void ModelManager::DestroyAicpuSession(uint64_t session_id) {
  165. std::lock_guard<std::recursive_mutex> lock(map_mutex_);
  166. auto it = sess_ids_.find(session_id);
  167. if (it == sess_ids_.end()) {
  168. GELOGI("The session: %lu not created.", session_id);
  169. return;
  170. } else {
  171. rtContext_t ctx = nullptr;
  172. bool has_ctx = (rtCtxGetCurrent(&ctx) == RT_ERROR_NONE);
  173. if (!has_ctx) {
  174. GELOGI("Set device %u.", GetContext().DeviceId());
  175. GE_CHK_RT(rtSetDevice(static_cast<int32_t>(GetContext().DeviceId())));
  176. }
  177. Status ret = KernelLaunchEx(aicpu::FWKAdapter::FWKOperateType::FWK_ADPT_SESSION_DESTROY, session_id, 0, 0);
  178. if (ret != SUCCESS) {
  179. GELOGW("The session: %lu destroy failed.", session_id);
  180. } else {
  181. (void)sess_ids_.erase(session_id);
  182. GELOGI("The session: %lu destroyed.", session_id);
  183. }
  184. if (!has_ctx) {
  185. GELOGI("Reset device %u.", GetContext().DeviceId());
  186. GE_CHK_RT(rtDeviceReset(static_cast<int32_t>(GetContext().DeviceId())));
  187. }
  188. }
  189. }
  190. ge::Status ModelManager::DestroyAicpuSessionForInfer(uint32_t model_id) {
  191. std::lock_guard<std::recursive_mutex> lock(map_mutex_);
  192. auto hybrid_davinci_model = hybrid_model_map_.find(model_id);
  193. if (hybrid_davinci_model != hybrid_model_map_.end()) {
  194. uint64_t session_id = hybrid_davinci_model->second->GetSessionId();
  195. DestroyAicpuSession(session_id);
  196. return SUCCESS;
  197. }
  198. auto it = model_map_.find(model_id);
  199. if (it == model_map_.end()) {
  200. GELOGE(ACL_ERROR_GE_EXEC_MODEL_ID_INVALID, "model id %u does not exists.", model_id);
  201. return ACL_ERROR_GE_EXEC_MODEL_ID_INVALID;
  202. }
  203. uint64_t session_id = it->second->GetSessionId();
  204. DestroyAicpuSession(session_id);
  205. return SUCCESS;
  206. }
  207. ge::Status ModelManager::DestroyAicpuKernel(uint64_t session_id, uint32_t model_id, uint32_t sub_model_id) {
  208. GELOGD("destroy aicpu kernel in session_id %lu, model_id %u.", session_id, model_id);
  209. std::lock_guard<std::recursive_mutex> lock(map_mutex_);
  210. std::string model_key = std::to_string(session_id) + "_" + std::to_string(model_id) + "_" +
  211. std::to_string(sub_model_id);
  212. if (model_aicpu_kernel_.find(model_key) != model_aicpu_kernel_.end()) {
  213. Status ret = KernelLaunchEx(aicpu::FWKAdapter::FWKOperateType::FWK_ADPT_KERNEL_DESTROY, session_id, model_id,
  214. sub_model_id);
  215. if (ret != SUCCESS) {
  216. GELOGE(FAILED, "Destroy aicpu kernel failed.");
  217. return FAILED;
  218. }
  219. }
  220. return SUCCESS;
  221. }
  222. ge::Status ModelManager::CreateAicpuKernel(uint64_t session_id, uint32_t model_id, uint32_t sub_model_id,
  223. uint64_t kernel_id) {
  224. std::lock_guard<std::recursive_mutex> lock(map_mutex_);
  225. std::vector<uint64_t> v_aicpu_kernel;
  226. std::string model_key = std::to_string(session_id) + "_" + std::to_string(model_id) + "_" +
  227. std::to_string(sub_model_id);
  228. if (model_aicpu_kernel_.find(model_key) != model_aicpu_kernel_.end()) {
  229. v_aicpu_kernel = model_aicpu_kernel_.at(model_key);
  230. }
  231. v_aicpu_kernel.push_back(kernel_id);
  232. model_aicpu_kernel_[model_key] = v_aicpu_kernel;
  233. return SUCCESS;
  234. }
  235. ModelManager::~ModelManager() {
  236. std::lock_guard<std::recursive_mutex> lock(map_mutex_);
  237. model_map_.clear();
  238. model_aicpu_kernel_.clear();
  239. cust_aicpu_so_.clear();
  240. GE_IF_BOOL_EXEC(device_count > 0, GE_CHK_RT(rtDeviceReset(0)));
  241. }
  242. ge::Status ModelManager::SetDynamicSize(uint32_t model_id, const std::vector<uint64_t> &batch_num,
  243. int32_t dynamic_type) {
  244. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  245. GE_CHECK_NOTNULL(davinci_model);
  246. davinci_model->SetDynamicSize(batch_num, dynamic_type);
  247. return SUCCESS;
  248. }
  249. ge::Status ModelManager::DoLoadHybridModelOnline(uint32_t model_id, const string &model_name, const shared_ptr<ge::GeRootModel> &ge_root_model,
  250. const shared_ptr<ModelListener> &listener) {
  251. auto hybrid_model = hybrid::HybridDavinciModel::Create(ge_root_model);
  252. GE_CHECK_NOTNULL(hybrid_model);
  253. hybrid_model->SetListener(listener);
  254. hybrid_model->SetModelId(model_id);
  255. hybrid_model->SetDeviceId(GetContext().DeviceId());
  256. hybrid_model->SetModelName(model_name);
  257. GE_CHK_STATUS_RET(hybrid_model->Init(), "Failed to init hybrid model. model_id = %u", model_id);
  258. auto shared_model = std::shared_ptr<hybrid::HybridDavinciModel>(hybrid_model.release());
  259. InsertModel(model_id, shared_model);
  260. return SUCCESS;
  261. }
  262. ///
  263. /// @ingroup domi_ome
  264. /// @brief load model online
  265. /// @return Status run result
  266. ///
  267. Status ModelManager::LoadModelOnline(uint32_t &model_id, const shared_ptr<ge::GeRootModel> &ge_root_model,
  268. std::shared_ptr<ModelListener> listener) {
  269. GE_CHK_BOOL_RET_STATUS(listener.get() != nullptr, PARAM_INVALID, "Param incorrect, listener is null");
  270. if (model_id == INVALID_MODEL_ID) {
  271. GenModelId(&model_id);
  272. }
  273. bool is_shape_unknown = false;
  274. string model_name = "";
  275. GE_CHK_STATUS_RET(ge_root_model->CheckIsUnknownShape(is_shape_unknown), "CheckIsUnknownShape failed, model id:%u",
  276. model_id);
  277. if (is_shape_unknown || GetContext().GetHostExecFlag()) {
  278. return DoLoadHybridModelOnline(model_id, model_name, ge_root_model, listener);
  279. }
  280. mmTimespec timespec = mmGetTickCount();
  281. std::shared_ptr<DavinciModel> davinci_model = MakeShared<DavinciModel>(0, listener);
  282. if (davinci_model == nullptr) {
  283. GELOGE(FAILED, "davinci_model is nullptr");
  284. return FAILED;
  285. }
  286. davinci_model->SetProfileTime(MODEL_LOAD_START, (timespec.tv_sec * kTimeSpecNano +
  287. timespec.tv_nsec)); // 1000 ^ 3 converts second to nanosecond
  288. davinci_model->SetId(model_id);
  289. davinci_model->SetDeviceId(GetContext().DeviceId());
  290. const DumpProperties &dump_properties = DumpManager::GetInstance().GetDumpProperties(GetContext().SessionId());
  291. davinci_model->SetDumpProperties(dump_properties);
  292. dump_properties_ = dump_properties;
  293. auto root_graph = ge_root_model->GetRootGraph();
  294. GE_CHECK_NOTNULL(root_graph);
  295. string root_model_name = root_graph->GetName();
  296. auto name_to_model = ge_root_model->GetSubgraphInstanceNameToModel();
  297. GeModelPtr ge_model = name_to_model[root_model_name];
  298. Status ret = SUCCESS;
  299. do {
  300. GE_TIMESTAMP_START(Assign);
  301. GE_IF_BOOL_EXEC(SUCCESS != (ret = davinci_model->Assign(ge_model)), GELOGW("assign model to modeldef failed.");
  302. break;);
  303. GE_TIMESTAMP_END(Assign, "GraphLoader::ModelAssign");
  304. GE_TIMESTAMP_START(Init);
  305. GE_IF_BOOL_EXEC(SUCCESS != (ret = davinci_model->Init()), GELOGW("DavinciInit failed."); break;);
  306. GE_TIMESTAMP_END(Init, "GraphLoader::ModelInit");
  307. InsertModel(model_id, davinci_model);
  308. GELOGI("Parse model %u success.", model_id);
  309. } while (0);
  310. return ret;
  311. }
  312. void ModelManager::InsertModel(uint32_t id, std::shared_ptr<DavinciModel> &davinci_model) {
  313. GE_CHK_BOOL_EXEC(davinci_model != nullptr, return, "davinci_model ptr is null, id: %u", id);
  314. std::lock_guard<std::recursive_mutex> lock(map_mutex_);
  315. model_map_[id] = davinci_model;
  316. }
  317. void ModelManager::InsertModel(uint32_t id, shared_ptr<hybrid::HybridDavinciModel> &hybrid_model) {
  318. GE_CHK_BOOL_EXEC(hybrid_model != nullptr, return, "hybrid_model ptr is null, id: %u", id);
  319. std::lock_guard<std::recursive_mutex> lock(map_mutex_);
  320. hybrid_model_map_[id] = hybrid_model;
  321. }
  322. Status ModelManager::DeleteModel(uint32_t id) {
  323. std::lock_guard<std::recursive_mutex> lock(map_mutex_);
  324. auto it = model_map_.find(id);
  325. auto hybrid_model_it = hybrid_model_map_.find(id);
  326. if (it != model_map_.end()) {
  327. uint64_t session_id = it->second->GetSessionId();
  328. std::string model_key = std::to_string(session_id) + "_" + std::to_string(id) + "_" +
  329. std::to_string(it->second->SubModelId());
  330. auto iter_aicpu_kernel = model_aicpu_kernel_.find(model_key);
  331. if (iter_aicpu_kernel != model_aicpu_kernel_.end()) {
  332. (void)model_aicpu_kernel_.erase(iter_aicpu_kernel);
  333. }
  334. (void)model_map_.erase(it);
  335. } else if (hybrid_model_it != hybrid_model_map_.end()) {
  336. (void)hybrid_model_map_.erase(hybrid_model_it);
  337. } else {
  338. GELOGE(ACL_ERROR_GE_EXEC_MODEL_ID_INVALID, "model id %u does not exists.", id);
  339. return ACL_ERROR_GE_EXEC_MODEL_ID_INVALID;
  340. }
  341. return SUCCESS;
  342. }
  343. std::shared_ptr<DavinciModel> ModelManager::GetModel(uint32_t id) {
  344. std::lock_guard<std::recursive_mutex> lock(map_mutex_);
  345. auto it = model_map_.find(id);
  346. return (it == model_map_.end()) ? nullptr : it->second;
  347. }
  348. std::shared_ptr<hybrid::HybridDavinciModel> ModelManager::GetHybridModel(uint32_t id) {
  349. std::lock_guard<std::recursive_mutex> lock(map_mutex_);
  350. auto it = hybrid_model_map_.find(id);
  351. return (it == hybrid_model_map_.end()) ? nullptr : it->second;
  352. }
  353. Status ModelManager::Unload(uint32_t model_id) {
  354. GE_CHK_STATUS_RET(DeleteModel(model_id), "failed to unload model id: %u", model_id);
  355. if (device_count > 0) {
  356. device_count--;
  357. GELOGI("Unload model %u success.", model_id);
  358. } else {
  359. GELOGI("Unload model %u success.no need reset device,device_count: %u", model_id, device_count);
  360. }
  361. std::lock_guard<std::mutex> lock(exeception_infos_mutex_);
  362. exception_infos_.clear();
  363. return SUCCESS;
  364. }
  365. Status ModelManager::UnloadModeldef(uint32_t model_id) {
  366. GE_CHK_STATUS_RET(DeleteModel(model_id), "failed to unload modeldef id: %u", model_id);
  367. return SUCCESS;
  368. }
  369. Status ModelManager::DataInput(const InputData &input_data, OutputData &output_data) {
  370. GELOGI("calling the DataInput");
  371. shared_ptr<InputDataWrapper> data_wrap(new (std::nothrow) InputDataWrapper());
  372. GE_CHECK_NOTNULL(data_wrap);
  373. Status status = data_wrap->Init(input_data, output_data);
  374. if (status != SUCCESS) {
  375. GELOGE(domi::PUSH_DATA_FAILED, "Init InputDataWrapper failed, input data index: %u.", input_data.index);
  376. return domi::PUSH_DATA_FAILED;
  377. }
  378. uint32_t model_id = input_data.model_id;
  379. output_data.model_id = model_id;
  380. std::shared_ptr<DavinciModel> model = GetModel(model_id);
  381. GE_CHK_BOOL_RET_STATUS(model != nullptr, PARAM_INVALID, "Invalid model id %u in InputData! ", model_id);
  382. GE_IF_BOOL_EXEC(model->GetDataInputTid() == 0, model->SetDataInputTid(mmGetTid()));
  383. DataInputer *inputer = model->GetDataInputer();
  384. GE_CHECK_NOTNULL(inputer);
  385. if (inputer->Push(data_wrap) != SUCCESS) {
  386. GELOGE(domi::DATA_QUEUE_ISFULL, "Data queue is full, please call again later, model_id %u ", model_id);
  387. return domi::DATA_QUEUE_ISFULL;
  388. }
  389. GELOGD("Data input success, model id:%u", model_id);
  390. return SUCCESS;
  391. }
  392. Status ModelManager::GetCurDynamicDims(const vector<vector<int64_t>> &user_real_input_dims,
  393. const vector<pair<string, vector<int64_t>>> &user_input_dims,
  394. vector<int32_t> &cur_dynamic_dims) {
  395. GELOGD("Start get cur dynamic dims.");
  396. if (user_real_input_dims.size() != user_input_dims.size()) {
  397. GELOGE(INTERNAL_ERROR,
  398. "The input count of user: %zu should be equal to the data count of graph: %zu",
  399. user_real_input_dims.size(), user_input_dims.size());
  400. return INTERNAL_ERROR;
  401. }
  402. for (size_t i = 0; i < user_input_dims.size(); ++i) {
  403. if (user_real_input_dims[i].size() != user_input_dims[i].second.size()) {
  404. GELOGE(INTERNAL_ERROR,
  405. "The shape size: %zu of dynamic input: %s should be equal to the shape size of input shape: %zu.",
  406. user_real_input_dims[i].size(), user_input_dims[i].first.c_str(), user_input_dims[i].second.size());
  407. return INTERNAL_ERROR;
  408. }
  409. for (size_t j = 0; j < user_input_dims.at(i).second.size(); ++j) {
  410. if (user_input_dims.at(i).second.at(j) < 0) {
  411. cur_dynamic_dims.emplace_back(static_cast<int32_t>(user_real_input_dims[i][j]));
  412. }
  413. }
  414. }
  415. GELOGD("Cur dynamic dims is %s.", formats::JoinToString(cur_dynamic_dims).c_str());
  416. bool cur_dynamic_dims_valid = false;
  417. std::vector<std::string> shape_strs = ge::StringUtils::Split(GetLocalOmgContext().dynamic_dims, ';');
  418. for (auto dynamic_dim : shape_strs) {
  419. if (dynamic_dim == formats::JoinToString(cur_dynamic_dims)) {
  420. cur_dynamic_dims_valid = true;
  421. break;
  422. }
  423. }
  424. if (!cur_dynamic_dims_valid) {
  425. GELOGE(INTERNAL_ERROR, "Cur dynamic dims is %s, not exist in options.",
  426. formats::JoinToString(cur_dynamic_dims).c_str());
  427. return INTERNAL_ERROR;
  428. }
  429. return SUCCESS;
  430. }
  431. ///
  432. /// @ingroup domi_ome
  433. /// @brief load Input and output TensorInfo for Model
  434. /// @return Status run result
  435. ///
  436. Status ModelManager::DataInputTensor(uint32_t model_id, const std::vector<InputTensorInfo> &inputs) {
  437. std::shared_ptr<DavinciModel> model = GetModel(model_id);
  438. auto hybrid_model = GetHybridModel(model_id);
  439. if (hybrid_model == nullptr) {
  440. GE_CHECK_NOTNULL(model);
  441. }
  442. InputData input_data;
  443. input_data.model_id = model_id;
  444. input_data.timeout = 0;
  445. input_data.timestamp = 0;
  446. input_data.index = 0;
  447. for (size_t i = 0; i < inputs.size(); ++i) {
  448. DataBuffer data;
  449. data.data = inputs[i].data;
  450. data.length = inputs[i].length;
  451. input_data.shapes.emplace_back(inputs[i].dims);
  452. input_data.blobs.push_back(data);
  453. }
  454. if (!GetLocalOmgContext().user_input_dims.empty() && GetLocalOmgContext().need_multi_batch) {
  455. std::vector<int32_t> cur_dynamic_dims;
  456. if (!GetLocalOmgContext().user_real_input_dims.empty()) {
  457. if (GetCurDynamicDims(GetLocalOmgContext().user_real_input_dims, GetLocalOmgContext().user_input_dims,
  458. cur_dynamic_dims) != SUCCESS) {
  459. GELOGE(INTERNAL_ERROR, "[Train_Dynamic] Failed to Parse real_dynamic_dims.");
  460. return INTERNAL_ERROR;
  461. }
  462. DataBuffer data;
  463. data.data = new(std::nothrow) int32_t[cur_dynamic_dims.size()];
  464. GE_CHECK_NOTNULL(data.data);
  465. uint32_t length = static_cast<uint32_t>(cur_dynamic_dims.size() * sizeof(int32_t));
  466. GE_CHK_BOOL_EXEC(memcpy_s(data.data, length, cur_dynamic_dims.data(), length) == EOK, return INTERNAL_ERROR,
  467. "Failed to memcpy data.");
  468. data.length = length;
  469. input_data.blobs.push_back(data);
  470. }
  471. }
  472. OutputData output_data;
  473. output_data.model_id = model_id;
  474. output_data.index = 0;
  475. shared_ptr<InputDataWrapper> data_wrap(new (std::nothrow) InputDataWrapper());
  476. GE_CHECK_NOTNULL(data_wrap);
  477. GE_CHK_STATUS_EXEC(data_wrap->Init(input_data, output_data), return domi::PUSH_DATA_FAILED,
  478. "Init InputDataWrapper failed,input data model_id is : %u.", model_id);
  479. if (hybrid_model != nullptr) {
  480. GE_CHK_STATUS_RET(hybrid_model->EnqueueData(data_wrap), "Data queue is full, please call again later, model_id %u ",
  481. model_id);
  482. return SUCCESS;
  483. }
  484. GE_CHK_BOOL_RET_STATUS(model != nullptr, PARAM_INVALID, "Invalid model id %u in InputData! ", model_id);
  485. DataInputer *inputer = model->GetDataInputer();
  486. GE_CHECK_NOTNULL(inputer);
  487. GE_CHK_STATUS_EXEC(inputer->Push(data_wrap), return domi::DATA_QUEUE_ISFULL,
  488. "Data queue is full, please call again later, model_id %u ", model_id);
  489. GELOGD("Data input success, model id:%u", model_id);
  490. return SUCCESS;
  491. }
  492. ///
  493. /// @ingroup domi_ome
  494. /// @brief create model thread, start to execute model
  495. /// @param [in] model_id Model ID to be started
  496. /// @return Status model run result
  497. /// @author
  498. ///
  499. Status ModelManager::Start(uint32_t model_id) {
  500. auto hybrid_model = GetHybridModel(model_id);
  501. if (hybrid_model != nullptr) {
  502. GE_CHK_STATUS_RET_NOLOG(hybrid_model->ModelRunStart());
  503. GELOGI("Start hybrid model %u success.", model_id);
  504. return SUCCESS;
  505. }
  506. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  507. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, PARAM_INVALID, "Invalid model id %u to start! ", model_id);
  508. Status status = davinci_model->ModelRunStart();
  509. if (status == SUCCESS) {
  510. GELOGI("Start model %u success.", model_id);
  511. }
  512. return status;
  513. }
  514. ///
  515. /// @ingroup domi_ome
  516. /// @brief Model ID stop
  517. /// @only when unloaded
  518. /// @param [in] model_id Model ID to be stopped
  519. /// @return Status model stop result
  520. /// @author
  521. ///
  522. Status ModelManager::Stop(uint32_t model_id) {
  523. auto hybrid_model = GetHybridModel(model_id);
  524. if (hybrid_model != nullptr) {
  525. GE_CHK_STATUS_RET_NOLOG(hybrid_model->ModelRunStop());
  526. GELOGI("Stop hybrid model %u success.", model_id);
  527. return SUCCESS;
  528. }
  529. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  530. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, PARAM_INVALID, "Invalid model id %u to stop!", model_id);
  531. Status status = davinci_model->ModelRunStop();
  532. if (status == SUCCESS) {
  533. GELOGI("Stop model %u success.", model_id);
  534. }
  535. return status;
  536. }
  537. ///
  538. /// @ingroup domi_ome
  539. /// @brief Command handle
  540. /// @iterator 1 only Ieference, Debug 2 modes
  541. /// @param [in] command command to handle
  542. /// @return Status command handle result
  543. /// @author
  544. ///
  545. Status ModelManager::HandleCommand(const Command &command) {
  546. static const std::map<std::string, std::function<uint32_t(const Command &)>> cmds = {
  547. {kCmdTypeDump, HandleDumpCommand}, {kCmdTypeProfInit, HandleProfInitCommand},
  548. {kCmdTypeProfFinalize, HandleProfFinalizeCommand}, {kCmdTypeProfStart, HandleProfStartCommand},
  549. {kCmdTypeProfStop, HandleProfStopCommand},
  550. {kCmdTypeProfModelSubscribe, HandleProfModelSubscribeCommand},
  551. {kCmdTypeProfModelUnsubscribe, HandleProfModelUnsubscribeCommand}};
  552. auto iter = cmds.find(command.cmd_type);
  553. if (iter == cmds.end()) {
  554. GELOGE(PARAM_INVALID, "Unsupported command: %s", command.cmd_type.c_str());
  555. return PARAM_INVALID;
  556. } else {
  557. return iter->second(command);
  558. }
  559. }
  560. Status ModelManager::GetModelByCmd(const Command &command,
  561. std::shared_ptr<DavinciModel> &davinci_model) {
  562. if (command.cmd_params.size() < kCmdParSize) {
  563. GELOGE(PARAM_INVALID, "When the cmd_type is '%s', the size of cmd_params must larger than 2.",
  564. command.cmd_type.c_str());
  565. return PARAM_INVALID;
  566. }
  567. std::string map_key = command.cmd_params[0];
  568. std::string value = command.cmd_params[1];
  569. if (map_key == PROFILE_MODEL_ID) {
  570. int32_t model_id = 0;
  571. try {
  572. model_id = std::stoi(value);
  573. } catch (std::invalid_argument &) {
  574. GELOGE(PARAM_INVALID, "Model id: %s is invalid.", value.c_str());
  575. return PARAM_INVALID;
  576. } catch (std::out_of_range &) {
  577. GELOGE(PARAM_INVALID, "Model id: %s is out of range.", value.c_str());
  578. return PARAM_INVALID;
  579. } catch (...) {
  580. GELOGE(FAILED, "Model id: %s cannot change to int.", value.c_str());
  581. return FAILED;
  582. }
  583. auto model_manager = ModelManager::GetInstance();
  584. GE_CHECK_NOTNULL(model_manager);
  585. davinci_model = model_manager->GetModel(static_cast<uint32_t>(model_id));
  586. if (davinci_model == nullptr) {
  587. GELOGE(FAILED, "Model id: %d is invaild or model is not loaded.", model_id);
  588. return FAILED;
  589. }
  590. } else {
  591. GELOGE(FAILED, "The model_id parameter is not found in the command.");
  592. return FAILED;
  593. }
  594. return SUCCESS;
  595. }
  596. Status ModelManager::HandleProfModelSubscribeCommand(const Command &command) {
  597. std::shared_ptr<DavinciModel> davinci_model = nullptr;
  598. Status ret = GetModelByCmd(command, davinci_model);
  599. if (ret != SUCCESS) {
  600. return ret;
  601. }
  602. if (ProfilingManager::Instance().ProfModelSubscribe(command.module_index,
  603. static_cast<void *>(davinci_model.get())) != SUCCESS) {
  604. GELOGE(FAILED, "Handle prof model subscribe failed.");
  605. return FAILED;
  606. }
  607. return SUCCESS;
  608. }
  609. Status ModelManager::HandleProfModelUnsubscribeCommand(const Command &command) {
  610. std::shared_ptr<DavinciModel> davinci_model = nullptr;
  611. Status ret = GetModelByCmd(command, davinci_model);
  612. if (ret != SUCCESS) {
  613. return ret;
  614. }
  615. if (ProfilingManager::Instance().ProfModelUnsubscribe(static_cast<void *>(davinci_model.get())) != SUCCESS) {
  616. GELOGE(FAILED, "Handle prof model unsubscribe failed.");
  617. return FAILED;
  618. }
  619. return SUCCESS;
  620. }
  621. Status ModelManager::HandleProfInitCommand(const Command &command) {
  622. uint64_t module_index = command.module_index;
  623. if (ProfilingManager::Instance().ProfInit(module_index) != SUCCESS) {
  624. GELOGE(FAILED, "Handle prof init failed.");
  625. return FAILED;
  626. }
  627. return SUCCESS;
  628. }
  629. Status ModelManager::HandleProfFinalizeCommand(const Command &command) {
  630. if (ProfilingManager::Instance().ProfFinalize() != SUCCESS) {
  631. GELOGE(FAILED, "Handle prof finalize failed.");
  632. return FAILED;
  633. }
  634. return SUCCESS;
  635. }
  636. /*
  637. * cmd para when prof start
  638. * "devNums:2"
  639. * "devIdList:1,2"
  640. * "profilingOption:PROF_OP_TRACE"
  641. * "aicoreMetrics:AICORE_ARITHMATIC_THROUGHPUT"
  642. */
  643. Status ModelManager::HandleProfStartCommand(const Command &command) {
  644. if (command.cmd_params.size() < kProfStartCmdParaSize) {
  645. GELOGE(PARAM_INVALID, "When the cmd_type is 'profile start', the size of cmd_params must larger than 2.");
  646. return PARAM_INVALID;
  647. }
  648. if (command.cmd_params.size() > kProfCmdParaMaxSize) {
  649. GELOGE(PARAM_INVALID, "Command para size[%zu] larger than max[1000].", command.cmd_params.size());
  650. return PARAM_INVALID;
  651. }
  652. std::map<std::string, std::string> cmd_params_map;
  653. uint32_t step = 2;
  654. for (uint32_t i = 0; i < command.cmd_params.size(); i += step) {
  655. if (i + 1 >= command.cmd_params.size()) {
  656. continue;
  657. }
  658. cmd_params_map[command.cmd_params[i]] = command.cmd_params[i + 1];
  659. }
  660. uint64_t module_index = command.module_index;
  661. if (ProfilingManager::Instance().ProfStartProfiling(module_index, cmd_params_map) != SUCCESS) {
  662. GELOGE(FAILED, "Handle prof start failed.");
  663. return FAILED;
  664. }
  665. return SUCCESS;
  666. }
  667. Status ModelManager::HandleProfStopCommand(const Command &command) {
  668. if (command.cmd_params.size() < kProfStartCmdParaSize) {
  669. GELOGE(PARAM_INVALID, "When the cmd_type is 'profile stop', the size of cmd_params must larger than 2.");
  670. return PARAM_INVALID;
  671. }
  672. if (command.cmd_params.size() > kProfCmdParaMaxSize) {
  673. GELOGE(PARAM_INVALID, "Command para size[%zu] larger than max[1000].", command.cmd_params.size());
  674. return PARAM_INVALID;
  675. }
  676. std::map<std::string, std::string> cmd_params_map;
  677. uint32_t step = 2;
  678. for (uint32_t i = 0; i < command.cmd_params.size(); i += step) {
  679. if (i + 1 >= command.cmd_params.size()) {
  680. continue;
  681. }
  682. cmd_params_map[command.cmd_params[i]] = command.cmd_params[i + 1];
  683. }
  684. uint64_t module_index = command.module_index;
  685. if (ProfilingManager::Instance().ProfStopProfiling(module_index, cmd_params_map) != SUCCESS) {
  686. GELOGE(FAILED, "Handle prof finalize failed.");
  687. return FAILED;
  688. }
  689. return SUCCESS;
  690. }
  691. static Status ParserPara(const Command &command, const string &dump_key, string &dump_value) {
  692. auto iter = std::find(command.cmd_params.begin(), command.cmd_params.end(), dump_key);
  693. if (iter != command.cmd_params.end()) {
  694. ++iter;
  695. if (iter == command.cmd_params.end()) {
  696. GELOGE(PARAM_INVALID, "Invalid access.");
  697. return PARAM_INVALID;
  698. }
  699. dump_value = *iter;
  700. }
  701. return SUCCESS;
  702. }
  703. Status ModelManager::HandleDumpCommand(const Command &command) {
  704. if (command.cmd_params.size() % kDumpCmdPairSize != 0) {
  705. GELOGE(PARAM_INVALID, "When the cmd_type is 'dump', the size of cmd_params must be a even number.");
  706. return PARAM_INVALID;
  707. }
  708. std::string dump_status("off");
  709. std::string dump_model(DUMP_ALL_MODEL);
  710. std::string dump_path("/");
  711. std::string dump_mode("output");
  712. std::set<std::string> dump_layers;
  713. auto ret = ParserPara(command, DUMP_STATUS, dump_status);
  714. if (ret != SUCCESS) {
  715. GELOGE(PARAM_INVALID, "parser dump status failed");
  716. return FAILED;
  717. }
  718. GELOGI("dump status = %s.", dump_status.c_str());
  719. ret = ParserPara(command, DUMP_MODEL, dump_model);
  720. if (ret != SUCCESS) {
  721. GELOGE(PARAM_INVALID, "parser dump model failed");
  722. return FAILED;
  723. }
  724. GELOGI("dump model = %s.", dump_model.c_str());
  725. if (dump_status == "off" || dump_status == "OFF") {
  726. dump_properties_.DeletePropertyValue(dump_model);
  727. return SUCCESS;
  728. }
  729. for (size_t i = 0; i < command.cmd_params.size() / kDumpCmdPairSize; ++i) {
  730. if (command.cmd_params.at(i * kDumpCmdPairSize).find(DUMP_LAYER) != std::string::npos) {
  731. GELOGI("dump layer: %s.", command.cmd_params.at(i * kDumpCmdPairSize + 1).c_str());
  732. dump_layers.insert(command.cmd_params.at(i * kDumpCmdPairSize + 1));
  733. }
  734. }
  735. ret = ParserPara(command, DUMP_FILE_PATH, dump_path);
  736. if (ret != SUCCESS) {
  737. GELOGE(PARAM_INVALID, "parser dump path failed");
  738. return FAILED;
  739. }
  740. if (!dump_path.empty() && dump_path[dump_path.size() - 1] != '/') {
  741. dump_path = dump_path + "/";
  742. }
  743. dump_path = dump_path + CurrentTimeInStr() + "/";
  744. GELOGI("dump path = %s.", dump_path.c_str());
  745. ret = ParserPara(command, DUMP_MODE, dump_mode);
  746. if (ret != SUCCESS) {
  747. GELOGE(PARAM_INVALID, "parser dump mode failed");
  748. return FAILED;
  749. }
  750. GELOGI("dump mode = %s", dump_mode.c_str());
  751. dump_properties_.AddPropertyValue(dump_model, dump_layers);
  752. dump_properties_.SetDumpPath(dump_path);
  753. dump_properties_.SetDumpMode(dump_mode);
  754. return SUCCESS;
  755. }
  756. Status ModelManager::GetMaxUsedMemory(const uint32_t model_id, uint64_t &max_size) {
  757. auto hybrid_model = GetHybridModel(model_id);
  758. if (hybrid_model != nullptr) {
  759. max_size = 0;
  760. return SUCCESS;
  761. }
  762. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  763. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, PARAM_INVALID, "GetMaxUsedMemory Failed, Invalid model id %u!",
  764. model_id);
  765. max_size = davinci_model->TotalMemSize();
  766. return SUCCESS;
  767. }
  768. Status ModelManager::GetInputOutputDescInfo(const uint32_t model_id, vector<InputOutputDescInfo> &input_desc,
  769. vector<InputOutputDescInfo> &output_desc) {
  770. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  771. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, PARAM_INVALID, "GetInputOutputDescInfo Failed, Invalid model id %u!",
  772. model_id);
  773. return davinci_model->GetInputOutputDescInfo(input_desc, output_desc);
  774. }
  775. Status ModelManager::GetInputOutputDescInfo(const uint32_t model_id, vector<InputOutputDescInfo> &input_desc,
  776. vector<InputOutputDescInfo> &output_desc,
  777. std::vector<uint32_t> &inputFormats, std::vector<uint32_t> &outputFormats,
  778. bool new_model_desc) {
  779. std::shared_ptr<hybrid::HybridDavinciModel> hybrid_davinci_model = GetHybridModel(model_id);
  780. if (hybrid_davinci_model != nullptr) {
  781. hybrid_davinci_model->SetModelDescVersion(new_model_desc);
  782. return hybrid_davinci_model->GetInputOutputDescInfo(input_desc, output_desc, inputFormats, outputFormats);
  783. }
  784. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  785. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, ACL_ERROR_GE_EXEC_MODEL_ID_INVALID,
  786. "GetInputOutputDescInfo Failed, Invalid model id %u!", model_id);
  787. return davinci_model->GetInputOutputDescInfo(input_desc, output_desc, inputFormats, outputFormats, new_model_desc);
  788. }
  789. ///
  790. /// @ingroup ge
  791. /// @brief Get dynamic batch_info
  792. /// @param [in] model_id
  793. /// @param [out] batch_info
  794. /// @return execute result
  795. ///
  796. Status ModelManager::GetDynamicBatchInfo(const uint32_t model_id, std::vector<std::vector<int64_t>> &batch_info,
  797. int32_t &dynamic_type) {
  798. std::shared_ptr<hybrid::HybridDavinciModel> hybrid_davinci_model = GetHybridModel(model_id);
  799. if (hybrid_davinci_model != nullptr) {
  800. return hybrid_davinci_model->GetDynamicBatchInfo(batch_info, dynamic_type);
  801. }
  802. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  803. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, ACL_ERROR_GE_EXEC_MODEL_ID_INVALID,
  804. "GetDynamicBatchInfo failed, Invalid model id %u!", model_id);
  805. return davinci_model->GetDynamicBatchInfo(batch_info, dynamic_type);
  806. }
  807. ///
  808. /// @ingroup ge
  809. /// @brief Get combined dynamic dims info
  810. /// @param [in] model_id
  811. /// @param [out] batch_info
  812. /// @return execute result
  813. ///
  814. Status ModelManager::GetCombinedDynamicDims(const uint32_t model_id, vector<vector<int64_t>> &batch_info) {
  815. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  816. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, ACL_ERROR_GE_EXEC_MODEL_ID_INVALID,
  817. "GetCombinedDynamicDims Failed, Invalid Model ID %u!", model_id);
  818. davinci_model->GetCombinedDynamicDims(batch_info);
  819. return SUCCESS;
  820. }
  821. ///
  822. /// @ingroup ge
  823. /// @brief Get user designate shape order
  824. /// @param [in] model_id
  825. /// @param [out] user_input_shape_order
  826. /// @return execute result
  827. ///
  828. Status ModelManager::GetUserDesignateShapeOrder(const uint32_t model_id,
  829. std::vector<std::string> &user_input_shape_order) {
  830. auto hybrid_davinci_model = GetHybridModel(model_id);
  831. if (hybrid_davinci_model != nullptr) {
  832. hybrid_davinci_model->GetUserDesignateShapeOrder(user_input_shape_order);
  833. return SUCCESS;
  834. }
  835. auto davinci_model = GetModel(model_id);
  836. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, ACL_ERROR_GE_EXEC_MODEL_ID_INVALID,
  837. "GetUserDesignateShapeOrder Failed, Invalid Model ID %u!", model_id)
  838. davinci_model->GetUserDesignateShapeOrder(user_input_shape_order);
  839. return SUCCESS;
  840. }
  841. Status ModelManager::GetCurShape(const uint32_t model_id, std::vector<int64_t> &batch_info, int32_t &dynamic_type) {
  842. auto davinci_model = GetModel(model_id);
  843. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, ACL_ERROR_GE_EXEC_MODEL_ID_INVALID,
  844. "GetCurShape Failed, Invalid Model ID %u!", model_id);
  845. davinci_model->GetCurShape(batch_info, dynamic_type);
  846. return SUCCESS;
  847. }
  848. Status ModelManager::GetModelAttr(uint32_t model_id, std::vector<string> &dynamic_output_shape_info) {
  849. std::shared_ptr<hybrid::HybridDavinciModel> hybrid_davinci_model = GetHybridModel(model_id);
  850. if (hybrid_davinci_model != nullptr) {
  851. hybrid_davinci_model->GetModelAttr(dynamic_output_shape_info);
  852. return SUCCESS;
  853. }
  854. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  855. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, ACL_ERROR_GE_EXEC_MODEL_ID_INVALID,
  856. "GetModelAttr Failed, Invalid Model ID %u!", model_id);
  857. davinci_model->GetModelAttr(dynamic_output_shape_info);
  858. return SUCCESS;
  859. }
  860. ///
  861. /// @ingroup ge
  862. /// @brief Get AIPP info
  863. /// @param [in] model_id
  864. /// @param [in] index
  865. /// @param [out] aipp_info
  866. /// @return execute result
  867. ///
  868. Status ModelManager::GetAippInfo(const uint32_t model_id, uint32_t index, AippConfigInfo &aipp_info) {
  869. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  870. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, ACL_ERROR_GE_EXEC_MODEL_ID_INVALID,
  871. "GetAIPPInfo failed, invalid model_id is %u.", model_id);
  872. return davinci_model->GetAippInfo(index, aipp_info);
  873. }
  874. Status ModelManager::GetAippType(uint32_t model_id, uint32_t index, InputAippType &type, size_t &aipp_index) {
  875. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  876. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, ACL_ERROR_GE_EXEC_MODEL_ID_INVALID,
  877. "GetAIPPInfo failed, invalid model_id is %u.", model_id);
  878. return davinci_model->GetAippType(index, type, aipp_index);
  879. }
  880. Status ModelManager::GenSessionId(uint64_t &session_id) {
  881. const uint64_t kSessionTimeMask = 0xffffffffffff0000;
  882. const uint64_t kSessionPidMask = 0x000000000000ff00;
  883. const uint64_t kSessionBiasMask = 0x00000000000000ff;
  884. const uint64_t kMaskPerOffset = 8;
  885. std::lock_guard<std::mutex> lock(session_id_create_mutex_);
  886. mmTimeval tv;
  887. if (mmGetTimeOfDay(&tv, nullptr) != 0) {
  888. GELOGE(INTERNAL_ERROR, "Failed to get current time.");
  889. return INTERNAL_ERROR;
  890. }
  891. uint64_t timestamp = static_cast<uint64_t>(tv.tv_sec * kTimeSpecMiro + tv.tv_usec); // 1000000us
  892. static uint32_t pid = mmGetPid();
  893. session_id_bias_++;
  894. session_id = ((timestamp<<kMaskPerOffset<<kMaskPerOffset) & kSessionTimeMask) +
  895. ((pid<<kMaskPerOffset) & kSessionPidMask) + (session_id_bias_ & kSessionBiasMask);
  896. GELOGD("Generate new session id: %lu.", session_id);
  897. return SUCCESS;
  898. }
  899. Status ModelManager::LoadModelOffline(uint32_t &model_id, const ModelData &model, shared_ptr<ModelListener> listener,
  900. void *dev_ptr, size_t mem_size, void *weight_ptr, size_t weight_size) {
  901. GE_CHK_BOOL_RET_STATUS(model.key.empty() || mmAccess2(model.key.c_str(), M_F_OK) == EN_OK,
  902. ACL_ERROR_GE_PARAM_INVALID, "Input key file path %s is invalid, %s", model.key.c_str(), strerror(errno));
  903. GenModelId(&model_id);
  904. mmTimespec timespec = mmGetTickCount();
  905. ModelHelper model_helper;
  906. Status ret = model_helper.LoadRootModel(model);
  907. if (ret != SUCCESS) {
  908. GELOGE(ret, "load model failed.");
  909. return ret;
  910. }
  911. if (model_helper.GetModelType()) {
  912. bool is_shape_unknown = false;
  913. GE_CHK_STATUS_RET(model_helper.GetGeRootModel()->CheckIsUnknownShape(is_shape_unknown),
  914. "CheckIsUnknownShape failed, model id:%u", model_id);
  915. if (is_shape_unknown || GetContext().GetHostExecFlag()) {
  916. return DoLoadHybridModelOnline(model_id, model.om_name, model_helper.GetGeRootModel(), listener);
  917. }
  918. }
  919. do {
  920. GeModelPtr ge_model = model_helper.GetGeModel();
  921. shared_ptr<DavinciModel> davinci_model = MakeShared<DavinciModel>(model.priority, listener);
  922. if (davinci_model == nullptr) {
  923. GELOGE(ACL_ERROR_GE_MEMORY_ALLOCATION, "Make shared failed");
  924. return ACL_ERROR_GE_MEMORY_ALLOCATION;
  925. }
  926. davinci_model->SetProfileTime(MODEL_LOAD_START, (timespec.tv_sec * kTimeSpecNano +
  927. timespec.tv_nsec)); // 1000 ^ 3 converts second to nanosecond
  928. ret = davinci_model->Assign(ge_model);
  929. if (ret != SUCCESS) {
  930. GELOGW("assign model failed.");
  931. break;
  932. }
  933. davinci_model->SetId(model_id);
  934. int32_t device_id = 0;
  935. rtError_t rt_ret = rtGetDevice(&device_id);
  936. if (rt_ret != RT_ERROR_NONE || device_id < 0) {
  937. GELOGE(rt_ret, "Call rtGetDevice failed, ret = 0x%X, device_id = %d.", rt_ret, device_id);
  938. return RT_ERROR_TO_GE_STATUS(rt_ret);
  939. }
  940. davinci_model->SetDeviceId(device_id);
  941. davinci_model->SetOmName(model.om_name);
  942. if (DumpManager::GetInstance().GetDumpProperties(kInferSessionId).IsDumpOpen()) {
  943. davinci_model->SetDumpProperties(DumpManager::GetInstance().GetDumpProperties(kInferSessionId));
  944. } else {
  945. davinci_model->SetDumpProperties(dump_properties_);
  946. }
  947. /// In multi-threaded inference, using the same session_id among multiple threads may cause some threads to fail.
  948. /// These session_ids come from the same model, so the values of session_id are the same.
  949. /// Update session_id for infer in load model to avoid the same session_id.
  950. uint64_t new_session_id;
  951. ret = GenSessionId(new_session_id);
  952. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(ret != SUCCESS, break, "Generate session_id for inference failed.");
  953. ret = davinci_model->UpdateSessionId(new_session_id);
  954. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(ret != SUCCESS, break, "Update session_id for inference failed.");
  955. ret = davinci_model->Init(dev_ptr, mem_size, weight_ptr, weight_size);
  956. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(ret != SUCCESS, break, "DavinciInit failed.");
  957. InsertModel(model_id, davinci_model);
  958. GELOGI("Parse model %u success.", model_id);
  959. GE_IF_BOOL_EXEC(ret == SUCCESS, device_count++);
  960. } while (0);
  961. return ret;
  962. }
  963. ///
  964. /// @ingroup ge
  965. /// @brief ACL case, Load task list with queue.
  966. /// @param [out] model_id: model id for manager.
  967. /// @param [in] model_data: Model data load from offline model file.
  968. /// @param [in] input_que_ids: input queue ids from user, num equals Data Op.
  969. /// @param [in] output_que_ids: input queue ids from user, num equals NetOutput Op.
  970. /// @return: 0 for success / others for fail
  971. ///
  972. Status ModelManager::LoadModelWithQ(uint32_t &model_id, const ModelData &model_data,
  973. const std::vector<uint32_t> &input_queue_ids,
  974. const std::vector<uint32_t> &output_queue_ids) {
  975. GE_CHK_BOOL_RET_STATUS(model_data.key.empty() || mmAccess2(model_data.key.c_str(), M_F_OK) == EN_OK,
  976. ACL_ERROR_GE_PARAM_INVALID, "input key file path %s is not valid, %s",
  977. model_data.key.c_str(), strerror(errno));
  978. ModelHelper model_helper;
  979. Status ret = model_helper.LoadModel(model_data);
  980. if (ret != SUCCESS) {
  981. GELOGE(ret, "load model failed.");
  982. return ret;
  983. }
  984. shared_ptr<DavinciModel> davinci_model = MakeShared<DavinciModel>(model_data.priority, nullptr);
  985. if (davinci_model == nullptr) {
  986. GELOGE(ACL_ERROR_GE_MEMORY_ALLOCATION, "create model failed.");
  987. return ACL_ERROR_GE_MEMORY_ALLOCATION;
  988. }
  989. ret = davinci_model->Assign(model_helper.GetGeModel());
  990. if (ret != SUCCESS) {
  991. GELOGE(ret, "assign model failed.");
  992. return ret;
  993. }
  994. /// In multi-threaded inference, using the same session_id among multiple threads may cause some threads to fail.
  995. /// These session_ids come from the same model, so the values of session_id are the same.
  996. /// Update session_id for infer in load model to avoid the same session_id.
  997. uint64_t new_session_id;
  998. ret = GenSessionId(new_session_id);
  999. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(ret != SUCCESS, return ret, "Generate session_id for infer failed.");
  1000. ret = davinci_model->UpdateSessionId(new_session_id);
  1001. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(ret != SUCCESS, return ret, "Update session_id for infer failed.");
  1002. GenModelId(&model_id);
  1003. davinci_model->SetId(model_id);
  1004. ret = davinci_model->SetQueIds(input_queue_ids, output_queue_ids);
  1005. if (ret != SUCCESS) {
  1006. GELOGE(ret, "set model queue ids failed.");
  1007. return ret;
  1008. }
  1009. davinci_model->SetDumpProperties(dump_properties_);
  1010. ret = davinci_model->Init();
  1011. if (ret != SUCCESS) {
  1012. GELOGE(ret, "init model failed.");
  1013. return ret;
  1014. }
  1015. InsertModel(model_id, davinci_model);
  1016. GELOGI("Parse model %u success.", model_id);
  1017. return SUCCESS;
  1018. }
  1019. ///
  1020. /// @ingroup domi_ome
  1021. /// @brief ACL case, not start new thread, return result
  1022. /// @param [in] model_id mode id
  1023. /// @param [in] stream model stream
  1024. /// @param [in] async_mode is asynchronize mode.
  1025. /// @param [in] input_data input data
  1026. /// @param [in] input_desc description of input data
  1027. /// @param [out] output_data output data
  1028. /// @param [out] output_desc description of output data
  1029. ///
  1030. Status ModelManager::ExecuteModel(uint32_t model_id, rtStream_t stream, bool async_mode, const InputData &input_data,
  1031. const std::vector<GeTensorDesc> &input_desc, OutputData &output_data,
  1032. std::vector<GeTensorDesc> &output_desc) {
  1033. std::shared_ptr<hybrid::HybridDavinciModel> hybrid_davinci_model = GetHybridModel(model_id);
  1034. if (hybrid_davinci_model != nullptr) {
  1035. auto inputs = input_data.blobs;
  1036. auto outputs = output_data.blobs;
  1037. Status status = hybrid_davinci_model->Execute(inputs, input_desc, outputs, output_desc, stream);
  1038. if (status == SUCCESS) {
  1039. GELOGI("Execute model %u success.", model_id);
  1040. }
  1041. return status;
  1042. }
  1043. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  1044. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, ACL_ERROR_GE_EXEC_MODEL_ID_INVALID,
  1045. "Invalid model id %u, check whether model has been loaded or not.", model_id);
  1046. if (davinci_model->NeedDestroyAicpuKernel()) {
  1047. GELOGI("Start to destroy specified aicpu kernel.");
  1048. // Zero copy is enabled by default, no need to judge.
  1049. uint64_t session_id_davinci = davinci_model->GetSessionId();
  1050. uint32_t model_id_davinci = davinci_model->GetModelId();
  1051. uint32_t sub_model_id = davinci_model->SubModelId();
  1052. Status status = DestroyAicpuKernel(session_id_davinci, model_id_davinci, sub_model_id);
  1053. if (status != SUCCESS) {
  1054. GELOGW("Destroy specified aicpu kernel failed, session id is %lu, model id is %u.", session_id_davinci,
  1055. model_id_davinci);
  1056. }
  1057. }
  1058. Status status = davinci_model->NnExecute(stream, async_mode, input_data, output_data);
  1059. if (status == SUCCESS) {
  1060. GELOGD("Execute model %u success.", model_id);
  1061. }
  1062. return status;
  1063. }
  1064. Status ModelManager::CreateAicpuSession(uint64_t session_id) {
  1065. std::lock_guard<std::recursive_mutex> lock(map_mutex_);
  1066. auto it = sess_ids_.find(session_id);
  1067. // never been created by any model
  1068. if (it == sess_ids_.end()) {
  1069. Status ret = KernelLaunchEx(aicpu::FWKAdapter::FWKOperateType::FWK_ADPT_SESSION_CREATE, session_id, 0, 0);
  1070. if (ret == SUCCESS) {
  1071. (void)sess_ids_.insert(session_id);
  1072. GELOGI("The session: %lu create success.", session_id);
  1073. }
  1074. return ret;
  1075. }
  1076. return SUCCESS;
  1077. }
  1078. Status ModelManager::LoadCustAicpuSo(const OpDescPtr &op_desc, const string &so_name, bool &loaded) {
  1079. GELOGD("LoadCustAicpuSo in, op name %s, so name %s", op_desc->GetName().c_str(), so_name.c_str());
  1080. std::lock_guard<std::mutex> lock(cust_aicpu_mutex_);
  1081. CustAICPUKernelPtr aicpu_kernel = op_desc->TryGetExtAttr(OP_EXTATTR_CUSTAICPU_KERNEL, CustAICPUKernelPtr());
  1082. if (aicpu_kernel == nullptr) {
  1083. GELOGI("cust aicpu op %s has no corresponding kernel!", op_desc->GetName().c_str());
  1084. return SUCCESS;
  1085. }
  1086. // get current context
  1087. rtContext_t rt_cur_ctx = nullptr;
  1088. auto rt_error = rtCtxGetCurrent(&rt_cur_ctx);
  1089. if (rt_error != RT_ERROR_NONE) {
  1090. GELOGE(RT_FAILED, "get current context failed, runtime result is %d", static_cast<int>(rt_error));
  1091. return RT_FAILED;
  1092. }
  1093. // use current context as resource key
  1094. uintptr_t resource_id = reinterpret_cast<uintptr_t>(rt_cur_ctx);
  1095. auto it = cust_aicpu_so_.find(resource_id);
  1096. if (it == cust_aicpu_so_.end()) {
  1097. std::map<string, CustAICPUKernelPtr> new_so_name;
  1098. new_so_name.insert({so_name, aicpu_kernel});
  1099. cust_aicpu_so_[resource_id] = new_so_name;
  1100. loaded = false;
  1101. GELOGD("LoadCustAicpuSo new aicpu so name %s, resource id %lu", so_name.c_str(), resource_id);
  1102. return SUCCESS;
  1103. }
  1104. auto it_so_name = it->second.find(so_name);
  1105. if (it_so_name == it->second.end()) {
  1106. it->second.insert({so_name, aicpu_kernel});
  1107. loaded = false;
  1108. GELOGD("LoadCustAicpuSo add aicpu so name %s, resource id %lu", so_name.c_str(), resource_id);
  1109. return SUCCESS;
  1110. }
  1111. loaded = true;
  1112. GELOGD("LoadCustAicpuSo so name %s has been loaded.", so_name.c_str());
  1113. return SUCCESS;
  1114. }
  1115. Status ModelManager::LaunchKernelCustAicpuSo(const string &kernel_name) {
  1116. GELOGD("Aicpu kernel launch task in, kernel name %s.", kernel_name.c_str());
  1117. std::lock_guard<std::mutex> lock(cust_aicpu_mutex_);
  1118. if (cust_aicpu_so_.size() == 0) return SUCCESS;
  1119. // get current context
  1120. rtContext_t rt_cur_ctx = nullptr;
  1121. auto rt_error = rtCtxGetCurrent(&rt_cur_ctx);
  1122. if (rt_error != RT_ERROR_NONE) {
  1123. GELOGE(RT_FAILED, "get current context failed, runtime result is %d", static_cast<int>(rt_error));
  1124. return RT_FAILED;
  1125. }
  1126. uintptr_t resource_id = reinterpret_cast<uintptr_t>(rt_cur_ctx);
  1127. auto it = cust_aicpu_so_.find(resource_id);
  1128. if (it == cust_aicpu_so_.end()) {
  1129. GELOGI("Cust aicpu so map is empty, context id %lu", resource_id);
  1130. return SUCCESS;
  1131. }
  1132. vector<void *> allocated_mem;
  1133. rtError_t status;
  1134. rtStream_t stream = nullptr;
  1135. vector<CustAicpuSoBuf> v_cust_so;
  1136. void *args = nullptr;
  1137. for (const auto &it_so : it->second) {
  1138. const void *aicpu_data = it_so.second->GetBinData();
  1139. uint32_t aicpu_data_length = it_so.second->GetBinDataSize();
  1140. string so_name = it_so.first;
  1141. void *d_aicpu_data = nullptr;
  1142. void *d_so_name = nullptr;
  1143. status = rtMalloc(&d_aicpu_data, aicpu_data_length, RT_MEMORY_HBM);
  1144. if (status != RT_ERROR_NONE) {
  1145. GELOGE(RT_FAILED, "Call rt failed, status: 0x%x", status);
  1146. return RT_ERROR_TO_GE_STATUS(status);
  1147. }
  1148. allocated_mem.push_back(d_aicpu_data);
  1149. status = rtMalloc(&d_so_name, so_name.size(), RT_MEMORY_HBM);
  1150. if (status != RT_ERROR_NONE) {
  1151. GELOGE(RT_FAILED, "Call rt failed, status: 0x%x", status);
  1152. return RT_ERROR_TO_GE_STATUS(status);
  1153. }
  1154. allocated_mem.push_back(d_so_name);
  1155. GE_CHK_RT(rtMemcpy(d_aicpu_data, aicpu_data_length, aicpu_data, aicpu_data_length, RT_MEMCPY_HOST_TO_DEVICE));
  1156. GE_CHK_RT(rtMemcpy(d_so_name, so_name.size(), reinterpret_cast<const void *>(so_name.c_str()),
  1157. so_name.size(), RT_MEMCPY_HOST_TO_DEVICE));
  1158. CustAicpuSoBuf cust_aicpu_so_buf;
  1159. cust_aicpu_so_buf.kernelSoBuf = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(d_aicpu_data));
  1160. cust_aicpu_so_buf.kernelSoBufLen = aicpu_data_length;
  1161. cust_aicpu_so_buf.kernelSoName = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(d_so_name));
  1162. cust_aicpu_so_buf.kernelSoNameLen = so_name.size();
  1163. v_cust_so.push_back(cust_aicpu_so_buf);
  1164. }
  1165. if (kernel_name == kDeleteCustOp) {
  1166. (void)cust_aicpu_so_.erase(it);
  1167. }
  1168. uint32_t args_size = sizeof(CustAicpuSoBuf) * v_cust_so.size();
  1169. status = rtMalloc(&args, args_size, RT_MEMORY_HBM);
  1170. if (status != RT_ERROR_NONE) {
  1171. GELOGE(RT_FAILED, "Call rt failed, status: 0x%x", status);
  1172. return RT_ERROR_TO_GE_STATUS(status);
  1173. }
  1174. allocated_mem.push_back(args);
  1175. GE_CHK_RT(rtMemcpy(args, args_size, v_cust_so.data(), args_size, RT_MEMCPY_HOST_TO_DEVICE));
  1176. BatchLoadOpFromBufArgs batch_cust_so;
  1177. batch_cust_so.soNum = v_cust_so.size();
  1178. batch_cust_so.args = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(args));
  1179. void *batch_args = nullptr;
  1180. uint32_t batch_args_size = sizeof(BatchLoadOpFromBufArgs);
  1181. status = rtMalloc(&batch_args, batch_args_size, RT_MEMORY_HBM);
  1182. if (status != RT_ERROR_NONE) {
  1183. GELOGE(RT_FAILED, "Call rt failed, status: 0x%x", status);
  1184. return RT_ERROR_TO_GE_STATUS(status);
  1185. }
  1186. allocated_mem.push_back(batch_args);
  1187. GE_CHK_RT(rtMemcpy(batch_args, batch_args_size, static_cast<void *>(&batch_cust_so),
  1188. batch_args_size, RT_MEMCPY_HOST_TO_DEVICE));
  1189. GE_CHK_RT(rtStreamCreate(&stream, 0));
  1190. GE_CHK_RT(rtCpuKernelLaunch(nullptr, kernel_name.c_str(), 1, batch_args, batch_args_size, nullptr, stream));
  1191. status = rtStreamSynchronize(stream);
  1192. if (status != RT_ERROR_NONE) {
  1193. GELOGE(RT_FAILED, "Call rt stream sync failed, status: 0x%x", status);
  1194. return RT_ERROR_TO_GE_STATUS(status);
  1195. }
  1196. std::function<void()> callback = [&]() {
  1197. for (auto mem : allocated_mem) {
  1198. GE_CHK_RT(rtFree(mem));
  1199. }
  1200. GE_CHK_RT(rtStreamDestroy(stream));
  1201. };
  1202. GE_MAKE_GUARD(release, callback);
  1203. GELOGI("Cpu kernel launch task success.");
  1204. return SUCCESS;
  1205. }
  1206. Status ModelManager::ClearAicpuSo() {
  1207. GE_CHK_STATUS_RET(LaunchKernelCustAicpuSo(kDeleteCustOp), "delete cust op so failed.");
  1208. return SUCCESS;
  1209. }
  1210. Status ModelManager::LaunchCustAicpuSo() {
  1211. GE_CHK_STATUS_RET(LaunchKernelCustAicpuSo(kBatchLoadBuf), "launch cust op so failed.");
  1212. return SUCCESS;
  1213. }
  1214. ///
  1215. /// @ingroup ge
  1216. /// @brief get model memory size and weight
  1217. /// @param [in] const ModelData model: model type
  1218. /// @param [out] size_t memSize: model memory usage
  1219. /// size_t weightSize: model weight and memory size
  1220. /// @return SUCCESS success / others failure
  1221. ///
  1222. Status ModelManager::GetModelMemAndWeightSize(const ModelData &model, size_t &mem_size, size_t &weight_size) {
  1223. uint8_t *model_data = nullptr;
  1224. uint32_t model_len = 0;
  1225. Status ret = ModelParserBase::ParseModelContent(model, model_data, model_len);
  1226. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(ret != SUCCESS, return ACL_ERROR_GE_PARAM_INVALID, "parse model content failed!");
  1227. OmFileLoadHelper om_file_helper;
  1228. ret = om_file_helper.Init(model_data, model_len);
  1229. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(ret != SUCCESS, return ret, "om file helperInit failed!");
  1230. auto partition_table = reinterpret_cast<ModelPartitionTable *>(model_data);
  1231. if (partition_table->num == 1) {
  1232. GELOGE(ACL_ERROR_GE_PARAM_INVALID, "om model is error,please use executable om model");
  1233. return ACL_ERROR_GE_PARAM_INVALID;
  1234. }
  1235. ModelPartition task_partition;
  1236. if (om_file_helper.GetModelPartition(ModelPartitionType::TASK_INFO, task_partition) != SUCCESS) {
  1237. GELOGE(ACL_ERROR_GE_EXEC_LOAD_TASK_PARTITION_FAILED, "get task model partition failed.");
  1238. return ACL_ERROR_GE_EXEC_LOAD_TASK_PARTITION_FAILED;
  1239. }
  1240. std::shared_ptr<domi::ModelTaskDef> model_task_def = MakeShared<domi::ModelTaskDef>();
  1241. if (model_task_def == nullptr) {
  1242. return MEMALLOC_FAILED;
  1243. }
  1244. if (task_partition.size != 0) {
  1245. if (!ReadProtoFromArray(task_partition.data, static_cast<int>(task_partition.size), model_task_def.get())) {
  1246. GELOGE(ACL_ERROR_GE_EXEC_LOAD_TASK_PARTITION_FAILED, "ReadProtoFromArray failed.");
  1247. return ACL_ERROR_GE_EXEC_LOAD_TASK_PARTITION_FAILED;
  1248. }
  1249. }
  1250. ModelPartition partition_weight;
  1251. ret = om_file_helper.GetModelPartition(ModelPartitionType::WEIGHTS_DATA, partition_weight);
  1252. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(ret != SUCCESS, return ACL_ERROR_GE_EXEC_LOAD_WEIGHT_PARTITION_FAILED,
  1253. "Get weight partition failed. ret = %u", ret);
  1254. mem_size = model_task_def->memory_size();
  1255. weight_size = partition_weight.size;
  1256. return SUCCESS;
  1257. }
  1258. void ModelManager::GenModelId(uint32_t *id) {
  1259. if (id == nullptr) {
  1260. return;
  1261. }
  1262. std::lock_guard<std::recursive_mutex> lock(map_mutex_);
  1263. *id = ++max_model_id_;
  1264. }
  1265. Status ModelManager::GetOrigInputInfo(uint32_t model_id, uint32_t index, OriginInputInfo &orig_input_info) {
  1266. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  1267. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, ACL_ERROR_GE_EXEC_MODEL_ID_INVALID,
  1268. "GetOrigInputInfo failed, invalid model_id is %u.",
  1269. model_id);
  1270. return davinci_model->GetOrigInputInfo(index, orig_input_info);
  1271. }
  1272. Status ModelManager::GetAllAippInputOutputDims(uint32_t model_id, uint32_t index,
  1273. std::vector<InputOutputDims> &input_dims,
  1274. std::vector<InputOutputDims> &output_dims) {
  1275. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  1276. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, ACL_ERROR_GE_EXEC_MODEL_ID_INVALID,
  1277. "GetAllAippInputOutputDims failed, invalid model_id is %u.", model_id);
  1278. return davinci_model->GetAllAippInputOutputDims(index, input_dims, output_dims);
  1279. }
  1280. bool ModelManager::IsDynamicShape(uint32_t model_id) {
  1281. auto model = GetHybridModel(model_id);
  1282. return model != nullptr;
  1283. }
  1284. ge::Status ModelManager::SyncExecuteModel(uint32_t model_id, const vector<GeTensor> &inputs,
  1285. vector<GeTensor> &outputs) {
  1286. auto model = GetHybridModel(model_id);
  1287. if (model == nullptr) {
  1288. GELOGE(FAILED, "Hybrid model not found. model id = %u.", model_id);
  1289. return FAILED;
  1290. }
  1291. return model->Execute(inputs, outputs);
  1292. }
  1293. Status ModelManager::GetOpDescInfo(uint32_t device_id, uint32_t stream_id, uint32_t task_id, OpDescInfo &op_desc_info) {
  1294. for (const auto &model : model_map_) {
  1295. auto davinci_model = model.second;
  1296. if (davinci_model->GetDeviceId() == device_id) {
  1297. GELOGI("Start to GetOpDescInfo of device_id: %u.", device_id);
  1298. if (davinci_model->GetOpDescInfo(stream_id, task_id, op_desc_info)) {
  1299. GELOGI("Find specific node of stream_id: %u, task_id: %u.", stream_id, task_id);
  1300. return SUCCESS;
  1301. }
  1302. }
  1303. }
  1304. return FAILED;
  1305. }
  1306. Status ModelManager::EnableExceptionDump(const std::map<string, string> &options) {
  1307. auto iter = options.find(OPTION_EXEC_ENABLE_EXCEPTION_DUMP);
  1308. if (iter != options.end()) {
  1309. GELOGI("Find option enable_exeception_dump is %s", iter->second.c_str());
  1310. if (iter->second == "1") {
  1311. rtError_t rt_ret = rtSetTaskFailCallback(reinterpret_cast<rtTaskFailCallback>(ExceptionCallback));
  1312. if (rt_ret != RT_ERROR_NONE) {
  1313. GELOGE(RT_FAILED, "rtSetTaskFailCallback failed");
  1314. return RT_ERROR_TO_GE_STATUS(rt_ret);
  1315. }
  1316. } else {
  1317. GELOGI("Option enable exception dump is %s", iter->second.c_str());
  1318. }
  1319. } else {
  1320. GELOGI("Not find option enable exception dump");
  1321. }
  1322. return SUCCESS;
  1323. }
  1324. Status ModelManager::LaunchKernelCheckAicpuOp(std::vector<std::string> &aicpu_optype_list,
  1325. std::vector<std::string> &aicpu_tf_optype_list) {
  1326. std::string kernel_name = "checkOpType";
  1327. GELOGI("LaunchKernelCheckAicpuOpType in, kernel name %s", kernel_name.c_str());
  1328. std::lock_guard<std::mutex> lock(cust_aicpu_mutex_);
  1329. std::vector<SysOpInfo> req_aicpu_op_info_list;
  1330. std::vector<SysOpInfo> res_aicpu_op_info_list;
  1331. std::vector<ReturnCode> res_ret_code_list;
  1332. if (aicpu_optype_list.empty() && aicpu_tf_optype_list.empty()) {
  1333. GELOGI("No need to check aicpu op type.");
  1334. return SUCCESS;
  1335. }
  1336. vector<void *> allocated_mem;
  1337. rtError_t status;
  1338. rtStream_t stream = nullptr;
  1339. void *args = nullptr;
  1340. void *d_req_op_list = nullptr;
  1341. void *d_res_op_list = nullptr;
  1342. void *d_ret_code_list = nullptr;
  1343. size_t aicpu_op_nums = aicpu_optype_list.size();
  1344. size_t tf_op_nums = aicpu_tf_optype_list.size();
  1345. size_t op_nums = aicpu_op_nums + tf_op_nums;
  1346. std::function<void()> callback = [&]() {
  1347. for (auto mem : allocated_mem) {
  1348. GE_CHK_RT(rtFree(mem));
  1349. }
  1350. };
  1351. GE_MAKE_GUARD(release, callback);
  1352. // malloc sysOpInfoList in SysOpCheckInfo
  1353. status = rtMalloc(&d_req_op_list, op_nums * sizeof(SysOpInfo), RT_MEMORY_HBM);
  1354. if (status != RT_ERROR_NONE) {
  1355. GELOGE(RT_FAILED, "Call rt failed, status: 0x%x", status);
  1356. return RT_ERROR_TO_GE_STATUS(status);
  1357. }
  1358. allocated_mem.push_back(d_req_op_list);
  1359. // malloc sysOpInfoList in SysOpCheckResp
  1360. status = rtMalloc(&d_res_op_list, op_nums * sizeof(SysOpInfo), RT_MEMORY_HBM);
  1361. if (status != RT_ERROR_NONE) {
  1362. GELOGE(RT_FAILED, "Call rt failed, status: 0x%x", status);
  1363. return RT_ERROR_TO_GE_STATUS(status);
  1364. }
  1365. allocated_mem.push_back(d_res_op_list);
  1366. // malloc returnCodeList in SysOpCheckResp
  1367. status = rtMalloc(&d_ret_code_list, op_nums * sizeof(ReturnCode), RT_MEMORY_HBM);
  1368. if (status != RT_ERROR_NONE) {
  1369. GELOGE(RT_FAILED, "Call rt failed, status: 0x%x", status);
  1370. return RT_ERROR_TO_GE_STATUS(status);
  1371. }
  1372. allocated_mem.push_back(d_ret_code_list);
  1373. for (const auto &op_type : aicpu_optype_list) {
  1374. SysOpInfo op_info;
  1375. // malloc op_type name in SysOpInfo
  1376. void *d_op_type_name = nullptr;
  1377. status = rtMalloc(&d_op_type_name, op_type.length(), RT_MEMORY_HBM);
  1378. if (status != RT_ERROR_NONE) {
  1379. GELOGE(RT_FAILED, "Call rt failed, status: 0x%x", status);
  1380. return RT_ERROR_TO_GE_STATUS(status);
  1381. }
  1382. allocated_mem.push_back(d_op_type_name);
  1383. GE_CHK_RT(rtMemcpy(d_op_type_name, op_type.length(), op_type.c_str(), op_type.length(), RT_MEMCPY_HOST_TO_DEVICE));
  1384. op_info.opType = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(d_op_type_name));
  1385. op_info.opLen = op_type.length();
  1386. op_info.kernelsType = CPU_KERNEL;
  1387. req_aicpu_op_info_list.emplace_back(op_info);
  1388. }
  1389. for (const auto &op_type : aicpu_tf_optype_list) {
  1390. SysOpInfo op_info;
  1391. // malloc op_type name in SysOpInfo
  1392. void *d_op_type_name = nullptr;
  1393. status = rtMalloc(&d_op_type_name, op_type.size(), RT_MEMORY_HBM);
  1394. if (status != RT_ERROR_NONE) {
  1395. GELOGE(RT_FAILED, "Call rt failed, status: 0x%x", status);
  1396. return RT_ERROR_TO_GE_STATUS(status);
  1397. }
  1398. allocated_mem.push_back(d_op_type_name);
  1399. GE_CHK_RT(rtMemcpy(d_op_type_name, op_type.size(), op_type.c_str(), op_type.size(), RT_MEMCPY_HOST_TO_DEVICE));
  1400. op_info.opType = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(d_op_type_name));
  1401. op_info.opLen = op_type.size();
  1402. op_info.kernelsType = TF_KERNEL;
  1403. req_aicpu_op_info_list.emplace_back(op_info);
  1404. }
  1405. GELOGI("Check aicpu op all attr size: %zu, real attr size: %zu.", op_nums, req_aicpu_op_info_list.size());
  1406. GE_CHK_RT(rtMemcpy(d_req_op_list, sizeof(SysOpInfo) * req_aicpu_op_info_list.size(), req_aicpu_op_info_list.data(),
  1407. sizeof(SysOpInfo) * req_aicpu_op_info_list.size(), RT_MEMCPY_HOST_TO_DEVICE));
  1408. SysOpCheckInfo op_check_info_req = { 0 };
  1409. SysOpCheckResp op_check_info_res = { 0 };
  1410. op_check_info_req.opListNum = op_nums;
  1411. op_check_info_req.offSetLen = sizeof(SysOpCheckInfo);
  1412. op_check_info_req.sysOpInfoList = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(d_req_op_list));
  1413. op_check_info_res.opListNum = 0;
  1414. op_check_info_res.isWithoutJson = 0;
  1415. op_check_info_res.returnCodeList = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(d_ret_code_list));
  1416. op_check_info_res.sysOpInfoList = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(d_res_op_list));
  1417. uint32_t args_size = sizeof(SysOpCheckInfo) + sizeof(SysOpCheckResp);
  1418. status = rtMalloc(&args, args_size, RT_MEMORY_HBM);
  1419. if (status != RT_ERROR_NONE) {
  1420. GELOGE(RT_FAILED, "Call rt failed, status: 0x%x", status);
  1421. return RT_ERROR_TO_GE_STATUS(status);
  1422. }
  1423. allocated_mem.push_back(args);
  1424. GE_CHK_RT(rtMemcpy(args, sizeof(SysOpCheckInfo), reinterpret_cast<void *>(&op_check_info_req), sizeof(SysOpCheckInfo),
  1425. RT_MEMCPY_HOST_TO_DEVICE));
  1426. GE_CHK_RT(rtMemcpy(
  1427. reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<uint64_t>(reinterpret_cast<uintptr_t>(args)) +
  1428. op_check_info_req.offSetLen)), sizeof(SysOpCheckResp), reinterpret_cast<void *>(&op_check_info_res),
  1429. sizeof(SysOpCheckResp), RT_MEMCPY_HOST_TO_DEVICE));
  1430. GE_CHK_RT(rtStreamCreate(&stream, 0));
  1431. GE_CHK_RT(rtCpuKernelLaunch(nullptr, kernel_name.c_str(), 1, args, args_size, nullptr, stream));
  1432. status = rtStreamSynchronize(stream);
  1433. if (status != RT_ERROR_NONE) {
  1434. GELOGE(RT_FAILED, "Call rt stream sync failed, status: 0x%x", status);
  1435. GE_CHK_RT(rtStreamDestroy(stream));
  1436. return RT_ERROR_TO_GE_STATUS(status);
  1437. }
  1438. // Check the response
  1439. SysOpCheckResp *d_op_check_info_res =
  1440. reinterpret_cast<SysOpCheckResp *>(reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<uint64_t>(
  1441. reinterpret_cast<uintptr_t>(args)) + op_check_info_req.offSetLen)));
  1442. (void)memset_s(&op_check_info_res, sizeof(SysOpCheckResp), 0, sizeof(SysOpCheckResp));
  1443. GE_CHK_RT(rtMemcpy(&op_check_info_res, sizeof(SysOpCheckResp), d_op_check_info_res, sizeof(SysOpCheckResp),
  1444. RT_MEMCPY_DEVICE_TO_HOST));
  1445. if (op_check_info_res.isWithoutJson) {
  1446. GELOGI("No need to check aicpu in this scenoria.");
  1447. GE_CHK_RT(rtStreamDestroy(stream));
  1448. return SUCCESS;
  1449. }
  1450. uint64_t res_op_nums = op_check_info_res.opListNum;
  1451. GELOGI("Check aicpu type, is without json: %d, res op num: %lu.", op_check_info_res.isWithoutJson, res_op_nums);
  1452. if (res_op_nums != 0) {
  1453. res_ret_code_list.clear();
  1454. res_ret_code_list.resize(res_op_nums);
  1455. res_aicpu_op_info_list.clear();
  1456. res_aicpu_op_info_list.resize(res_op_nums);
  1457. GE_CHK_RT(rtMemcpy(res_ret_code_list.data(), sizeof(ReturnCode) * res_op_nums,
  1458. reinterpret_cast<void *>(static_cast<uintptr_t>(op_check_info_res.returnCodeList)),
  1459. sizeof(ReturnCode) * res_op_nums, RT_MEMCPY_DEVICE_TO_HOST));
  1460. GE_CHK_RT(rtMemcpy(res_aicpu_op_info_list.data(), sizeof(SysOpInfo) * res_op_nums,
  1461. reinterpret_cast<void *>(static_cast<uintptr_t>(op_check_info_res.sysOpInfoList)),
  1462. sizeof(SysOpInfo) * res_op_nums, RT_MEMCPY_DEVICE_TO_HOST));
  1463. if (res_ret_code_list.size() != res_aicpu_op_info_list.size() || res_ret_code_list.size() != res_op_nums) {
  1464. GELOGE(FAILED, "Number of retcode is not equal to number of op type.");
  1465. GE_CHK_RT(rtStreamDestroy(stream));
  1466. return FAILED;
  1467. }
  1468. std::string fail_reason;
  1469. for (uint32_t i = 0; i < res_op_nums; i++) {
  1470. ReturnCode ret_code = res_ret_code_list.at(i);
  1471. SysOpInfo aicpu_info = res_aicpu_op_info_list.at(i);
  1472. GELOGI("Not support aicpu op type: %lu, kernel_type:%d, opLen:%lu, ret_code:%d", aicpu_info.opType,
  1473. aicpu_info.kernelsType, aicpu_info.opLen, ret_code);
  1474. std::vector<char> op_name;
  1475. op_name.clear();
  1476. op_name.resize(kOpNameMaxSize);
  1477. GE_CHK_RT(rtMemcpy(op_name.data(), aicpu_info.opLen, reinterpret_cast<void *>(aicpu_info.opType),
  1478. aicpu_info.opLen, RT_MEMCPY_DEVICE_TO_HOST));
  1479. std::string kernel_type =
  1480. (static_cast<OpKernelType>(aicpu_info.kernelsType) == TF_KERNEL) ? "TF_KERNEL" : "CPU_KERNEL";
  1481. string op_name_str(op_name.data());
  1482. fail_reason += "op_type: " + op_name_str + " kernel_type: " + kernel_type +
  1483. " ret code:" + std::to_string(static_cast<int>(ret_code)) +
  1484. "<0: op_type, 1: format, 2: datatype> \n";
  1485. }
  1486. fail_reason += "not support.";
  1487. GELOGE(FAILED, "Check aicpu op_type failed. details: %s", fail_reason.c_str());
  1488. GE_CHK_RT(rtStreamDestroy(stream));
  1489. return FAILED;
  1490. }
  1491. GE_CHK_RT(rtStreamDestroy(stream));
  1492. GELOGI("Cpu kernel launch check optype task success.");
  1493. return SUCCESS;
  1494. }
  1495. Status ModelManager::CheckAicpuOpList(GeModelPtr ge_model) {
  1496. std::vector<std::string> aicpu_optype_list;
  1497. std::vector<std::string> aicpu_tf_optype_list;
  1498. bool aicpu_need_check = ge::AttrUtils::GetListStr(ge_model, "needCheckCpu", aicpu_optype_list);
  1499. bool tf_need_check = ge::AttrUtils::GetListStr(ge_model, "needCheckTf", aicpu_tf_optype_list);
  1500. if (!aicpu_need_check && !tf_need_check) {
  1501. GELOGI("Graph:%s No need to check aicpu optype.", ge_model->GetGraph().GetName().c_str());
  1502. return SUCCESS;
  1503. }
  1504. GE_CHK_STATUS_RET(LaunchKernelCheckAicpuOp(aicpu_optype_list, aicpu_tf_optype_list),
  1505. "Launch check aicpu op type failed.");
  1506. return SUCCESS;
  1507. }
  1508. } // namespace ge

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