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

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