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.

util.cc 20 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 "framework/common/util.h"
  17. #include <fcntl.h>
  18. #include <sys/stat.h>
  19. #include <unistd.h>
  20. #include <algorithm>
  21. #include <climits>
  22. #include <cstdlib>
  23. #include <ctime>
  24. #include <fstream>
  25. #include <regex>
  26. #include "external/ge/ge_api_error_codes.h"
  27. #include "common/util/error_manager/error_manager.h"
  28. #include "framework/common/debug/ge_log.h"
  29. #include "framework/common/fmk_types.h"
  30. #include "framework/common/ge_inner_error_codes.h"
  31. #include "google/protobuf/io/coded_stream.h"
  32. #include "google/protobuf/io/zero_copy_stream_impl.h"
  33. #include "mmpa/mmpa_api.h"
  34. using google::protobuf::io::CodedInputStream;
  35. using google::protobuf::io::FileInputStream;
  36. using google::protobuf::io::ZeroCopyInputStream;
  37. namespace {
  38. /*
  39. * kProtoReadBytesLimit and kWarningThreshold are real arguments of CodedInputStream::SetTotalBytesLimit.
  40. * In order to prevent integer overflow and excessive memory allocation during protobuf processing,
  41. * it is necessary to limit the length of proto message (call SetTotalBytesLimit function).
  42. * In theory, the minimum message length that causes an integer overflow is 512MB, and the default is 64MB.
  43. * If the limit of warning_threshold is exceeded, the exception information will be printed in stderr.
  44. * If such an exception is encountered during operation,
  45. * the proto file can be divided into several small files or the limit value can be increased.
  46. */
  47. const int kProtoReadBytesLimit = INT_MAX; // Max size of 2 GB minus 1 byte.
  48. const int kWarningThreshold = 536870912 * 2; // 536870912 represent 512M
  49. /// The maximum length of the file.
  50. /// Based on the security coding specification and the current actual (protobuf) model size, it is determined as 2G-1
  51. const int kMaxFileSizeLimit = INT_MAX;
  52. const char *const kPathValidReason = "The path can only contains 'a-z' 'A-Z' '0-9' '-' '.' '_' and chinese character";
  53. } // namespace
  54. namespace ge {
  55. static bool ReadProtoFromCodedInputStream(CodedInputStream &coded_stream, Message *proto) {
  56. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(proto == nullptr, return false, "incorrect parameter. nullptr == proto");
  57. coded_stream.SetTotalBytesLimit(kProtoReadBytesLimit, kWarningThreshold);
  58. return proto->ParseFromCodedStream(&coded_stream);
  59. }
  60. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool ReadProtoFromBinaryFile(const char *file, Message *proto) {
  61. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG((file == nullptr || proto == nullptr), return false,
  62. "Input parameter file or proto is nullptr!");
  63. std::string real_path = RealPath(file);
  64. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(real_path.empty(), return false, "pb file path '%s' not valid", file);
  65. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(GetFileLength(real_path) == -1, return false, "file size not valid.");
  66. std::ifstream fs(real_path, std::ifstream::in | std::ifstream::binary);
  67. if (!fs.is_open()) {
  68. ErrorManager::GetInstance().ATCReportErrMessage("E19001", {"file", "errmsg"}, {file, "ifstream is_open failed"});
  69. GELOGE(ge::FAILED, "Open real path[%s] failed.", file);
  70. return false;
  71. }
  72. google::protobuf::io::IstreamInputStream istream(&fs);
  73. google::protobuf::io::CodedInputStream coded_stream(&istream);
  74. bool ret = ReadProtoFromCodedInputStream(coded_stream, proto);
  75. fs.close();
  76. if (!ret) {
  77. ErrorManager::GetInstance().ATCReportErrMessage("E19005", {"file"}, {file});
  78. GELOGE(ge::FAILED, "Parse file[%s] failed.", file);
  79. return ret;
  80. }
  81. return ret;
  82. }
  83. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool ReadProtoFromArray(const void *data, int size, Message *proto) {
  84. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG((proto == nullptr || data == nullptr || size == 0), return false,
  85. "incorrect parameter. proto is nullptr || data is nullptr || size is 0");
  86. google::protobuf::io::CodedInputStream coded_stream(reinterpret_cast<uint8_t *>(const_cast<void *>(data)), size);
  87. return ReadProtoFromCodedInputStream(coded_stream, proto);
  88. }
  89. // Get file length
  90. long GetFileLength(const std::string &input_file) {
  91. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(input_file.empty(), return -1, "input_file path is null.");
  92. std::string real_path = RealPath(input_file.c_str());
  93. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(real_path.empty(), return -1, "input_file path '%s' not valid", input_file.c_str());
  94. unsigned long long file_length = 0;
  95. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
  96. mmGetFileSize(input_file.c_str(), &file_length) != EN_OK,
  97. ErrorManager::GetInstance().ATCReportErrMessage("E19001", {"file", "errmsg"}, {input_file, strerror(errno)});
  98. return -1, "Open file[%s] failed. %s", input_file.c_str(), strerror(errno));
  99. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG((file_length == 0),
  100. ErrorManager::GetInstance().ATCReportErrMessage("E19015", {"filepath"}, {input_file});
  101. return -1, "File[%s] size is 0, not valid.", input_file.c_str());
  102. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
  103. file_length > kMaxFileSizeLimit, ErrorManager::GetInstance().ATCReportErrMessage(
  104. "E19016", {"filepath", "filesize", "maxlen"},
  105. {input_file, std::to_string(file_length), std::to_string(kMaxFileSizeLimit)});
  106. return -1, "File[%s] size %lld is out of limit: %d.", input_file.c_str(), file_length, kMaxFileSizeLimit);
  107. return static_cast<long>(file_length);
  108. }
  109. /** @ingroup domi_common
  110. * @brief Read all data from binary file
  111. * @param [in] file_name File path
  112. * @param [out] buffer The address of the output memory, which needs to be released by the caller
  113. * @param [out] length Output memory size
  114. * @return false fail
  115. * @return true success
  116. */
  117. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool ReadBytesFromBinaryFile(const char *file_name, char **buffer,
  118. int &length) {
  119. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG((file_name == nullptr), return false, "incorrect parameter. file is nullptr");
  120. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG((buffer == nullptr), return false, "incorrect parameter. buffer is nullptr");
  121. std::string real_path = RealPath(file_name);
  122. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(real_path.empty(), return false, "file path '%s' not valid", file_name);
  123. std::ifstream file(real_path.c_str(), std::ios::binary | std::ios::ate);
  124. if (!file.is_open()) {
  125. GELOGE(ge::FAILED, "Read file %s failed.", file_name);
  126. return false;
  127. }
  128. length = static_cast<int>(file.tellg());
  129. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG((length <= 0), file.close(); return false, "file length <= 0");
  130. file.seekg(0, std::ios::beg);
  131. *buffer = new (std::nothrow) char[length]();
  132. GE_CHK_BOOL_TRUE_EXEC_RET_STATUS(*buffer == nullptr, false, file.close(), "new an object failed.");
  133. file.read(*buffer, length);
  134. file.close();
  135. return true;
  136. }
  137. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool ReadBytesFromBinaryFile(const char *file_name,
  138. std::vector<char> &buffer) {
  139. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG((file_name == nullptr), return false, "incorrect parameter. file path is null");
  140. std::string real_path = RealPath(file_name);
  141. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(real_path.empty(), return false, "file path '%s' not valid", file_name);
  142. std::ifstream file(real_path.c_str(), std::ios::binary | std::ios::ate);
  143. if (!file.is_open()) {
  144. GELOGE(ge::FAILED, "Read file %s failed.", file_name);
  145. return false;
  146. }
  147. std::streamsize size = file.tellg();
  148. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG((size <= 0), file.close(); return false, "file length <= 0, not valid.");
  149. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(size > kMaxFileSizeLimit, file.close();
  150. return false, "file size %ld is out of limit: %d.", size, kMaxFileSizeLimit);
  151. file.seekg(0, std::ios::beg); // [no need to check value]
  152. buffer.resize(static_cast<uint64_t>(size)); // [no need to check value]
  153. file.read(&buffer[0], size); // [no need to check value]
  154. file.close();
  155. GELOGI("Read size:%ld", size);
  156. return true;
  157. }
  158. /**
  159. * @ingroup domi_common
  160. * @brief Create directory, support to create multi-level directory
  161. * @param [in] directory_path Path, can be multi-level directory
  162. * @return -1 fail
  163. * @return 0 success
  164. */
  165. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY int CreateDirectory(const std::string &directory_path) {
  166. GE_CHK_BOOL_EXEC(!directory_path.empty(), return -1, "directory path is empty.");
  167. auto dir_path_len = directory_path.length();
  168. if (dir_path_len >= PATH_MAX) {
  169. ErrorManager::GetInstance().ATCReportErrMessage("E19002", {"filepath", "size"},
  170. {directory_path, std::to_string(PATH_MAX)});
  171. GELOGW("Path[%s] len is too long, it must be less than %d", directory_path.c_str(), PATH_MAX);
  172. return -1;
  173. }
  174. char tmp_dir_path[PATH_MAX] = {0};
  175. for (size_t i = 0; i < dir_path_len; i++) {
  176. tmp_dir_path[i] = directory_path[i];
  177. if ((tmp_dir_path[i] == '\\') || (tmp_dir_path[i] == '/')) {
  178. if (access(tmp_dir_path, F_OK) != 0) {
  179. int32_t ret = mmMkdir(tmp_dir_path, S_IRUSR | S_IWUSR | S_IXUSR); // 700
  180. if (ret != 0) {
  181. if (errno != EEXIST) {
  182. ErrorManager::GetInstance().ATCReportErrMessage("E19006", {"path"}, {directory_path});
  183. GELOGW("Can not create directory %s. Make sure the directory exists and writable.", directory_path.c_str());
  184. return ret;
  185. }
  186. }
  187. }
  188. }
  189. }
  190. int32_t ret = mmMkdir(const_cast<char *>(directory_path.c_str()), S_IRUSR | S_IWUSR | S_IXUSR); // 700
  191. if (ret != 0) {
  192. if (errno != EEXIST) {
  193. ErrorManager::GetInstance().ATCReportErrMessage("E19006", {"path"}, {directory_path});
  194. GELOGW("Can not create directory %s. Make sure the directory exists and writable.", directory_path.c_str());
  195. return ret;
  196. }
  197. }
  198. return 0;
  199. }
  200. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY std::string CurrentTimeInStr() {
  201. std::time_t now = std::time(nullptr);
  202. std::tm *ptm = std::localtime(&now);
  203. if (ptm == nullptr) {
  204. GELOGE(ge::FAILED, "Localtime failed.");
  205. return "";
  206. }
  207. const int kTimeBufferLen = 32;
  208. char buffer[kTimeBufferLen + 1] = {0};
  209. // format: 20171122042550
  210. std::strftime(buffer, kTimeBufferLen, "%Y%m%d%H%M%S", ptm);
  211. return std::string(buffer);
  212. }
  213. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool ReadProtoFromText(const char *file,
  214. google::protobuf::Message *message) {
  215. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG((file == nullptr || message == nullptr), return false,
  216. "incorrect parameter. nullptr == file || nullptr == message");
  217. std::string real_path = RealPath(file);
  218. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(real_path.empty(), ErrorManager::GetInstance().ATCReportErrMessage(
  219. "E19000", {"path", "errmsg"}, {file, strerror(errno)});
  220. return false, "Path[%s]'s realpath is empty, errmsg[%s]", file, strerror(errno));
  221. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(GetFileLength(real_path) == -1, return false, "file size not valid.");
  222. std::ifstream fs(real_path.c_str(), std::ifstream::in);
  223. if (!fs.is_open()) {
  224. ErrorManager::GetInstance().ATCReportErrMessage("E19017", {"realpth", "protofile"}, {real_path, file});
  225. GELOGE(ge::FAILED, "Fail to open proto file real path is '%s' when orginal file path is '%s'.", real_path.c_str(),
  226. file);
  227. return false;
  228. }
  229. google::protobuf::io::IstreamInputStream input(&fs);
  230. bool ret = google::protobuf::TextFormat::Parse(&input, message);
  231. GE_IF_BOOL_EXEC(!ret, ErrorManager::GetInstance().ATCReportErrMessage("E19018", {"protofile"}, {file});
  232. GELOGE(ret,
  233. "Parse file[%s] through [google::protobuf::TextFormat::Parse] failed, "
  234. "please check whether the file is a valid protobuf format file.",
  235. file));
  236. fs.close();
  237. return ret;
  238. }
  239. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool ReadProtoFromMem(const char *data, int size,
  240. google::protobuf::Message *message) {
  241. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG((data == nullptr || message == nullptr), return false,
  242. "incorrect parameter. data is nullptr || message is nullptr");
  243. std::string str(data, static_cast<size_t>(size));
  244. std::istringstream fs(str);
  245. google::protobuf::io::IstreamInputStream input(&fs);
  246. bool ret = google::protobuf::TextFormat::Parse(&input, message);
  247. GE_IF_BOOL_EXEC(
  248. !ret, GELOGE(ret, "Call [google::protobuf::TextFormat::Parse] func ret fail, please check your text file."));
  249. return ret;
  250. }
  251. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY uint64_t GetCurrentTimestap() {
  252. struct timeval tv {};
  253. int ret = gettimeofday(&tv, nullptr);
  254. GE_LOGE_IF(ret != 0, "Func gettimeofday may failed: ret=%d", ret);
  255. auto total_use_time = tv.tv_usec + tv.tv_sec * 1000000; // 1000000: seconds to microseconds
  256. return static_cast<uint64_t>(total_use_time);
  257. }
  258. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool CheckInt64MulOverflow(int64_t a, int64_t b) {
  259. if (a > 0) {
  260. if (b > 0) {
  261. if (a > (INT64_MAX / b)) {
  262. return false;
  263. }
  264. } else {
  265. if (b < (INT64_MIN / a)) {
  266. return false;
  267. }
  268. }
  269. } else {
  270. if (b > 0) {
  271. if (a < (INT64_MIN / b)) {
  272. return false;
  273. }
  274. } else {
  275. if ((a != 0) && (b < (INT64_MAX / a))) {
  276. return false;
  277. }
  278. }
  279. }
  280. return true;
  281. }
  282. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY std::string RealPath(const char *path) {
  283. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(path == nullptr, return "", "path pointer is NULL.");
  284. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
  285. strlen(path) >= PATH_MAX,
  286. ErrorManager::GetInstance().ATCReportErrMessage("E19002", {"filepath", "size"}, {path, std::to_string(PATH_MAX)});
  287. return "", "Path[%s] len is too long, it must be less than %d", path, PATH_MAX);
  288. // PATH_MAX is the system's own macro, indicating the maximum file path length supported
  289. std::shared_ptr<char> resolved_path(new (std::nothrow) char[PATH_MAX](), std::default_delete<char[]>());
  290. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(resolved_path == nullptr, return "", "Path[%s] new string object len[%d] failed.",
  291. path, PATH_MAX);
  292. // Nullptr is returned when the path does not exist or there is no permission
  293. // Return absolute path when path is accessible
  294. std::string res;
  295. if (realpath(path, resolved_path.get()) != nullptr) {
  296. res = resolved_path.get();
  297. }
  298. return res;
  299. }
  300. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool CheckInputPathValid(const std::string &file_path,
  301. const std::string &atc_param) {
  302. // The specified path is empty
  303. std::map<std::string, std::string> args_map;
  304. if (file_path.empty()) {
  305. ErrorManager::GetInstance().ATCReportErrMessage("E10004", {"parameter"}, {atc_param});
  306. GELOGW("Input parameter %s is empty.", file_path.c_str());
  307. return false;
  308. }
  309. std::string real_path = RealPath(file_path.c_str());
  310. // Unable to get absolute path (does not exist or does not have permission to access)
  311. if (real_path.empty()) {
  312. ErrorManager::GetInstance().ATCReportErrMessage("E19000", {"path", "errmsg"}, {file_path, strerror(errno)});
  313. GELOGW("Path[%s]'s realpath is empty, errmsg[%s]", file_path.c_str(), strerror(errno));
  314. return false;
  315. }
  316. // A regular matching expression to verify the validity of the input file path
  317. // ^(/|./|(../)+|)([.]?[\u4e00-\u9fa5A-Za-z0-9_.-]+/)*[\u4e00-\u9fa5A-Za-z0-9_+.-]+$
  318. // Path section:Support upper and lower case letters, numbers dots(.) chinese and underscores
  319. // File name section:Support upper and lower case letters, numbers, underscores chinese and dots(.)
  320. std::string mode = "^(/+|./+|(../+)+|)(../|([.]?[\u4e00-\u9fa5A-Za-z0-9_.-]+)/+)*[\u4e00-\u9fa5A-Za-z0-9_+.-]+$";
  321. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
  322. !ValidateStr(real_path, mode),
  323. ErrorManager::GetInstance().ATCReportErrMessage("E10001", {"parameter", "value", "reason"},
  324. {atc_param, real_path, kPathValidReason});
  325. return false, "Invalid value for %s[%s], %s.", atc_param.c_str(), real_path.c_str(), kPathValidReason);
  326. // The absolute path points to a file that is not readable
  327. if (access(real_path.c_str(), R_OK) != 0) {
  328. ErrorManager::GetInstance().ATCReportErrMessage("E19003", {"file", "errmsg"}, {file_path.c_str(), strerror(errno)});
  329. GELOGW("Read file[%s] failed, errmsg[%s]", file_path.c_str(), strerror(errno));
  330. return false;
  331. }
  332. return true;
  333. }
  334. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool CheckOutputPathValid(const std::string &file_path,
  335. const std::string &atc_param) {
  336. // The specified path is empty
  337. if (file_path.empty()) {
  338. ErrorManager::GetInstance().ATCReportErrMessage("E10004", {"parameter"}, {atc_param});
  339. GELOGW("Input parameter's value is empty.");
  340. return false;
  341. }
  342. std::string real_path = RealPath(file_path.c_str());
  343. // Can get absolute path (file exists)
  344. if (!real_path.empty()) {
  345. // A regular matching expression to verify the validity of the input file path
  346. // ^(/|./|(../)+|)([.]?[\u4e00-\u9fa5A-Za-z0-9_-]+/)*[\u4e00-\u9fa5A-Za-z0-9_+.-]+$
  347. // Path section:Support upper and lower case letters, numbers dots(.) chinese and underscores
  348. // File name section:Support upper and lower case letters, numbers, underscores chinese and dots(.)
  349. std::string mode = "^(/+|./+|(../+)+|)(../|([.]?[\u4e00-\u9fa5A-Za-z0-9_.-]+)/+)*[\u4e00-\u9fa5A-Za-z0-9_+.-]+$";
  350. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
  351. !ValidateStr(real_path, mode),
  352. ErrorManager::GetInstance().ATCReportErrMessage("E10001", {"parameter", "value", "reason"},
  353. {atc_param, real_path, kPathValidReason});
  354. return false, "Invalid value for %s[%s], %s.", atc_param.c_str(), real_path.c_str(), kPathValidReason);
  355. // File is not readable or writable
  356. if (access(real_path.c_str(), W_OK | F_OK) != 0) {
  357. ErrorManager::GetInstance().ATCReportErrMessage("E19004", {"file", "errmsg"}, {real_path, strerror(errno)});
  358. GELOGW("Write file[%s] failed, errmsg[%s]", real_path.c_str(), strerror(errno));
  359. return false;
  360. }
  361. } else {
  362. // Find the last separator
  363. int path_split_pos = static_cast<int>(file_path.size() - 1);
  364. for (; path_split_pos >= 0; path_split_pos--) {
  365. if (file_path[path_split_pos] == '\\' || file_path[path_split_pos] == '/') {
  366. break;
  367. }
  368. }
  369. if (path_split_pos == 0) {
  370. return true;
  371. }
  372. if (path_split_pos != -1) {
  373. std::string prefix_path = std::string(file_path).substr(0, static_cast<size_t>(path_split_pos));
  374. // Determine whether the specified path is valid by creating the path
  375. if (CreateDirectory(prefix_path) != 0) {
  376. ErrorManager::GetInstance().ATCReportErrMessage("E19006", {"path"}, {file_path});
  377. GELOGW("Can not create directory[%s].", file_path.c_str());
  378. return false;
  379. }
  380. }
  381. }
  382. return true;
  383. }
  384. FMK_FUNC_HOST_VISIBILITY bool ValidateStr(const std::string &str, const std::string &mode) {
  385. #ifndef OS_CENTOS
  386. std::regex reg(mode);
  387. // Matching string part
  388. std::smatch match;
  389. bool res = regex_match(str, match, reg);
  390. res = regex_search(str, std::regex("[`!@#$%^&*()|{}':;',\\[\\]<>?]"));
  391. return !(res) && (str.size() == match.str().size());
  392. #else
  393. return true;
  394. #endif
  395. }
  396. } // namespace ge

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