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

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