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

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

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