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 53 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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  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 "common/dump/dump_manager.h"
  19. #include "common/l2_cache_optimize.h"
  20. #include "common/profiling/profiling_manager.h"
  21. #include "common/properties_manager.h"
  22. #include "framework/common/debug/ge_log.h"
  23. #include "framework/common/util.h"
  24. #include "graph/common/ge_call_wrapper.h"
  25. #include "graph/debug/ge_attr_define.h"
  26. #include "graph/load/new_model_manager/davinci_model.h"
  27. #include "graph/load/new_model_manager/davinci_model_parser.h"
  28. #include "model/ge_root_model.h"
  29. namespace ge {
  30. thread_local uint32_t device_count = 0;
  31. namespace {
  32. const int kCmdParSize = 2;
  33. const int kDumpCmdPairSize = 2;
  34. const int kProfStartCmdParaSize = 2;
  35. const std::string kCmdTypeProfile = "profile";
  36. const std::string kCmdTypeDump = "dump";
  37. const std::string kCmdTypeProfiling = "profiling";
  38. const std::string kCmdTypeProfInit = "prof_init";
  39. const std::string kCmdTypeProfFinalize = "prof_finalize";
  40. const std::string kCmdTypeProfStart = "prof_start";
  41. const std::string kCmdTypeProfStop = "prof_stop";
  42. const char *const kBatchLoadBuf = "batchLoadsoFrombuf";
  43. const char *const kDeleteCustOp = "deleteCustOp";
  44. struct CustAicpuSoBuf {
  45. uint64_t kernelSoBuf;
  46. uint32_t kernelSoBufLen;
  47. uint64_t kernelSoName;
  48. uint32_t kernelSoNameLen;
  49. } __attribute__((packed));
  50. struct BatchLoadOpFromBufArgs {
  51. uint32_t soNum;
  52. uint64_t args;
  53. } __attribute__((packed));
  54. } // namespace
  55. DumpProperties ModelManager::dump_properties_;
  56. std::mutex ModelManager::exeception_infos_mutex_;
  57. std::shared_ptr<ModelManager> ModelManager::GetInstance() {
  58. static const std::shared_ptr<ModelManager> instance_ptr =
  59. shared_ptr<ModelManager>(new (std::nothrow) ModelManager(), ModelManager::FinalizeForPtr);
  60. return instance_ptr;
  61. }
  62. ModelManager::ModelManager() {
  63. max_model_id_ = 0;
  64. session_id_bias_ = 0;
  65. }
  66. Status ModelManager::KernelLaunchEx(aicpu::FWKAdapter::FWKOperateType op_type, uint64_t session_id, uint32_t model_id) {
  67. STR_FWK_OP_KERNEL param_base = {};
  68. void *devicebase = nullptr;
  69. void *aicpu_kernel_addr = nullptr;
  70. const uint32_t kKernelType = 0;
  71. param_base.fwkKernelType = kKernelType;
  72. param_base.fwkKernelBase.fwk_kernel.opType = op_type;
  73. param_base.fwkKernelBase.fwk_kernel.sessionID = session_id;
  74. if (op_type == aicpu::FWKAdapter::FWKOperateType::FWK_ADPT_KERNEL_DESTROY) {
  75. std::vector<uint64_t> v_aicpu_kernel;
  76. std::string model_key = std::to_string(session_id) + "_" + std::to_string(model_id);
  77. auto iter = model_aicpu_kernel_.find(model_key);
  78. if (iter != model_aicpu_kernel_.end()) {
  79. GELOGD("kernel destroy session_id %lu, model_id %u.", session_id, model_id);
  80. v_aicpu_kernel = model_aicpu_kernel_.at(model_key);
  81. // Insert size of aicpu kernel vector in the first element
  82. v_aicpu_kernel.insert(v_aicpu_kernel.begin(), v_aicpu_kernel.size());
  83. auto kernel_size = sizeof(uint64_t) * (v_aicpu_kernel.size());
  84. rtError_t rt_ret = rtMalloc(&aicpu_kernel_addr, kernel_size, RT_MEMORY_HBM);
  85. GE_IF_BOOL_EXEC(rt_ret != RT_ERROR_NONE, GELOGE(RT_FAILED, "rtMalloc error, ret: 0x%X", rt_ret);
  86. return RT_ERROR_TO_GE_STATUS(rt_ret);)
  87. rt_ret = rtMemcpy(aicpu_kernel_addr, kernel_size, v_aicpu_kernel.data(), kernel_size, RT_MEMCPY_HOST_TO_DEVICE);
  88. GE_IF_BOOL_EXEC(rt_ret != RT_ERROR_NONE, GELOGE(RT_FAILED, "rtMemcpy to input_output_addr_ error: 0x%X", rt_ret);
  89. GE_CHK_RT(rtFree(aicpu_kernel_addr)); return RT_ERROR_TO_GE_STATUS(rt_ret);)
  90. uint64_t kernel_id_addr = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(aicpu_kernel_addr));
  91. param_base.fwkKernelBase.fwk_kernel.kernelID = kernel_id_addr;
  92. // In the scene of loading once and running many times, the kernel needs to be destroyed many times,
  93. // and connot be removed from kernel map.
  94. }
  95. }
  96. rtError_t rt_ret = rtMalloc(&(devicebase), sizeof(STR_FWK_OP_KERNEL), RT_MEMORY_HBM);
  97. if (rt_ret != RT_ERROR_NONE) {
  98. GELOGE(RT_FAILED, "malloc device memory failed. ret: 0x%X", rt_ret);
  99. GE_IF_BOOL_EXEC(aicpu_kernel_addr != nullptr, GE_CHK_RT(rtFree(aicpu_kernel_addr)));
  100. return RT_ERROR_TO_GE_STATUS(rt_ret);
  101. }
  102. rt_ret =
  103. rtMemcpy(devicebase, sizeof(STR_FWK_OP_KERNEL), &param_base, sizeof(STR_FWK_OP_KERNEL), RT_MEMCPY_HOST_TO_DEVICE);
  104. if (rt_ret != RT_ERROR_NONE) {
  105. GELOGE(RT_FAILED, "memory copy to device failed. ret: 0x%X", rt_ret);
  106. GE_IF_BOOL_EXEC(aicpu_kernel_addr != nullptr, GE_CHK_RT(rtFree(aicpu_kernel_addr)));
  107. GE_CHK_RT(rtFree(devicebase));
  108. return RT_ERROR_TO_GE_STATUS(rt_ret);
  109. }
  110. rtStream_t stream = nullptr;
  111. rt_ret = rtStreamCreate(&stream, 0);
  112. if (rt_ret != RT_ERROR_NONE) {
  113. GELOGE(RT_FAILED, "create stream 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. rt_ret = rtKernelLaunchEx(devicebase, sizeof(STR_FWK_OP_KERNEL), 0, stream);
  119. if (rt_ret != RT_ERROR_NONE) {
  120. GELOGE(RT_FAILED, "rtKernelLaunchEx failed. ret: 0x%X", rt_ret);
  121. GE_IF_BOOL_EXEC(aicpu_kernel_addr != nullptr, GE_CHK_RT(rtFree(aicpu_kernel_addr)));
  122. GE_CHK_RT(rtFree(devicebase));
  123. GE_CHK_RT(rtStreamDestroy(stream));
  124. return RT_ERROR_TO_GE_STATUS(rt_ret);
  125. }
  126. rt_ret = rtStreamSynchronize(stream);
  127. if (rt_ret != RT_ERROR_NONE) {
  128. GELOGE(RT_FAILED, "rtStreamSynchronize 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. if (aicpu_kernel_addr != nullptr) {
  135. rt_ret = rtFree(aicpu_kernel_addr);
  136. if (rt_ret != RT_ERROR_NONE) {
  137. GELOGE(RT_FAILED, "free memory failed. ret: 0x%X", rt_ret);
  138. GE_CHK_RT(rtFree(devicebase));
  139. GE_CHK_RT(rtStreamDestroy(stream));
  140. return RT_ERROR_TO_GE_STATUS(rt_ret);
  141. }
  142. }
  143. rt_ret = rtFree(devicebase);
  144. if (rt_ret != RT_ERROR_NONE) {
  145. GELOGE(RT_FAILED, "free memory failed. ret: 0x%X", rt_ret);
  146. GE_CHK_RT(rtStreamDestroy(stream));
  147. return RT_ERROR_TO_GE_STATUS(rt_ret);
  148. }
  149. rt_ret = rtStreamDestroy(stream);
  150. if (rt_ret != RT_ERROR_NONE) {
  151. GELOGE(RT_FAILED, "rtStreamDestroy failed. ret: 0x%X", rt_ret);
  152. return RT_ERROR_TO_GE_STATUS(rt_ret);
  153. }
  154. return SUCCESS;
  155. }
  156. void ModelManager::DestroyAicpuSession(uint64_t session_id) {
  157. std::lock_guard<std::mutex> lock(sess_ids_mutex_);
  158. auto it = sess_ids_.find(session_id);
  159. if (it == sess_ids_.end()) {
  160. GELOGI("The session: %lu not created.", session_id);
  161. return;
  162. } else {
  163. rtContext_t ctx = nullptr;
  164. bool has_ctx = (rtCtxGetCurrent(&ctx) == RT_ERROR_NONE);
  165. if (!has_ctx) {
  166. GELOGI("Set device %u.", GetContext().DeviceId());
  167. GE_CHK_RT(rtSetDevice(static_cast<int32_t>(GetContext().DeviceId())));
  168. }
  169. Status ret = KernelLaunchEx(aicpu::FWKAdapter::FWKOperateType::FWK_ADPT_SESSION_DESTROY, session_id, 0);
  170. if (ret != SUCCESS) {
  171. GELOGW("The session: %lu destroy failed.", session_id);
  172. } else {
  173. (void)sess_ids_.erase(session_id);
  174. GELOGI("The session: %lu destroyed.", session_id);
  175. }
  176. if (!has_ctx) {
  177. GELOGI("Reset device %u.", GetContext().DeviceId());
  178. GE_CHK_RT(rtDeviceReset(static_cast<int32_t>(GetContext().DeviceId())));
  179. }
  180. }
  181. }
  182. ge::Status ModelManager::DestroyAicpuSessionForInfer(uint32_t model_id) {
  183. GELOGI("Destroy aicpu session for infer, model id is %u.", model_id);
  184. std::lock_guard<std::mutex> lock(map_mutex_);
  185. auto it = model_map_.find(model_id);
  186. if (it == model_map_.end()) {
  187. GELOGE(GE_EXEC_MODEL_ID_INVALID, "model id %u does not exists.", model_id);
  188. return GE_EXEC_MODEL_ID_INVALID;
  189. }
  190. uint64_t session_id = it->second->GetSessionId();
  191. GELOGI("Destroy aicpu session for infer, session id is %lu.", session_id);
  192. DestroyAicpuSession(session_id);
  193. return SUCCESS;
  194. }
  195. ge::Status ModelManager::DestroyAicpuKernel(uint64_t session_id, uint32_t model_id) {
  196. GELOGD("destroy aicpu kernel in session_id %lu, model_id %u.", session_id, model_id);
  197. std::lock_guard<std::mutex> lock(sess_ids_mutex_);
  198. std::string model_key = std::to_string(session_id) + "_" + std::to_string(model_id);
  199. if (model_aicpu_kernel_.find(model_key) != model_aicpu_kernel_.end()) {
  200. Status ret = KernelLaunchEx(aicpu::FWKAdapter::FWKOperateType::FWK_ADPT_KERNEL_DESTROY, session_id, model_id);
  201. if (ret != SUCCESS) {
  202. GELOGE(FAILED, "Destroy aicpu kernel failed.");
  203. return FAILED;
  204. }
  205. }
  206. return SUCCESS;
  207. }
  208. ge::Status ModelManager::CreateAicpuKernel(uint64_t session_id, uint32_t model_id, uint64_t kernel_id) {
  209. std::lock_guard<std::mutex> lock(sess_ids_mutex_);
  210. std::vector<uint64_t> v_aicpu_kernel;
  211. std::string model_key = std::to_string(session_id) + "_" + std::to_string(model_id);
  212. if (model_aicpu_kernel_.find(model_key) != model_aicpu_kernel_.end()) {
  213. v_aicpu_kernel = model_aicpu_kernel_.at(model_key);
  214. }
  215. v_aicpu_kernel.push_back(kernel_id);
  216. model_aicpu_kernel_[model_key] = v_aicpu_kernel;
  217. return SUCCESS;
  218. }
  219. ModelManager::~ModelManager() {
  220. std::lock_guard<std::mutex> lock(map_mutex_);
  221. model_map_.clear();
  222. model_aicpu_kernel_.clear();
  223. cust_aicpu_so_.clear();
  224. GE_IF_BOOL_EXEC(device_count > 0, GE_CHK_RT(rtDeviceReset(0)));
  225. }
  226. ///
  227. /// @ingroup domi_ome
  228. /// @brief set Device. If no device available, return failure
  229. /// @return Status run result
  230. /// @author
  231. ///
  232. Status ModelManager::SetDevice(int32_t deviceId) const {
  233. GE_CHK_RT_RET(rtSetDevice(deviceId));
  234. return SUCCESS;
  235. }
  236. ge::Status ModelManager::SetDynamicSize(uint32_t model_id, const std::vector<uint64_t> &batch_num,
  237. int32_t dynamic_type) {
  238. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  239. GE_CHECK_NOTNULL(davinci_model);
  240. davinci_model->SetDynamicSize(batch_num, dynamic_type);
  241. return SUCCESS;
  242. }
  243. ge::Status ModelManager::DoLoadHybridModelOnline(uint32_t model_id, const shared_ptr<ge::GeRootModel> &ge_root_model,
  244. const shared_ptr<ModelListener> &listener) {
  245. auto hybrid_model = hybrid::HybridDavinciModel::Create(ge_root_model);
  246. GE_CHECK_NOTNULL(hybrid_model);
  247. hybrid_model->SetListener(listener);
  248. hybrid_model->SetModelId(model_id);
  249. hybrid_model->SetDeviceId(GetContext().DeviceId());
  250. GE_CHK_STATUS_RET(hybrid_model->Init(), "Failed to init hybrid model. model_id = %u", model_id);
  251. auto shared_model = std::shared_ptr<hybrid::HybridDavinciModel>(hybrid_model.release());
  252. InsertModel(model_id, shared_model);
  253. return SUCCESS;
  254. }
  255. ///
  256. /// @ingroup domi_ome
  257. /// @brief load model online
  258. /// @return Status run result
  259. ///
  260. Status ModelManager::LoadModelOnline(uint32_t &model_id, const shared_ptr<ge::GeRootModel> &ge_root_model,
  261. std::shared_ptr<ModelListener> listener) {
  262. GE_CHK_BOOL_RET_STATUS(listener.get() != nullptr, PARAM_INVALID, "Param incorrect, listener is null");
  263. if (model_id == INVALID_MODEL_ID) {
  264. GenModelId(&model_id);
  265. }
  266. bool is_shape_unknown = false;
  267. GE_CHK_STATUS_RET(ge_root_model->CheckIsUnknownShape(is_shape_unknown), "CheckIsUnknownShape failed, model id:%u",
  268. model_id);
  269. if (is_shape_unknown || GetContext().GetHostExecFlag()) {
  270. return DoLoadHybridModelOnline(model_id, ge_root_model, listener);
  271. }
  272. GE_CHK_STATUS_RET(SetDevice(static_cast<int32_t>(GetContext().DeviceId())), "Set device failed, model id:%u.",
  273. model_id);
  274. mmTimespec timespec = mmGetTickCount();
  275. std::shared_ptr<DavinciModel> davinci_model = MakeShared<DavinciModel>(0, listener);
  276. if (davinci_model == nullptr) {
  277. GELOGE(FAILED, "davinci_model is nullptr");
  278. return FAILED;
  279. }
  280. davinci_model->SetId(model_id);
  281. davinci_model->SetDeviceId(GetContext().DeviceId());
  282. const DumpProperties &dump_properties = PropertiesManager::Instance().GetDumpProperties(GetContext().SessionId());
  283. davinci_model->SetDumpProperties(dump_properties);
  284. dump_properties_ = dump_properties;
  285. auto root_graph = ge_root_model->GetRootGraph();
  286. GE_CHECK_NOTNULL(root_graph);
  287. string root_model_name = root_graph->GetName();
  288. auto name_to_model = ge_root_model->GetSubgraphInstanceNameToModel();
  289. GeModelPtr ge_model = name_to_model[root_model_name];
  290. Status ret = SUCCESS;
  291. do {
  292. GE_TIMESTAMP_START(Assign);
  293. GE_IF_BOOL_EXEC(SUCCESS != (ret = davinci_model->Assign(ge_model)), GELOGW("assign model to modeldef failed.");
  294. break;);
  295. GE_TIMESTAMP_END(Assign, "GraphLoader::ModelAssign");
  296. GE_TIMESTAMP_START(Init);
  297. GE_IF_BOOL_EXEC(SUCCESS != (ret = davinci_model->Init()), GELOGW("DavinciInit failed."); break;);
  298. GE_TIMESTAMP_END(Init, "GraphLoader::ModelInit");
  299. InsertModel(model_id, davinci_model);
  300. GELOGI("Parse model %u success.", model_id);
  301. if (ProfilingManager::Instance().ProfilingModelLoadOn()) {
  302. davinci_model->SetProfileTime(MODEL_LOAD_START, (timespec.tv_sec * 1000 * 1000 * 1000 +
  303. timespec.tv_nsec)); // 1000 ^ 3 converts second to nanosecond
  304. davinci_model->SetProfileTime(MODEL_LOAD_END);
  305. }
  306. } while (0);
  307. GE_CHK_RT(rtDeviceReset(static_cast<int32_t>(GetContext().DeviceId())));
  308. return ret;
  309. }
  310. void ModelManager::InsertModel(uint32_t id, std::shared_ptr<DavinciModel> &davinci_model) {
  311. GE_CHK_BOOL_EXEC(davinci_model != nullptr, return, "davinci_model ptr is null, id: %u", id);
  312. std::lock_guard<std::mutex> lock(map_mutex_);
  313. model_map_[id] = davinci_model;
  314. }
  315. void ModelManager::InsertModel(uint32_t id, shared_ptr<hybrid::HybridDavinciModel> &hybrid_model) {
  316. GE_CHK_BOOL_EXEC(hybrid_model != nullptr, return, "hybrid_model ptr is null, id: %u", id);
  317. std::lock_guard<std::mutex> lock(map_mutex_);
  318. hybrid_model_map_[id] = hybrid_model;
  319. }
  320. Status ModelManager::DeleteModel(uint32_t id) {
  321. std::lock_guard<std::mutex> lock(map_mutex_);
  322. auto it = model_map_.find(id);
  323. auto hybrid_model_it = hybrid_model_map_.find(id);
  324. if (it != model_map_.end()) {
  325. uint64_t session_id = it->second->GetSessionId();
  326. std::string model_key = std::to_string(session_id) + "_" + std::to_string(id);
  327. auto iter_aicpu_kernel = model_aicpu_kernel_.find(model_key);
  328. if (iter_aicpu_kernel != model_aicpu_kernel_.end()) {
  329. (void)model_aicpu_kernel_.erase(iter_aicpu_kernel);
  330. }
  331. (void)model_map_.erase(it);
  332. } else if (hybrid_model_it != hybrid_model_map_.end()) {
  333. (void)hybrid_model_map_.erase(hybrid_model_it);
  334. } else {
  335. GELOGE(GE_EXEC_MODEL_ID_INVALID, "model id %u does not exists.", id);
  336. return GE_EXEC_MODEL_ID_INVALID;
  337. }
  338. return SUCCESS;
  339. }
  340. std::shared_ptr<DavinciModel> ModelManager::GetModel(uint32_t id) {
  341. std::lock_guard<std::mutex> lock(map_mutex_);
  342. auto it = model_map_.find(id);
  343. return (it == model_map_.end()) ? nullptr : it->second;
  344. }
  345. std::shared_ptr<hybrid::HybridDavinciModel> ModelManager::GetHybridModel(uint32_t id) {
  346. std::lock_guard<std::mutex> lock(map_mutex_);
  347. auto it = hybrid_model_map_.find(id);
  348. return (it == hybrid_model_map_.end()) ? nullptr : it->second;
  349. }
  350. Status ModelManager::Unload(uint32_t model_id) {
  351. GE_CHK_STATUS_RET(DeleteModel(model_id), "failed to unload model id: %u", model_id);
  352. if (device_count > 0) {
  353. device_count--;
  354. GELOGI("Unload model %u success.", model_id);
  355. } else {
  356. GELOGI("Unload model %u success.no need reset device,device_count: %u", model_id, device_count);
  357. }
  358. std::lock_guard<std::mutex> lock(exeception_infos_mutex_);
  359. exception_infos_.clear();
  360. return SUCCESS;
  361. }
  362. Status ModelManager::UnloadModeldef(uint32_t model_id) {
  363. GE_CHK_STATUS_RET(DeleteModel(model_id), "failed to unload modeldef id: %u", model_id);
  364. return SUCCESS;
  365. }
  366. Status ModelManager::DataInput(const InputData &input_data, OutputData &output_data) {
  367. GELOGI("calling the DataInput");
  368. shared_ptr<InputDataWrapper> data_wrap(new (std::nothrow) InputDataWrapper());
  369. GE_CHECK_NOTNULL(data_wrap);
  370. Status status = data_wrap->Init(input_data, output_data);
  371. if (status != SUCCESS) {
  372. GELOGE(domi::PUSH_DATA_FAILED, "Init InputDataWrapper failed, input data index: %u.", input_data.index);
  373. return domi::PUSH_DATA_FAILED;
  374. }
  375. uint32_t model_id = input_data.model_id;
  376. output_data.model_id = model_id;
  377. std::shared_ptr<DavinciModel> model = GetModel(model_id);
  378. GE_CHK_BOOL_RET_STATUS(model != nullptr, PARAM_INVALID, "Invalid model id %u in InputData! ", model_id);
  379. GE_IF_BOOL_EXEC(model->GetDataInputTid() == 0, model->SetDataInputTid(mmGetTid()));
  380. DataInputer *inputer = model->GetDataInputer();
  381. GE_CHECK_NOTNULL(inputer);
  382. if (inputer->Push(data_wrap) != SUCCESS) {
  383. GELOGE(domi::DATA_QUEUE_ISFULL, "Data queue is full, please call again later, model_id %u ", model_id);
  384. return domi::DATA_QUEUE_ISFULL;
  385. }
  386. GELOGD("Data input success, model id:%u", model_id);
  387. return SUCCESS;
  388. }
  389. ///
  390. /// @ingroup domi_ome
  391. /// @brief load Input and output TensorInfo for Model
  392. /// @return Status run result
  393. ///
  394. Status ModelManager::DataInputTensor(uint32_t model_id, const std::vector<InputTensorInfo> &inputs) {
  395. std::shared_ptr<DavinciModel> model = GetModel(model_id);
  396. auto hybrid_model = GetHybridModel(model_id);
  397. if (hybrid_model == nullptr) {
  398. GE_CHECK_NOTNULL(model);
  399. }
  400. InputData input_data;
  401. input_data.model_id = model_id;
  402. input_data.timeout = 0;
  403. input_data.timestamp = 0;
  404. input_data.index = 0;
  405. for (size_t i = 0; i < inputs.size(); ++i) {
  406. DataBuffer data;
  407. data.data = inputs[i].data;
  408. data.length = inputs[i].length;
  409. input_data.blobs.push_back(data);
  410. }
  411. OutputData output_data;
  412. output_data.model_id = model_id;
  413. output_data.index = 0;
  414. shared_ptr<InputDataWrapper> data_wrap(new (std::nothrow) InputDataWrapper());
  415. GE_CHECK_NOTNULL(data_wrap);
  416. GE_CHK_STATUS_EXEC(data_wrap->Init(input_data, output_data), return domi::PUSH_DATA_FAILED,
  417. "Init InputDataWrapper failed,input data model_id is : %u.", model_id);
  418. if (hybrid_model != nullptr) {
  419. GE_CHK_STATUS_RET(hybrid_model->EnqueueData(data_wrap), "Data queue is full, please call again later, model_id %u ",
  420. model_id);
  421. return SUCCESS;
  422. }
  423. GE_CHK_BOOL_RET_STATUS(model != nullptr, PARAM_INVALID, "Invalid model id %u in InputData! ", model_id);
  424. DataInputer *inputer = model->GetDataInputer();
  425. GE_CHECK_NOTNULL(inputer);
  426. GE_CHK_STATUS_EXEC(inputer->Push(data_wrap), return domi::DATA_QUEUE_ISFULL,
  427. "Data queue is full, please call again later, model_id %u ", model_id);
  428. GELOGD("Data input success, model id:%u", model_id);
  429. return SUCCESS;
  430. }
  431. ///
  432. /// @ingroup domi_ome
  433. /// @brief create model thread, start to execute model
  434. /// @param [in] model_id Model ID to be started
  435. /// @return Status model run result
  436. /// @author
  437. ///
  438. Status ModelManager::Start(uint32_t model_id) {
  439. auto hybrid_model = GetHybridModel(model_id);
  440. if (hybrid_model != nullptr) {
  441. GE_CHK_STATUS_RET_NOLOG(hybrid_model->ModelRunStart());
  442. GELOGI("Start hybrid model %u success.", model_id);
  443. return SUCCESS;
  444. }
  445. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  446. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, PARAM_INVALID, "Invalid model id %u to start! ", model_id);
  447. Status status = davinci_model->ModelRunStart();
  448. if (status == SUCCESS) {
  449. GELOGI("Start model %u success.", model_id);
  450. }
  451. return status;
  452. }
  453. ///
  454. /// @ingroup domi_ome
  455. /// @brief Model ID stop
  456. /// @only when unloaded
  457. /// @param [in] model_id Model ID to be stopped
  458. /// @return Status model stop result
  459. /// @author
  460. ///
  461. Status ModelManager::Stop(uint32_t model_id) {
  462. auto hybrid_model = GetHybridModel(model_id);
  463. if (hybrid_model != nullptr) {
  464. GE_CHK_STATUS_RET_NOLOG(hybrid_model->ModelRunStop());
  465. GELOGI("Stop hybrid model %u success.", model_id);
  466. return SUCCESS;
  467. }
  468. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  469. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, PARAM_INVALID, "Invalid model id %u to stop!", model_id);
  470. Status status = davinci_model->ModelRunStop();
  471. if (status == SUCCESS) {
  472. GELOGI("Stop model %u success.", model_id);
  473. }
  474. return status;
  475. }
  476. ///
  477. /// @ingroup domi_ome
  478. /// @brief Command handle
  479. /// @iterator 1 only Ieference, Debug 2 modes
  480. /// @param [in] command command to handle
  481. /// @return Status command handle result
  482. /// @author
  483. ///
  484. Status ModelManager::HandleCommand(const Command &command) {
  485. static const std::map<std::string, std::function<uint32_t(const Command &)>> cmds = {
  486. {kCmdTypeProfile, HandleProfileCommand}, {kCmdTypeDump, HandleDumpCommand},
  487. {kCmdTypeProfiling, HandleAclProfilingCommand}, {kCmdTypeProfInit, HandleProfInitCommand},
  488. {kCmdTypeProfFinalize, HandleProfFinalizeCommand}, {kCmdTypeProfStart, HandleProfStartCommand},
  489. {kCmdTypeProfStop, HandleProfStopCommand}};
  490. auto iter = cmds.find(command.cmd_type);
  491. if (iter == cmds.end()) {
  492. GELOGE(PARAM_INVALID, "Unsupported command: %s", command.cmd_type.c_str());
  493. return PARAM_INVALID;
  494. } else {
  495. return iter->second(command);
  496. }
  497. }
  498. Status ModelManager::HandleAclProfilingCommand(const Command &command) {
  499. if (command.cmd_params.size() < kCmdParSize) {
  500. GELOGE(PARAM_INVALID, "When the cmd_type is 'profiling', the size of cmd_params must larger than 2.");
  501. return PARAM_INVALID;
  502. }
  503. std::string map_key = command.cmd_params[0];
  504. std::string value = command.cmd_params[1];
  505. if (map_key == PROFILE_CONFIG) {
  506. ProfilingManager::Instance().SetProfilingConfig(value);
  507. }
  508. return SUCCESS;
  509. }
  510. Status ModelManager::HandleProfInitCommand(const Command &command) {
  511. uint64_t module_index = command.module_index;
  512. if (ProfilingManager::Instance().ProfInit(module_index) != SUCCESS) {
  513. GELOGE(FAILED, "Handle prof init failed.");
  514. return FAILED;
  515. }
  516. return SUCCESS;
  517. }
  518. Status ModelManager::HandleProfFinalizeCommand(const Command &command) {
  519. if (ProfilingManager::Instance().ProfFinalize() != SUCCESS) {
  520. GELOGE(FAILED, "Handle prof finalize failed.");
  521. return FAILED;
  522. }
  523. return SUCCESS;
  524. }
  525. /*
  526. * cmd para when prof start
  527. * "devNums:2"
  528. * "devIdList:1,2"
  529. * "profilingOption:PROF_OP_TRACE"
  530. * "aicoreMetrics:AICORE_ARITHMATIC_THROUGHPUT"
  531. */
  532. Status ModelManager::HandleProfStartCommand(const Command &command) {
  533. if (command.cmd_params.size() < kProfStartCmdParaSize) {
  534. GELOGE(PARAM_INVALID, "When the cmd_type is 'profile start', the size of cmd_params must larger than 2.");
  535. return PARAM_INVALID;
  536. }
  537. std::map<std::string, std::string> cmd_params_map;
  538. uint32_t step = 2;
  539. for (uint32_t i = 0; i < command.cmd_params.size(); i += step) {
  540. if (i + 1 >= command.cmd_params.size()) {
  541. continue;
  542. }
  543. cmd_params_map[command.cmd_params[i]] = command.cmd_params[i + 1];
  544. }
  545. uint64_t module_index = command.module_index;
  546. if (ProfilingManager::Instance().ProfStartProfiling(module_index, cmd_params_map) != SUCCESS) {
  547. GELOGE(FAILED, "Handle prof start failed.");
  548. return FAILED;
  549. }
  550. return SUCCESS;
  551. }
  552. Status ModelManager::HandleProfStopCommand(const Command &command) {
  553. if (command.cmd_params.size() < kProfStartCmdParaSize) {
  554. GELOGE(PARAM_INVALID, "When the cmd_type is 'profile stop', the size of cmd_params must larger than 2.");
  555. return PARAM_INVALID;
  556. }
  557. std::map<std::string, std::string> cmd_params_map;
  558. uint32_t step = 2;
  559. for (uint32_t i = 0; i < command.cmd_params.size(); i += step) {
  560. if (i + 1 >= command.cmd_params.size()) {
  561. continue;
  562. }
  563. cmd_params_map[command.cmd_params[i]] = command.cmd_params[i + 1];
  564. }
  565. uint64_t module_index = command.module_index;
  566. if (ProfilingManager::Instance().ProfStopProfiling(module_index, cmd_params_map) != SUCCESS) {
  567. GELOGE(FAILED, "Handle prof finalize failed.");
  568. return FAILED;
  569. }
  570. return SUCCESS;
  571. }
  572. Status ModelManager::HandleProfileCommand(const Command &command) {
  573. if (command.cmd_params.size() < kCmdParSize) {
  574. GELOGE(PARAM_INVALID, "When the cmd_type is 'profile', the size of cmd_params must larger than 2.");
  575. return PARAM_INVALID;
  576. }
  577. std::string map_key = command.cmd_params[0];
  578. std::string value = command.cmd_params[1];
  579. GELOGI("Profiling mode, Command key:%s , value:%s ", map_key.c_str(), value.c_str());
  580. auto iter = PROFILE_COMPONENT_MAP.find(map_key);
  581. if (iter != PROFILE_COMPONENT_MAP.end()) {
  582. std::string property_value = (value == "on") ? "1" : "0";
  583. PropertiesManager::Instance().SetPropertyValue(iter->second, property_value);
  584. }
  585. if ((map_key == PROFILER_JOBCTX || map_key == PROFILER_TARGET_PATH || map_key == RTS_PROFILE_PATH)) {
  586. PropertiesManager::Instance().SetPropertyValue(map_key, value);
  587. }
  588. return SUCCESS;
  589. }
  590. static Status ParserPara(const Command &command, const string &dump_key, string &dump_value) {
  591. auto iter = std::find(command.cmd_params.begin(), command.cmd_params.end(), dump_key);
  592. if (iter != command.cmd_params.end()) {
  593. ++iter;
  594. if (iter == command.cmd_params.end()) {
  595. GELOGE(PARAM_INVALID, "Invalid access.");
  596. return PARAM_INVALID;
  597. }
  598. dump_value = *iter;
  599. }
  600. return SUCCESS;
  601. }
  602. Status ModelManager::HandleDumpCommand(const Command &command) {
  603. if (command.cmd_params.size() % kDumpCmdPairSize != 0) {
  604. GELOGE(PARAM_INVALID, "When the cmd_type is 'dump', the size of cmd_params must be a even number.");
  605. return PARAM_INVALID;
  606. }
  607. std::string dump_status("off");
  608. std::string dump_model(DUMP_ALL_MODEL);
  609. std::string dump_path("/");
  610. std::string dump_mode("output");
  611. std::set<std::string> dump_layers;
  612. auto ret = ParserPara(command, DUMP_STATUS, dump_status);
  613. if (ret != SUCCESS) {
  614. GELOGE(PARAM_INVALID, "parser dump status failed");
  615. return FAILED;
  616. }
  617. GELOGI("dump status = %s.", dump_status.c_str());
  618. ret = ParserPara(command, DUMP_MODEL, dump_model);
  619. if (ret != SUCCESS) {
  620. GELOGE(PARAM_INVALID, "parser dump model failed");
  621. return FAILED;
  622. }
  623. GELOGI("dump model = %s.", dump_model.c_str());
  624. if (dump_status == "off" || dump_status == "OFF") {
  625. dump_properties_.DeletePropertyValue(dump_model);
  626. return SUCCESS;
  627. }
  628. for (size_t i = 0; i < command.cmd_params.size() / kDumpCmdPairSize; ++i) {
  629. if (command.cmd_params.at(i * kDumpCmdPairSize).find(DUMP_LAYER) != std::string::npos) {
  630. GELOGI("dump layer: %s.", command.cmd_params.at(i * kDumpCmdPairSize + 1).c_str());
  631. dump_layers.insert(command.cmd_params.at(i * kDumpCmdPairSize + 1));
  632. }
  633. }
  634. ret = ParserPara(command, DUMP_FILE_PATH, dump_path);
  635. if (ret != SUCCESS) {
  636. GELOGE(PARAM_INVALID, "parser dump path failed");
  637. return FAILED;
  638. }
  639. if (!dump_path.empty() && dump_path[dump_path.size() - 1] != '/') {
  640. dump_path = dump_path + "/";
  641. }
  642. dump_path = dump_path + CurrentTimeInStr() + "/";
  643. GELOGI("dump path = %s.", dump_path.c_str());
  644. ret = ParserPara(command, DUMP_MODE, dump_mode);
  645. if (ret != SUCCESS) {
  646. GELOGE(PARAM_INVALID, "parser dump mode failed");
  647. return FAILED;
  648. }
  649. GELOGI("dump mode = %s", dump_mode.c_str());
  650. dump_properties_.AddPropertyValue(dump_model, dump_layers);
  651. dump_properties_.SetDumpPath(dump_path);
  652. dump_properties_.SetDumpMode(dump_mode);
  653. return SUCCESS;
  654. }
  655. Status ModelManager::GetMaxUsedMemory(const uint32_t model_id, uint64_t &max_size) {
  656. auto hybrid_model = GetHybridModel(model_id);
  657. if (hybrid_model != nullptr) {
  658. max_size = 0;
  659. return SUCCESS;
  660. }
  661. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  662. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, PARAM_INVALID, "GetMaxUsedMemory Failed, Invalid model id %u!",
  663. model_id);
  664. max_size = davinci_model->TotalMemSize();
  665. return SUCCESS;
  666. }
  667. Status ModelManager::GetInputOutputDescInfo(const uint32_t model_id, vector<InputOutputDescInfo> &input_desc,
  668. vector<InputOutputDescInfo> &output_desc) {
  669. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  670. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, PARAM_INVALID, "GetInputOutputDescInfo Failed, Invalid model id %u!",
  671. model_id);
  672. return davinci_model->GetInputOutputDescInfo(input_desc, output_desc);
  673. }
  674. Status ModelManager::GetInputOutputDescInfo(const uint32_t model_id, vector<InputOutputDescInfo> &input_desc,
  675. vector<InputOutputDescInfo> &output_desc,
  676. std::vector<uint32_t> &inputFormats, std::vector<uint32_t> &outputFormats,
  677. bool new_model_desc) {
  678. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  679. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, GE_EXEC_MODEL_ID_INVALID,
  680. "GetInputOutputDescInfo Failed, Invalid model id %u!", model_id);
  681. davinci_model->SetModelDescVersion(new_model_desc);
  682. return davinci_model->GetInputOutputDescInfo(input_desc, output_desc, inputFormats, outputFormats);
  683. }
  684. ///
  685. /// @ingroup ge
  686. /// @brief Get dynamic batch_info
  687. /// @param [in] model_id
  688. /// @param [out] batch_info
  689. /// @return execute result
  690. ///
  691. Status ModelManager::GetDynamicBatchInfo(const uint32_t model_id, std::vector<std::vector<int64_t>> &batch_info,
  692. int32_t &dynamic_type) {
  693. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  694. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, GE_EXEC_MODEL_ID_INVALID,
  695. "GetDynamicBatchInfo failed, Invalid model id %u!", model_id);
  696. return davinci_model->GetDynamicBatchInfo(batch_info, dynamic_type);
  697. }
  698. ///
  699. /// @ingroup ge
  700. /// @brief Get combined dynamic dims info
  701. /// @param [in] model_id
  702. /// @param [out] batch_info
  703. /// @return execute result
  704. ///
  705. Status ModelManager::GetCombinedDynamicDims(const uint32_t model_id, vector<vector<int64_t>> &batch_info) {
  706. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  707. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, PARAM_INVALID, "GetCombinedDynamicDims Failed, Invalid Model ID %u!",
  708. model_id);
  709. davinci_model->GetCombinedDynamicDims(batch_info);
  710. return SUCCESS;
  711. }
  712. ///
  713. /// @ingroup ge
  714. /// @brief Get user designate shape order
  715. /// @param [in] model_id
  716. /// @param [out] user_input_shape_order
  717. /// @return execute result
  718. ///
  719. Status ModelManager::GetUserDesignateShapeOrder(const uint32_t model_id,
  720. std::vector<std::string> &user_input_shape_order) {
  721. auto davinci_model = GetModel(model_id);
  722. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, PARAM_INVALID,
  723. "GetUserDesignateShapeOrder Failed, Invalid Model ID %u!", model_id)
  724. davinci_model->GetUserDesignateShapeOrder(user_input_shape_order);
  725. return SUCCESS;
  726. }
  727. Status ModelManager::GetCurShape(const uint32_t model_id, std::vector<int64_t> &batch_info, int32_t &dynamic_type) {
  728. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  729. GE_CHECK_NOTNULL(davinci_model);
  730. davinci_model->GetCurShape(batch_info, dynamic_type);
  731. return SUCCESS;
  732. }
  733. Status ModelManager::GetModelAttr(uint32_t model_id, std::vector<string> &dynamic_output_shape_info) {
  734. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  735. GE_CHECK_NOTNULL(davinci_model);
  736. davinci_model->GetModelAttr(dynamic_output_shape_info);
  737. return SUCCESS;
  738. }
  739. Status ModelManager::GetInputOutputDescInfoForZeroCopy(const uint32_t model_id, vector<InputOutputDescInfo> &input_desc,
  740. vector<InputOutputDescInfo> &output_desc,
  741. std::vector<uint32_t> &inputFormats,
  742. std::vector<uint32_t> &outputFormats) {
  743. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  744. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, PARAM_INVALID, "GetInputOutputDescInfo Failed, Invalid model id %u!",
  745. model_id);
  746. return davinci_model->GetInputOutputDescInfoForZeroCopy(input_desc, output_desc, inputFormats, outputFormats);
  747. }
  748. ///
  749. /// @ingroup ge
  750. /// @brief Get AIPP info
  751. /// @param [in] model_id
  752. /// @param [in] index
  753. /// @param [out] aipp_info
  754. /// @return execute result
  755. ///
  756. Status ModelManager::GetAIPPInfo(const uint32_t model_id, uint32_t index, AippConfigInfo &aipp_info) {
  757. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  758. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, PARAM_INVALID, "GetAIPPInfo failed, invalid model_id is %u.",
  759. model_id);
  760. return davinci_model->GetAIPPInfo(index, aipp_info);
  761. }
  762. Status ModelManager::GetAippType(uint32_t model_id, uint32_t index, InputAippType &type, size_t &aipp_index) {
  763. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  764. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, PARAM_INVALID, "GetAIPPInfo failed, invalid model_id is %u.",
  765. model_id);
  766. return davinci_model->GetAippType(index, type, aipp_index);
  767. }
  768. Status ModelManager::GenSessionId(uint64_t &session_id) {
  769. std::lock_guard<std::mutex> lock(session_id_create_mutex_);
  770. struct timeval tv;
  771. if (gettimeofday(&tv, nullptr) != 0) {
  772. GELOGE(INTERNAL_ERROR, "Failed to get current time.");
  773. return INTERNAL_ERROR;
  774. }
  775. session_id = static_cast<uint64_t>(tv.tv_sec * 1000000 + tv.tv_usec); // 1000000us
  776. session_id_bias_++;
  777. // max bais 100.
  778. session_id_bias_ = session_id_bias_ % 100;
  779. session_id = session_id * 100 + session_id_bias_;
  780. GELOGD("Generate new session id: %lu.", session_id);
  781. return SUCCESS;
  782. }
  783. Status ModelManager::LoadModelOffline(uint32_t &model_id, const ModelData &model, shared_ptr<ModelListener> listener,
  784. void *dev_ptr, size_t mem_size, void *weight_ptr, size_t weight_size) {
  785. GE_CHK_BOOL_RET_STATUS(model.key.empty() || access(model.key.c_str(), F_OK) == 0, GE_EXEC_MODEL_KEY_PATH_INVALID,
  786. "input key file path %s is invalid, %s", model.key.c_str(), strerror(errno));
  787. GenModelId(&model_id);
  788. shared_ptr<DavinciModel> davinci_model = nullptr;
  789. mmTimespec timespec = mmGetTickCount();
  790. ModelHelper model_helper;
  791. Status ret = model_helper.LoadModel(model);
  792. if (ret != SUCCESS) {
  793. GELOGE(ret, "load model failed.");
  794. return ret;
  795. }
  796. do {
  797. GeModelPtr ge_model = model_helper.GetGeModel();
  798. try {
  799. davinci_model = std::make_shared<DavinciModel>(model.priority, listener);
  800. } catch (std::bad_alloc &) {
  801. GELOGE(MEMALLOC_FAILED, "Make shared failed");
  802. return MEMALLOC_FAILED;
  803. } catch (...) {
  804. GELOGE(INTERNAL_ERROR, "Make shared failed since other exception raise");
  805. return INTERNAL_ERROR;
  806. }
  807. ret = davinci_model->Assign(ge_model);
  808. if (ret != SUCCESS) {
  809. GELOGW("assign model failed.");
  810. break;
  811. }
  812. davinci_model->SetId(model_id);
  813. int32_t device_id = 0;
  814. rtError_t rt_ret = rtGetDevice(&device_id);
  815. if (rt_ret != RT_ERROR_NONE || device_id < 0) {
  816. GELOGE(RT_FAILED, "Call rtGetDevice failed, ret = 0x%X, device_id = %d.", rt_ret, device_id);
  817. return RT_ERROR_TO_GE_STATUS(rt_ret);
  818. }
  819. davinci_model->SetDeviceId(device_id);
  820. davinci_model->SetOmName(model.om_name);
  821. if (DumpManager::GetInstance().GetDumpProperties().IsDumpOpen()) {
  822. davinci_model->SetDumpProperties(DumpManager::GetInstance().GetDumpProperties());
  823. } else {
  824. davinci_model->SetDumpProperties(dump_properties_);
  825. }
  826. /// In multi-threaded inference, using the same session_id among multiple threads may cause some threads to fail.
  827. /// These session_ids come from the same model, so the values of session_id are the same.
  828. /// Update session_id for infer in load model to avoid the same session_id.
  829. uint64_t new_session_id;
  830. ret = GenSessionId(new_session_id);
  831. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(ret != SUCCESS, break, "Generate session_id for infer failed.");
  832. ret = davinci_model->UpdateSessionId(new_session_id);
  833. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(ret != SUCCESS, break, "Update session_id for infer failed.");
  834. ret = davinci_model->Init(dev_ptr, mem_size, weight_ptr, weight_size);
  835. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(ret != SUCCESS, break, "DavinciInit failed.");
  836. InsertModel(model_id, davinci_model);
  837. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(davinci_model == nullptr, ret = PARAM_INVALID; break, "Insert model failed");
  838. GELOGI("Parse model %u success.", model_id);
  839. if (ProfilingManager::Instance().ProfilingModelLoadOn()) {
  840. davinci_model->SetProfileTime(MODEL_LOAD_START, (timespec.tv_sec * 1000 * 1000 * 1000 +
  841. timespec.tv_nsec)); // 1000 ^ 3 converts second to nanosecond
  842. davinci_model->SetProfileTime(MODEL_LOAD_END);
  843. }
  844. GE_IF_BOOL_EXEC(ret == SUCCESS, device_count++);
  845. return SUCCESS;
  846. } while (0);
  847. return ret;
  848. }
  849. ///
  850. /// @ingroup ge
  851. /// @brief ACL case, Load task list with queue.
  852. /// @param [out] model_id: model id for manager.
  853. /// @param [in] model_data: Model data load from offline model file.
  854. /// @param [in] input_que_ids: input queue ids from user, num equals Data Op.
  855. /// @param [in] output_que_ids: input queue ids from user, num equals NetOutput Op.
  856. /// @return: 0 for success / others for fail
  857. ///
  858. Status ModelManager::LoadModelWithQ(uint32_t &model_id, const ModelData &model_data,
  859. const std::vector<uint32_t> &input_queue_ids,
  860. const std::vector<uint32_t> &output_queue_ids) {
  861. GE_CHK_BOOL_RET_STATUS(model_data.key.empty() || access(model_data.key.c_str(), F_OK) == 0,
  862. GE_EXEC_MODEL_KEY_PATH_INVALID, "input key file path %s is not valid, %s",
  863. model_data.key.c_str(), strerror(errno));
  864. ModelHelper model_helper;
  865. Status ret = model_helper.LoadModel(model_data);
  866. if (ret != SUCCESS) {
  867. GELOGE(ret, "load model failed.");
  868. return ret;
  869. }
  870. shared_ptr<DavinciModel> davinci_model = MakeShared<DavinciModel>(model_data.priority, nullptr);
  871. if (davinci_model == nullptr) {
  872. GELOGE(MEMALLOC_FAILED, "create model failed.");
  873. return MEMALLOC_FAILED;
  874. }
  875. ret = davinci_model->Assign(model_helper.GetGeModel());
  876. if (ret != SUCCESS) {
  877. GELOGE(ret, "assign model failed.");
  878. return ret;
  879. }
  880. /// In multi-threaded inference, using the same session_id among multiple threads may cause some threads to fail.
  881. /// These session_ids come from the same model, so the values of session_id are the same.
  882. /// Update session_id for infer in load model to avoid the same session_id.
  883. uint64_t new_session_id;
  884. ret = GenSessionId(new_session_id);
  885. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(ret != SUCCESS, return ret, "Generate session_id for infer failed.");
  886. ret = davinci_model->UpdateSessionId(new_session_id);
  887. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(ret != SUCCESS, return ret, "Update session_id for infer failed.");
  888. GenModelId(&model_id);
  889. davinci_model->SetId(model_id);
  890. ret = davinci_model->SetQueIds(input_queue_ids, output_queue_ids);
  891. if (ret != SUCCESS) {
  892. GELOGE(ret, "set model queue ids failed.");
  893. return ret;
  894. }
  895. davinci_model->SetDumpProperties(dump_properties_);
  896. ret = davinci_model->Init();
  897. if (ret != SUCCESS) {
  898. GELOGE(ret, "init model failed.");
  899. return ret;
  900. }
  901. InsertModel(model_id, davinci_model);
  902. GELOGI("Parse model %u success.", model_id);
  903. return SUCCESS;
  904. }
  905. ///
  906. /// @ingroup domi_ome
  907. /// @brief ACL case, not start new thread, return result
  908. /// @param [in] model_id mode id
  909. /// @param [in] stream model stream
  910. /// @param [in] async_mode is asynchronize mode.
  911. /// @param [in] input_data input data
  912. /// @param [out] output_data output data
  913. ///
  914. Status ModelManager::ExecuteModel(uint32_t model_id, rtStream_t stream, bool async_mode, const InputData &input_data,
  915. OutputData &output_data) {
  916. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  917. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, PARAM_INVALID, "Invalid model id %u.", model_id);
  918. if (davinci_model->NeedDestroyAicpuKernel()) {
  919. GELOGI("Start to destroy specified aicpu kernel.");
  920. // Zero copy is enabled by default, no need to judge.
  921. uint64_t session_id_davinci = davinci_model->GetSessionId();
  922. uint32_t model_id_davinci = davinci_model->GetModelId();
  923. Status status = DestroyAicpuKernel(session_id_davinci, model_id_davinci);
  924. if (status != SUCCESS) {
  925. GELOGW("Destroy specified aicpu kernel failed, session id is %lu, model id is %u.", session_id_davinci,
  926. model_id_davinci);
  927. }
  928. }
  929. Status status = davinci_model->NnExecute(stream, async_mode, input_data, output_data);
  930. if (status == SUCCESS) {
  931. GELOGI("Execute model %u success.", model_id);
  932. }
  933. return status;
  934. }
  935. Status ModelManager::CreateAicpuSession(uint64_t session_id) {
  936. std::lock_guard<std::mutex> lock(sess_ids_mutex_);
  937. auto it = sess_ids_.find(session_id);
  938. // never been created by any model
  939. if (it == sess_ids_.end()) {
  940. Status ret = KernelLaunchEx(aicpu::FWKAdapter::FWKOperateType::FWK_ADPT_SESSION_CREATE, session_id, 0);
  941. if (ret == SUCCESS) {
  942. (void)sess_ids_.insert(session_id);
  943. GELOGI("The session: %lu create success.", session_id);
  944. }
  945. return ret;
  946. }
  947. return SUCCESS;
  948. }
  949. Status ModelManager::LoadCustAicpuSo(const OpDescPtr &op_desc, const string &so_name) {
  950. GELOGI("LoadCustAicpuSo in, op name %s, so name %s", op_desc->GetName().c_str(), so_name.c_str());
  951. std::lock_guard<std::mutex> lock(cust_aicpu_mutex_);
  952. CustAICPUKernelPtr aicpu_kernel = op_desc->TryGetExtAttr(OP_EXTATTR_CUSTAICPU_KERNEL, CustAICPUKernelPtr());
  953. if (aicpu_kernel == nullptr) {
  954. GELOGE(INTERNAL_ERROR, "cust aicpu op %s can't find kernel!", op_desc->GetName().c_str());
  955. return INTERNAL_ERROR;
  956. }
  957. // get current context
  958. rtContext_t rt_cur_ctx = nullptr;
  959. auto rt_error = rtCtxGetCurrent(&rt_cur_ctx);
  960. if (rt_error != RT_ERROR_NONE) {
  961. GELOGE(RT_FAILED, "get current context failed, runtime result is %d", static_cast<int>(rt_error));
  962. return RT_FAILED;
  963. }
  964. // use current context as resource key
  965. uintptr_t resource_id = reinterpret_cast<uintptr_t>(rt_cur_ctx);
  966. auto it = cust_aicpu_so_.find(resource_id);
  967. if (it == cust_aicpu_so_.end()) {
  968. std::map<string, CustAICPUKernelPtr> new_so_name;
  969. new_so_name.insert({so_name, aicpu_kernel});
  970. cust_aicpu_so_[resource_id] = new_so_name;
  971. GELOGI("LoadCustAicpuSo new aicpu so resource id %lu", resource_id);
  972. return SUCCESS;
  973. }
  974. auto it_so_name = it->second.find(so_name);
  975. if (it_so_name == it->second.end()) {
  976. it->second.insert({so_name, aicpu_kernel});
  977. GELOGI("LoadCustAicpuSo add aicpu so resource id %lu", resource_id);
  978. }
  979. return SUCCESS;
  980. }
  981. Status ModelManager::LaunchKernelCustAicpuSo(const string &kernel_name) {
  982. GELOGI("LaunchCustAucpuSo in, kernel name %s", kernel_name.c_str());
  983. std::lock_guard<std::mutex> lock(cust_aicpu_mutex_);
  984. if (cust_aicpu_so_.size() == 0) return SUCCESS;
  985. // get current context
  986. rtContext_t rt_cur_ctx = nullptr;
  987. auto rt_error = rtCtxGetCurrent(&rt_cur_ctx);
  988. if (rt_error != RT_ERROR_NONE) {
  989. GELOGE(RT_FAILED, "get current context failed, runtime result is %d", static_cast<int>(rt_error));
  990. return RT_FAILED;
  991. }
  992. uintptr_t resource_id = reinterpret_cast<uintptr_t>(rt_cur_ctx);
  993. auto it = cust_aicpu_so_.find(resource_id);
  994. if (it == cust_aicpu_so_.end()) {
  995. GELOGI("Cust aicpu so map is empty, context id %lu", resource_id);
  996. return SUCCESS;
  997. }
  998. vector<void *> allocated_mem;
  999. rtError_t status;
  1000. rtStream_t stream = nullptr;
  1001. vector<CustAicpuSoBuf> v_cust_so;
  1002. void *args = nullptr;
  1003. for (const auto &it_so : it->second) {
  1004. const void *aicpu_data = it_so.second->GetBinData();
  1005. uint32_t aicpu_data_length = it_so.second->GetBinDataSize();
  1006. string so_name = it_so.first;
  1007. void *d_aicpu_data = nullptr;
  1008. void *d_so_name = nullptr;
  1009. status = rtMalloc(&d_aicpu_data, aicpu_data_length, RT_MEMORY_HBM);
  1010. if (status != RT_ERROR_NONE) {
  1011. GELOGE(RT_FAILED, "Call rt failed, status: 0x%x", status);
  1012. return RT_ERROR_TO_GE_STATUS(status);
  1013. }
  1014. allocated_mem.push_back(d_aicpu_data);
  1015. status = rtMalloc(&d_so_name, so_name.size(), RT_MEMORY_HBM);
  1016. if (status != RT_ERROR_NONE) {
  1017. GELOGE(RT_FAILED, "Call rt failed, status: 0x%x", status);
  1018. return RT_ERROR_TO_GE_STATUS(status);
  1019. }
  1020. allocated_mem.push_back(d_so_name);
  1021. GE_CHK_RT(rtMemcpy(d_aicpu_data, aicpu_data_length, aicpu_data, aicpu_data_length, RT_MEMCPY_HOST_TO_DEVICE));
  1022. GE_CHK_RT(rtMemcpy(d_so_name, so_name.size(), reinterpret_cast<const void *>(so_name.c_str()),
  1023. so_name.size(), RT_MEMCPY_HOST_TO_DEVICE));
  1024. CustAicpuSoBuf cust_aicpu_so_buf;
  1025. cust_aicpu_so_buf.kernelSoBuf = reinterpret_cast<uint64_t>(reinterpret_cast<uintptr_t>(d_aicpu_data));
  1026. cust_aicpu_so_buf.kernelSoBufLen = aicpu_data_length;
  1027. cust_aicpu_so_buf.kernelSoName = reinterpret_cast<uint64_t>(reinterpret_cast<uintptr_t>(d_so_name));
  1028. cust_aicpu_so_buf.kernelSoNameLen = so_name.size();
  1029. v_cust_so.push_back(cust_aicpu_so_buf);
  1030. }
  1031. if (kernel_name == kDeleteCustOp) {
  1032. (void)cust_aicpu_so_.erase(it);
  1033. }
  1034. uint32_t args_size = sizeof(CustAicpuSoBuf) * v_cust_so.size();
  1035. status = rtMalloc(&args, args_size, RT_MEMORY_HBM);
  1036. if (status != RT_ERROR_NONE) {
  1037. GELOGE(RT_FAILED, "Call rt failed, status: 0x%x", status);
  1038. return RT_ERROR_TO_GE_STATUS(status);
  1039. }
  1040. allocated_mem.push_back(args);
  1041. GE_CHK_RT(rtMemcpy(args, args_size, v_cust_so.data(), args_size, RT_MEMCPY_HOST_TO_DEVICE));
  1042. BatchLoadOpFromBufArgs batch_cust_so;
  1043. batch_cust_so.soNum = v_cust_so.size();
  1044. batch_cust_so.args = reinterpret_cast<uint64_t>(reinterpret_cast<uintptr_t>(args));
  1045. void *batch_args = nullptr;
  1046. uint32_t batch_args_size = sizeof(BatchLoadOpFromBufArgs);
  1047. status = rtMalloc(&batch_args, batch_args_size, RT_MEMORY_HBM);
  1048. if (status != RT_ERROR_NONE) {
  1049. GELOGE(RT_FAILED, "Call rt failed, status: 0x%x", status);
  1050. return RT_ERROR_TO_GE_STATUS(status);
  1051. }
  1052. allocated_mem.push_back(batch_args);
  1053. GE_CHK_RT(rtMemcpy(batch_args, batch_args_size, static_cast<void *>(&batch_cust_so),
  1054. batch_args_size, RT_MEMCPY_HOST_TO_DEVICE));
  1055. GE_CHK_RT(rtStreamCreate(&stream, 0));
  1056. GE_CHK_RT(rtCpuKernelLaunch(nullptr, kernel_name.c_str(), 1, batch_args, batch_args_size, nullptr, stream));
  1057. status = rtStreamSynchronize(stream);
  1058. if (status != RT_ERROR_NONE) {
  1059. GELOGE(RT_FAILED, "Call rt stream sync failed, status: 0x%x", status);
  1060. return RT_ERROR_TO_GE_STATUS(status);
  1061. }
  1062. std::function<void()> callback = [&]() {
  1063. for (auto mem : allocated_mem) {
  1064. GE_CHK_RT(rtFree(mem));
  1065. }
  1066. GE_CHK_RT(rtStreamDestroy(stream));
  1067. };
  1068. GE_MAKE_GUARD(release, callback);
  1069. GELOGI("Cpu kernel launch task success.");
  1070. return SUCCESS;
  1071. }
  1072. Status ModelManager::ClearAicpuSo() {
  1073. GE_CHK_STATUS_RET(LaunchKernelCustAicpuSo(kDeleteCustOp), "delete cust op so failed.");
  1074. return SUCCESS;
  1075. }
  1076. Status ModelManager::LaunchCustAicpuSo() {
  1077. GE_CHK_STATUS_RET(LaunchKernelCustAicpuSo(kBatchLoadBuf), "launch cust op so failed.");
  1078. return SUCCESS;
  1079. }
  1080. ///
  1081. /// @ingroup ge
  1082. /// @brief get model memory size and weight
  1083. /// @param [in] const ModelData model: model type
  1084. /// @param [out] size_t memSize: model memory usage
  1085. /// size_t weightSize: model weight and memory size
  1086. /// @return SUCCESS success / others failure
  1087. ///
  1088. Status ModelManager::GetModelMemAndWeightSize(const ModelData &model, size_t &mem_size, size_t &weight_size) {
  1089. uint8_t *model_data = nullptr;
  1090. uint32_t model_len = 0;
  1091. Status ret = DavinciModelParser::ParseModelContent(model, model_data, model_len);
  1092. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(ret != SUCCESS, return ret, "parse model content failed!");
  1093. OmFileLoadHelper om_file_helper;
  1094. ret = om_file_helper.Init(model_data, model_len);
  1095. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(ret != SUCCESS, return ret, "om file helperInit failed!");
  1096. auto partition_table = reinterpret_cast<ModelPartitionTable *>(model_data);
  1097. if (partition_table->num == 1) {
  1098. GELOGE(GE_EXEC_MODEL_PARTITION_NUM_INVALID, "om model is error,please use executable om model");
  1099. return GE_EXEC_MODEL_PARTITION_NUM_INVALID;
  1100. }
  1101. ModelPartition task_partition;
  1102. if (om_file_helper.GetModelPartition(ModelPartitionType::TASK_INFO, task_partition) != SUCCESS) {
  1103. GELOGE(GE_EXEC_LOAD_TASK_PARTITION_FAILED, "get task model partition failed.");
  1104. return GE_EXEC_LOAD_TASK_PARTITION_FAILED;
  1105. }
  1106. std::shared_ptr<domi::ModelTaskDef> model_task_def = MakeShared<domi::ModelTaskDef>();
  1107. if (model_task_def == nullptr) {
  1108. return MEMALLOC_FAILED;
  1109. }
  1110. if (task_partition.size != 0) {
  1111. if (!ReadProtoFromArray(task_partition.data, static_cast<int>(task_partition.size), model_task_def.get())) {
  1112. GELOGE(GE_EXEC_LOAD_TASK_PARTITION_FAILED, "ReadProtoFromArray failed.");
  1113. return GE_EXEC_LOAD_TASK_PARTITION_FAILED;
  1114. }
  1115. }
  1116. ModelPartition partition_weight;
  1117. ret = om_file_helper.GetModelPartition(ModelPartitionType::WEIGHTS_DATA, partition_weight);
  1118. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(ret != SUCCESS, return GE_EXEC_LOAD_WEIGHT_PARTITION_FAILED,
  1119. "Get weight partition failed. ret = %u", ret);
  1120. mem_size = model_task_def->memory_size();
  1121. weight_size = partition_weight.size;
  1122. return SUCCESS;
  1123. }
  1124. void ModelManager::GenModelId(uint32_t *id) {
  1125. if (id == nullptr) {
  1126. return;
  1127. }
  1128. std::lock_guard<std::mutex> lock(map_mutex_);
  1129. *id = ++max_model_id_;
  1130. }
  1131. Status ModelManager::GetOrigInputInfo(uint32_t model_id, uint32_t index, OriginInputInfo &orig_input_info) {
  1132. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  1133. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, PARAM_INVALID, "GetOrigInputInfo failed, invalid model_id is %u.",
  1134. model_id);
  1135. return davinci_model->GetOrigInputInfo(index, orig_input_info);
  1136. }
  1137. Status ModelManager::GetAllAippInputOutputDims(uint32_t model_id, uint32_t index,
  1138. std::vector<InputOutputDims> &input_dims,
  1139. std::vector<InputOutputDims> &output_dims) {
  1140. std::shared_ptr<DavinciModel> davinci_model = GetModel(model_id);
  1141. GE_CHK_BOOL_RET_STATUS(davinci_model != nullptr, PARAM_INVALID,
  1142. "GetAllAippInputOutputDims failed, invalid model_id is %u.", model_id);
  1143. return davinci_model->GetAllAippInputOutputDims(index, input_dims, output_dims);
  1144. }
  1145. bool ModelManager::IsDynamicShape(uint32_t model_id) {
  1146. auto model = GetHybridModel(model_id);
  1147. return model != nullptr;
  1148. }
  1149. ge::Status ModelManager::SyncExecuteModel(uint32_t model_id, const vector<GeTensor> &inputs,
  1150. vector<GeTensor> &outputs) {
  1151. auto model = GetHybridModel(model_id);
  1152. if (model == nullptr) {
  1153. GELOGE(FAILED, "Hybrid model not found. model id = %u.", model_id);
  1154. return FAILED;
  1155. }
  1156. return model->Execute(inputs, outputs);
  1157. }
  1158. Status ModelManager::GetOpDescInfo(uint32_t device_id, uint32_t stream_id, uint32_t task_id, OpDescInfo &op_desc_info) {
  1159. for (const auto &model : model_map_) {
  1160. auto davinci_model = model.second;
  1161. if (davinci_model->GetDeviceId() == device_id) {
  1162. GELOGI("Start to GetOpDescInfo of device_id: %u.", device_id);
  1163. if (davinci_model->GetOpDescInfo(stream_id, task_id, op_desc_info)) {
  1164. GELOGI("Find specific node of stream_id: %u, task_id: %u.", stream_id, task_id);
  1165. return SUCCESS;
  1166. }
  1167. }
  1168. }
  1169. return FAILED;
  1170. }
  1171. Status ModelManager::EnableExceptionDump(const std::map<string, string> &options) {
  1172. auto iter = options.find(OPTION_EXEC_ENABLE_EXCEPTION_DUMP);
  1173. if (iter != options.end()) {
  1174. GELOGI("Find option enable_exeception_dump is %s", iter->second.c_str());
  1175. if (iter->second == "1") {
  1176. rtError_t rt_ret = rtSetTaskFailCallback(ExceptionCallback);
  1177. if (rt_ret != RT_ERROR_NONE) {
  1178. GELOGE(RT_FAILED, "rtSetTaskFailCallback failed");
  1179. return RT_ERROR_TO_GE_STATUS(rt_ret);
  1180. }
  1181. } else {
  1182. GELOGI("Option enable exception dump is %s", iter->second.c_str());
  1183. }
  1184. } else {
  1185. GELOGI("Not find option enable exception dump");
  1186. }
  1187. return SUCCESS;
  1188. }
  1189. } // namespace ge

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