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.

file_saver.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
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
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
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 "common/auth/file_saver.h"
  17. #include <securec.h>
  18. #include <cstdlib>
  19. #include <fstream>
  20. #include <vector>
  21. #include "common/math/math_util.h"
  22. #include "framework/common/debug/ge_log.h"
  23. #include "framework/common/debug/log.h"
  24. #include "framework/common/util.h"
  25. namespace {
  26. const int kFileOpSuccess = 0;
  27. } // namespace
  28. namespace ge {
  29. Status FileSaver::OpenFile(int32_t &fd, const std::string &file_path) {
  30. if (CheckPath(file_path) != SUCCESS) {
  31. GELOGE(FAILED, "[Check][FilePath]Check output file failed, file_path:%s.", file_path.c_str());
  32. REPORT_CALL_ERROR("E19999", "Check output file failed, file_path:%s.", file_path.c_str());
  33. return FAILED;
  34. }
  35. char real_path[MMPA_MAX_PATH] = {0};
  36. GE_IF_BOOL_EXEC(mmRealPath(file_path.c_str(), real_path, MMPA_MAX_PATH) != EN_OK,
  37. GELOGI("File %s is not exist, it will be created.", file_path.c_str()));
  38. // Open file
  39. mmMode_t mode = M_IRUSR | M_IWUSR;
  40. fd = mmOpen2(real_path, M_RDWR | M_CREAT | O_TRUNC, mode);
  41. if (fd == EN_INVALID_PARAM || fd == EN_ERROR) {
  42. // -1: Failed to open file; - 2: Illegal parameter
  43. GELOGE(FAILED, "[Open][File]Failed. mmpa_errno = %d, %s", fd, strerror(errno));
  44. REPORT_INNER_ERROR("E19999", "Open file failed, mmpa_errno = %d, error:%s.", fd, strerror(errno));
  45. return FAILED;
  46. }
  47. return SUCCESS;
  48. }
  49. Status FileSaver::WriteData(const void *data, uint32_t size, int32_t fd) {
  50. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(size == 0 || data == nullptr, return PARAM_INVALID);
  51. mmSsize_t write_count;
  52. uint32_t size_2g = 2147483648; // 0x1 << 31
  53. uint32_t size_1g = 1073741824; // 0x1 << 30
  54. // Write data
  55. if (size > size_2g) {
  56. auto seek = reinterpret_cast<uint8_t *>(const_cast<void *>(data));
  57. while (size > size_1g) {
  58. write_count = mmWrite(fd, reinterpret_cast<void *>(seek), size_1g);
  59. if (write_count == EN_INVALID_PARAM || write_count == EN_ERROR) {
  60. GELOGE(FAILED, "[Write][Data]Failed, mmpa_errorno = %ld, error:%s", write_count, strerror(errno));
  61. REPORT_INNER_ERROR("E19999", "Write data failed, mmpa_errorno = %ld, error:%s.",
  62. write_count, strerror(errno));
  63. return FAILED;
  64. }
  65. size -= size_1g;
  66. seek += size_1g;
  67. }
  68. write_count = mmWrite(fd, reinterpret_cast<void *>(seek), size);
  69. } else {
  70. write_count = mmWrite(fd, const_cast<void *>(data), size);
  71. }
  72. // -1: Failed to write to file; - 2: Illegal parameter
  73. if (write_count == EN_INVALID_PARAM || write_count == EN_ERROR) {
  74. GELOGE(FAILED, "[Write][Data]Failed. mmpa_errorno = %ld, error:%s", write_count, strerror(errno));
  75. REPORT_INNER_ERROR("E19999", "Write data failed, mmpa_errorno = %ld, error:%s.",
  76. write_count, strerror(errno));
  77. return FAILED;
  78. }
  79. return SUCCESS;
  80. }
  81. Status FileSaver::SaveWithFileHeader(const std::string &file_path, const ModelFileHeader &file_header, const void *data,
  82. int len) {
  83. if (data == nullptr || len <= 0) {
  84. GELOGE(FAILED, "[Check][Param]Failed, model_data is null or the length[%d] is less than 1.", len);
  85. REPORT_INNER_ERROR("E19999", "Save file failed, model_data is null or the length:%d is less than 1.", len);
  86. return FAILED;
  87. }
  88. // Open file
  89. int32_t fd = 0;
  90. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(OpenFile(fd, file_path) != SUCCESS, return FAILED, "OpenFile FAILED");
  91. Status ret = SUCCESS;
  92. do {
  93. // Write file header
  94. GE_CHK_BOOL_EXEC(WriteData(static_cast<const void *>(&file_header), sizeof(ModelFileHeader), fd) == SUCCESS,
  95. ret = FAILED;
  96. break, "WriteData FAILED");
  97. // write data
  98. GE_CHK_BOOL_EXEC(WriteData(data, static_cast<uint32_t>(len), fd) == SUCCESS, ret = FAILED, "WriteData FAILED");
  99. } while (0);
  100. // Close file
  101. if (mmClose(fd) != 0) { // mmClose 0: success
  102. GELOGE(FAILED, "[Close][File]Failed, error_code:%u errmsg:%s", ret, strerror(errno));
  103. REPORT_INNER_ERROR("E19999", "Close file failed, error_code:%u errmsg:%s", ret, strerror(errno));
  104. ret = FAILED;
  105. }
  106. return ret;
  107. }
  108. Status FileSaver::SaveWithFileHeader(const std::string &file_path, const ModelFileHeader &file_header,
  109. ModelPartitionTable &model_partition_table,
  110. const std::vector<ModelPartition> &partition_datas) {
  111. GE_CHK_BOOL_RET_STATUS(!partition_datas.empty() && model_partition_table.num != 0
  112. && model_partition_table.num == partition_datas.size(), FAILED,
  113. "Invalid param:partition data size is (%u), model_partition_table.num is (%zu).",
  114. model_partition_table.num, partition_datas.size());
  115. // Open file
  116. int32_t fd = 0;
  117. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(OpenFile(fd, file_path) != SUCCESS, return FAILED);
  118. Status ret = SUCCESS;
  119. do {
  120. // Write file header
  121. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
  122. WriteData(static_cast<const void *>(&file_header), sizeof(ModelFileHeader), fd) != SUCCESS, ret = FAILED;
  123. break);
  124. // Write model partition table
  125. uint32_t table_size = static_cast<uint32_t>(SIZE_OF_MODEL_PARTITION_TABLE(model_partition_table));
  126. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
  127. WriteData(static_cast<const void *>(&model_partition_table), table_size, fd) != SUCCESS, ret = FAILED; break);
  128. // Write partition data
  129. for (const auto &partitionData : partition_datas) {
  130. GELOGI("GC:size[%u]", partitionData.size);
  131. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
  132. WriteData(static_cast<const void *>(partitionData.data), partitionData.size, fd) != SUCCESS, ret = FAILED;
  133. break);
  134. }
  135. } while (0);
  136. // Close file
  137. if (mmClose(fd) != EN_OK) {
  138. GELOGE(FAILED, "[Close][File]Failed, error_code:%u errmsg:%s", ret, strerror(errno));
  139. REPORT_CALL_ERROR("E19999", "Close file failed, error_code:%u errmsg:%s", ret, strerror(errno));
  140. ret = FAILED;
  141. }
  142. return ret;
  143. }
  144. Status FileSaver::SaveToBuffWithFileHeader(const ModelFileHeader &file_header,
  145. ModelPartitionTable &model_partition_table,
  146. const std::vector<ModelPartition> &partition_datas,
  147. ge::ModelBufferData &model) {
  148. const vector<ModelPartitionTable *> model_partition_tables = { &model_partition_table };
  149. const std::vector<std::vector<ModelPartition>> all_partition_datas = { partition_datas };
  150. return SaveToBuffWithFileHeader(file_header, model_partition_tables, all_partition_datas, model);
  151. }
  152. Status FileSaver::SaveToBuffWithFileHeader(const ModelFileHeader &file_header,
  153. const vector<ModelPartitionTable *> &model_partition_tables,
  154. const std::vector<std::vector<ModelPartition>> &all_partition_datas,
  155. ge::ModelBufferData &model) {
  156. GE_CHK_BOOL_RET_STATUS(model_partition_tables.size() == all_partition_datas.size(), PARAM_INVALID,
  157. "Model table size %zu does not match partition size %zu.",
  158. model_partition_tables.size(), all_partition_datas.size());
  159. for (size_t index = 0; index < model_partition_tables.size(); ++index) {
  160. auto &cur_partiton_data = all_partition_datas[index];
  161. auto &cur_model_partition_table = *model_partition_tables[index];
  162. GE_CHK_BOOL_RET_STATUS(!cur_partiton_data.empty() && cur_model_partition_table.num != 0
  163. && cur_model_partition_table.num == cur_partiton_data.size(), FAILED,
  164. "Invalid param: partition data size is (%zu), model_partition_table.num is (%u).",
  165. cur_partiton_data.size(), cur_model_partition_table.num);
  166. }
  167. uint64_t model_header_size = sizeof(ModelFileHeader);
  168. uint64_t total_size = model_header_size;
  169. for (size_t index = 0; index < model_partition_tables.size(); ++index) {
  170. auto &cur_model_partition_table = *model_partition_tables[index];
  171. total_size += static_cast<uint64_t>(SIZE_OF_MODEL_PARTITION_TABLE(cur_model_partition_table));
  172. auto &cur_partition_data = all_partition_datas[index];
  173. for (const auto &partition_data : cur_partition_data) {
  174. auto ret = ge::CheckUint64AddOverflow(total_size, partition_data.size);
  175. GE_CHK_BOOL_RET_STATUS(ret == SUCCESS, FAILED, "Add uint64 overflow!");
  176. total_size += partition_data.size;
  177. }
  178. }
  179. // save to buff
  180. auto buff = reinterpret_cast<uint8_t *>(malloc(total_size));
  181. GE_CHK_BOOL_RET_STATUS(buff != nullptr, FAILED, "Malloc failed!");
  182. GE_PRINT_DYNAMIC_MEMORY(malloc, "File buffer.", total_size)
  183. model.data.reset(buff, [](uint8_t *buff) {
  184. GELOGD("Free online model memory.");
  185. free(buff);
  186. buff = nullptr;
  187. });
  188. model.length = total_size;
  189. uint64_t left_space = total_size;
  190. auto ret_mem = memcpy_s(buff, left_space, reinterpret_cast<void *>(const_cast<ModelFileHeader *>(&file_header)),
  191. model_header_size);
  192. GE_CHK_BOOL_RET_STATUS(ret_mem == EOK, FAILED, "Memcpy_s failed!");
  193. buff += model_header_size;
  194. left_space -= model_header_size;
  195. for (size_t index = 0; index < model_partition_tables.size(); ++index) {
  196. auto &cur_tabel = *model_partition_tables[index];
  197. uint64_t table_size = static_cast<uint64_t>(SIZE_OF_MODEL_PARTITION_TABLE(cur_tabel));
  198. ret_mem = memcpy_s(buff, left_space, reinterpret_cast<void *>(&cur_tabel), table_size);
  199. GE_CHK_BOOL_RET_STATUS(ret_mem == EOK, FAILED, "Memcpy_s failed!");
  200. buff += table_size;
  201. left_space -= table_size;
  202. auto &cur_partition_data = all_partition_datas[index];
  203. for (const auto &partition_data : cur_partition_data) {
  204. ret_mem = memcpy_s(buff, left_space, reinterpret_cast<void *>(const_cast<uint8_t *>(partition_data.data)),
  205. partition_data.size);
  206. GE_CHK_BOOL_RET_STATUS(ret_mem == EOK, FAILED, "Memcpy_s failed!");
  207. buff += partition_data.size;
  208. left_space -= partition_data.size;
  209. }
  210. }
  211. return SUCCESS;
  212. }
  213. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status FileSaver::CheckPath(const std::string &file_path) {
  214. // Determine file path length
  215. if (file_path.size() >= MMPA_MAX_PATH) {
  216. GELOGE(FAILED, "[Check][FilePath]Failed, file path's length:%zu > mmpa_max_path:%d",
  217. file_path.size(), MMPA_MAX_PATH);
  218. REPORT_INNER_ERROR("E19999", "Check file path failed, file path's length:%zu > mmpa_max_path:%d",
  219. file_path.size(), MMPA_MAX_PATH);
  220. return FAILED;
  221. }
  222. // Find the last separator
  223. int path_split_pos = static_cast<int>(file_path.size() - 1);
  224. for (; path_split_pos >= 0; path_split_pos--) {
  225. if (file_path[path_split_pos] == '\\' || file_path[path_split_pos] == '/') {
  226. break;
  227. }
  228. }
  229. if (path_split_pos == 0) {
  230. return SUCCESS;
  231. }
  232. // If there is a path before the file name, create the path
  233. if (path_split_pos != -1) {
  234. if (CreateDirectory(std::string(file_path).substr(0, static_cast<size_t>(path_split_pos))) != kFileOpSuccess) {
  235. GELOGE(FAILED, "[Create][Directory]Failed, file path:%s.", file_path.c_str());
  236. return FAILED;
  237. }
  238. }
  239. return SUCCESS;
  240. }
  241. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status
  242. FileSaver::SaveToFile(const string &file_path, const ge::ModelData &model, const ModelFileHeader *model_file_header) {
  243. if (file_path.empty() || model.model_data == nullptr || model.model_len == 0) {
  244. GELOGE(FAILED, "[Save][File]Incorrect input param, file_path is empty or model_data is nullptr or model_len is 0");
  245. REPORT_INNER_ERROR("E19999", "Save file failed, at least one of the input parameters(file_path, model_data, model_len) is incorrect");
  246. return FAILED;
  247. }
  248. ModelFileHeader file_header;
  249. int32_t copy_header_ret = 0;
  250. GE_IF_BOOL_EXEC(model_file_header != nullptr, copy_header_ret = memcpy_s(&file_header, sizeof(ModelFileHeader),
  251. model_file_header, sizeof(ModelFileHeader)));
  252. GE_CHK_BOOL_RET_STATUS(copy_header_ret == 0, FAILED, "Copy ModelFileHeader failed, memcpy_s return: %d",
  253. copy_header_ret);
  254. file_header.length = model.model_len;
  255. file_header.is_encrypt = ModelEncryptType::UNENCRYPTED;
  256. const Status ret = SaveWithFileHeader(file_path, file_header, model.model_data, file_header.length);
  257. if (ret != SUCCESS) {
  258. GELOGE(FAILED, "[Save][File]Failed, file_path:%s, file_header_len:%u, error_code:%u.",
  259. file_path.c_str(), file_header.length, ret);
  260. return FAILED;
  261. }
  262. return SUCCESS;
  263. }
  264. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status
  265. FileSaver::SaveToFile(const string &file_path, ModelFileHeader &file_header, ModelPartitionTable &model_partition_table,
  266. const std::vector<ModelPartition> &partition_datas) {
  267. file_header.is_encrypt = ModelEncryptType::UNENCRYPTED;
  268. const Status ret = SaveWithFileHeader(file_path, file_header, model_partition_table, partition_datas);
  269. GE_CHK_BOOL_RET_STATUS(ret == SUCCESS, FAILED, "save file failed, file_path:%s, file header len:%u.",
  270. file_path.c_str(), file_header.length);
  271. return SUCCESS;
  272. }
  273. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status
  274. FileSaver::SaveToFile(const string &file_path, ModelFileHeader &file_header,
  275. vector<ModelPartitionTable *> &model_partition_tables,
  276. const vector<vector<ModelPartition>> &all_partition_datas) {
  277. file_header.is_encrypt = ModelEncryptType::UNENCRYPTED;
  278. const Status ret = SaveWithFileHeader(file_path, file_header, model_partition_tables, all_partition_datas);
  279. GE_CHK_BOOL_RET_STATUS(ret == SUCCESS, FAILED, "save file failed, file_path:%s, file header len:%u.",
  280. file_path.c_str(), file_header.length);
  281. return SUCCESS;
  282. }
  283. Status FileSaver::SaveWithFileHeader(const std::string &file_path, const ModelFileHeader &file_header,
  284. vector<ModelPartitionTable *> &model_partition_tables,
  285. const vector<vector<ModelPartition>> &all_partition_datas) {
  286. GE_CHK_BOOL_EXEC(model_partition_tables.size() == all_partition_datas.size(),
  287. return PARAM_INVALID,
  288. "model table size %zu does not match partition size %zu",
  289. model_partition_tables.size(), all_partition_datas.size())
  290. for (size_t index = 0; index < model_partition_tables.size(); ++index) {
  291. auto &cur_partiton_data = all_partition_datas[index];
  292. auto &cur_model_partition_table = *model_partition_tables[index];
  293. GE_CHK_BOOL_RET_STATUS(!cur_partiton_data.empty() && cur_model_partition_table.num != 0
  294. && cur_model_partition_table.num == cur_partiton_data.size(), FAILED,
  295. "Invalid param:partition data size is (%u), model_partition_table.num is (%zu).",
  296. cur_model_partition_table.num, cur_partiton_data.size());
  297. }
  298. // Open file
  299. int32_t fd = 0;
  300. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(OpenFile(fd, file_path) != SUCCESS, return FAILED);
  301. Status ret = SUCCESS;
  302. do {
  303. // Write file header
  304. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
  305. WriteData(static_cast<const void *>(&file_header), sizeof(ModelFileHeader), fd) != SUCCESS, ret = FAILED;
  306. break);
  307. for (size_t index = 0; index < model_partition_tables.size(); ++index) {
  308. // Write model partition table
  309. auto &cur_tabel = *model_partition_tables[index];
  310. uint32_t table_size = static_cast<uint32_t>(SIZE_OF_MODEL_PARTITION_TABLE(cur_tabel));
  311. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
  312. WriteData(static_cast<const void *>(&cur_tabel), table_size, fd) != SUCCESS, ret = FAILED; break);
  313. // Write partition data
  314. auto &cur_partition_datas = all_partition_datas[index];
  315. for (const auto &partition_data : cur_partition_datas) {
  316. GELOGI("part_size[%u]", partition_data.size);
  317. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
  318. WriteData(static_cast<const void *>(partition_data.data), partition_data.size, fd) != SUCCESS, ret = FAILED;
  319. break);
  320. }
  321. }
  322. } while (0);
  323. // Close file
  324. if (mmClose(fd) != 0) { // mmClose 0: success
  325. GELOGE(FAILED, "[Close][File]Failed, error_code:%u errmsg:%s", ret, strerror(errno));
  326. REPORT_CALL_ERROR("E19999", "Close file failed, error_code:%u errmsg:%s", ret, strerror(errno));
  327. ret = FAILED;
  328. }
  329. return ret;
  330. }
  331. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status FileSaver::SaveToFile(const string &file_path, const void *data,
  332. int len) {
  333. if (data == nullptr || len <= 0) {
  334. GELOGE(FAILED, "[Check][Param]Failed, model_data is null or the length[%d] is less than 1.", len);
  335. REPORT_INNER_ERROR("E19999", "Save file failed, the model_data is null or its length:%d is less than 1.", len);
  336. return FAILED;
  337. }
  338. // Open file
  339. int32_t fd = 0;
  340. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(OpenFile(fd, file_path) != SUCCESS, return FAILED, "OpenFile FAILED");
  341. Status ret = SUCCESS;
  342. // write data
  343. GE_CHK_BOOL_EXEC(SUCCESS == WriteData(data, (uint32_t)len, fd), ret = FAILED, "WriteData FAILED");
  344. // Close file
  345. if (mmClose(fd) != 0) { // mmClose 0: success
  346. GELOGE(FAILED, "[Close][File]Failed, error_code:%u errmsg:%s", ret, strerror(errno));
  347. REPORT_CALL_ERROR("E19999", "Close file failed, error_code:%u errmsg:%s", ret, strerror(errno));
  348. ret = FAILED;
  349. }
  350. return ret;
  351. }
  352. } // namespace ge

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