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

model_manager.cc 70 kB

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

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