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

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