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

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