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

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