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

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