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

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