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

model_manager.cc 70 kB

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

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