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

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