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.

pooling.cpp 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /**
  2. * \file dnn/test/cuda/pooling.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2020 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 implied.
  10. */
  11. #include "test/cuda/fixture.h"
  12. #include "megdnn/tensor_iter.h"
  13. #include "test/common/checker.h"
  14. #include "test/common/pooling.h"
  15. #include "src/common/utils.h"
  16. #include "test/cuda/utils.h"
  17. // to check cudnn version
  18. #include <cudnn.h>
  19. #include "test/cuda/benchmark.h"
  20. namespace megdnn {
  21. namespace test {
  22. TEST_F(CUDA, POOLING_FORWARD)
  23. {
  24. auto args = pooling::get_args();
  25. using Format = param::Pooling::Format;
  26. std::vector<DType> dtypes{dtype::Float16(), dtype::Float32()};
  27. if (check_compute_capability(6, 0)) {
  28. // int pooling is supported only for Pascal or higher
  29. dtypes.push_back(dtype::Int8());
  30. }
  31. for (auto dtype: dtypes)
  32. for (auto format: {Format::NCHW, Format::NHWC})
  33. for (auto &&arg: args) {
  34. auto param = arg.param;
  35. auto src = arg.ishape;
  36. param.format = format;
  37. if (param.format == Format::NHWC) {
  38. src = cvt_src_or_dst_nchw2nhwc(src);
  39. }
  40. Checker<Pooling> checker(handle_cuda());
  41. if (dtype == dtype::Int8()) {
  42. // different versions of cuDNN differs in rounding behavior;
  43. // setting eps to 1 to allow for rounding errors.
  44. checker.set_epsilon(1 + 1e-3);
  45. } else {
  46. checker.set_epsilon(1e-2);
  47. }
  48. checker.set_param(param)
  49. .set_dtype(0, dtype)
  50. .set_dtype(1, dtype)
  51. .exec(TensorShapeArray{
  52. src, {}});
  53. }
  54. /* add test for new Mode temporarily */
  55. for (auto dtype: dtypes)
  56. for (auto format: {Format::NCHW, Format::NHWC})
  57. for(auto &&arg : args) {
  58. auto param = arg.param;
  59. if(param.mode == Pooling::Mode::AVERAGE)
  60. param.mode = Pooling::Mode::AVERAGE_COUNT_EXCLUDE_PADDING;
  61. else continue;
  62. auto src = arg.ishape;
  63. param.format = format;
  64. if (param.format == Format::NHWC) {
  65. src = cvt_src_or_dst_nchw2nhwc(src);
  66. }
  67. Checker<Pooling> checker(handle_cuda());
  68. if (dtype == dtype::Int8()) {
  69. // different versions of cuDNN differs in rounding behavior;
  70. // setting eps to 1 to allow for rounding errors.
  71. checker.set_epsilon(1 + 1e-3);
  72. } else {
  73. checker.set_epsilon(1e-2);
  74. }
  75. checker.set_param(param)
  76. .set_dtype(0, dtype)
  77. .set_dtype(1, dtype)
  78. .exec(TensorShapeArray{
  79. src, {}});
  80. }
  81. }
  82. TEST_F(CUDA, POOLING_BACKWARD)
  83. {
  84. auto args = pooling::get_args();
  85. for (auto &&arg: args) {
  86. Checker<PoolingBackward> checker(handle_cuda());
  87. TensorLayout ilayout = TensorLayout(arg.ishape, dtype::Float32());
  88. TensorLayout olayout;
  89. auto constraint = [this,
  90. arg](CheckerHelper::TensorValueArray& tensors_orig) {
  91. megdnn_assert(tensors_orig.size() == 4);
  92. auto opr = handle_cuda()->create_operator<PoolingForward>();
  93. opr->param() = arg.param;
  94. auto tensors_cuda_storage = CheckerHelper::alloc_tensors(
  95. handle_cuda(),
  96. {tensors_orig[0].layout, tensors_orig[1].layout}, 0);
  97. auto&& tensors_cuda = *tensors_cuda_storage;
  98. auto span = tensors_cuda[0].layout.span();
  99. auto dst = static_cast<dt_byte*>(tensors_cuda[0].raw_ptr) +
  100. span.low_byte;
  101. auto src = static_cast<const dt_byte*>(tensors_orig[0].raw_ptr) +
  102. span.low_byte;
  103. megdnn_memcpy_H2D(handle_cuda(), dst, src, span.dist_byte());
  104. auto workspace_size = opr->get_workspace_in_bytes(
  105. tensors_cuda[0].layout, tensors_cuda[1].layout);
  106. auto workspace_cuda = megdnn_malloc(handle_cuda(), workspace_size);
  107. Workspace workspace{static_cast<dt_byte*>(workspace_cuda),
  108. workspace_size};
  109. opr->exec(tensors_cuda[0], tensors_cuda[1], workspace);
  110. megdnn_free(handle_cuda(), workspace_cuda);
  111. span = tensors_cuda[1].layout.span();
  112. dst = static_cast<dt_byte*>(tensors_orig[1].raw_ptr) +
  113. span.low_byte;
  114. src = static_cast<const dt_byte*>(tensors_cuda[1].raw_ptr) +
  115. span.low_byte;
  116. megdnn_memcpy_D2H(handle_cuda(), dst, src, span.dist_byte());
  117. };
  118. {
  119. auto opr = handle_cuda()->create_operator<PoolingForward>();
  120. opr->param() = arg.param;
  121. opr->deduce_layout(ilayout, olayout);
  122. }
  123. auto set_dtype = [&checker](DType dtype)
  124. {
  125. checker.set_dtype(0, dtype).
  126. set_dtype(1, dtype).
  127. set_dtype(2, dtype).
  128. set_dtype(3, dtype);
  129. };
  130. checker.set_tensors_constraint(constraint);
  131. set_dtype(dtype::Float32());
  132. checker.set_param(arg.param).exec(TensorShapeArray{
  133. ilayout, olayout, olayout, ilayout});
  134. Float16PeriodicalRNG rng;
  135. set_dtype(dtype::Float16());
  136. checker
  137. .set_param(arg.param)
  138. .set_rng(0, &rng)
  139. .set_epsilon(1e-2)
  140. .exec(TensorShapeArray{
  141. ilayout, olayout, olayout, ilayout});
  142. }
  143. /* add test for new Mode temporarily */
  144. for(auto &&arg : args) {
  145. if(arg.param.mode == Pooling::Mode::AVERAGE)
  146. arg.param.mode = Pooling::Mode::AVERAGE_COUNT_EXCLUDE_PADDING;
  147. else continue;
  148. Checker<PoolingBackward> checker(handle_cuda());
  149. TensorLayout ilayout = TensorLayout(arg.ishape, dtype::Float32());
  150. TensorLayout olayout;
  151. auto constraint = [this,
  152. arg](CheckerHelper::TensorValueArray& tensors_orig) {
  153. megdnn_assert(tensors_orig.size() == 4);
  154. auto opr = handle_cuda()->create_operator<PoolingForward>();
  155. opr->param() = arg.param;
  156. auto tensors_cuda_storage = CheckerHelper::alloc_tensors(
  157. handle_cuda(),
  158. {tensors_orig[0].layout, tensors_orig[1].layout}, 0);
  159. auto&& tensors_cuda = *tensors_cuda_storage;
  160. auto span = tensors_cuda[0].layout.span();
  161. auto dst = static_cast<dt_byte*>(tensors_cuda[0].raw_ptr) +
  162. span.low_byte;
  163. auto src = static_cast<const dt_byte*>(tensors_orig[0].raw_ptr) +
  164. span.low_byte;
  165. megdnn_memcpy_H2D(handle_cuda(), dst, src, span.dist_byte());
  166. auto workspace_size = opr->get_workspace_in_bytes(
  167. tensors_cuda[0].layout, tensors_cuda[1].layout);
  168. auto workspace_cuda = megdnn_malloc(handle_cuda(), workspace_size);
  169. Workspace workspace{static_cast<dt_byte*>(workspace_cuda),
  170. workspace_size};
  171. opr->exec(tensors_cuda[0], tensors_cuda[1], workspace);
  172. megdnn_free(handle_cuda(), workspace_cuda);
  173. span = tensors_cuda[1].layout.span();
  174. dst = static_cast<dt_byte*>(tensors_orig[1].raw_ptr) +
  175. span.low_byte;
  176. src = static_cast<const dt_byte*>(tensors_cuda[1].raw_ptr) +
  177. span.low_byte;
  178. megdnn_memcpy_D2H(handle_cuda(), dst, src, span.dist_byte());
  179. };
  180. {
  181. auto opr = handle_cuda()->create_operator<PoolingForward>();
  182. opr->param() = arg.param;
  183. opr->deduce_layout(ilayout, olayout);
  184. }
  185. auto set_dtype = [&checker](DType dtype)
  186. {
  187. checker.set_dtype(0, dtype).
  188. set_dtype(1, dtype).
  189. set_dtype(2, dtype).
  190. set_dtype(3, dtype);
  191. };
  192. checker.set_tensors_constraint(constraint);
  193. set_dtype(dtype::Float32());
  194. checker.set_param(arg.param).exec(TensorShapeArray{
  195. ilayout, olayout, olayout, ilayout});
  196. Float16PeriodicalRNG rng;
  197. set_dtype(dtype::Float16());
  198. checker
  199. .set_param(arg.param)
  200. .set_rng(0, &rng)
  201. .set_epsilon(1e-2)
  202. .exec(TensorShapeArray{
  203. ilayout, olayout, olayout, ilayout});
  204. }
  205. }
  206. TEST_F(CUDA, POOLING_FORWARD_NCHW4) {
  207. require_compute_capability(7, 5);
  208. using Param = param::Pooling;
  209. Checker<Pooling> checker(handle_cuda());
  210. Param param;
  211. checker.set_dtype(0, dtype::QuantizedS8(0.1f));
  212. param.format = Param::Format::NCHW4;
  213. checker.set_epsilon(1 + 1e-3);
  214. checker.set_param(param).exec({{20, 3, 50, 50, 4}, {}});
  215. }
  216. #if CUDNN_VERSION >= 7500
  217. TEST_F(CUDA, POOLING_FORWARD_NCHW32) {
  218. require_compute_capability(7, 5);
  219. using Param = param::Pooling;
  220. Checker<Pooling> checker(handle_cuda());
  221. Param param;
  222. auto i8_min = std::numeric_limits<int8_t>().min();
  223. auto i8_max = std::numeric_limits<int8_t>().max();
  224. UniformIntRNG int_rng{i8_min, i8_max};
  225. checker.set_dtype(0, dtype::QuantizedS8(0.1f));
  226. param.format = Param::Format::NCHW32;
  227. checker.set_epsilon(1e-3).set_rng(0, &int_rng);
  228. checker.set_param(param).exec({{64, 8, 28, 28, 32}, {}});
  229. }
  230. #endif
  231. TEST_F(CUDA, POOLING_FORWARD_CHWN4) {
  232. require_compute_capability(6, 1);
  233. using Param = param::Pooling;
  234. Checker<Pooling> checker(handle_cuda());
  235. Param param;
  236. auto i8_min = std::numeric_limits<int8_t>().min();
  237. auto i8_max = std::numeric_limits<int8_t>().max();
  238. UniformIntRNG int_rng{i8_min, i8_max};
  239. checker.set_dtype(0, dtype::QuantizedS8(0.1f));
  240. param.format = Param::Format::CHWN4;
  241. for (auto mode : {Param::Mode::MAX, Param::Mode::AVERAGE,
  242. Param::Mode::AVERAGE_COUNT_EXCLUDE_PADDING}) {
  243. param.mode = mode;
  244. checker.set_epsilon(1e-3).set_rng(0, &int_rng);
  245. checker.set_param(param).exec({{8, 28, 28, 64, 4}, {}});
  246. checker.set_param(param).exec({{8, 28, 28, 15, 4}, {}});
  247. checker.set_param(param).exec({{8, 28, 28, 30, 4}, {}});
  248. }
  249. }
  250. #if MEGDNN_WITH_BENCHMARK
  251. TEST_F(CUDA, BENCHMARK_POOLING_CHWN4) {
  252. CUBenchmarker<Pooling> bencher(handle_cuda());
  253. size_t nr_times = 1000;
  254. bencher.set_times(nr_times);
  255. using Param = param::Pooling;
  256. Param param;
  257. auto run_bench = [&](size_t N, size_t C, size_t H, size_t W, size_t stride,
  258. size_t padding, size_t window,
  259. Param::Mode mode = Param::Mode::MAX) {
  260. param.mode = mode;
  261. param.pad_h = param.pad_w = padding;
  262. param.window_h = param.window_w = window;
  263. param.stride_h = param.stride_w = stride;
  264. param.format = Param::Format::NCHW4;
  265. bencher.set_dtype(0, dtype::QuantizedS8{0.1f});
  266. bencher.set_param(param);
  267. auto time_cudnn = bencher.execs({{N, C / 4, H, W, 4}, {}}) / nr_times;
  268. param.format = Param::Format::CHWN4;
  269. bencher.set_param(param);
  270. auto time_chwn4 = bencher.execs({{C / 4, H, W, N, 4}, {}}) / nr_times;
  271. size_t oh = infer_conv_shape(H, window, stride, padding),
  272. ow = infer_conv_shape(W, window, stride, padding);
  273. float io = (N * C * H * W + N * C * oh * ow) * sizeof(int8_t);
  274. printf("time(cudnn)=%.2f ms, time(chwn4)=%.2f ms, "
  275. "bandwidth(cudnn)=%.2f Gb/s, bandwidth(chwn4)=%.2f Gb/s\n",
  276. time_cudnn, time_chwn4, io / (1e6 * time_cudnn),
  277. io / (1e6 * time_chwn4));
  278. };
  279. run_bench(64, 64, 112, 112, 2, 1, 2);
  280. run_bench(256, 64, 112, 112, 2, 1, 2);
  281. run_bench(64, 64, 112, 112, 2, 1, 2, Param::Mode::AVERAGE);
  282. run_bench(256, 64, 112, 112, 2, 1, 2, Param::Mode::AVERAGE);
  283. run_bench(64, 64, 112, 112, 2, 1, 2,
  284. Param::Mode::AVERAGE_COUNT_EXCLUDE_PADDING);
  285. run_bench(256, 64, 112, 112, 2, 1, 2,
  286. Param::Mode::AVERAGE_COUNT_EXCLUDE_PADDING);
  287. }
  288. #endif
  289. } // namespace test
  290. } // namespace megdnn
  291. // vim: syntax=cpp.doxygen

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