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.1 kB

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