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_parser.cc 5.6 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "common/model_parser/model_parser.h"
  17. #include <fstream>
  18. #include <string>
  19. #include "securec.h"
  20. #include "framework/common/helper/model_helper.h"
  21. namespace ge {
  22. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ModelParserBase::ModelParserBase() {}
  23. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ModelParserBase::~ModelParserBase() {}
  24. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ModelParserBase::LoadFromFile(const char *model_path,
  25. int32_t priority,
  26. ge::ModelData &model_data) {
  27. std::string real_path = RealPath(model_path);
  28. if (real_path.empty()) {
  29. GELOGE(ACL_ERROR_GE_EXEC_MODEL_PATH_INVALID, "[Check][Param]Model file path %s is invalid",
  30. model_path);
  31. REPORT_CALL_ERROR("E19999", "Model file path %s is invalid", model_path);
  32. return ACL_ERROR_GE_EXEC_MODEL_PATH_INVALID;
  33. }
  34. if (GetFileLength(model_path) == -1) {
  35. GELOGE(ACL_ERROR_GE_EXEC_MODEL_PATH_INVALID, "[Check][Param]File size not valid, file %s",
  36. model_path);
  37. REPORT_INNER_ERROR("E19999", "File size not valid, file %s", model_path);
  38. return ACL_ERROR_GE_EXEC_MODEL_PATH_INVALID;
  39. }
  40. std::ifstream fs(real_path.c_str(), std::ifstream::binary);
  41. if (!fs.is_open()) {
  42. GELOGE(ACL_ERROR_GE_EXEC_MODEL_PATH_INVALID, "[Open][File]Failed, file %s, error %s",
  43. model_path, strerror(errno));
  44. REPORT_CALL_ERROR("E19999", "Open file %s failed, error %s", model_path, strerror(errno));
  45. return ACL_ERROR_GE_EXEC_MODEL_PATH_INVALID;
  46. }
  47. // get length of file:
  48. (void)fs.seekg(0, std::ifstream::end);
  49. uint32_t len = static_cast<uint32_t>(fs.tellg());
  50. GE_CHECK_GE(len, 1);
  51. (void)fs.seekg(0, std::ifstream::beg);
  52. char *data = new (std::nothrow) char[len];
  53. if (data == nullptr) {
  54. GELOGE(ACL_ERROR_GE_MEMORY_ALLOCATION, "[Load][ModelFromFile]Failed, "
  55. "bad memory allocation occur(need %u), file %s", len, model_path);
  56. REPORT_CALL_ERROR("E19999", "Load model from file %s failed, "
  57. "bad memory allocation occur(need %u)", model_path, len);
  58. return ACL_ERROR_GE_MEMORY_ALLOCATION;
  59. }
  60. // read data as a block:
  61. (void)fs.read(data, len);
  62. ModelHelper model_helper;
  63. model_helper.GetBaseNameFromFileName(model_path, model_data.om_name);
  64. // Set the model data parameter
  65. model_data.model_data = data;
  66. model_data.model_len = len;
  67. model_data.priority = priority;
  68. return SUCCESS;
  69. }
  70. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ModelParserBase::ParseModelContent(const ge::ModelData &model,
  71. uint8_t *&model_data,
  72. uint32_t &model_len) {
  73. // Parameter validity check
  74. GE_CHECK_NOTNULL(model.model_data);
  75. // Model length too small
  76. GE_CHK_BOOL_EXEC(model.model_len >= sizeof(ModelFileHeader),
  77. REPORT_INPUT_ERROR("E10003", std::vector<std::string>({"parameter", "value", "reason"}),
  78. std::vector<std::string>({"om", model.om_name.c_str(), "invalid om file"}));
  79. GELOGE(ACL_ERROR_GE_EXEC_MODEL_DATA_SIZE_INVALID,
  80. "[Check][Param] Invalid model. Model data size %u must be greater than or equal to %zu.",
  81. model.model_len, sizeof(ModelFileHeader));
  82. return ACL_ERROR_GE_EXEC_MODEL_DATA_SIZE_INVALID;);
  83. // Get file header
  84. auto file_header = reinterpret_cast<ModelFileHeader *>(model.model_data);
  85. // Determine whether the file length and magic number match
  86. GE_CHK_BOOL_EXEC(file_header->length == model.model_len - sizeof(ModelFileHeader) &&
  87. file_header->magic == MODEL_FILE_MAGIC_NUM,
  88. REPORT_INPUT_ERROR("E10003", std::vector<std::string>({"parameter", "value", "reason"}),
  89. std::vector<std::string>({"om", model.om_name.c_str(), "invalid om file"}));
  90. GELOGE(ACL_ERROR_GE_EXEC_MODEL_DATA_SIZE_INVALID,
  91. "[Check][Param] Invalid model, file_header->length[%u] + sizeof(ModelFileHeader)[%zu] != "
  92. "model->model_len[%u] || MODEL_FILE_MAGIC_NUM[%u] != file_header->magic[%u]",
  93. file_header->length, sizeof(ModelFileHeader), model.model_len,
  94. MODEL_FILE_MAGIC_NUM, file_header->magic);
  95. return ACL_ERROR_GE_EXEC_MODEL_DATA_SIZE_INVALID;);
  96. Status res = SUCCESS;
  97. // Get data address
  98. uint8_t *data = reinterpret_cast<uint8_t *>(model.model_data) + sizeof(ModelFileHeader);
  99. model_data = data;
  100. model_len = file_header->length;
  101. GELOGD("Model_len is %u, model_file_head_len is %zu.", model_len, sizeof(ModelFileHeader));
  102. return res;
  103. }
  104. } // namespace ge

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