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

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