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.

elemwise_helper.cpp 9.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /**
  2. * \file dnn/src/cuda/elemwise_helper.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or
  10. * implied.
  11. */
  12. #include "src/cuda/elemwise_helper.cuh"
  13. #include "src/cuda/query_blocksize.cuh"
  14. #include "src/cuda/utils.h"
  15. #include "src/common/utils.h"
  16. #include <limits>
  17. #include <mutex>
  18. #include <unordered_map>
  19. #define _cb_check_ndim(n) megdnn::TensorShape::MAX_NDIM == n ||
  20. static_assert(MEGDNN_FOREACH_TENSOR_NDIM(_cb_check_ndim) false,
  21. "bad foreach ndim");
  22. #undef _cb_check_ndim
  23. namespace megdnn {
  24. namespace cuda {
  25. // ParamElemVisitor::init impls
  26. namespace elemwise_intl {
  27. #pragma GCC diagnostic push
  28. #pragma GCC diagnostic ignored "-Warray-bounds"
  29. template <int ndim, typename ctype>
  30. void ParamVisitorBase<ndim, ctype, BCAST_OTHER>::host_init(
  31. const TensorND& rv, int /*grid_size*/, int /*block_size*/,
  32. int /*packed_size*/) {
  33. megdnn_assert(rv.layout.ndim && rv.layout.ndim <= ndim);
  34. m_ptr = rv.ptr<ctype>();
  35. for (size_t i = 0; i < rv.layout.ndim; ++i) {
  36. m_stride[i] = rv.layout.stride[i];
  37. if (i + 1 < rv.layout.ndim)
  38. m_shape_highdim[i] = rv.layout.shape[i + 1];
  39. }
  40. for (int i = rv.layout.ndim - 1; i < ndim - 1; ++i) {
  41. m_shape_highdim[i] = 1;
  42. }
  43. for (int i = rv.layout.ndim; i < ndim; ++i) {
  44. m_stride[i] = 0;
  45. }
  46. }
  47. #pragma GCC diagnostic pop
  48. template <typename ctype>
  49. void ParamVisitorBase<3, ctype, BCAST_101>::host_init(const TensorND& rv,
  50. int grid_size,
  51. int block_size,
  52. int packed_size) {
  53. uint32_t shape2, shape1;
  54. int stride1;
  55. if (rv.layout.ndim == 3) {
  56. megdnn_assert(!rv.layout.stride[0] && !rv.layout.stride[2]);
  57. shape1 = rv.layout[1];
  58. shape2 = rv.layout[2];
  59. stride1 = rv.layout.stride[1];
  60. } else {
  61. megdnn_assert(rv.layout.ndim == 2 && !rv.layout.stride[1]);
  62. shape1 = rv.layout[0];
  63. shape2 = rv.layout[1];
  64. stride1 = rv.layout.stride[0];
  65. }
  66. m_ptr = rv.ptr<ctype>();
  67. m_stride1 = stride1;
  68. m_shape12.host_init(packed_size * grid_size * block_size, shape2, shape1);
  69. }
  70. template <typename ctype>
  71. void ParamVisitorBase<2, ctype, BCAST_10>::host_init(const TensorND& rv,
  72. int grid_size,
  73. int block_size,
  74. int packed_size) {
  75. megdnn_assert(rv.layout.ndim == NDIM && !rv.layout.stride[0]);
  76. m_ptr = rv.ptr<ctype>();
  77. m_stride1 = rv.layout.stride[1];
  78. m_shape1.host_init(packed_size * grid_size * block_size,
  79. rv.layout.shape[1]);
  80. }
  81. template <typename ctype>
  82. void ParamVisitorBase<2, ctype, BCAST_01>::host_init(const TensorND& rv,
  83. int grid_size,
  84. int block_size,
  85. int packed_size) {
  86. megdnn_assert(rv.layout.ndim == NDIM && !rv.layout.stride[1]);
  87. m_ptr = rv.ptr<ctype>();
  88. m_stride0 = rv.layout.stride[0];
  89. m_shape1.host_init(packed_size * grid_size * block_size,
  90. rv.layout.shape[1]);
  91. }
  92. template <typename ctype>
  93. void ParamVisitorBase<1, ctype, BCAST_FULL>::host_init(const TensorND& rv,
  94. int /*grid_size*/,
  95. int /*block_size*/,
  96. int /*packed_size*/) {
  97. megdnn_assert(rv.layout.ndim == NDIM && !rv.layout.stride[0]);
  98. m_ptr = rv.ptr<ctype>();
  99. }
  100. template <typename ctype>
  101. void ParamVectVisitor<4, ctype, BCAST_1010>::host_init(const TensorND& rv,
  102. int grid_size,
  103. int block_size) {
  104. megdnn_assert(rv.layout.ndim == NDIM && !rv.layout.stride[0] &&
  105. !rv.layout.stride[2]);
  106. m_ptr = rv.ptr<ctype>();
  107. m_stride1 = rv.layout.stride[1];
  108. m_stride3 = rv.layout.stride[3];
  109. uint32_t shape1 = rv.layout.shape[1];
  110. uint32_t shape2 = rv.layout.shape[2];
  111. uint32_t shape3 = rv.layout.shape[3];
  112. m_shape123.host_init(packed_size * grid_size * block_size, shape2 * shape3,
  113. shape1);
  114. m_shape3.host_init(packed_size * grid_size * block_size, shape3);
  115. }
  116. #define INST(ndim, ctype, brd) template class ParamVisitorBase<ndim, ctype, brd>
  117. #define INST_FOR_CTYPE \
  118. MEGDNN_FOREACH_TENSOR_NDIM(ndim_cb) \
  119. INST(3, ct, BCAST_101); \
  120. INST(2, ct, BCAST_10); \
  121. INST(2, ct, BCAST_01); \
  122. INST(1, ct, BCAST_FULL);
  123. #define ndim_cb(_ndim) INST(_ndim, ct, BCAST_OTHER);
  124. #define ct dt_byte
  125. INST_FOR_CTYPE
  126. #undef ct
  127. #define ct dt_int32
  128. INST_FOR_CTYPE
  129. #undef ct
  130. #define ct dt_float32
  131. INST_FOR_CTYPE
  132. #undef ct
  133. #define ct dt_float16
  134. INST_FOR_CTYPE
  135. #undef ct
  136. #define ct dt_bfloat16
  137. INST_FOR_CTYPE
  138. #undef ct
  139. #define ct dt_int8
  140. INST_FOR_CTYPE
  141. #undef ct
  142. #define ct dt_uint8
  143. INST_FOR_CTYPE
  144. #undef ct
  145. #define ct dt_int16
  146. INST_FOR_CTYPE
  147. #undef ct
  148. #define ct dt_quint8
  149. INST_FOR_CTYPE
  150. #undef ct
  151. #define ct dt_qint8
  152. INST_FOR_CTYPE
  153. #undef ct
  154. #define ct dt_qint32
  155. INST_FOR_CTYPE
  156. #undef ct
  157. #define ct dt_bool
  158. INST_FOR_CTYPE
  159. #undef ct
  160. #undef INST_FOR_CTYPE
  161. #undef INST
  162. #define INST(ndim, ctype, brd) template class ParamElemVisitor<ndim, ctype, brd>
  163. #define INST_FOR_CTYPE \
  164. MEGDNN_FOREACH_TENSOR_NDIM(ndim_cb) \
  165. INST(3, ct, BCAST_101); \
  166. INST(2, ct, BCAST_10); \
  167. INST(2, ct, BCAST_01); \
  168. INST(1, ct, BCAST_FULL);
  169. #define ndim_cb(_ndim) INST(_ndim, ct, BCAST_OTHER);
  170. #define ct dt_byte
  171. INST_FOR_CTYPE
  172. #undef ct
  173. #define ct dt_int32
  174. INST_FOR_CTYPE
  175. #undef ct
  176. #define ct dt_float32
  177. INST_FOR_CTYPE
  178. #undef ct
  179. #define ct dt_float16
  180. INST_FOR_CTYPE
  181. #undef ct
  182. #define ct dt_bfloat16
  183. INST_FOR_CTYPE
  184. #undef ct
  185. #define ct dt_int8
  186. INST_FOR_CTYPE
  187. #undef ct
  188. #define ct dt_uint8
  189. INST_FOR_CTYPE
  190. #undef ct
  191. #define ct dt_int16
  192. INST_FOR_CTYPE
  193. #undef ct
  194. #define ct dt_quint8
  195. INST_FOR_CTYPE
  196. #undef ct
  197. #define ct dt_qint8
  198. INST_FOR_CTYPE
  199. #undef ct
  200. #define ct dt_qint32
  201. INST_FOR_CTYPE
  202. #undef ct
  203. #define ct dt_bool
  204. INST_FOR_CTYPE
  205. #undef ct
  206. #undef ndim_cb
  207. #undef INST_FOR_CTYPE
  208. #undef INST
  209. #define INST(dt_ibyte) template class ParamVectVisitor<4, dt_ibyte, BCAST_1010>
  210. INST(dt_int8);
  211. INST(dt_uint8);
  212. INST(dt_bool);
  213. INST(dt_qint8);
  214. INST(dt_quint8);
  215. #undef dt_ibyte
  216. template <int ndim>
  217. void ParamElemVisitor4bitBase<ndim, BCAST_OTHER>::host_init(
  218. const TensorND& rv, int /*grid_size*/, int /*block_size*/) {
  219. m_ptr = reinterpret_cast<Storage*>(rv.raw_ptr);
  220. auto min_stride = rv.layout.stride[0];
  221. for (size_t i = 0; i < rv.layout.ndim; ++i) {
  222. m_stride[i] = rv.layout.stride[i];
  223. m_shape[i] = rv.layout.shape[i];
  224. if (i + 1 < rv.layout.ndim) {
  225. m_shape_highdim[i] = rv.layout.shape[i + 1];
  226. if (rv.layout.stride[i + 1] == 1)
  227. m_align_shape_highdim[i] =
  228. (uint32_t)round_up((int)rv.layout.shape[i + 1], 2);
  229. else
  230. m_align_shape_highdim[i] = rv.layout.shape[i + 1];
  231. }
  232. if (min_stride > rv.layout.stride[i]) {
  233. min_stride = rv.layout.stride[i];
  234. }
  235. }
  236. megdnn_assert(min_stride == 1 || min_stride == 2);
  237. m_is_min_stride_2 = (min_stride == 2);
  238. for (size_t i = rv.layout.ndim - 1; i < ndim - 1; ++i) {
  239. m_shape_highdim[i] = 1;
  240. m_align_shape_highdim[i] = 1;
  241. }
  242. for (size_t i = rv.layout.ndim; i < ndim; ++i) {
  243. m_stride[i] = 0;
  244. m_shape[i] = 1;
  245. }
  246. m_is_physical_contiguous = rv.layout.is_physical_contiguous();
  247. }
  248. #define ndim_cb(_ndim) \
  249. template class ParamElemVisitor4bitBase<_ndim, BCAST_OTHER>;
  250. MEGDNN_FOREACH_TENSOR_NDIM(ndim_cb)
  251. #undef ndim_cb
  252. } // namespace elemwise_intl
  253. void elemwise_intl::get_launch_spec(const void* kern, size_t size,
  254. int* grid_size, int* block_size) {
  255. safe_size_in_kern(size);
  256. auto config = query_launch_config_for_kernel(kern);
  257. *block_size = config.block_size;
  258. int a = size / (config.block_size * 2),
  259. b = (size - 1) / (config.block_size * 3) + 1;
  260. if (current_device_prop().major <= 3) {
  261. // for Kepler, less blocks (more work per thread) is faster
  262. *grid_size = b;
  263. } else {
  264. *grid_size = std::max(a, b);
  265. }
  266. if (!*grid_size) {
  267. *block_size = std::min<int>(std::max<int>(size / 64, 1) * 32, 1024);
  268. *grid_size = std::max<int>(size / *block_size, 1);
  269. }
  270. // because we unroll 3 times in the kernel
  271. megdnn_assert(static_cast<size_t>(*block_size) * *grid_size * 3 >=
  272. size);
  273. }
  274. void elemwise_intl::on_bad_ndim(int ndim) {
  275. megdnn_throw(ssprintf("invalid ndim: %d", ndim));
  276. MEGDNN_MARK_USED_VAR(ndim);
  277. }
  278. } // namespace cuda
  279. } // namespace megdnn
  280. // vim: ft=cpp syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台