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 17 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
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
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 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
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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/auth/file_saver.h"
  20. #include "common/math/math_util.h"
  21. #include "framework/common/debug/ge_log.h"
  22. #include "framework/common/debug/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. 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. Status OmFileLoadHelper::Init(uint8_t *model_data, const uint32_t model_data_size) {
  41. Status status = LoadModelPartitionTable(model_data, model_data_size);
  42. if (status != SUCCESS) {
  43. return status;
  44. }
  45. is_inited_ = true;
  46. return SUCCESS;
  47. }
  48. Status OmFileLoadHelper::Init(uint8_t *model_data, uint32_t model_data_size, uint32_t model_num) {
  49. Status status = LoadModelPartitionTable(model_data, model_data_size, model_num);
  50. if (status != SUCCESS) {
  51. return status;
  52. }
  53. is_inited_ = true;
  54. return SUCCESS;
  55. }
  56. // Use both
  57. Status OmFileLoadHelper::GetModelPartition(ModelPartitionType type, ModelPartition &partition) {
  58. if (!is_inited_) {
  59. GELOGE(PARAM_INVALID, "OmFileLoadHelper has not been initialized!");
  60. return PARAM_INVALID;
  61. }
  62. bool found = false;
  63. for (ModelPartition &part : context_.partition_datas_) {
  64. if (part.type == type) {
  65. partition = part;
  66. found = true;
  67. break;
  68. }
  69. }
  70. if (!found) {
  71. if (type != ModelPartitionType::TBE_KERNELS && type != ModelPartitionType::WEIGHTS_DATA &&
  72. type != ModelPartitionType::CUST_AICPU_KERNELS) {
  73. GELOGE(FAILED, "GetModelPartition:type:%d is not in partition_datas!", static_cast<int>(type));
  74. return FAILED;
  75. }
  76. }
  77. return SUCCESS;
  78. }
  79. Status OmFileLoadHelper::GetModelPartition(ModelPartitionType type, ModelPartition &partition, size_t model_index) {
  80. if (!is_inited_) {
  81. GELOGE(PARAM_INVALID, "OmFileLoadHelper has not been initialized!");
  82. return PARAM_INVALID;
  83. }
  84. if (model_index >= model_contexts_.size()) {
  85. GELOGE(PARAM_INVALID, "cur index : %zu, model_contexts size:%zu", model_index, model_contexts_.size());
  86. return PARAM_INVALID;
  87. }
  88. auto &cur_ctx = model_contexts_[model_index];
  89. bool found = false;
  90. for (ModelPartition &part : cur_ctx.partition_datas_) {
  91. if (part.type == type) {
  92. partition = part;
  93. found = true;
  94. break;
  95. }
  96. }
  97. if (!found) {
  98. if (type != ModelPartitionType::TBE_KERNELS && type != ModelPartitionType::WEIGHTS_DATA &&
  99. type != ModelPartitionType::CUST_AICPU_KERNELS) {
  100. GELOGE(FAILED, "GetModelPartition:type:%d is not in partition_datas!", static_cast<int>(type));
  101. return FAILED;
  102. }
  103. }
  104. return SUCCESS;
  105. }
  106. Status OmFileLoadHelper::CheckModelValid(const ge::ModelData &model) const {
  107. // Parameter validity check
  108. if (model.model_data == nullptr) {
  109. GELOGE(PARAM_INVALID, "Model_data must not be null!");
  110. return PARAM_INVALID;
  111. }
  112. // Model length too small
  113. if (model.model_len < (sizeof(ModelFileHeader) + sizeof(ModelPartitionTable))) {
  114. GELOGE(PARAM_INVALID,
  115. "Invalid model. length[%u] < sizeof(ModelFileHeader)[%zu] + sizeof(ModelPartitionTable)[%zu].",
  116. model.model_len, sizeof(ModelFileHeader), sizeof(ModelPartitionTable));
  117. return PARAM_INVALID;
  118. }
  119. // Get file header
  120. auto model_header = reinterpret_cast<ModelFileHeader *>(model.model_data);
  121. // Determine whether the file length and magic number match
  122. if ((model_header->length != model.model_len - sizeof(ModelFileHeader)) ||
  123. (MODEL_FILE_MAGIC_NUM != model_header->magic)) {
  124. GELOGE(PARAM_INVALID,
  125. "Invalid model. file_header->length[%u] + sizeof(ModelFileHeader)[%zu] != model->model_len[%u] || "
  126. "MODEL_FILE_MAGIC_NUM[%u] != file_header->magic[%u]",
  127. model_header->length, sizeof(ModelFileHeader), model.model_len, MODEL_FILE_MAGIC_NUM, model_header->magic);
  128. return PARAM_INVALID;
  129. }
  130. return SUCCESS;
  131. }
  132. Status OmFileLoadHelper::LoadModelPartitionTable(uint8_t *model_data, const uint32_t model_data_size) {
  133. if (model_data == nullptr) {
  134. GELOGE(ACL_ERROR_GE_EXEC_MODEL_ADDR_INVALID, "Param model_data must not be null!");
  135. return ACL_ERROR_GE_EXEC_MODEL_ADDR_INVALID;
  136. }
  137. // Init partition table
  138. auto partition_table = reinterpret_cast<ModelPartitionTable *>(model_data);
  139. // Davinici model partition include graph-info weight-info task-info tbe-kernel :
  140. // Original model partition include graph-info
  141. if ((partition_table->num != PARTITION_SIZE) && (partition_table->num != (PARTITION_SIZE - 1)) &&
  142. (partition_table->num != (PARTITION_SIZE - kOptionalNum)) && (partition_table->num != 1)) {
  143. GELOGE(ACL_ERROR_GE_PARAM_INVALID, "Invalid partition_table->num:%u", partition_table->num);
  144. return ACL_ERROR_GE_PARAM_INVALID;
  145. }
  146. size_t mem_offset = SIZE_OF_MODEL_PARTITION_TABLE(*partition_table);
  147. GELOGD("ModelPartitionTable num:%u, ModelFileHeader length:%zu, ModelPartitionTable length:%zu",
  148. partition_table->num, sizeof(ModelFileHeader), mem_offset);
  149. if (model_data_size <= mem_offset) {
  150. GELOGE(ACL_ERROR_GE_EXEC_MODEL_DATA_SIZE_INVALID, "invalid model data, partition_table->num:%u, model data size %u",
  151. partition_table->num, model_data_size);
  152. return ACL_ERROR_GE_EXEC_MODEL_DATA_SIZE_INVALID;
  153. }
  154. for (uint32_t i = 0; i < partition_table->num; i++) {
  155. ModelPartition partition;
  156. partition.size = partition_table->partition[i].mem_size;
  157. partition.data = model_data + mem_offset;
  158. partition.type = partition_table->partition[i].type;
  159. context_.partition_datas_.push_back(partition);
  160. if (partition.size > model_data_size || mem_offset > model_data_size - partition.size) {
  161. GELOGE(ACL_ERROR_GE_EXEC_MODEL_DATA_SIZE_INVALID,
  162. "The partition size %zu is greater than the model data size %u.",
  163. partition.size + mem_offset, model_data_size);
  164. return ACL_ERROR_GE_EXEC_MODEL_DATA_SIZE_INVALID;
  165. }
  166. mem_offset += partition.size;
  167. GELOGD("Partition, type:%d, size:%u", static_cast<int>(partition.type), partition.size);
  168. }
  169. return SUCCESS;
  170. }
  171. Status OmFileLoadHelper::LoadModelPartitionTable(uint8_t *model_data, uint32_t model_data_size, uint32_t model_num) {
  172. if (model_data == nullptr) {
  173. GELOGE(PARAM_INVALID, "Param model_data must not be null!");
  174. return PARAM_INVALID;
  175. }
  176. uint32_t cur_offset = 0;
  177. for (uint32_t index = 0; index < model_num; ++index) {
  178. // Init partition table
  179. auto partition_table = reinterpret_cast<ModelPartitionTable *>(model_data + cur_offset);
  180. size_t partition_table_size = SIZE_OF_MODEL_PARTITION_TABLE(*partition_table);
  181. cur_offset += partition_table_size;
  182. GELOGD("Cur model index %u: ModelPartitionTable num :%u, "
  183. "ModelFileHeader length :%zu, ModelPartitionTable length :%zu",
  184. index, partition_table->num, sizeof(ModelFileHeader), partition_table_size);
  185. if (model_data_size <= cur_offset) {
  186. GELOGE(ACL_ERROR_GE_EXEC_MODEL_DATA_SIZE_INVALID,
  187. "invalid model data, partition_table->num:%u, model data size %u",
  188. partition_table->num, model_data_size);
  189. return ACL_ERROR_GE_EXEC_MODEL_DATA_SIZE_INVALID;
  190. }
  191. for (uint32_t i = 0; i < partition_table->num; i++) {
  192. ModelPartition partition;
  193. partition.size = partition_table->partition[i].mem_size;
  194. partition.data = model_data + cur_offset;
  195. partition.type = partition_table->partition[i].type;
  196. if (index >= model_contexts_.size()) {
  197. if (index != model_contexts_.size()) {
  198. GELOGE(FAILED, "cur index is %u make model_contexts_ overflow", index);
  199. return FAILED;
  200. }
  201. OmFileContext tmp_ctx;
  202. tmp_ctx.partition_datas_.push_back(partition);
  203. model_contexts_.push_back(tmp_ctx);
  204. } else {
  205. model_contexts_[index].partition_datas_.push_back(partition);
  206. }
  207. if (partition.size > model_data_size || cur_offset > model_data_size - partition.size) {
  208. GELOGE(ACL_ERROR_GE_EXEC_MODEL_DATA_SIZE_INVALID,
  209. "The partition size %u is greater than the model data size %u.",
  210. partition.size + cur_offset, model_data_size);
  211. return ACL_ERROR_GE_EXEC_MODEL_DATA_SIZE_INVALID;
  212. }
  213. cur_offset += partition.size;
  214. GELOGD("Partition, type:%d, size:%u, model_index:%u", static_cast<int>(partition.type), partition.size, index);
  215. }
  216. }
  217. if (cur_offset != model_data_size) {
  218. GELOGE(FAILED, "do not get the complete model, read end offset:%u, all size:%u", cur_offset, model_data_size);
  219. return FAILED;
  220. }
  221. return SUCCESS;
  222. }
  223. const std::vector<ModelPartition> &OmFileSaveHelper::GetModelPartitions() const {
  224. return context_.partition_datas_;
  225. }
  226. ModelPartitionTable *OmFileSaveHelper::GetPartitionTable() {
  227. auto partition_size = static_cast<uint32_t>(context_.partition_datas_.size());
  228. // Build ModelPartitionTable, flex array
  229. context_.partition_table_.clear();
  230. context_.partition_table_.resize(sizeof(ModelPartitionTable) + sizeof(ModelPartitionMemInfo) * partition_size, 0);
  231. auto partition_table = reinterpret_cast<ModelPartitionTable *>(context_.partition_table_.data());
  232. partition_table->num = partition_size;
  233. uint32_t mem_offset = 0;
  234. for (uint32_t i = 0; i < partition_size; i++) {
  235. ModelPartition partition = context_.partition_datas_[i];
  236. partition_table->partition[i] = {partition.type, mem_offset, partition.size};
  237. mem_offset += partition.size;
  238. GELOGD("Partition, type:%d, size:%u", static_cast<int>(partition.type), partition.size);
  239. }
  240. return partition_table;
  241. }
  242. ModelPartitionTable *OmFileSaveHelper::GetPartitionTable(size_t cur_ctx_index) {
  243. auto &cur_ctx = model_contexts_[cur_ctx_index];
  244. auto partition_size = static_cast<uint32_t>(cur_ctx.partition_datas_.size());
  245. // Build ModelPartitionTable, flex array
  246. cur_ctx.partition_table_.clear();
  247. cur_ctx.partition_table_.resize(sizeof(ModelPartitionTable) + sizeof(ModelPartitionMemInfo) * partition_size, 0);
  248. auto partition_table = reinterpret_cast<ModelPartitionTable *>(cur_ctx.partition_table_.data());
  249. partition_table->num = partition_size;
  250. uint32_t mem_offset = 0;
  251. for (uint32_t i = 0; i < partition_size; i++) {
  252. ModelPartition partition = cur_ctx.partition_datas_[i];
  253. partition_table->partition[i] = {partition.type, mem_offset, partition.size};
  254. mem_offset += partition.size;
  255. GELOGD("Partition, type:%d, size:%u", static_cast<int>(partition.type), partition.size);
  256. }
  257. return partition_table;
  258. }
  259. Status OmFileSaveHelper::AddPartition(ModelPartition &partition) {
  260. if (ge::CheckUint32AddOverflow(context_.model_data_len_, partition.size) != SUCCESS) {
  261. GELOGE(FAILED, "UINT32 %u and %u addition can result in overflow!", context_.model_data_len_, partition.size);
  262. return FAILED;
  263. }
  264. context_.partition_datas_.push_back(partition);
  265. context_.model_data_len_ += partition.size;
  266. return SUCCESS;
  267. }
  268. Status OmFileSaveHelper::AddPartition(ModelPartition &partition, size_t cur_index) {
  269. if (ge::CheckUint32AddOverflow(context_.model_data_len_, partition.size) != SUCCESS) {
  270. GELOGE(FAILED, "UINT32 %u and %u addition can result in overflow!", context_.model_data_len_, partition.size);
  271. return FAILED;
  272. }
  273. if (cur_index >= model_contexts_.size()) {
  274. if (cur_index != model_contexts_.size()) {
  275. GELOGE(FAILED, "cur index is %zu make model_contexts_ overflow", cur_index);
  276. return FAILED;
  277. }
  278. OmFileContext tmp_ctx;
  279. tmp_ctx.model_data_len_ += partition.size;
  280. tmp_ctx.partition_datas_.push_back(partition);
  281. model_contexts_.push_back(tmp_ctx);
  282. } else {
  283. model_contexts_[cur_index].model_data_len_ += partition.size;
  284. model_contexts_[cur_index].partition_datas_.push_back(partition);
  285. }
  286. return SUCCESS;
  287. }
  288. Status OmFileSaveHelper::SaveModel(const SaveParam &save_param, const char *output_file, ModelBufferData &model,
  289. bool is_offline) {
  290. (void)save_param.cert_file;
  291. (void)save_param.ek_file;
  292. (void)save_param.encode_mode;
  293. (void)save_param.hw_key_file;
  294. (void)save_param.pri_key_file;
  295. Status ret = SaveModelToFile(output_file, model, is_offline);
  296. if (ret == SUCCESS) {
  297. GELOGD("Generate model with encrypt.");
  298. }
  299. return ret;
  300. }
  301. Status OmFileSaveHelper::SaveModelToFile(const char *output_file, ModelBufferData &model, bool is_offline) {
  302. #if !defined(NONSUPPORT_SAVE_TO_FILE)
  303. if (context_.partition_datas_.empty()) {
  304. GE_CHK_BOOL_EXEC(!model_contexts_.empty(), return FAILED, "mode contexts empty");
  305. context_ = model_contexts_.front();
  306. }
  307. uint32_t model_data_len = context_.model_data_len_;
  308. if (model_data_len == 0) {
  309. GELOGE(domi::PARAM_INVALID, "Model data len error! should not be 0");
  310. return domi::PARAM_INVALID;
  311. }
  312. ModelPartitionTable *partition_table = GetPartitionTable();
  313. if (partition_table == nullptr) {
  314. GELOGE(ge::GE_GRAPH_SAVE_FAILED, "SaveModelToFile execute failed: partition_table is NULL.");
  315. return ge::GE_GRAPH_SAVE_FAILED;
  316. }
  317. uint32_t size_of_table = SIZE_OF_MODEL_PARTITION_TABLE(*partition_table);
  318. FMK_UINT32_ADDCHECK(size_of_table, model_data_len)
  319. model_header_.length = size_of_table + model_data_len;
  320. GELOGD("Sizeof(ModelFileHeader):%zu,sizeof(ModelPartitionTable):%u, model_data_len:%u, model_total_len:%zu",
  321. sizeof(ModelFileHeader), size_of_table, model_data_len, model_header_.length + sizeof(ModelFileHeader));
  322. std::vector<ModelPartition> partition_datas = context_.partition_datas_;
  323. Status ret;
  324. if (is_offline) {
  325. ret = FileSaver::SaveToFile(output_file, model_header_, *partition_table, partition_datas);
  326. } else {
  327. ret = FileSaver::SaveToBuffWithFileHeader(model_header_, *partition_table, partition_datas, model);
  328. }
  329. if (ret == SUCCESS) {
  330. GELOGD("Save model success without encrypt.");
  331. }
  332. return ret;
  333. #else
  334. return SUCCESS;
  335. #endif
  336. }
  337. Status OmFileSaveHelper::SaveRootModel(const SaveParam &save_param, const char *output_file, ModelBufferData &model,
  338. bool is_offline) {
  339. (void)save_param.cert_file;
  340. (void)save_param.ek_file;
  341. (void)save_param.encode_mode;
  342. (void)save_param.hw_key_file;
  343. (void)save_param.pri_key_file;
  344. #if !defined(NONSUPPORT_SAVE_TO_FILE)
  345. vector<ModelPartitionTable *> model_partition_tabels;
  346. vector<vector<ModelPartition>> all_model_partitions;
  347. for (size_t ctx_index = 0; ctx_index < model_contexts_.size(); ++ctx_index) {
  348. auto &cur_ctx = model_contexts_[ctx_index];
  349. uint32_t cur_model_data_len = cur_ctx.model_data_len_;
  350. if (cur_model_data_len == 0) {
  351. GELOGE(domi::PARAM_INVALID, "Model data len error! should not be 0");
  352. return domi::PARAM_INVALID;
  353. }
  354. auto tmp_table = GetPartitionTable(ctx_index);
  355. if (tmp_table == nullptr) {
  356. GELOGE(ge::GE_GRAPH_SAVE_FAILED, "SaveModelToFile execute failed: partition_table is NULL.");
  357. return ge::GE_GRAPH_SAVE_FAILED;
  358. }
  359. uint32_t size_of_table = SIZE_OF_MODEL_PARTITION_TABLE(*tmp_table);
  360. FMK_UINT32_ADDCHECK(size_of_table, cur_model_data_len)
  361. FMK_UINT32_ADDCHECK(size_of_table + cur_model_data_len, model_header_.length)
  362. model_header_.length += size_of_table + cur_model_data_len;
  363. model_partition_tabels.push_back(tmp_table);
  364. all_model_partitions.push_back(cur_ctx.partition_datas_);
  365. GELOGD("sizeof(ModelPartitionTable):%u, cur_model_data_len:%u, cur_context_index:%zu", size_of_table,
  366. cur_model_data_len, ctx_index);
  367. }
  368. Status ret;
  369. if (is_offline) {
  370. ret = FileSaver::SaveToFile(output_file, model_header_, model_partition_tabels, all_model_partitions);
  371. } else {
  372. ret = FileSaver::SaveToBuffWithFileHeader(model_header_, model_partition_tabels, all_model_partitions, model);
  373. }
  374. if (ret == SUCCESS) {
  375. GELOGD("Save model success without encrypt.");
  376. }
  377. return ret;
  378. #else
  379. return SUCCESS;
  380. #endif
  381. }
  382. } // namespace ge

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