You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

model_manager.cc 70 kB

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

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