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.

resize.cpp 9.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. * \file dnn/test/cuda/resize.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 implied.
  10. */
  11. #include "test/common/resize.h"
  12. #include "src/common/cv/enums.h"
  13. #include "test/common/benchmarker.h"
  14. #include "test/common/checker.h"
  15. #include "test/cuda/fixture.h"
  16. namespace megdnn {
  17. namespace test {
  18. namespace resize {
  19. TEST_F(CUDA, RESIZE_CV) {
  20. using namespace resize;
  21. std::vector<TestArg> args = get_cv_args();
  22. Checker<Resize> checker(handle_cuda());
  23. for (auto&& arg : args) {
  24. checker.set_param(arg.param)
  25. .set_dtype(0, dtype::Uint8())
  26. .set_dtype(1, dtype::Uint8())
  27. .set_epsilon(1)
  28. .set_max_avg_error(0.4)
  29. .execs({arg.src, arg.dst});
  30. }
  31. for (auto&& arg : args) {
  32. checker.set_param(arg.param)
  33. .set_dtype(0, dtype::Float32())
  34. .set_dtype(1, dtype::Float32())
  35. .set_epsilon(1e-3)
  36. .execs({arg.src, arg.dst});
  37. }
  38. }
  39. TEST_F(CUDA, RESIZE_FORWARD) {
  40. using namespace resize;
  41. IMode modes[] = {IMode::INTER_LINEAR, IMode::NEAREST, IMode::INTER_CUBIC};
  42. for (auto imode : modes) {
  43. std::vector<TestArg> args = get_args(imode);
  44. Checker<Resize> checker(handle_cuda());
  45. for (auto&& arg : args) {
  46. checker.set_param(arg.param)
  47. .set_dtype(0, dtype::Uint8())
  48. .set_dtype(1, dtype::Uint8())
  49. .execs({arg.src, arg.dst});
  50. }
  51. for (auto&& arg : args) {
  52. checker.set_param(arg.param)
  53. .set_dtype(0, dtype::Float32())
  54. .set_dtype(1, dtype::Float32())
  55. .set_epsilon(1e-3)
  56. .execs({arg.src, arg.dst});
  57. }
  58. for (auto&& arg : args) {
  59. checker.set_param(arg.param)
  60. .set_dtype(0, dtype::Int8())
  61. .set_dtype(1, dtype::Int8())
  62. .set_epsilon(1e-3)
  63. .execs({arg.src, arg.dst});
  64. }
  65. }
  66. }
  67. TEST_F(CUDA, RESIZE_NCHW4) {
  68. using namespace resize;
  69. Checker<Resize> checker(handle_cuda());
  70. auto args = get_nchw4_args();
  71. for (auto&& arg : args) {
  72. checker.set_param(arg.param)
  73. .set_dtype(0, dtype::QuantizedS8(0.1f))
  74. .set_dtype(1, dtype::QuantizedS8(0.1f))
  75. .set_epsilon(1 + 1e-3)
  76. .execs({arg.src, arg.dst});
  77. }
  78. }
  79. TEST_F(CUDA, RESIZE_NCHW_WITH_STRIDE) {
  80. IMode modes[] = {IMode::INTER_LINEAR, IMode::NEAREST, IMode::INTER_CUBIC};
  81. for (auto imode : modes) {
  82. param::Resize param;
  83. param.format = param::Resize::Format::NCHW;
  84. param.imode = imode;
  85. Checker<Resize> checker(handle_cuda());
  86. checker.set_epsilon(1 + 1e-3)
  87. .set_param(param);
  88. auto run = [&](TensorShape src_shape, std::vector<ptrdiff_t> src_layout,
  89. TensorShape dst_shape, DType dtype) {
  90. checker.set_dtype(0, dtype)
  91. .set_dtype(1, dtype)
  92. .execl({{src_shape, src_layout, dtype}, {dst_shape, dtype}});
  93. };
  94. for (DType& dtype : std::vector<DType>{dtype::Float32(), dtype::Uint8(),
  95. dtype::Int8()}) {
  96. run({2, 3, 4, 4}, {256, 32, 8, 1}, {2, 3, 3, 3}, dtype);
  97. run({1, 3, 4, 3}, {105, 35, 7, 2}, {1, 3, 5, 5}, dtype);
  98. run({1, 3, 40, 40}, {25600, 3200, 80, 1}, {1, 3, 30, 30}, dtype);
  99. run({2, 3, 4, 4}, {-256, 32, -8, 1}, {2, 3, 3, 3}, dtype);
  100. run({2, 3, 4, 4}, {256, -32, 8, -1}, {2, 3, 3, 3}, dtype);
  101. run({2, 3, 4, 4}, {-256, -32, -8, -1}, {2, 3, 3, 3}, dtype);
  102. }
  103. }
  104. }
  105. TEST_F(CUDA, RESIZE_BACKWARD) {
  106. IMode modes[] = {IMode::INTER_LINEAR, IMode::NEAREST, IMode::INTER_CUBIC};
  107. for (auto imode : modes) {
  108. Checker<ResizeBackward> checker(handle_cuda());
  109. param::Resize param;
  110. param.format = param::Resize::Format::NCHW;
  111. param.imode = imode;
  112. checker.set_param(param);
  113. checker.execs({{2, 3, 4, 5}, {2, 3, 8, 9}});
  114. checker.execs({{2, 5, 8, 9}, {2, 5, 4, 5}});
  115. checker.execs({{2, 5, 8, 5}, {2, 5, 4, 9}});
  116. checker.execs({{2, 5, 4, 9}, {2, 5, 8, 5}});
  117. }
  118. }
  119. #if MEGDNN_WITH_BENCHMARK
  120. TEST_F(CUDA, BENCHMARK_RESIZE_CV) {
  121. Benchmarker<Resize> benchmarker(handle_cuda());
  122. param::Resize param;
  123. param.format = param::Resize::Format::NHWC;
  124. param.imode = param::Resize::InterpolationMode::LANCZOS4;
  125. benchmarker.set_param(param);
  126. benchmarker.set_display(false);
  127. auto run = [&benchmarker](const TensorShape& src, const TensorShape& dst) {
  128. auto used = benchmarker.execs({src, dst});
  129. //! bandwith: each dst elem require 4 read and 1 write
  130. //! gflops: each dst elem require 4 mul + 3 add
  131. printf("run %s->%s used: %f ms %f GBPS %f Gflops\n",
  132. src.to_string().c_str(), dst.to_string().c_str(), used,
  133. dst.total_nr_elems() * (4.f + 1.f) * sizeof(float) /
  134. (1024 * 1024 * 1024) / used * 1e3,
  135. dst.total_nr_elems() * (4.f + 3.f) / (1024 * 1024 * 1024) /
  136. used * 1e3);
  137. };
  138. run({1, 128, 128, 3}, {1, 256, 256, 3});
  139. }
  140. TEST_F(CUDA, BENCHMARK_RESIZE_FORWARD) {
  141. Benchmarker<Resize> benchmarker(handle_cuda());
  142. param::Resize param;
  143. param.format = param::Resize::Format::NCHW;
  144. param.imode = param::Resize::InterpolationMode::LINEAR;
  145. benchmarker.set_param(param);
  146. benchmarker.set_display(false);
  147. auto run = [&benchmarker](const TensorShape& src, const TensorShape& dst) {
  148. auto used = benchmarker.execs({src, dst});
  149. //! bandwith: each dst elem require 4 read and 1 write
  150. //! gflops: each dst elem require 4 mul + 3 add
  151. printf("run %s->%s used: %f ms %f GBPS %f Gflops\n",
  152. src.to_string().c_str(), dst.to_string().c_str(), used,
  153. dst.total_nr_elems() * (4.f + 1.f) * sizeof(float) /
  154. (1024 * 1024 * 1024) / used * 1e3,
  155. dst.total_nr_elems() * (4.f + 3.f) / (1024 * 1024 * 1024) /
  156. used * 1e3);
  157. };
  158. run({1, 100, 256, 256}, {1, 100, 256, 5120});
  159. run({1, 100, 256, 5120}, {1, 100, 256, 256});
  160. run({1, 100, 256, 256}, {1, 100, 512, 512});
  161. run({1, 100, 512, 512}, {1, 100, 256, 256});
  162. }
  163. TEST_F(CUDA, BENCHMARK_RESIZE_FORWARD_NCHW4) {
  164. Benchmarker<Resize> benchmarker(handle_cuda());
  165. param::Resize param;
  166. param.imode = param::Resize::InterpolationMode::LINEAR;
  167. benchmarker.set_display(false);
  168. auto run = [&benchmarker](const TensorShape& src, const TensorShape& dst) {
  169. auto used = benchmarker.execs({src, dst});
  170. //! bandwith: each dst elem require 4 read and 1 write
  171. //! gflops: each dst elem require 4 mul + 3 add
  172. printf("run %s->%s used: %f ms %f GBPS %f Gflops\n",
  173. src.to_string().c_str(), dst.to_string().c_str(), used,
  174. dst.total_nr_elems() * (4.f + 1.f) /
  175. (1024 * 1024 * 1024) / used * 1e3,
  176. dst.total_nr_elems() * (4.f + 3.f) / (1024 * 1024 * 1024) /
  177. used * 1e3);
  178. };
  179. param.format = param::Resize::Format::NCHW;
  180. benchmarker.set_param(param);
  181. benchmarker.set_dtype(0, dtype::Int8());
  182. benchmarker.set_dtype(1, dtype::Int8());
  183. run({1, 100, 256, 256}, {1, 100, 256, 5120});
  184. run({1, 100, 256, 5120}, {1, 100, 256, 256});
  185. run({1, 100, 256, 256}, {1, 100, 512, 512});
  186. run({1, 100, 512, 512}, {1, 100, 256, 256});
  187. param.format = param::Resize::Format::NCHW4;
  188. benchmarker.set_param(param);
  189. benchmarker.set_dtype(0, dtype::QuantizedS8(1.0f));
  190. benchmarker.set_dtype(1, dtype::QuantizedS8(1.0f));
  191. run({1, 25, 256, 256, 4}, {1, 25, 256, 5120, 4});
  192. run({1, 25, 256, 5120, 4}, {1, 25, 256, 256, 4});
  193. run({1, 25, 256, 256, 4}, {1, 25, 512, 512, 4});
  194. run({1, 25, 512, 512, 4}, {1, 25, 256, 256, 4});
  195. }
  196. TEST_F(CUDA, BENCHMARK_RESIZE_BACKWARD) {
  197. Benchmarker<ResizeBackward> benchmarker(handle_cuda());
  198. param::Resize param;
  199. param.format = param::Resize::Format::NCHW;
  200. param.imode = param::Resize::InterpolationMode::LINEAR;
  201. benchmarker.set_param(param);
  202. benchmarker.set_display(false);
  203. const size_t RUNS = 5;
  204. benchmarker.set_times(RUNS);
  205. auto run = [&benchmarker](const TensorShape& diff,
  206. const TensorShape& grad) {
  207. auto used = benchmarker.execs({diff, grad});
  208. used /= RUNS;
  209. //! bandwith: each dst elem require 1 read and 4 write
  210. //! gflops: each dst elem require 4 add
  211. printf("run %s<-%s used: %f ms %f GBPS %f Gflops\n",
  212. diff.to_string().c_str(), grad.to_string().c_str(), used,
  213. diff.total_nr_elems() * (4.f + 1.f) * sizeof(float) /
  214. (1024 * 1024 * 1024) / used * 1e3,
  215. diff.total_nr_elems() * 4.f / (1024 * 1024 * 1024) / used * 1e3);
  216. };
  217. run({1, 100, 256, 256}, {1, 100, 256, 5120});
  218. run({1, 100, 256, 5120}, {1, 100, 256, 256});
  219. run({1, 100, 256, 256}, {1, 100, 512, 512});
  220. run({1, 100, 512, 512}, {1, 100, 256, 256});
  221. }
  222. #endif
  223. } // namespace resize
  224. } // namespace test
  225. } // namespace megdnn
  226. // vim: syntax=cpp.doxygen

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