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 12 kB

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
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
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /**
  2. * Copyright 2019-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 <fcntl.h>
  18. #include <securec.h>
  19. #include <unistd.h>
  20. #include <cstdlib>
  21. #include <fstream>
  22. #include <vector>
  23. #include "common/math/math_util.h"
  24. #include "framework/common/debug/ge_log.h"
  25. #include "framework/common/debug/log.h"
  26. #include "framework/common/util.h"
  27. namespace {
  28. const int kFileOpSuccess = 0;
  29. } // namespace
  30. namespace ge {
  31. Status FileSaver::OpenFile(int32_t &fd, const std::string &file_path) {
  32. if (CheckPath(file_path) != SUCCESS) {
  33. GELOGE(FAILED, "Check output file failed.");
  34. return FAILED;
  35. }
  36. char real_path[PATH_MAX] = {0};
  37. GE_IF_BOOL_EXEC(realpath(file_path.c_str(), real_path) == nullptr,
  38. GELOGI("File %s is not exist, it will be created.", file_path.c_str()));
  39. // Open file
  40. mode_t mode = S_IRUSR | S_IWUSR;
  41. fd = mmOpen2(real_path, O_RDWR | O_CREAT | O_TRUNC, mode);
  42. if (fd == EN_INVALID_PARAM || fd == EN_ERROR) {
  43. // -1: Failed to open file; - 2: Illegal parameter
  44. GELOGE(FAILED, "Open file failed. mmpa_errno = %d, %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 = ((uint32_t) 0x1 << 31);
  53. uint32_t size_1g = ((uint32_t) 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 = %d, %s", write_count, strerror(errno));
  61. return FAILED;
  62. }
  63. size -= size_1g;
  64. seek += size_1g;
  65. }
  66. write_count = mmWrite(fd, reinterpret_cast<void *>(seek), size);
  67. } else {
  68. write_count = mmWrite(fd, const_cast<void *>(data), size);
  69. }
  70. // -1: Failed to write to file; - 2: Illegal parameter
  71. if (write_count == EN_INVALID_PARAM || write_count == EN_ERROR) {
  72. GELOGE(FAILED, "Write data failed. mmpa_errorno = %d, %s", write_count, strerror(errno));
  73. return FAILED;
  74. }
  75. return SUCCESS;
  76. }
  77. Status FileSaver::SaveWithFileHeader(const std::string &file_path, const ModelFileHeader &file_header, const void *data,
  78. int len) {
  79. if (data == nullptr || len <= 0) {
  80. GELOGE(FAILED, "Model_data is null or the length[%d] less than 1.", len);
  81. return FAILED;
  82. }
  83. // Open file
  84. int32_t fd = 0;
  85. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(OpenFile(fd, file_path) != SUCCESS, return FAILED, "OpenFile FAILED");
  86. Status ret = SUCCESS;
  87. do {
  88. // Write file header
  89. GE_CHK_BOOL_EXEC(WriteData(static_cast<const void *>(&file_header), sizeof(ModelFileHeader), fd) == SUCCESS,
  90. ret = FAILED;
  91. break, "WriteData FAILED");
  92. // write data
  93. GE_CHK_BOOL_EXEC(WriteData(data, static_cast<uint32_t>(len), fd) == SUCCESS, ret = FAILED, "WriteData FAILED");
  94. } while (0);
  95. // Close file
  96. if (mmClose(fd) != 0) { // mmClose 0: success
  97. GELOGE(FAILED, "Close file failed.");
  98. ret = FAILED;
  99. }
  100. return ret;
  101. }
  102. Status FileSaver::SaveWithFileHeader(const std::string &file_path, const ModelFileHeader &file_header,
  103. ModelPartitionTable &model_partition_table,
  104. const std::vector<ModelPartition> &partition_datas) {
  105. GE_CHK_BOOL_RET_STATUS(!partition_datas.empty() && model_partition_table.num != 0
  106. && model_partition_table.num == partition_datas.size(), FAILED,
  107. "Invalid param:partition data size is (%u), model_partition_table.num is (%zu).",
  108. model_partition_table.num, partition_datas.size());
  109. // Open file
  110. int32_t fd = 0;
  111. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(OpenFile(fd, file_path) != SUCCESS, return FAILED);
  112. Status ret = SUCCESS;
  113. do {
  114. // Write file header
  115. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
  116. WriteData(static_cast<const void *>(&file_header), sizeof(ModelFileHeader), fd) != SUCCESS, ret = FAILED;
  117. break);
  118. // Write model partition table
  119. uint32_t table_size = static_cast<uint32_t>(SIZE_OF_MODEL_PARTITION_TABLE(model_partition_table));
  120. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
  121. WriteData(static_cast<const void *>(&model_partition_table), table_size, fd) != SUCCESS, ret = FAILED; break);
  122. // Write partition data
  123. for (const auto &partitionData : partition_datas) {
  124. GELOGI("GC:size[%zu]", partitionData.size);
  125. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
  126. WriteData(static_cast<const void *>(partitionData.data), partitionData.size, fd) != SUCCESS, ret = FAILED;
  127. break);
  128. }
  129. } while (0);
  130. // Close file
  131. GE_CHK_BOOL_RET_STATUS(mmClose(fd) == EN_OK, FAILED, "Close file failed.");
  132. return ret;
  133. }
  134. Status FileSaver::SaveToBuffWithFileHeader(const ModelFileHeader &file_header,
  135. ModelPartitionTable &model_partition_table,
  136. const std::vector<ModelPartition> &partitionDatas,
  137. ge::ModelBufferData &model) {
  138. GE_CHK_BOOL_RET_STATUS(
  139. !partitionDatas.empty() && model_partition_table.num != 0 && model_partition_table.num == partitionDatas.size(),
  140. FAILED, "Invalid param:partition data size is (%u), model_partition_table.num is (%zu).",
  141. model_partition_table.num, partitionDatas.size());
  142. uint32_t model_header_size = sizeof(ModelFileHeader);
  143. uint32_t table_size = static_cast<uint32_t>(SIZE_OF_MODEL_PARTITION_TABLE(model_partition_table));
  144. uint32_t total_size = model_header_size + table_size;
  145. for (const auto &partitionData : partitionDatas) {
  146. auto ret = ge::CheckUint32AddOverflow(total_size, partitionData.size);
  147. GE_CHK_BOOL_RET_STATUS(ret == SUCCESS, FAILED, "add uint32 overflow!");
  148. total_size = total_size + partitionData.size;
  149. }
  150. auto buff = reinterpret_cast<uint8_t *>(malloc(total_size));
  151. GE_CHK_BOOL_RET_STATUS(buff != nullptr, FAILED, "malloc failed!");
  152. GE_PRINT_DYNAMIC_MEMORY(malloc, "file buffer.", total_size)
  153. model.data.reset(buff, [](uint8_t *buff) {
  154. GELOGD("Free online model memory.");
  155. free(buff);
  156. buff = nullptr;
  157. });
  158. model.length = total_size;
  159. uint32_t left_space = total_size;
  160. auto ret_mem1 = memcpy_s(buff, left_space, reinterpret_cast<void *>(const_cast<ModelFileHeader *>(&file_header)),
  161. model_header_size);
  162. GE_CHK_BOOL_RET_STATUS(ret_mem1 == 0, FAILED, "memcpy_s failed!");
  163. buff += model_header_size;
  164. left_space -= model_header_size;
  165. auto ret_mem2 = memcpy_s(buff, left_space, reinterpret_cast<void *>(&model_partition_table), table_size);
  166. GE_CHK_BOOL_RET_STATUS(ret_mem2 == 0, FAILED, "memcpy_s failed!");
  167. buff += table_size;
  168. left_space -= table_size;
  169. for (const auto &partitionData : partitionDatas) {
  170. auto ret_mem3 = memcpy_s(buff, left_space, reinterpret_cast<void *>(const_cast<uint8_t *>(partitionData.data)),
  171. partitionData.size);
  172. GE_CHK_BOOL_RET_STATUS(ret_mem3 == 0, FAILED, "memcpy failed!");
  173. buff += partitionData.size;
  174. left_space -= partitionData.size;
  175. }
  176. return SUCCESS;
  177. }
  178. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status FileSaver::CheckPath(const std::string &file_path) {
  179. // Determine file path length
  180. if (file_path.size() >= PATH_MAX) {
  181. GELOGE(FAILED, "Path is too long:%zu", file_path.size());
  182. return FAILED;
  183. }
  184. // Find the last separator
  185. int path_split_pos = static_cast<int>(file_path.size() - 1);
  186. for (; path_split_pos >= 0; path_split_pos--) {
  187. if (file_path[path_split_pos] == '\\' || file_path[path_split_pos] == '/') {
  188. break;
  189. }
  190. }
  191. if (path_split_pos == 0) {
  192. return SUCCESS;
  193. }
  194. // If there is a path before the file name, create the path
  195. if (path_split_pos != -1) {
  196. if (CreateDirectory(std::string(file_path).substr(0, static_cast<size_t>(path_split_pos))) != kFileOpSuccess) {
  197. GELOGE(FAILED, "CreateDirectory failed, file path:%s.", file_path.c_str());
  198. return FAILED;
  199. }
  200. }
  201. return SUCCESS;
  202. }
  203. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status
  204. FileSaver::SaveToFile(const string &file_path, const ge::ModelData &model, const ModelFileHeader *model_file_header) {
  205. if (file_path.empty() || model.model_data == nullptr || model.model_len == 0) {
  206. GELOGE(FAILED, "Incorrected input param. file_path.empty() || model.model_data == nullptr || model.model_len == 0");
  207. return FAILED;
  208. }
  209. ModelFileHeader file_header;
  210. int32_t copy_header_ret = 0;
  211. GE_IF_BOOL_EXEC(model_file_header != nullptr, copy_header_ret = memcpy_s(&file_header, sizeof(ModelFileHeader),
  212. model_file_header, sizeof(ModelFileHeader)));
  213. GE_CHK_BOOL_RET_STATUS(copy_header_ret == 0, FAILED, "Copy ModelFileHeader failed, memcpy_s return: %d",
  214. copy_header_ret);
  215. file_header.length = model.model_len;
  216. file_header.is_encrypt = ModelEncryptType::UNENCRYPTED;
  217. const Status ret = SaveWithFileHeader(file_path, file_header, model.model_data, file_header.length);
  218. if (ret != SUCCESS) {
  219. GELOGE(FAILED, "Save file failed, file_path:%s, file header len:%u.", file_path.c_str(), file_header.length);
  220. return FAILED;
  221. }
  222. return SUCCESS;
  223. }
  224. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status
  225. FileSaver::SaveToFile(const string &file_path, ModelFileHeader &file_header, ModelPartitionTable &model_partition_table,
  226. const std::vector<ModelPartition> &partition_datas) {
  227. file_header.is_encrypt = ModelEncryptType::UNENCRYPTED;
  228. const Status ret = SaveWithFileHeader(file_path, file_header, model_partition_table, partition_datas);
  229. GE_CHK_BOOL_RET_STATUS(ret == SUCCESS, FAILED, "save file failed, file_path:%s, file header len:%u.",
  230. file_path.c_str(), file_header.length);
  231. return SUCCESS;
  232. }
  233. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status FileSaver::SaveToFile(const string &file_path, const void *data,
  234. int len) {
  235. if (data == nullptr || len <= 0) {
  236. GELOGE(FAILED, "Model_data is null or the length[%d] less than 1.", len);
  237. return FAILED;
  238. }
  239. // Open file
  240. int32_t fd = 0;
  241. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(OpenFile(fd, file_path) != SUCCESS, return FAILED, "OpenFile FAILED");
  242. Status ret = SUCCESS;
  243. // write data
  244. GE_CHK_BOOL_EXEC(SUCCESS == WriteData(data, (uint32_t)len, fd), ret = FAILED, "WriteData FAILED");
  245. // Close file
  246. if (mmClose(fd) != 0) { // mmClose 0: success
  247. GELOGE(FAILED, "Close file failed.");
  248. ret = FAILED;
  249. }
  250. return ret;
  251. }
  252. } // namespace ge

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