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

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