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_helper.cc 20 kB

5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 "framework/common/helper/model_helper.h"
  17. #include "common/ge/ge_util.h"
  18. #include "common/util/error_manager/error_manager.h"
  19. #include "framework/common/debug/log.h"
  20. #include "framework/common/util.h"
  21. #include "framework/common/debug/ge_log.h"
  22. #include "framework/omg/version.h"
  23. #include "graph/debug/ge_attr_define.h"
  24. #include "graph/load/new_model_manager/davinci_model_parser.h"
  25. #include "graph/utils/attr_utils.h"
  26. #include "graph/utils/graph_utils.h"
  27. using std::string;
  28. using domi::ModelTaskDef;
  29. namespace {
  30. const int64_t kOriginalOmPartitionNum = 1;
  31. }
  32. namespace ge {
  33. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ModelHelper::~ModelHelper() { (void)ReleaseLocalModelData(); }
  34. Status ModelHelper::SaveModelPartition(std::shared_ptr<OmFileSaveHelper> &om_file_save_helper, ModelPartitionType type,
  35. const uint8_t *data, size_t size) {
  36. if (size < 1 || size > UINT32_MAX) {
  37. GELOGE(PARAM_INVALID, "Add model partition failed, partition size %zu invalid", size);
  38. if (size > UINT32_MAX) {
  39. string item = "item";
  40. if (type == MODEL_DEF) {
  41. item = "model info";
  42. } else if (type == WEIGHTS_DATA) {
  43. item = "weight data";
  44. } else if (type == TASK_INFO) {
  45. item = "task info";
  46. } else if (type == TBE_KERNELS) {
  47. item = "tbe kernels";
  48. } else if (type == CUST_AICPU_KERNELS) {
  49. item = "aicpu kernels";
  50. }
  51. ErrorManager::GetInstance().ATCReportErrMessage("E19023", {"size", "item", "maxsize"},
  52. {std::to_string(size), item, std::to_string(UINT32_MAX)});
  53. }
  54. return PARAM_INVALID;
  55. }
  56. if (data == nullptr) {
  57. GELOGE(PARAM_INVALID, "Add model partition failed, data is null");
  58. return PARAM_INVALID;
  59. }
  60. ModelPartition partition_model;
  61. partition_model.data = const_cast<uint8_t *>(data);
  62. partition_model.size = static_cast<uint32_t>(size);
  63. partition_model.type = type;
  64. if (om_file_save_helper->AddPartition(partition_model) != SUCCESS) {
  65. GELOGE(PARAM_INVALID, "Add model partition failed, partition size %zu", size);
  66. return PARAM_INVALID;
  67. }
  68. return SUCCESS;
  69. }
  70. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ModelHelper::SaveToOmModel(const GeModelPtr &ge_model,
  71. const SaveParam &save_param,
  72. const std::string &output_file,
  73. ModelBufferData& model) {
  74. if (output_file.empty()) {
  75. GELOGE(FAILED, "GraphBuilder SaveModel received invalid file name prefix");
  76. return FAILED;
  77. }
  78. GE_IF_BOOL_EXEC(ge_model == nullptr, GELOGE(FAILED, "Ge_model is nullptr"); return FAILED);
  79. std::shared_ptr<OmFileSaveHelper> om_file_save_helper = ge::MakeShared<OmFileSaveHelper>();
  80. GE_CHECK_NOTNULL(om_file_save_helper);
  81. ModelPtr model_tmp = ge::MakeShared<ge::Model>(ge_model->GetName(), ge_model->GetPlatformVersion());
  82. if (model_tmp == nullptr) {
  83. GELOGE(FAILED, "Create Model %s Ptr failed", ge_model->GetName().c_str());
  84. return FAILED;
  85. }
  86. model_tmp->SetGraph(ge_model->GetGraph());
  87. model_tmp->SetVersion(ge_model->GetVersion());
  88. model_tmp->SetAttr(ge_model->MutableAttrMap());
  89. ge::Buffer model_buffer;
  90. (void)model_tmp->Save(model_buffer);
  91. GELOGD("MODEL_DEF size is %zu", model_buffer.GetSize());
  92. if (model_buffer.GetSize() > 0) {
  93. if (SaveModelPartition(om_file_save_helper, ModelPartitionType::MODEL_DEF, model_buffer.GetData(),
  94. model_buffer.GetSize()) != SUCCESS) {
  95. GELOGE(PARAM_INVALID, "Add model graph partition failed");
  96. return PARAM_INVALID;
  97. }
  98. }
  99. auto ge_model_weight = ge_model->GetWeight();
  100. GELOGD("WEIGHTS_DATA size is %zu, %p", ge_model_weight.GetSize(), ge_model_weight.GetData());
  101. // weight is not necessary
  102. if (ge_model_weight.GetSize() > 0) {
  103. GE_CHK_STATUS_RET(SaveModelPartition(om_file_save_helper,
  104. ModelPartitionType::WEIGHTS_DATA,
  105. ge_model_weight.GetData(),
  106. ge_model_weight.GetSize()), "Add weight partition failed");
  107. }
  108. TBEKernelStore tbe_kernel_store = ge_model->GetTBEKernelStore();
  109. GELOGD("TBE_KERNELS size is %zu", tbe_kernel_store.DataSize());
  110. if (tbe_kernel_store.DataSize() > 0) {
  111. GE_CHK_STATUS_RET(SaveModelPartition(om_file_save_helper,
  112. ModelPartitionType::TBE_KERNELS,
  113. tbe_kernel_store.Data(),
  114. tbe_kernel_store.DataSize()), "Add tbe kernel partition failed");
  115. }
  116. // no need to check value, DATA->NetOutput
  117. (void)tbe_kernel_store.Load(tbe_kernel_store.Data(), tbe_kernel_store.DataSize());
  118. CustAICPUKernelStore cust_aicpu_kernel_store = ge_model->GetCustAICPUKernelStore();
  119. GELOGD("cust aicpu kernels size is %zu", cust_aicpu_kernel_store.DataSize());
  120. if (cust_aicpu_kernel_store.DataSize() > 0) {
  121. GE_CHK_STATUS_RET(SaveModelPartition(om_file_save_helper,
  122. ModelPartitionType::CUST_AICPU_KERNELS,
  123. cust_aicpu_kernel_store.Data(),
  124. cust_aicpu_kernel_store.DataSize()),
  125. "Add cust aicpu kernel partition failed");
  126. }
  127. std::shared_ptr<ModelTaskDef> model_task_def = ge_model->GetModelTaskDefPtr();
  128. if (model_task_def == nullptr) {
  129. GELOGE(ACL_ERROR_GE_MEMORY_ALLOCATION, "Create model task def ptr failed");
  130. return ACL_ERROR_GE_MEMORY_ALLOCATION;
  131. }
  132. size_t partition_task_size = model_task_def->ByteSizeLong();
  133. GE_IF_BOOL_EXEC(partition_task_size == 0 || partition_task_size > INT_MAX,
  134. GELOGE(FAILED, "Model_def's byte size (%zu) is invalid!", partition_task_size);
  135. return FAILED);
  136. ge::Buffer task_buffer(partition_task_size);
  137. if (task_buffer.GetSize() == 0) {
  138. GELOGE(ACL_ERROR_GE_MEMORY_ALLOCATION, "Alloc model task def buffer failed");
  139. return ACL_ERROR_GE_MEMORY_ALLOCATION;
  140. }
  141. (void)model_task_def->SerializePartialToArray(task_buffer.GetData(), static_cast<int>(partition_task_size));
  142. GELOGD("TASK_INFO op_size:%d, stream_num:%u", model_task_def->op().size(), model_task_def->stream_num());
  143. GELOGD("TASK_INFO size is %zu", partition_task_size);
  144. if (SaveModelPartition(om_file_save_helper, ModelPartitionType::TASK_INFO, task_buffer.GetData(),
  145. partition_task_size) != SUCCESS) {
  146. GELOGE(PARAM_INVALID, "Add model task def partition failed");
  147. return PARAM_INVALID;
  148. }
  149. // Save target/version to model_header
  150. ModelFileHeader &model_header = om_file_save_helper->GetModelFileHeader();
  151. model_header.platform_type = ge_model->GetPlatformType();
  152. model_header.om_ir_version = ge_model->GetVersion();
  153. std::string platform_version = ge_model->GetPlatformVersion();
  154. errno_t err;
  155. err = memcpy_s(model_header.platform_version, PLATFORM_VERSION_LEN, platform_version.c_str(),
  156. platform_version.size() + 1);
  157. if (err != EOK) {
  158. GELOGE(ACL_ERROR_GE_MEMORY_ALLOCATION, "ModelHelper SaveModel failed while allocating memory for platform_version.");
  159. return ACL_ERROR_GE_MEMORY_ALLOCATION;
  160. }
  161. string version = reinterpret_cast<char *>(model_header.platform_version);
  162. GELOGD("Platform version save: %s", version.c_str());
  163. size_t name_size = ge_model->GetName().size();
  164. name_size = name_size > (MODEL_NAME_LENGTH - 1) ? (MODEL_NAME_LENGTH - 1) : name_size;
  165. err = memcpy_s(model_header.name, MODEL_NAME_LENGTH, ge_model->GetName().c_str(), name_size);
  166. if (err != EOK) {
  167. GELOGE(ACL_ERROR_GE_MEMORY_ALLOCATION, "ModelHelper SaveModel failed while allocating memory for name");
  168. return ACL_ERROR_GE_MEMORY_ALLOCATION;
  169. }
  170. string model_name = reinterpret_cast<char *>(model_header.name);
  171. GELOGD("Model name save:%s", model_name.c_str());
  172. Status ret = om_file_save_helper->SaveModel(save_param, output_file.c_str(), model, is_offline_);
  173. if (ret != SUCCESS) {
  174. GELOGE(FAILED, "OmFileSaveHelper SaveModel return fail.");
  175. return FAILED;
  176. }
  177. return SUCCESS;
  178. }
  179. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status
  180. ModelHelper::SaveOriginalGraphToOmModel(const ge::Graph &graph, const std::string &output_file) {
  181. if (output_file.empty()) {
  182. GELOGE(FAILED, "SaveModel received invalid file name prefix");
  183. return FAILED;
  184. }
  185. // Get computegraph from graph
  186. auto compute_graph = ge::GraphUtils::GetComputeGraph(graph);
  187. if (compute_graph == nullptr) {
  188. GELOGE(FAILED, "SaveModel fail for compute_graph null");
  189. return FAILED;
  190. }
  191. GE_DUMP(compute_graph, "OriginalGraph");
  192. // Model
  193. ModelPtr model_ptr = ge::MakeShared<ge::Model>();
  194. GE_CHECK_NOTNULL_EXEC(model_ptr, return MEMALLOC_FAILED);
  195. std::string original_model_name = compute_graph->GetName() + "_original";
  196. model_ptr->SetName(original_model_name);
  197. model_ptr->SetGraph(graph);
  198. model_ptr->SetVersion(static_cast<uint32_t>(OM_PROTO_VERSION));
  199. string framework_version;
  200. Status frame_rt = PlatformVersionManager::GetPlatformVersion(framework_version);
  201. if (frame_rt == SUCCESS) {
  202. uint32_t counter = 0;
  203. string model_framework_version = framework_version + "." + std::to_string(counter);
  204. model_ptr->SetPlatformVersion(model_framework_version);
  205. }
  206. // Model def
  207. ge::Buffer model_buffer;
  208. ge::graphStatus status = model_ptr->Save(model_buffer);
  209. if (status != ge::GRAPH_SUCCESS) {
  210. GELOGE(FAILED, "SaveModel fail for save buffer fail");
  211. return FAILED;
  212. }
  213. std::shared_ptr<OmFileSaveHelper> om_file_save_helper = ge::MakeShared<OmFileSaveHelper>();
  214. GE_CHECK_NOTNULL_EXEC(om_file_save_helper, return MEMALLOC_FAILED);
  215. ModelPartition partition_model;
  216. partition_model.data = model_buffer.GetData();
  217. partition_model.size = static_cast<uint32_t>(model_buffer.GetSize());
  218. partition_model.type = ModelPartitionType::MODEL_DEF;
  219. GELOGI("Original Model type[%u],size[%u]", partition_model.type, partition_model.size);
  220. if (partition_model.data != nullptr && partition_model.size > 0) {
  221. (void)om_file_save_helper->AddPartition(partition_model);
  222. // Condition of AddPartition is established, no need to check value
  223. }
  224. // Save target/version to model_header
  225. ModelFileHeader &model_header = om_file_save_helper->GetModelFileHeader();
  226. model_header.om_ir_version = model_ptr->GetVersion();
  227. model_header.headsize = MODEL_FILE_HEAD_LEN;
  228. std::string platform_version = model_ptr->GetPlatformVersion();
  229. errno_t err = memcpy_s(model_header.platform_version, PLATFORM_VERSION_LEN, platform_version.c_str(),
  230. platform_version.size() + 1);
  231. if (err != EOK) {
  232. GELOGE(FAILED, "ModelHelper SaveModel failed for platform_version");
  233. return FAILED;
  234. }
  235. size_t name_size = model_ptr->GetName().size();
  236. name_size = name_size > (MODEL_NAME_LENGTH - 1) ? (MODEL_NAME_LENGTH - 1) : name_size;
  237. err = memcpy_s(model_header.name, MODEL_NAME_LENGTH, model_ptr->GetName().c_str(), name_size);
  238. if (err != EOK) {
  239. GELOGE(FAILED, "ModelHelper SaveModel memory copy failed");
  240. return FAILED;
  241. }
  242. ModelBufferData model;
  243. Status ret = om_file_save_helper->SaveModelToFile(output_file.c_str(), model, is_offline_);
  244. return (ret == SUCCESS ? SUCCESS : FAILED);
  245. }
  246. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ModelHelper::LoadModel(const ge::ModelData &model_data) {
  247. if (model_data.model_data == nullptr || model_data.model_len == 0) {
  248. GELOGE(ACL_ERROR_GE_EXEC_MODEL_DATA_SIZE_INVALID, "Model_data is nullptr, or model_data_size is 0");
  249. return ACL_ERROR_GE_EXEC_MODEL_DATA_SIZE_INVALID;
  250. }
  251. if (is_assign_model_) {
  252. GELOGE(ACL_ERROR_GE_EXEC_LOAD_MODEL_REPEATED, "Model helper has already loaded!");
  253. return ACL_ERROR_GE_EXEC_LOAD_MODEL_REPEATED;
  254. }
  255. if (ReleaseLocalModelData() != SUCCESS) {
  256. GELOGE(ACL_ERROR_GE_EXEC_RELEASE_MODEL_DATA, "ReleaseLocalModelData failed.");
  257. return ACL_ERROR_GE_EXEC_RELEASE_MODEL_DATA;
  258. }
  259. Status status = ge::DavinciModelParser::ParseModelContent(model_data, model_addr_tmp_, model_len_tmp_);
  260. if (status != SUCCESS) {
  261. GELOGE(status, "Parse model content failed!");
  262. return status;
  263. }
  264. file_header_ = reinterpret_cast<ModelFileHeader *>(model_data.model_data);
  265. OmFileLoadHelper om_load_helper;
  266. status = om_load_helper.Init(model_addr_tmp_, model_len_tmp_);
  267. if (status != SUCCESS) {
  268. GELOGE(status, "Om_load_helper init failed");
  269. model_addr_tmp_ = nullptr;
  270. return status;
  271. }
  272. auto partition_table = reinterpret_cast<ModelPartitionTable *>(model_addr_tmp_);
  273. if (partition_table->num == kOriginalOmPartitionNum) {
  274. model_addr_tmp_ = nullptr;
  275. GELOGE(ACL_ERROR_GE_PARAM_INVALID, "om model is error,please use executable om model");
  276. return ACL_ERROR_GE_PARAM_INVALID;
  277. }
  278. // Encrypt model need to del temp model/no encrypt model don't need to del model
  279. model_addr_tmp_ = nullptr;
  280. status = GenerateGeModel(om_load_helper);
  281. if (status != SUCCESS) {
  282. GELOGE(status, "GenerateGeModel failed");
  283. return status;
  284. }
  285. is_assign_model_ = true;
  286. return SUCCESS;
  287. }
  288. Status ModelHelper::GenerateGeModel(OmFileLoadHelper &om_load_helper) {
  289. model_ = ge::MakeShared<ge::GeModel>();
  290. GE_CHECK_NOTNULL(model_);
  291. Status ret = LoadModelData(om_load_helper);
  292. if (ret != SUCCESS) {
  293. return ACL_ERROR_GE_EXEC_LOAD_MODEL_PARTITION_FAILED;
  294. }
  295. ret = LoadWeights(om_load_helper);
  296. if (ret != SUCCESS) {
  297. return ACL_ERROR_GE_EXEC_LOAD_WEIGHT_PARTITION_FAILED;
  298. }
  299. ret = LoadTask(om_load_helper);
  300. if (ret != SUCCESS) {
  301. return ACL_ERROR_GE_EXEC_LOAD_TASK_PARTITION_FAILED;
  302. }
  303. ret = LoadTBEKernelStore(om_load_helper);
  304. if (ret != SUCCESS) {
  305. return ACL_ERROR_GE_EXEC_LOAD_KERNEL_PARTITION_FAILED;
  306. }
  307. ret = LoadCustAICPUKernelStore(om_load_helper);
  308. if (ret != SUCCESS) {
  309. return ACL_ERROR_GE_EXEC_LOAD_KERNEL_PARTITION_FAILED;
  310. }
  311. return SUCCESS;
  312. }
  313. Status ModelHelper::LoadModelData(OmFileLoadHelper &om_load_helper) {
  314. ModelPartition partition_model_def;
  315. // no need to check value, DATA->NetOutput
  316. om_load_helper.GetModelPartition(ModelPartitionType::MODEL_DEF, partition_model_def);
  317. GELOGD("Model_def partition addr:%p,size:%u", partition_model_def.data, partition_model_def.size);
  318. ge::Model model;
  319. if (ge::Model::Load(partition_model_def.data, partition_model_def.size, model) != SUCCESS) {
  320. GELOGE(INTERNAL_ERROR, "Load model failed.");
  321. return INTERNAL_ERROR;
  322. }
  323. SetModelToGeModel(model);
  324. return SUCCESS;
  325. }
  326. void ModelHelper::SetModelToGeModel(ge::Model &model) {
  327. model_->SetGraph(model.GetGraph());
  328. model_->SetName(model.GetName());
  329. model_->SetVersion(model.GetVersion());
  330. model_->SetPlatformVersion(model.GetPlatformVersion());
  331. model_->SetAttr(model.MutableAttrMap());
  332. }
  333. Status ModelHelper::LoadWeights(OmFileLoadHelper &om_load_helper) {
  334. ModelPartition partition;
  335. if (om_load_helper.GetModelPartition(ModelPartitionType::WEIGHTS_DATA, partition) != SUCCESS) {
  336. GELOGE(FAILED, "Get weight model partition failed.");
  337. return FAILED;
  338. }
  339. ge::Buffer weight = ge::Buffer::CopyFrom(partition.data, partition.size);
  340. model_->SetWeight(weight);
  341. GELOGD("GetWeight size:%u", partition.size);
  342. return SUCCESS;
  343. }
  344. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ModelHelper::LoadTask(OmFileLoadHelper &om_load_helper) {
  345. ModelPartition task_partition;
  346. if (om_load_helper.GetModelPartition(ModelPartitionType::TASK_INFO, task_partition) != SUCCESS) {
  347. GELOGE(FAILED, "Get task model partition failed.");
  348. return FAILED;
  349. }
  350. std::shared_ptr<ModelTaskDef> task = ge::MakeShared<ModelTaskDef>();
  351. GE_CHECK_NOTNULL(task);
  352. if (task_partition.size != 0) {
  353. if (!ReadProtoFromArray(task_partition.data, task_partition.size, task.get())) {
  354. GELOGE(INTERNAL_ERROR, "ReadProtoFromArray failed.");
  355. return INTERNAL_ERROR;
  356. }
  357. GELOGD("TASK_INFO op_size:%d, stream_num:%u", task->op().size(), task->stream_num());
  358. }
  359. model_->SetModelTaskDef(task);
  360. return SUCCESS;
  361. }
  362. Status ModelHelper::LoadTBEKernelStore(OmFileLoadHelper &om_load_helper) {
  363. // Load tbe kernels
  364. ModelPartition partition_kernel_def;
  365. TBEKernelStore kernel_store;
  366. if (om_load_helper.GetModelPartition(ModelPartitionType::TBE_KERNELS, partition_kernel_def) == SUCCESS) {
  367. GELOGD("Kernels partition size:%u", partition_kernel_def.size);
  368. if (kernel_store.Load(partition_kernel_def.data, partition_kernel_def.size)) {
  369. GELOGD("Load tbe kernels success");
  370. } else {
  371. GELOGW("Load tbe kernels failed");
  372. }
  373. }
  374. model_->SetTBEKernelStore(kernel_store);
  375. return SUCCESS;
  376. }
  377. Status ModelHelper::LoadCustAICPUKernelStore(OmFileLoadHelper &om_load_helper) {
  378. // Load cust aicpu kernels
  379. ModelPartition partition_kernel_def;
  380. CustAICPUKernelStore kernel_store;
  381. if (om_load_helper.GetModelPartition(ModelPartitionType::CUST_AICPU_KERNELS, partition_kernel_def) == SUCCESS) {
  382. GELOGD("Kernels partition size:%u", partition_kernel_def.size);
  383. if (kernel_store.Load(partition_kernel_def.data, partition_kernel_def.size)) {
  384. GELOGI("Load cust aicpu kernels success");
  385. }
  386. }
  387. model_->SetCustAICPUKernelStore(kernel_store);
  388. return SUCCESS;
  389. }
  390. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY GeModelPtr ModelHelper::GetGeModel() {
  391. if (model_ != nullptr) {
  392. return model_;
  393. }
  394. GELOGI("Model has not been loaded!");
  395. std::shared_ptr<ge::GeModel> out_model = ge::MakeShared<ge::GeModel>();
  396. if (out_model == nullptr) {
  397. return nullptr;
  398. }
  399. return out_model;
  400. }
  401. Status ModelHelper::ReleaseLocalModelData() noexcept {
  402. Status result = SUCCESS;
  403. if (model_addr_tmp_ != nullptr) {
  404. errno_t ret = memset_s(static_cast<void *>(model_addr_tmp_), model_len_tmp_, 0, model_len_tmp_);
  405. if (ret != EOK) {
  406. GELOGE(FAILED, "Failed to memset memory, error-code %d", ret);
  407. result = FAILED;
  408. }
  409. delete[] model_addr_tmp_;
  410. model_addr_tmp_ = nullptr;
  411. model_len_tmp_ = 0;
  412. }
  413. return result;
  414. }
  415. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ModelHelper::GetBaseNameFromFileName(
  416. const string &file_name, string &base_name) {
  417. GELOGD("Get base_name from file, file_name:%s", file_name.c_str());
  418. GE_CHK_BOOL_EXEC_WARN(!file_name.empty(), return FAILED, "File path may not valid, check params --output");
  419. size_t start_position = 0;
  420. // using output as base_name (ignore ".om")
  421. size_t filename_suffixes = 3;
  422. if (file_name.find_last_of('/') != string::npos) {
  423. start_position = file_name.find_last_of('/') + 1;
  424. }
  425. size_t end_position = file_name.length() - filename_suffixes;
  426. base_name = file_name.substr(start_position, end_position - start_position);
  427. GE_CHK_BOOL_EXEC_WARN(!base_name.empty(), return FAILED, "Get base_name failed, check params --output");
  428. return SUCCESS;
  429. }
  430. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ModelHelper::GetModelNameFromMergedGraphName(
  431. const string &graph_name, string &model_name) {
  432. GELOGD("Get model_name from graph_name, graph_name:%s", graph_name.c_str());
  433. // this can only be used after merged graph(graph name will be append with "_x", x is index);
  434. GE_CHK_BOOL_EXEC_WARN(!graph_name.empty(), return FAILED, "File path may not valid, check params --output");
  435. size_t start_position = 0;
  436. size_t end_position = graph_name.length();
  437. // using graph as model_name (ignore "_x", x is the index of graph)
  438. if (graph_name.find_last_of('_') != string::npos) {
  439. end_position = graph_name.find_last_of('_');
  440. }
  441. model_name = graph_name.substr(start_position, end_position);
  442. GE_CHK_BOOL_EXEC_WARN(!model_name.empty(), return FAILED, "Get model_name failed, check params --output");
  443. return SUCCESS;
  444. }
  445. } // namespace ge

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