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
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
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 %u: 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(ACL_ERROR_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 ACL_ERROR_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 %u 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(ACL_ERROR_GE_EXEC_MODEL_DATA_SIZE_INVALID,
  214. "The partition size %u is greater than the model data size %u.",
  215. partition.size + cur_offset, model_data_size);
  216. return ACL_ERROR_GE_EXEC_MODEL_DATA_SIZE_INVALID;
  217. }
  218. cur_offset += partition.size;
  219. GELOGD("Partition, type:%d, size:%u, model_index:%u", static_cast<int>(partition.type), partition.size, index);
  220. }
  221. }
  222. if (cur_offset != model_data_size) {
  223. GELOGE(FAILED, "do not get the complete model, read end offset:%u, all size:%u", cur_offset, model_data_size);
  224. return FAILED;
  225. }
  226. return SUCCESS;
  227. }
  228. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY const std::vector<ModelPartition>
  229. &OmFileSaveHelper::GetModelPartitions() const {
  230. return context_.partition_datas_;
  231. }
  232. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ModelPartitionTable *OmFileSaveHelper::GetPartitionTable() {
  233. auto partition_size = static_cast<uint32_t>(context_.partition_datas_.size());
  234. // Build ModelPartitionTable, flex array
  235. context_.partition_table_.clear();
  236. context_.partition_table_.resize(sizeof(ModelPartitionTable) + sizeof(ModelPartitionMemInfo) * partition_size, 0);
  237. auto partition_table = reinterpret_cast<ModelPartitionTable *>(context_.partition_table_.data());
  238. partition_table->num = partition_size;
  239. uint32_t mem_offset = 0;
  240. for (uint32_t i = 0; i < partition_size; i++) {
  241. ModelPartition partition = context_.partition_datas_[i];
  242. partition_table->partition[i] = {partition.type, mem_offset, partition.size};
  243. mem_offset += partition.size;
  244. GELOGD("Partition, type:%d, size:%u", static_cast<int>(partition.type), partition.size);
  245. }
  246. return partition_table;
  247. }
  248. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ModelPartitionTable *OmFileSaveHelper::GetPartitionTable(
  249. size_t cur_ctx_index) {
  250. auto &cur_ctx = model_contexts_[cur_ctx_index];
  251. auto partition_size = static_cast<uint32_t>(cur_ctx.partition_datas_.size());
  252. // Build ModelPartitionTable, flex array
  253. cur_ctx.partition_table_.clear();
  254. cur_ctx.partition_table_.resize(sizeof(ModelPartitionTable) + sizeof(ModelPartitionMemInfo) * partition_size, 0);
  255. auto partition_table = reinterpret_cast<ModelPartitionTable *>(cur_ctx.partition_table_.data());
  256. partition_table->num = partition_size;
  257. uint32_t mem_offset = 0;
  258. for (uint32_t i = 0; i < partition_size; i++) {
  259. ModelPartition partition = cur_ctx.partition_datas_[i];
  260. partition_table->partition[i] = {partition.type, mem_offset, partition.size};
  261. mem_offset += partition.size;
  262. GELOGD("Partition, type:%d, size:%u", static_cast<int>(partition.type), partition.size);
  263. }
  264. return partition_table;
  265. }
  266. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status OmFileSaveHelper::AddPartition(ModelPartition &partition) {
  267. if (ge::CheckUint32AddOverflow(context_.model_data_len_, partition.size) != SUCCESS) {
  268. GELOGE(FAILED, "UINT32 %u and %u addition can result in overflow!", context_.model_data_len_, partition.size);
  269. return FAILED;
  270. }
  271. context_.partition_datas_.push_back(partition);
  272. context_.model_data_len_ += partition.size;
  273. return SUCCESS;
  274. }
  275. Status OmFileSaveHelper::AddPartition(ModelPartition &partition, size_t cur_index) {
  276. if (ge::CheckUint32AddOverflow(context_.model_data_len_, partition.size) != SUCCESS) {
  277. GELOGE(FAILED, "UINT32 %u and %u addition can result in overflow!", context_.model_data_len_, partition.size);
  278. return FAILED;
  279. }
  280. if (cur_index >= model_contexts_.size()) {
  281. if (cur_index != model_contexts_.size()) {
  282. GELOGE(FAILED, "cur index is %zu make model_contexts_ overflow", cur_index);
  283. return FAILED;
  284. }
  285. OmFileContext tmp_ctx;
  286. tmp_ctx.model_data_len_ += partition.size;
  287. tmp_ctx.partition_datas_.push_back(partition);
  288. model_contexts_.push_back(tmp_ctx);
  289. } else {
  290. model_contexts_[cur_index].model_data_len_ += partition.size;
  291. model_contexts_[cur_index].partition_datas_.push_back(partition);
  292. }
  293. return SUCCESS;
  294. }
  295. Status OmFileSaveHelper::SaveModel(const SaveParam &save_param, const char *output_file, ModelBufferData &model,
  296. bool is_offline) {
  297. (void)save_param.cert_file;
  298. (void)save_param.ek_file;
  299. (void)save_param.encode_mode;
  300. (void)save_param.hw_key_file;
  301. (void)save_param.pri_key_file;
  302. Status ret = SaveModelToFile(output_file, model, is_offline);
  303. if (ret == SUCCESS) {
  304. GELOGD("Generate model with encrypt.");
  305. }
  306. return ret;
  307. }
  308. Status OmFileSaveHelper::SaveModelToFile(const char *output_file, ModelBufferData &model, bool is_offline) {
  309. #if !defined(NONSUPPORT_SAVE_TO_FILE)
  310. if (context_.partition_datas_.empty()) {
  311. GE_CHK_BOOL_EXEC(!model_contexts_.empty(), return FAILED, "mode contexts empty");
  312. context_ = model_contexts_.front();
  313. }
  314. uint32_t model_data_len = context_.model_data_len_;
  315. if (model_data_len == 0) {
  316. GELOGE(domi::PARAM_INVALID, "Model data len error! should not be 0");
  317. return domi::PARAM_INVALID;
  318. }
  319. ModelPartitionTable *partition_table = GetPartitionTable();
  320. if (partition_table == nullptr) {
  321. GELOGE(ge::GE_GRAPH_SAVE_FAILED, "SaveModelToFile execute failed: partition_table is NULL.");
  322. return ge::GE_GRAPH_SAVE_FAILED;
  323. }
  324. uint32_t size_of_table = SIZE_OF_MODEL_PARTITION_TABLE(*partition_table);
  325. FMK_UINT32_ADDCHECK(size_of_table, model_data_len)
  326. model_header_.length = size_of_table + model_data_len;
  327. GELOGD("Sizeof(ModelFileHeader):%zu,sizeof(ModelPartitionTable):%u, model_data_len:%u, model_total_len:%zu",
  328. sizeof(ModelFileHeader), size_of_table, model_data_len, model_header_.length + sizeof(ModelFileHeader));
  329. std::vector<ModelPartition> partition_datas = context_.partition_datas_;
  330. Status ret;
  331. if (is_offline) {
  332. ret = FileSaver::SaveToFile(output_file, model_header_, *partition_table, partition_datas);
  333. } else {
  334. ret = FileSaver::SaveToBuffWithFileHeader(model_header_, *partition_table, partition_datas, model);
  335. }
  336. if (ret == SUCCESS) {
  337. GELOGD("Save model success without encrypt.");
  338. }
  339. return ret;
  340. #else
  341. return SUCCESS;
  342. #endif
  343. }
  344. Status OmFileSaveHelper::SaveRootModel(const SaveParam &save_param, const char *output_file,
  345. ModelBufferData &model, bool is_offline) {
  346. (void)save_param.cert_file;
  347. (void)save_param.ek_file;
  348. (void)save_param.encode_mode;
  349. (void)save_param.hw_key_file;
  350. (void)save_param.pri_key_file;
  351. #if !defined(NONSUPPORT_SAVE_TO_FILE)
  352. vector<ModelPartitionTable *> model_partition_tabels;
  353. vector<vector<ModelPartition>> all_model_partitions;
  354. for (size_t ctx_index = 0; ctx_index < model_contexts_.size(); ++ctx_index) {
  355. auto &cur_ctx = model_contexts_[ctx_index];
  356. uint32_t cur_model_data_len = cur_ctx.model_data_len_;
  357. if (cur_model_data_len == 0) {
  358. GELOGE(domi::PARAM_INVALID, "Model data len error! should not be 0");
  359. return domi::PARAM_INVALID;
  360. }
  361. auto tmp_table = GetPartitionTable(ctx_index);
  362. if (tmp_table == nullptr) {
  363. GELOGE(ge::GE_GRAPH_SAVE_FAILED, "SaveModelToFile execute failed: partition_table is NULL.");
  364. return ge::GE_GRAPH_SAVE_FAILED;
  365. }
  366. uint32_t size_of_table = SIZE_OF_MODEL_PARTITION_TABLE(*tmp_table);
  367. FMK_UINT32_ADDCHECK(size_of_table, cur_model_data_len)
  368. FMK_UINT32_ADDCHECK(size_of_table + cur_model_data_len, model_header_.length)
  369. model_header_.length += size_of_table + cur_model_data_len;
  370. model_partition_tabels.push_back(tmp_table);
  371. all_model_partitions.push_back(cur_ctx.partition_datas_);
  372. GELOGD("sizeof(ModelPartitionTable):%u, cur_model_data_len:%u, cur_context_index:%zu",
  373. size_of_table, cur_model_data_len, ctx_index);
  374. }
  375. Status ret;
  376. if (is_offline) {
  377. ret = FileSaver::SaveToFile(output_file, model_header_, model_partition_tabels, all_model_partitions);
  378. } else {
  379. GELOGW("do not support save ge root model to buff now");
  380. return FAILED;
  381. }
  382. if (ret == SUCCESS) {
  383. GELOGD("Save model success without encrypt.");
  384. }
  385. return ret;
  386. #else
  387. return SUCCESS;
  388. #endif
  389. }
  390. } // namespace ge

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