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
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /**
  2. * Copyright 2019-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. GELOGI("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. GELOGI("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. GELOGI("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. GELOGI("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(MEMALLOC_FAILED, "Create model task def ptr failed");
  130. return FAILED;
  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(MEMALLOC_FAILED, "Alloc model task def buffer failed");
  139. return MEMALLOC_FAILED;
  140. }
  141. (void)model_task_def->SerializePartialToArray(task_buffer.GetData(), static_cast<int>(partition_task_size));
  142. GELOGI("TASK_INFO op_size:%d, stream_num:%u", model_task_def->op().size(), model_task_def->stream_num());
  143. GELOGI("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. GELOGI("Platform version save: %s", platform_version.c_str());
  155. errno_t err;
  156. err = memcpy_s(model_header.platform_version, PLATFORM_VERSION_LEN, platform_version.c_str(),
  157. platform_version.size() + 1);
  158. if (err != EOK) {
  159. GELOGE(MEMALLOC_FAILED, "ModelHelper SaveModel failed while allocating memory for platform_version.");
  160. return MEMALLOC_FAILED;
  161. }
  162. string version = reinterpret_cast<char *>(model_header.platform_version);
  163. GELOGI("Platform version save: %s", version.c_str());
  164. size_t name_size = ge_model->GetName().size();
  165. name_size = name_size > (MODEL_NAME_LENGTH - 1) ? (MODEL_NAME_LENGTH - 1) : name_size;
  166. err = memcpy_s(model_header.name, MODEL_NAME_LENGTH, ge_model->GetName().c_str(), name_size);
  167. if (err != EOK) {
  168. GELOGE(MEMALLOC_FAILED, "ModelHelper SaveModel failed while allocating memory for name");
  169. return MEMALLOC_FAILED;
  170. }
  171. string model_name = reinterpret_cast<char *>(model_header.name);
  172. GELOGI("Model name save:%s", model_name.c_str());
  173. Status ret = om_file_save_helper->SaveModel(save_param, output_file.c_str(), model, is_offline_);
  174. if (ret != SUCCESS) {
  175. GELOGE(FAILED, "OmFileSaveHelper SaveModel return fail.");
  176. return FAILED;
  177. }
  178. return SUCCESS;
  179. }
  180. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status
  181. ModelHelper::SaveOriginalGraphToOmModel(const ge::Graph &graph, const std::string &output_file) {
  182. if (output_file.empty()) {
  183. GELOGE(FAILED, "SaveModel received invalid file name prefix");
  184. return FAILED;
  185. }
  186. // Get computegraph from graph
  187. auto compute_graph = ge::GraphUtils::GetComputeGraph(graph);
  188. if (compute_graph == nullptr) {
  189. GELOGE(FAILED, "SaveModel fail for compute_graph null");
  190. return FAILED;
  191. }
  192. GE_DUMP(compute_graph, "OriginalGraph");
  193. // Model
  194. ModelPtr model_ptr = ge::MakeShared<ge::Model>();
  195. GE_CHECK_NOTNULL_EXEC(model_ptr, return MEMALLOC_FAILED);
  196. std::string original_model_name = compute_graph->GetName() + "_original";
  197. model_ptr->SetName(original_model_name);
  198. model_ptr->SetGraph(graph);
  199. model_ptr->SetVersion(static_cast<uint32_t>(OM_PROTO_VERSION));
  200. string framework_version;
  201. Status frame_rt = PlatformVersionManager::GetPlatformVersion(framework_version);
  202. if (frame_rt == SUCCESS) {
  203. uint32_t counter = 0;
  204. string model_framework_version = framework_version + "." + std::to_string(counter);
  205. model_ptr->SetPlatformVersion(model_framework_version);
  206. }
  207. // Model def
  208. ge::Buffer model_buffer;
  209. ge::graphStatus status = model_ptr->Save(model_buffer);
  210. if (status != ge::GRAPH_SUCCESS) {
  211. GELOGE(FAILED, "SaveModel fail for save buffer fail");
  212. return FAILED;
  213. }
  214. std::shared_ptr<OmFileSaveHelper> om_file_save_helper = ge::MakeShared<OmFileSaveHelper>();
  215. GE_CHECK_NOTNULL_EXEC(om_file_save_helper, return MEMALLOC_FAILED);
  216. ModelPartition partition_model;
  217. partition_model.data = model_buffer.GetData();
  218. partition_model.size = static_cast<uint32_t>(model_buffer.GetSize());
  219. partition_model.type = ModelPartitionType::MODEL_DEF;
  220. GELOGI("Original Model type[%u],size[%u]", partition_model.type, partition_model.size);
  221. if (partition_model.data != nullptr && partition_model.size > 0) {
  222. (void)om_file_save_helper->AddPartition(partition_model);
  223. // Condition of AddPartition is established, no need to check value
  224. }
  225. // Save target/version to model_header
  226. ModelFileHeader &model_header = om_file_save_helper->GetModelFileHeader();
  227. model_header.om_ir_version = model_ptr->GetVersion();
  228. model_header.headsize = MODEL_FILE_HEAD_LEN;
  229. std::string platform_version = model_ptr->GetPlatformVersion();
  230. errno_t err = memcpy_s(model_header.platform_version, PLATFORM_VERSION_LEN, platform_version.c_str(),
  231. platform_version.size() + 1);
  232. if (err != EOK) {
  233. GELOGE(FAILED, "ModelHelper SaveModel failed for platform_version");
  234. return FAILED;
  235. }
  236. size_t name_size = model_ptr->GetName().size();
  237. name_size = name_size > (MODEL_NAME_LENGTH - 1) ? (MODEL_NAME_LENGTH - 1) : name_size;
  238. err = memcpy_s(model_header.name, MODEL_NAME_LENGTH, model_ptr->GetName().c_str(), name_size);
  239. if (err != EOK) {
  240. GELOGE(FAILED, "ModelHelper SaveModel memory copy failed");
  241. return FAILED;
  242. }
  243. ModelBufferData model;
  244. Status ret = om_file_save_helper->SaveModelToFile(output_file.c_str(), model, is_offline_);
  245. return (ret == SUCCESS ? SUCCESS : FAILED);
  246. }
  247. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ModelHelper::LoadModel(const ge::ModelData &model_data) {
  248. if (model_data.model_data == nullptr || model_data.model_len == 0) {
  249. GELOGE(GE_EXEC_MODEL_DATA_SIZE_INVALID, "Model_data is nullptr, or model_data_size is 0");
  250. return GE_EXEC_MODEL_DATA_SIZE_INVALID;
  251. }
  252. if (is_assign_model_) {
  253. GELOGE(GE_EXEC_LOAD_MODEL_REPEATED, "Model helper has already loaded!");
  254. return GE_EXEC_LOAD_MODEL_REPEATED;
  255. }
  256. if (ReleaseLocalModelData() != SUCCESS) {
  257. GELOGE(INTERNAL_ERROR, "ReleaseLocalModelData failed.");
  258. return INTERNAL_ERROR;
  259. }
  260. Status status = ge::DavinciModelParser::ParseModelContent(model_data, model_addr_tmp_, model_len_tmp_);
  261. if (status != SUCCESS) {
  262. GELOGE(status, "Parse model content failed!");
  263. return status;
  264. }
  265. file_header_ = reinterpret_cast<ModelFileHeader *>(model_data.model_data);
  266. OmFileLoadHelper om_load_helper;
  267. status = om_load_helper.Init(model_addr_tmp_, model_len_tmp_);
  268. if (status != SUCCESS) {
  269. GELOGE(status, "Om_load_helper init failed");
  270. model_addr_tmp_ = nullptr;
  271. return status;
  272. }
  273. auto partition_table = reinterpret_cast<ModelPartitionTable *>(model_addr_tmp_);
  274. if (partition_table->num == kOriginalOmPartitionNum) {
  275. model_addr_tmp_ = nullptr;
  276. GELOGE(GE_EXEC_MODEL_PARTITION_NUM_INVALID, "om model is error,please use executable om model");
  277. return GE_EXEC_MODEL_PARTITION_NUM_INVALID;
  278. }
  279. // Encrypt model need to del temp model/no encrypt model don't need to del model
  280. model_addr_tmp_ = nullptr;
  281. status = GenerateGeModel(om_load_helper);
  282. if (status != SUCCESS) {
  283. GELOGE(status, "GenerateGeModel failed");
  284. return status;
  285. }
  286. is_assign_model_ = true;
  287. return SUCCESS;
  288. }
  289. Status ModelHelper::GenerateGeModel(OmFileLoadHelper &om_load_helper) {
  290. model_ = ge::MakeShared<ge::GeModel>();
  291. GE_CHECK_NOTNULL(model_);
  292. Status ret = LoadModelData(om_load_helper);
  293. if (ret != SUCCESS) {
  294. return GE_EXEC_LOAD_MODEL_PARTITION_FAILED;
  295. }
  296. ret = LoadWeights(om_load_helper);
  297. if (ret != SUCCESS) {
  298. return GE_EXEC_LOAD_WEIGHT_PARTITION_FAILED;
  299. }
  300. ret = LoadTask(om_load_helper);
  301. if (ret != SUCCESS) {
  302. return GE_EXEC_LOAD_TASK_PARTITION_FAILED;
  303. }
  304. ret = LoadTBEKernelStore(om_load_helper);
  305. if (ret != SUCCESS) {
  306. return GE_EXEC_LOAD_KERNEL_PARTITION_FAILED;
  307. }
  308. ret = LoadCustAICPUKernelStore(om_load_helper);
  309. if (ret != SUCCESS) {
  310. return GE_EXEC_LOAD_KERNEL_PARTITION_FAILED;
  311. }
  312. return SUCCESS;
  313. }
  314. Status ModelHelper::LoadModelData(OmFileLoadHelper &om_load_helper) {
  315. ModelPartition partition_model_def;
  316. // no need to check value, DATA->NetOutput
  317. om_load_helper.GetModelPartition(ModelPartitionType::MODEL_DEF, partition_model_def);
  318. GELOGI("Model_def partition addr:%p,size:%u", partition_model_def.data, partition_model_def.size);
  319. ge::Model model;
  320. if (ge::Model::Load(partition_model_def.data, partition_model_def.size, model) != SUCCESS) {
  321. GELOGE(INTERNAL_ERROR, "Load model failed.");
  322. return INTERNAL_ERROR;
  323. }
  324. SetModelToGeModel(model);
  325. return SUCCESS;
  326. }
  327. void ModelHelper::SetModelToGeModel(ge::Model &model) {
  328. model_->SetGraph(model.GetGraph());
  329. model_->SetName(model.GetName());
  330. model_->SetVersion(model.GetVersion());
  331. model_->SetPlatformVersion(model.GetPlatformVersion());
  332. model_->SetAttr(model.MutableAttrMap());
  333. }
  334. Status ModelHelper::LoadWeights(OmFileLoadHelper &om_load_helper) {
  335. ModelPartition partition;
  336. if (om_load_helper.GetModelPartition(ModelPartitionType::WEIGHTS_DATA, partition) != SUCCESS) {
  337. GELOGE(FAILED, "Get weight model partition failed.");
  338. return FAILED;
  339. }
  340. ge::Buffer weight = ge::Buffer::CopyFrom(partition.data, partition.size);
  341. model_->SetWeight(weight);
  342. GELOGI("GetWeight size:%u", partition.size);
  343. return SUCCESS;
  344. }
  345. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ModelHelper::LoadTask(OmFileLoadHelper &om_load_helper) {
  346. ModelPartition task_partition;
  347. if (om_load_helper.GetModelPartition(ModelPartitionType::TASK_INFO, task_partition) != SUCCESS) {
  348. GELOGE(FAILED, "Get task model partition failed.");
  349. return FAILED;
  350. }
  351. std::shared_ptr<ModelTaskDef> task = ge::MakeShared<ModelTaskDef>();
  352. GE_CHECK_NOTNULL(task);
  353. if (task_partition.size != 0) {
  354. if (!ReadProtoFromArray(task_partition.data, task_partition.size, task.get())) {
  355. GELOGE(INTERNAL_ERROR, "ReadProtoFromArray failed.");
  356. return INTERNAL_ERROR;
  357. }
  358. GELOGI("TASK_INFO op_size:%zu, stream_num:%u", task->op().size(), task->stream_num());
  359. }
  360. model_->SetModelTaskDef(task);
  361. return SUCCESS;
  362. }
  363. Status ModelHelper::LoadTBEKernelStore(OmFileLoadHelper &om_load_helper) {
  364. // Load tbe kernels
  365. ModelPartition partition_kernel_def;
  366. TBEKernelStore kernel_store;
  367. if (om_load_helper.GetModelPartition(ModelPartitionType::TBE_KERNELS, partition_kernel_def) == SUCCESS) {
  368. GELOGI("Kernels partition size:%u", partition_kernel_def.size);
  369. if (kernel_store.Load(partition_kernel_def.data, partition_kernel_def.size)) {
  370. GELOGI("Load tbe kernels success");
  371. } else {
  372. GELOGW("Load tbe kernels failed");
  373. }
  374. }
  375. model_->SetTBEKernelStore(kernel_store);
  376. return SUCCESS;
  377. }
  378. Status ModelHelper::LoadCustAICPUKernelStore(OmFileLoadHelper &om_load_helper) {
  379. // Load cust aicpu kernels
  380. ModelPartition partition_kernel_def;
  381. CustAICPUKernelStore kernel_store;
  382. if (om_load_helper.GetModelPartition(ModelPartitionType::CUST_AICPU_KERNELS, partition_kernel_def) == SUCCESS) {
  383. GELOGI("Kernels partition size:%u", partition_kernel_def.size);
  384. if (kernel_store.Load(partition_kernel_def.data, partition_kernel_def.size)) {
  385. GELOGI("Load cust aicpu kernels success");
  386. } else {
  387. GELOGW("Load cust aicpu kernels failed");
  388. }
  389. }
  390. model_->SetCustAICPUKernelStore(kernel_store);
  391. return SUCCESS;
  392. }
  393. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY GeModelPtr ModelHelper::GetGeModel() {
  394. if (model_ != nullptr) {
  395. return model_;
  396. }
  397. GELOGI("Model has not been loaded!");
  398. std::shared_ptr<ge::GeModel> out_model = ge::MakeShared<ge::GeModel>();
  399. if (out_model == nullptr) {
  400. return nullptr;
  401. }
  402. return out_model;
  403. }
  404. Status ModelHelper::ReleaseLocalModelData() noexcept {
  405. Status result = SUCCESS;
  406. if (model_addr_tmp_ != nullptr) {
  407. errno_t ret = memset_s(static_cast<void *>(model_addr_tmp_), model_len_tmp_, 0, model_len_tmp_);
  408. if (ret != EOK) {
  409. GELOGE(FAILED, "Failed to memset memory, error-code %d", ret);
  410. result = FAILED;
  411. }
  412. delete[] model_addr_tmp_;
  413. model_addr_tmp_ = nullptr;
  414. model_len_tmp_ = 0;
  415. }
  416. return result;
  417. }
  418. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ModelHelper::GetBaseNameFromFileName(
  419. const string &file_name, string &base_name) {
  420. GELOGD("Get base_name from file, file_name:%s", file_name.c_str());
  421. GE_CHK_BOOL_EXEC_WARN(!file_name.empty(), return FAILED, "File path may not valid, check params --output");
  422. size_t start_position = 0;
  423. // using output as base_name (ignore ".om")
  424. size_t filename_suffixes = 3;
  425. if (file_name.find_last_of('/') != string::npos) {
  426. start_position = file_name.find_last_of('/') + 1;
  427. }
  428. size_t end_position = file_name.length() - filename_suffixes;
  429. base_name = file_name.substr(start_position, end_position - start_position);
  430. GE_CHK_BOOL_EXEC_WARN(!base_name.empty(), return FAILED, "Get base_name failed, check params --output");
  431. return SUCCESS;
  432. }
  433. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ModelHelper::GetModelNameFromMergedGraphName(
  434. const string &graph_name, string &model_name) {
  435. GELOGD("Get model_name from graph_name, graph_name:%s", graph_name.c_str());
  436. // this can only be used after merged graph(graph name will be append with "_x", x is index);
  437. GE_CHK_BOOL_EXEC_WARN(!graph_name.empty(), return FAILED, "File path may not valid, check params --output");
  438. size_t start_position = 0;
  439. size_t end_position = graph_name.length();
  440. // using graph as model_name (ignore "_x", x is the index of graph)
  441. if (graph_name.find_last_of('_') != string::npos) {
  442. end_position = graph_name.find_last_of('_');
  443. }
  444. model_name = graph_name.substr(start_position, end_position);
  445. GE_CHK_BOOL_EXEC_WARN(!model_name.empty(), return FAILED, "Get model_name failed, check params --output");
  446. return SUCCESS;
  447. }
  448. } // namespace ge

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