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

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