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

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