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

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