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.h 16 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
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
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
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
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
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. #ifndef INC_FRAMEWORK_COMMON_UTIL_H_
  17. #define INC_FRAMEWORK_COMMON_UTIL_H_
  18. #include <google/protobuf/text_format.h>
  19. #include <limits.h>
  20. #include <math.h>
  21. #include <sstream>
  22. #include <string>
  23. #include <vector>
  24. #include "framework/common/debug/ge_log.h"
  25. #include "framework/common/debug/log.h"
  26. #include "framework/common/scope_guard.h"
  27. #include "framework/common/ge_inner_error_codes.h"
  28. #include "mmpa/mmpa_api.h"
  29. #define GE_CHECK_POSITIVE_SIZE_RANGE(size) \
  30. do { \
  31. if (size <= 0) { \
  32. DOMI_LOGE("param[%s] is not a positive number", #size); \
  33. return PARAM_INVALID; \
  34. } \
  35. } while (0)
  36. #define CHECK_FALSE_EXEC(expr, exec_expr, ...) \
  37. { \
  38. bool b = (expr); \
  39. if (!b) { \
  40. exec_expr; \
  41. } \
  42. }
  43. // new ge marco
  44. // Encapsulate common resource releases
  45. #define GE_MAKE_GUARD_RTMEM(var) \
  46. GE_MAKE_GUARD(var, [&] { \
  47. if (var) GE_CHK_RT(rtFreeHost(var)); \
  48. });
  49. #define GE_MAKE_GUARD_RTSTREAM(var) \
  50. GE_MAKE_GUARD(var, [&] { \
  51. if (var) GE_CHK_RT(rtStreamDestroy(var)); \
  52. });
  53. // For propagating errors when calling a function.
  54. #define GE_RETURN_IF_ERROR(expr) \
  55. do { \
  56. const ::ge::Status _status = (expr); \
  57. if (_status) return _status; \
  58. } while (0)
  59. #define GE_RETURN_WITH_LOG_IF_ERROR(expr, ...) \
  60. do { \
  61. const ::ge::Status _status = (expr); \
  62. if (_status) { \
  63. DOMI_LOGE(__VA_ARGS__); \
  64. return _status; \
  65. } \
  66. } while (0)
  67. // check whether the parameter is true. If it is, return FAILED and record the error log
  68. #define GE_RETURN_WITH_LOG_IF_TRUE(condition, ...) \
  69. do { \
  70. if (condition) { \
  71. DOMI_LOGE(__VA_ARGS__); \
  72. return ge::FAILED; \
  73. } \
  74. } while (0)
  75. // Check if the parameter is false. If yes, return FAILED and record the error log
  76. #define GE_RETURN_WITH_LOG_IF_FALSE(condition, ...) \
  77. do { \
  78. bool _condition = (condition); \
  79. if (!_condition) { \
  80. DOMI_LOGE(__VA_ARGS__); \
  81. return ge::FAILED; \
  82. } \
  83. } while (0)
  84. // Checks whether the parameter is true. If so, returns PARAM_INVALID and records the error log
  85. #define GE_RT_PARAM_INVALID_WITH_LOG_IF_TRUE(condition, ...) \
  86. do { \
  87. if (condition) { \
  88. DOMI_LOGE(__VA_ARGS__); \
  89. return ge::PARAM_INVALID; \
  90. } \
  91. } while (0)
  92. // Check if the parameter is false. If yes, return PARAM_INVALID and record the error log
  93. #define GE_RT_PARAM_INVALID_WITH_LOG_IF_FALSE(condition, ...) \
  94. do { \
  95. bool _condition = (condition); \
  96. if (!_condition) { \
  97. DOMI_LOGE(__VA_ARGS__); \
  98. return ge::PARAM_INVALID; \
  99. } \
  100. } while (0)
  101. // Check if the parameter is null. If yes, return PARAM_INVALID and record the error
  102. #define GE_CHECK_NOTNULL(val) \
  103. do { \
  104. if (val == nullptr) { \
  105. REPORT_INNER_ERROR("E19999", "Param:%s is nullptr, check invalid when %s", \
  106. #val, __FUNCTION__); \
  107. DOMI_LOGE("[Check][Param:%s]null is invalid when %s.", #val, __FUNCTION__); \
  108. return ge::PARAM_INVALID; \
  109. } \
  110. } while (0)
  111. // Check if the parameter is null. If yes, just return and record the error
  112. #define GE_CHECK_NOTNULL_JUST_RETURN(val) \
  113. do { \
  114. if (val == nullptr) { \
  115. DOMI_LOGE("param[%s] must not be null.", #val); \
  116. return; \
  117. } \
  118. } while (0)
  119. // Check whether the parameter is null. If so, execute the exec_expr expression and record the error log
  120. #define GE_CHECK_NOTNULL_EXEC(val, exec_expr) \
  121. do { \
  122. if (val == nullptr) { \
  123. DOMI_LOGE("param[%s] must not be null.", #val); \
  124. exec_expr; \
  125. } \
  126. } while (0)
  127. // Check whether the parameter is null. If yes, return directly and record the error log
  128. #define GE_RT_VOID_CHECK_NOTNULL(val) \
  129. do { \
  130. if (val == nullptr) { \
  131. DOMI_LOGE("param[%s] must not be null.", #val); \
  132. return; \
  133. } \
  134. } while (0)
  135. // Check if the parameter is null. If yes, return false and record the error log
  136. #define GE_RT_FALSE_CHECK_NOTNULL(val) \
  137. do { \
  138. if (val == nullptr) { \
  139. DOMI_LOGE("param[%s] must not be null.", #val); \
  140. return false; \
  141. } \
  142. } while (0)
  143. // Check if the parameter is out of bounds
  144. #define GE_CHECK_SIZE(size) \
  145. do { \
  146. if (size == 0) { \
  147. DOMI_LOGE("param[%s] is out of range", #size); \
  148. return ge::PARAM_INVALID; \
  149. } \
  150. } while (0)
  151. // Check if the value on the left is greater than or equal to the value on the right
  152. #define GE_CHECK_GE(lhs, rhs) \
  153. do { \
  154. if (lhs < rhs) { \
  155. DOMI_LOGE("param[%s] is less than[%s]", #lhs, #rhs); \
  156. return ge::PARAM_INVALID; \
  157. } \
  158. } while (0)
  159. // Check if the value on the left is less than or equal to the value on the right
  160. #define GE_CHECK_LE(lhs, rhs) \
  161. do { \
  162. if (lhs > rhs) { \
  163. DOMI_LOGE("param[%s] is greater than[%s]", #lhs, #rhs); \
  164. return ge::PARAM_INVALID; \
  165. } \
  166. } while (0)
  167. #define GE_DELETE_NEW_SINGLE(var) \
  168. do { \
  169. if (var != nullptr) { \
  170. delete var; \
  171. var = nullptr; \
  172. } \
  173. } while (0)
  174. #define GE_DELETE_NEW_ARRAY(var) \
  175. do { \
  176. if (var != nullptr) { \
  177. delete[] var; \
  178. var = nullptr; \
  179. } \
  180. } while (0)
  181. #define GE_FREE_RT_LOG(addr) \
  182. do { \
  183. if (addr != nullptr) { \
  184. rtError_t error = rtFree(addr); \
  185. if (error != RT_ERROR_NONE) { \
  186. GELOGE(RT_FAILED, "Call rtFree failed, error: %#x", error); \
  187. } \
  188. addr = nullptr; \
  189. } \
  190. } while (0)
  191. /**
  192. * @ingroup domi_common
  193. * @brief version of om.proto file
  194. */
  195. static constexpr int32_t OM_PROTO_VERSION = 2;
  196. /**
  197. * Finding an Integer Ceiling Value Without Precision Loss
  198. */
  199. #define CEIL(N, n) (((N) + (n)-1) / (n))
  200. namespace ge {
  201. using google::protobuf::Message;
  202. ///
  203. /// @ingroup domi_common
  204. /// @brief Maximum file path length
  205. ///
  206. const int32_t DOMI_MAX_PATH_LEN = 256;
  207. ///
  208. /// @ingroup domi_common
  209. /// @brief proto file in bianary format
  210. /// @param [in] file path of proto file
  211. /// @param [out] proto memory for storing the proto file
  212. /// @return true success
  213. /// @return false fail
  214. ///
  215. GE_FUNC_VISIBILITY bool ReadProtoFromBinaryFile(const char *file, Message *proto);
  216. ///
  217. /// @ingroup domi_common
  218. /// @brief Reads the proto structure from an array.
  219. /// @param [in] data proto data to be read
  220. /// @param [in] size proto data size
  221. /// @param [out] proto Memory for storing the proto file
  222. /// @return true success
  223. /// @return false fail
  224. ///
  225. GE_FUNC_VISIBILITY bool ReadProtoFromArray(const void *data, int size, Message *proto);
  226. ///
  227. /// @ingroup domi_proto
  228. /// @brief Reads the proto file in the text format.
  229. /// @param [in] file path of proto file
  230. /// @param [out] message Memory for storing the proto file
  231. /// @return true success
  232. /// @return false fail
  233. ///
  234. GE_FUNC_VISIBILITY bool ReadProtoFromText(const char *file, google::protobuf::Message *message);
  235. GE_FUNC_VISIBILITY bool ReadProtoFromMem(const char *data, int size, google::protobuf::Message *message);
  236. ///
  237. /// @ingroup: domi_common
  238. /// @brief: get length of file
  239. /// @param [in] input_file: path of file
  240. /// @return long: File length. If the file length fails to be obtained, the value -1 is returned.
  241. ///
  242. GE_FUNC_VISIBILITY extern long GetFileLength(const std::string &input_file);
  243. ///
  244. /// @ingroup domi_common
  245. /// @brief Reads all data from a binary file.
  246. /// @param [in] file_name path of file
  247. /// @param [out] buffer Output memory address, which needs to be released by the caller.
  248. /// @param [out] length Output memory size
  249. /// @return false fail
  250. /// @return true success
  251. ///
  252. GE_FUNC_VISIBILITY bool ReadBytesFromBinaryFile(const char *file_name, char **buffer, int &length);
  253. GE_FUNC_VISIBILITY bool ReadBytesFromBinaryFile(const char *file_name, std::vector<char> &buffer);
  254. ///
  255. /// @ingroup domi_common
  256. /// @brief Recursively Creating a Directory
  257. /// @param [in] directory_path Path, which can be a multi-level directory.
  258. /// @return 0 success
  259. /// @return -1 fail
  260. ///
  261. GE_FUNC_VISIBILITY extern int CreateDirectory(const std::string &directory_path);
  262. ///
  263. /// @ingroup domi_common
  264. /// @brief Obtains the current time string.
  265. /// @return Time character string in the format : %Y%m%d%H%M%S, eg: 20171011083555
  266. ///
  267. GE_FUNC_VISIBILITY std::string CurrentTimeInStr();
  268. ///
  269. /// @ingroup domi_common
  270. /// @brief onverts Vector of a number to a string.
  271. /// @param [in] v Vector of a number
  272. /// @return string
  273. ///
  274. template <typename T>
  275. GE_FUNC_VISIBILITY std::string ToString(std::vector<T> &v) {
  276. std::stringstream ss;
  277. ss << "[";
  278. for (T x : v) {
  279. ss << x;
  280. ss << ", ";
  281. }
  282. std::string strRet =
  283. ss.str().substr(0, ss.str().length() - 2); // Delete the two extra characters at the end of the line.
  284. strRet += "]";
  285. return strRet;
  286. }
  287. ///
  288. /// @ingroup domi_common
  289. /// @brief Converts RepeatedField to String.
  290. /// @param [in] rpd_field RepeatedField
  291. /// @return string
  292. ///
  293. template <typename T>
  294. GE_FUNC_VISIBILITY std::string ToString(const google::protobuf::RepeatedField<T> &rpd_field) {
  295. std::stringstream ss;
  296. ss << "[";
  297. for (T x : rpd_field) {
  298. ss << x;
  299. ss << ", ";
  300. }
  301. std::string strRet =
  302. ss.str().substr(0, ss.str().length() - 2); // Delete the two extra characters at the end of the line.
  303. strRet += "]";
  304. return strRet;
  305. }
  306. ///
  307. /// @ingroup domi_common
  308. /// @brief Obtains the absolute time (timestamp) of the current system.
  309. /// @return Timestamp, in microseconds (US)
  310. ///
  311. ///
  312. GE_FUNC_VISIBILITY uint64_t GetCurrentTimestamp();
  313. ///
  314. /// @ingroup domi_common
  315. /// @brief Obtains the absolute time (timestamp) of the current system.
  316. /// @return Timestamp, in seconds (US)
  317. ///
  318. ///
  319. GE_FUNC_VISIBILITY uint32_t GetCurrentSecondTimestap();
  320. ///
  321. /// @ingroup domi_common
  322. /// @brief Check whether the product of two int64 numbers exceeds the int64 range.
  323. /// @param [in] a
  324. /// @param [in] b
  325. /// @return false: true: The result is within the normal int64 range.
  326. ///
  327. GE_FUNC_VISIBILITY bool CheckInt64MulOverflow(int64_t a, int64_t b);
  328. ///
  329. /// @ingroup domi_common
  330. /// @brief Absolute path for obtaining files.
  331. /// @param [in] path of input file
  332. /// @param [out] Absolute path of a file. If the absolute path cannot be obtained, an empty string is returned
  333. ///
  334. GE_FUNC_VISIBILITY std::string RealPath(const char *path);
  335. ///
  336. /// @ingroup domi_common
  337. /// @brief Check whether the specified input file path is valid.
  338. /// 1. The specified path cannot be empty.
  339. /// 2. The path can be converted to an absolute path.
  340. /// 3. The file path exists and is readable.
  341. /// @param [in] file_path path of input file
  342. /// @param [out] result
  343. ///
  344. GE_FUNC_VISIBILITY bool CheckInputPathValid(const std::string &file_path, const std::string &atc_param = "");
  345. ///
  346. /// @ingroup domi_common
  347. /// @brief Checks whether the specified output file path is valid.
  348. /// @param [in] file_path path of output file
  349. /// @param [out] result
  350. ///
  351. GE_FUNC_VISIBILITY bool CheckOutputPathValid(const std::string &file_path, const std::string &atc_param = "");
  352. ///
  353. /// @ingroup domi_common
  354. /// @brief Check whether the file path meets the whitelist verification requirements.
  355. /// @param [in] filePath file path
  356. /// @param [out] result
  357. ///
  358. GE_FUNC_VISIBILITY bool ValidateStr(const std::string &filePath, const std::string &mode);
  359. ///
  360. /// @ingroup domi_common
  361. /// @brief Check whether the file is normal file.
  362. /// @param [in] file_path file path
  363. /// @param [out] result
  364. ///
  365. GE_FUNC_VISIBILITY bool IsValidFile(const char *file_path);
  366. ///
  367. /// @ingroup domi_common
  368. /// @brief Check path invalid
  369. /// @param [in] path, path to be checked
  370. /// @param [in] length, length of path
  371. /// @return 0 success
  372. /// @return -1 fail
  373. ///
  374. GE_FUNC_VISIBILITY Status CheckPath(const char *path, size_t length);
  375. } // namespace ge
  376. #endif // INC_FRAMEWORK_COMMON_UTIL_H_

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