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.

ge_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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 COMMON_GRAPH_DEBUG_GE_UTIL_H_
  17. #define COMMON_GRAPH_DEBUG_GE_UTIL_H_
  18. #include <limits.h>
  19. #include <math.h>
  20. #include <iostream>
  21. #include <memory>
  22. #include <sstream>
  23. #include <string>
  24. #include <utility>
  25. #include <vector>
  26. #include "framework/common/debug/ge_log.h"
  27. #include "graph/debug/ge_log.h"
  28. #include "graph/ge_error_codes.h"
  29. #if !defined(__ANDROID__) && !defined(ANDROID)
  30. #define GE_DYNAMIC_CAST dynamic_cast
  31. #define GE_DYNAMIC_POINTER_CAST std::dynamic_pointer_cast
  32. #else
  33. #define GE_DYNAMIC_CAST static_cast
  34. #define GE_DYNAMIC_POINTER_CAST std::static_pointer_cast
  35. #endif
  36. #define GE_RETURN_IF_ERROR(expr) \
  37. do { \
  38. const ::ge::optStatus _status = (expr); \
  39. if (_status) return _status; \
  40. } while (0)
  41. #define GE_RETURN_WITH_LOG_IF_INFO(expr, ...) \
  42. do { \
  43. const ::ge::optStatus _status = (expr); \
  44. if (_status) { \
  45. GELOGI(__VA_ARGS__); \
  46. return _status; \
  47. } \
  48. } while (0)
  49. // Verify whether the parameter is true. If yes, return graph failed and record the error log
  50. #define GE_RETURN_WITH_LOG_IF_TRUE(condition, ...) \
  51. do { \
  52. if (condition) { \
  53. GELOGE(ge::GRAPH_FAILED, __VA_ARGS__); \
  54. return ge::GRAPH_FAILED; \
  55. } \
  56. } while (0)
  57. // Verify whether the parameter is false. If yes, return graph failed and record the error log
  58. #define GE_RETURN_WITH_LOG_IF_FALSE(condition, ...) \
  59. do { \
  60. bool _condition = (condition); \
  61. if (!_condition) { \
  62. GELOGE(ge::GRAPH_FAILED, __VA_ARGS__); \
  63. return ge::GRAPH_FAILED; \
  64. } \
  65. } while (0)
  66. // Verify whether the parameter is true. If yes, return GRAPH_PARAM_INVALID and record the error log
  67. #define GE_RT_PARAM_INVALID_WITH_LOG_IF_TRUE(condition, ...) \
  68. do { \
  69. if (condition) { \
  70. GELOGE(ge::GRAPH_PARAM_INVALID, __VA_ARGS__); \
  71. return ge::GRAPH_PARAM_INVALID; \
  72. } \
  73. } while (0)
  74. // Verify whether the parameter is false. If yes, return GRAPH_PARAM_INVALID and record the error log
  75. #define GE_RT_PARAM_INVALID_WITH_LOG_IF_FALSE(condition, ...) \
  76. do { \
  77. bool _condition = (condition); \
  78. if (!_condition) { \
  79. GELOGE(ge::GRAPH_PARAM_INVALID, __VA_ARGS__); \
  80. return ge::GRAPH_PARAM_INVALID; \
  81. } \
  82. } while (0)
  83. // Verify whether the parameter is null. If yes, return GRAPH_PARAM_INVALID and record the error log
  84. #define GE_CHECK_NOTNULL(val) \
  85. do { \
  86. if (val == nullptr) { \
  87. GELOGE(ge::GRAPH_PARAM_INVALID, "param[%s] must not be null.", #val); \
  88. return ge::GRAPH_PARAM_INVALID; \
  89. } \
  90. } while (0)
  91. // Verify whether the parameter is null. If yes, return GRAPH_PARAM_INVALID and record the error log
  92. #define GE_CHECK_NOTNULL_EXEC(val, expr) \
  93. do { \
  94. if (val == nullptr) { \
  95. GELOGE(ge::GRAPH_PARAM_INVALID, "param[%s] must not be null.", #val); \
  96. expr; \
  97. } \
  98. } while (0)
  99. // Verify whether the parameter is null. If yes, return false and record the error log
  100. #define GE_RT_FALSE_CHECK_NOTNULL(val) \
  101. do { \
  102. if (val == nullptr) { \
  103. GELOGE(ge::GRAPH_FAILED, "param[%s] must not be null.", #val); \
  104. return false; \
  105. } \
  106. } while (0)
  107. // Check whether the parameter is out of range
  108. #define GE_CHECK_SIZE(size) \
  109. do { \
  110. if (size == 0) { \
  111. GELOGE(ge::GRAPH_PARAM_INVALID, "param[%s] is out of range", #size); \
  112. return ge::GRAPH_PARAM_INVALID; \
  113. } \
  114. } while (0)
  115. ///
  116. /// @ingroup GE_common
  117. /// eg:GE_DEFINE_BYTE_SIZE(filter_byte, filter.data().size(), sizeof(float));
  118. ///
  119. #define GE_DEFINE_BYTE_SIZE(_var_name, _expr, _sizeof) \
  120. uint32_t _var_name; \
  121. do { \
  122. uint32_t _expr_size = (_expr); \
  123. uint32_t _sizeof_size = (_sizeof); \
  124. if (_expr_size > (0xffffffff) / _sizeof_size) { \
  125. GELOGE(ge::GRAPH_PARAM_INVALID, "byte size : %s is out of range", #_var_name); \
  126. return ge::GRAPH_PARAM_INVALID; \
  127. } \
  128. _var_name = _sizeof_size * _expr_size; \
  129. } while (0);
  130. // Check whether the container is empty
  131. #define GE_CHECK_VECTOR_NOT_EMPTY(vector) \
  132. do { \
  133. if (vector.empty()) { \
  134. GELOGE(ge::GRAPH_FAILED, "param[#vector] is empty", #vector); \
  135. return ge::GRAPH_FAILED; \
  136. } \
  137. } while (0)
  138. // Check whether the container is empty and return the specified status code
  139. #define GE_CHECK_VECTOR_NOT_EMPTY_RET_STATUS(vector, _status) \
  140. do { \
  141. if (vector.empty()) { \
  142. GELOGE(_status, "param[%s] is empty", #vector); \
  143. return _status; \
  144. } \
  145. } while (0)
  146. ///
  147. /// @ingroup GE_common
  148. /// @brief This macro provides the ability to disable copying constructors and assignment operators.
  149. /// It is usually placed under private
  150. ///
  151. #define GE_DISALLOW_COPY_AND_ASSIGN(TypeName) \
  152. TypeName(const TypeName &) = delete; \
  153. void operator=(const TypeName &) = delete
  154. /// Check whether the size is 0 or out of range
  155. /// @param:size:Size to be verified
  156. #define GE_CHECK_SIZE_RANGE(size) \
  157. do { \
  158. if (size == 0 || size >= UINT_MAX / 4) { \
  159. GELOGE(ge::GRAPH_PARAM_INVALID, "param[%s] is out of range", #size); \
  160. return ge::GRAPH_PARAM_INVALID; \
  161. } \
  162. } while (0)
  163. #define GE_CHECK_SHORT_SIZE_RANGE(size) \
  164. do { \
  165. if (size == 0 || size >= UINT_MAX / 2) { \
  166. GELOGE(ge::GRAPH_PARAM_INVALID, "param[%s] is out of range", #size); \
  167. return ge::GRAPH_PARAM_INVALID; \
  168. } \
  169. } while (0)
  170. #define GE_CHECK_POSITIVE_SIZE_RANGE(size) \
  171. do { \
  172. if (size <= 0) { \
  173. GELOGE(ge::GRAPH_PARAM_INVALID, "param[%s] is not a positive number", #size); \
  174. return ge::GRAPH_PARAM_INVALID; \
  175. } \
  176. } while (0)
  177. #define GE_CHECK_POSITIVE_SHORT_SIZE_RANGE(size) \
  178. do { \
  179. if (size <= 0 || size == 0 || size >= UINT_MAX / 4) { \
  180. GELOGE(ge::GRAPH_PARAM_INVALID, "param[%s] is out of range", #size); \
  181. return ge::GRAPH_PARAM_INVALID; \
  182. } \
  183. } while (0)
  184. // Verify that the value on the left is greater than or equal to the value on the right
  185. #define GE_CHECK_GE(lhs, rhs) \
  186. do { \
  187. if (lhs < rhs) { \
  188. GELOGE(ge::GRAPH_PARAM_INVALID, "param[%s] is less than[%s]", #lhs, #rhs); \
  189. return ge::GRAPH_PARAM_INVALID; \
  190. } \
  191. } while (0)
  192. // Check whether the parameters are equal
  193. #define GE_CHECK_EQ(val1, val2) \
  194. do { \
  195. if (val1 != val2) { \
  196. GELOGE(ge::GRAPH_PARAM_INVALID, "param[%s] is not equals to[%s]", #val1, #val2); \
  197. return ge::GRAPH_PARAM_INVALID; \
  198. } \
  199. } while (0)
  200. // Verify that the value on the left is less than or equal to the value on the right
  201. #define GE_CHECK_LE(lhs, rhs) \
  202. do { \
  203. if (lhs > rhs) { \
  204. GELOGE(ge::GRAPH_PARAM_INVALID, "param[%s] is greater than[%s]", #lhs, #rhs); \
  205. return ge::GRAPH_PARAM_INVALID; \
  206. } \
  207. } while (0)
  208. // Check whether the parameters are equal
  209. #define GE_CHECK_EQ_WITH_LOG(val1, val2, ...) \
  210. do { \
  211. if (val1 != val2) { \
  212. GELOGE(ge::GRAPH_PARAM_INVALID, __VA_ARGS__); \
  213. return ge::GRAPH_PARAM_INVALID; \
  214. } \
  215. } while (0)
  216. // If expr is false, the custom statement is executed
  217. #define CHECK_FALSE_EXEC(expr, exec_expr, ...) \
  218. do { \
  219. bool b = (expr); \
  220. if (!b) { \
  221. exec_expr; \
  222. } \
  223. } while (0)
  224. #define GE_DELETE_NEW_SINGLE(var) \
  225. do { \
  226. if (var != nullptr) { \
  227. delete var; \
  228. var = nullptr; \
  229. } \
  230. } while (0)
  231. #define GE_DELETE_NEW_ARRAY(var) \
  232. do { \
  233. if (var != nullptr) { \
  234. delete[] var; \
  235. var = nullptr; \
  236. } \
  237. } while (0)
  238. template <typename T, typename... Args>
  239. static inline std::shared_ptr<T> ComGraphMakeShared(Args &&... args) {
  240. using T_nc = typename std::remove_const<T>::type;
  241. std::shared_ptr<T> ret(new (std::nothrow) T_nc(std::forward<Args>(args)...));
  242. return ret;
  243. }
  244. #endif // COMMON_GRAPH_DEBUG_GE_UTIL_H_

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