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

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