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

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