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.

om_file_helper.cc 9.6 kB

5 years ago
5 years ago
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
4 years ago
5 years ago
4 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
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 "framework/common/helper/om_file_helper.h"
  17. #include <string>
  18. #include <vector>
  19. #include "common/math/math_util.h"
  20. #include "common/auth/file_saver.h"
  21. #include "framework/common/debug/log.h"
  22. #include "framework/common/debug/ge_log.h"
  23. #include "framework/common/ge_inner_error_codes.h"
  24. #include "framework/common/util.h"
  25. using std::string;
  26. namespace {
  27. const int32_t kOptionalNum = 2;
  28. }
  29. namespace ge {
  30. // For Load
  31. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status OmFileLoadHelper::Init(const ge::ModelData &model) {
  32. if (CheckModelValid(model) != SUCCESS) {
  33. return FAILED;
  34. }
  35. uint32_t model_data_size = model.model_len - sizeof(ModelFileHeader);
  36. uint8_t *model_data = static_cast<uint8_t *>(model.model_data) + sizeof(ModelFileHeader);
  37. Status ret = Init(model_data, model_data_size);
  38. return ret;
  39. }
  40. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status OmFileLoadHelper::Init(uint8_t *model_data,
  41. const uint32_t model_data_size) {
  42. Status status = LoadModelPartitionTable(model_data, model_data_size);
  43. if (status != SUCCESS) {
  44. return status;
  45. }
  46. is_inited_ = true;
  47. return SUCCESS;
  48. }
  49. // Use both
  50. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status OmFileLoadHelper::GetModelPartition(ModelPartitionType type,
  51. ModelPartition &partition) {
  52. if (!is_inited_) {
  53. GELOGE(PARAM_INVALID, "OmFileLoadHelper has not been initialized!");
  54. return PARAM_INVALID;
  55. }
  56. bool found = false;
  57. for (ModelPartition &part : context_.partition_datas_) {
  58. if (part.type == type) {
  59. partition = part;
  60. found = true;
  61. break;
  62. }
  63. }
  64. if (!found) {
  65. if (type != ModelPartitionType::TBE_KERNELS && type != ModelPartitionType::WEIGHTS_DATA &&
  66. type != ModelPartitionType::CUST_AICPU_KERNELS) {
  67. GELOGE(FAILED, "GetModelPartition:type:%d is not in partition_datas!", static_cast<int>(type));
  68. return FAILED;
  69. }
  70. }
  71. return SUCCESS;
  72. }
  73. Status OmFileLoadHelper::CheckModelValid(const ge::ModelData &model) const {
  74. // Parameter validity check
  75. if (model.model_data == nullptr) {
  76. GELOGE(PARAM_INVALID, "Model_data must not be null!");
  77. return PARAM_INVALID;
  78. }
  79. // Model length too small
  80. if (model.model_len < (sizeof(ModelFileHeader) + sizeof(ModelPartitionTable))) {
  81. GELOGE(PARAM_INVALID,
  82. "Invalid model. length[%u] < sizeof(ModelFileHeader)[%zu] + sizeof(ModelPartitionTable)[%zu].",
  83. model.model_len, sizeof(ModelFileHeader), sizeof(ModelPartitionTable));
  84. return PARAM_INVALID;
  85. }
  86. // Get file header
  87. auto model_header = reinterpret_cast<ModelFileHeader *>(model.model_data);
  88. // Determine whether the file length and magic number match
  89. if ((model_header->length != model.model_len - sizeof(ModelFileHeader)) ||
  90. (MODEL_FILE_MAGIC_NUM != model_header->magic)) {
  91. GELOGE(PARAM_INVALID,
  92. "Invalid model. file_header->length[%u] + sizeof(ModelFileHeader)[%zu] != model->model_len[%u] || "
  93. "MODEL_FILE_MAGIC_NUM[%u] != file_header->magic[%u]",
  94. model_header->length, sizeof(ModelFileHeader), model.model_len, MODEL_FILE_MAGIC_NUM, model_header->magic);
  95. return PARAM_INVALID;
  96. }
  97. return SUCCESS;
  98. }
  99. Status OmFileLoadHelper::LoadModelPartitionTable(uint8_t *model_data, const uint32_t model_data_size) {
  100. if (model_data == nullptr) {
  101. GELOGE(ACL_ERROR_GE_EXEC_MODEL_ADDR_INVALID, "Param model_data must not be null!");
  102. return ACL_ERROR_GE_EXEC_MODEL_ADDR_INVALID;
  103. }
  104. // Init partition table
  105. auto partition_table = reinterpret_cast<ModelPartitionTable *>(model_data);
  106. // Davinici model partition include graph-info weight-info task-info tbe-kernel :
  107. // Original model partition include graph-info
  108. if ((partition_table->num != PARTITION_SIZE) && (partition_table->num != (PARTITION_SIZE - 1)) &&
  109. (partition_table->num != (PARTITION_SIZE - kOptionalNum)) && (partition_table->num != 1)) {
  110. GELOGE(ACL_ERROR_GE_PARAM_INVALID, "Invalid partition_table->num:%u", partition_table->num);
  111. return ACL_ERROR_GE_PARAM_INVALID;
  112. }
  113. size_t mem_offset = SIZE_OF_MODEL_PARTITION_TABLE(*partition_table);
  114. GELOGD("ModelPartitionTable num :%u, ModelFileHeader length :%zu, ModelPartitionTable length :%zu",
  115. partition_table->num, sizeof(ModelFileHeader), mem_offset);
  116. if (model_data_size <= mem_offset) {
  117. GELOGE(ACL_ERROR_GE_EXEC_MODEL_DATA_SIZE_INVALID, "invalid model data, partition_table->num:%u, model data size %u",
  118. partition_table->num, model_data_size);
  119. return ACL_ERROR_GE_EXEC_MODEL_DATA_SIZE_INVALID;
  120. }
  121. for (uint32_t i = 0; i < partition_table->num; i++) {
  122. ModelPartition partition;
  123. partition.size = partition_table->partition[i].mem_size;
  124. partition.data = model_data + mem_offset;
  125. partition.type = partition_table->partition[i].type;
  126. context_.partition_datas_.push_back(partition);
  127. if (partition.size > model_data_size || mem_offset > model_data_size - partition.size) {
  128. GELOGE(ACL_ERROR_GE_EXEC_MODEL_DATA_SIZE_INVALID, "The partition size %zu is greater than the model data size %u.",
  129. partition.size + mem_offset, model_data_size);
  130. return ACL_ERROR_GE_EXEC_MODEL_DATA_SIZE_INVALID;
  131. }
  132. mem_offset += partition.size;
  133. GELOGD("Partition, type:%d, size:%u", static_cast<int>(partition.type), partition.size);
  134. }
  135. return SUCCESS;
  136. }
  137. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY const std::vector<ModelPartition>
  138. &OmFileSaveHelper::GetModelPartitions() const {
  139. return context_.partition_datas_;
  140. }
  141. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ModelPartitionTable *OmFileSaveHelper::GetPartitionTable() {
  142. auto partition_size = static_cast<uint32_t>(context_.partition_datas_.size());
  143. // Build ModelPartitionTable, flex array
  144. context_.partition_table_.clear();
  145. context_.partition_table_.resize(sizeof(ModelPartitionTable) + sizeof(ModelPartitionMemInfo) * partition_size, 0);
  146. auto partition_table = reinterpret_cast<ModelPartitionTable *>(context_.partition_table_.data());
  147. partition_table->num = partition_size;
  148. uint32_t mem_offset = 0;
  149. for (uint32_t i = 0; i < partition_size; i++) {
  150. ModelPartition partition = context_.partition_datas_[i];
  151. partition_table->partition[i] = {partition.type, mem_offset, partition.size};
  152. mem_offset += partition.size;
  153. GELOGD("Partition, type:%d, size:%u", static_cast<int>(partition.type), partition.size);
  154. }
  155. return partition_table;
  156. }
  157. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status OmFileSaveHelper::AddPartition(ModelPartition &partition) {
  158. if (ge::CheckUint32AddOverflow(context_.model_data_len_, partition.size) != SUCCESS) {
  159. GELOGE(FAILED, "UINT32 %u and %u addition can result in overflow!", context_.model_data_len_, partition.size);
  160. return FAILED;
  161. }
  162. context_.partition_datas_.push_back(partition);
  163. context_.model_data_len_ += partition.size;
  164. return SUCCESS;
  165. }
  166. Status OmFileSaveHelper::SaveModel(const SaveParam &save_param, const char *output_file, ModelBufferData &model,
  167. bool is_offline) {
  168. (void)save_param.cert_file;
  169. (void)save_param.ek_file;
  170. (void)save_param.encode_mode;
  171. (void)save_param.hw_key_file;
  172. (void)save_param.pri_key_file;
  173. Status ret = SaveModelToFile(output_file, model, is_offline);
  174. if (ret == SUCCESS) {
  175. GELOGD("Generate model with encrypt.");
  176. }
  177. return ret;
  178. }
  179. Status OmFileSaveHelper::SaveModelToFile(const char *output_file, ModelBufferData &model, bool is_offline) {
  180. #if !defined(NONSUPPORT_SAVE_TO_FILE)
  181. uint32_t model_data_len = context_.model_data_len_;
  182. if (model_data_len == 0) {
  183. GELOGE(domi::PARAM_INVALID, "Model data len error! should not be 0");
  184. return domi::PARAM_INVALID;
  185. }
  186. ModelPartitionTable *partition_table = GetPartitionTable();
  187. if (partition_table == nullptr) {
  188. GELOGE(ge::GE_GRAPH_SAVE_FAILED, "SaveModelToFile execute failed: partition_table is NULL.");
  189. return ge::GE_GRAPH_SAVE_FAILED;
  190. }
  191. uint32_t size_of_table = SIZE_OF_MODEL_PARTITION_TABLE(*partition_table);
  192. FMK_UINT32_ADDCHECK(size_of_table, model_data_len)
  193. model_header_.length = size_of_table + model_data_len;
  194. GELOGD("Sizeof(ModelFileHeader):%zu,sizeof(ModelPartitionTable):%u, model_data_len:%u, model_total_len:%zu",
  195. sizeof(ModelFileHeader), size_of_table, model_data_len, model_header_.length + sizeof(ModelFileHeader));
  196. std::vector<ModelPartition> partition_datas = context_.partition_datas_;
  197. Status ret;
  198. if (is_offline) {
  199. ret = FileSaver::SaveToFile(output_file, model_header_, *partition_table, partition_datas);
  200. } else {
  201. ret = FileSaver::SaveToBuffWithFileHeader(model_header_, *partition_table, partition_datas, model);
  202. }
  203. if (ret == SUCCESS) {
  204. GELOGD("Save model success without encrypt.");
  205. }
  206. return ret;
  207. #else
  208. return SUCCESS;
  209. #endif
  210. }
  211. } // namespace ge

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