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 69 kB

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

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