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.

warp_perspective.cpp 7.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include "test/x86/fixture.h"
  2. #include "test/common/benchmarker.h"
  3. #include "test/common/checker.h"
  4. #include "test/common/random_state.h"
  5. #include "test/common/rng.h"
  6. #include "test/common/task_record_check.h"
  7. #include "test/common/warp_affine.h"
  8. #include "test/common/warp_perspective.h"
  9. namespace megdnn {
  10. namespace test {
  11. TEST_F(X86, WARP_PERSPECTIVE_MAT_IDX) {
  12. warp_perspective::run_mat_idx_test(handle());
  13. }
  14. TEST_F(X86, WARP_PERSPECTIVE_MAT_IDX_RECORD) {
  15. constexpr int N_SRC = 5;
  16. TaskRecordChecker<WarpPerspectiveForward, WarpPerspectiveMatIdxProxy> checker(0);
  17. WarpPerspectiveMatRNG mat_rng;
  18. checker.set_rng(1, &mat_rng);
  19. UniformIntRNG mat_idx_rng{0, N_SRC - 1};
  20. checker.set_dtype(2, dtype::Int32());
  21. checker.set_rng(2, &mat_idx_rng);
  22. WarpPerspective::Param param;
  23. param.bmode = WarpPerspective::Param::BorderMode::REFLECT;
  24. param.imode = param::WarpPerspective::InterpolationMode::LINEAR;
  25. checker.set_param(param);
  26. checker.execs({{N_SRC, 3, 10, 11}, {2, 3, 3}, {2}, {2, 3, 11, 12}});
  27. checker.execs({{N_SRC, 14, 17, 13}, {123, 3, 3}, {123}, {123, 14, 16, 15}});
  28. }
  29. TEST_F(X86_MULTI_THREADS, WARP_PERSPECTIVE_MAT_IDX) {
  30. warp_perspective::run_mat_idx_test(handle());
  31. }
  32. TEST_F(X86_MULTI_THREADS, WARP_AFFINE_CV) {
  33. using namespace warp_affine;
  34. std::vector<TestArg> args = get_cv_args();
  35. Checker<WarpAffine> checker(handle());
  36. for (auto&& arg : args) {
  37. checker.set_param(arg.param)
  38. .set_epsilon(1 + 1e-3)
  39. .set_dtype(0, dtype::Uint8())
  40. .set_dtype(1, dtype::Float32())
  41. .set_dtype(2, dtype::Uint8())
  42. .execs({arg.src, arg.trans, arg.dst});
  43. }
  44. for (auto&& arg : args) {
  45. checker.set_param(arg.param)
  46. .set_dtype(0, dtype::Float32())
  47. .set_dtype(1, dtype::Float32())
  48. .set_dtype(2, dtype::Float32())
  49. .execs({arg.src, arg.trans, arg.dst});
  50. }
  51. }
  52. TEST_F(X86_MULTI_THREADS, WARP_AFFINE_CV_RECORD) {
  53. using namespace warp_affine;
  54. std::vector<TestArg> args = get_cv_args();
  55. TaskRecordChecker<WarpAffine> checker(0);
  56. for (auto&& arg : args) {
  57. checker.set_param(arg.param)
  58. .set_epsilon(1 + 1e-3)
  59. .set_dtype(0, dtype::Uint8())
  60. .set_dtype(1, dtype::Float32())
  61. .set_dtype(2, dtype::Uint8())
  62. .execs({arg.src, arg.trans, arg.dst});
  63. }
  64. for (auto&& arg : args) {
  65. checker.set_param(arg.param)
  66. .set_dtype(0, dtype::Float32())
  67. .set_dtype(1, dtype::Float32())
  68. .set_dtype(2, dtype::Float32())
  69. .execs({arg.src, arg.trans, arg.dst});
  70. }
  71. }
  72. #if MEGDNN_WITH_BENCHMARK
  73. namespace {
  74. template <typename Opr>
  75. void benchmark_impl(
  76. const typename Opr::Param& param, std::vector<SmallVector<TensorShape>> shapes,
  77. size_t RUNS, TaskExecutorConfig&& multi_thread_config,
  78. TaskExecutorConfig&& single_thread_config) {
  79. std::vector<float> multi_thread_times, single_thread_times;
  80. {
  81. auto multi_thread_hanle = create_cpu_handle(0, true, &multi_thread_config);
  82. auto benchmarker = Benchmarker<Opr>(multi_thread_hanle.get());
  83. benchmarker.set_times(RUNS).set_display(false).set_param(param);
  84. for (auto shape : shapes) {
  85. multi_thread_times.push_back(benchmarker.exec(shape) / RUNS);
  86. }
  87. }
  88. {
  89. auto single_thread_handle = create_cpu_handle(0, true, &single_thread_config);
  90. auto benchmarker = Benchmarker<Opr>(single_thread_handle.get());
  91. benchmarker.set_times(RUNS).set_display(false).set_param(param);
  92. for (auto shape : shapes) {
  93. single_thread_times.push_back(benchmarker.exec(shape) / RUNS);
  94. }
  95. }
  96. printf("Benchmark : Multi threads %zu, ", multi_thread_config.nr_thread);
  97. printf("core_ids:");
  98. for (size_t i = 0; i < multi_thread_config.affinity_core_set.size(); i++) {
  99. printf("%zu ", multi_thread_config.affinity_core_set[i]);
  100. }
  101. printf(", Single thread core_id %zu\n", single_thread_config.affinity_core_set[0]);
  102. for (size_t i = 0; i < shapes.size(); i++) {
  103. auto shape = shapes[i];
  104. printf("Case: ");
  105. for (auto sh : shape)
  106. printf("%s ", sh.to_string().c_str());
  107. printf("%zu threads time: %f,\n single thread time: "
  108. "%f. spead up = %f, speedup/cores=%f\n",
  109. multi_thread_config.nr_thread, multi_thread_times[i],
  110. single_thread_times[i], single_thread_times[i] / multi_thread_times[i],
  111. single_thread_times[i] / multi_thread_times[i] /
  112. multi_thread_config.nr_thread);
  113. }
  114. }
  115. } // namespace
  116. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_WARP_PERSPECTIVE) {
  117. constexpr size_t RUNS = 50;
  118. using BMode = param::WarpPerspective::BorderMode;
  119. using IMode = param::WarpPerspective::InterpolationMode;
  120. WarpPerspective::Param param;
  121. param.border_val = 0.3f;
  122. param.format = param::WarpPerspective::Format::NHWC;
  123. param.imode = IMode::INTER_LINEAR;
  124. param.bmode = BMode::REPLICATE;
  125. std::vector<SmallVector<TensorShape>> shapes;
  126. auto bench_case = [&](size_t N, size_t H, size_t W, size_t C) {
  127. SmallVector<TensorShape> shape{{N, H, W, C}, {N, 3, 3}, {N, 224, 224, C}};
  128. shapes.push_back(shape);
  129. };
  130. bench_case(1, 700, 490, 1);
  131. bench_case(1, 700, 490, 2);
  132. bench_case(1, 700, 490, 3);
  133. bench_case(1, 500, 334, 1);
  134. bench_case(1, 500, 334, 2);
  135. bench_case(1, 500, 334, 3);
  136. bench_case(1, 140, 144, 1);
  137. bench_case(1, 140, 144, 2);
  138. bench_case(1, 140, 114, 3);
  139. printf("Benchmark warp perspective\n");
  140. benchmark_impl<WarpPerspective>(param, shapes, RUNS, {4, {4, 5, 6, 7}}, {1, {4}});
  141. benchmark_impl<WarpPerspective>(param, shapes, RUNS, {4, {4, 5, 6, 7}}, {1, {7}});
  142. benchmark_impl<WarpPerspective>(param, shapes, RUNS, {2, {4, 5}}, {1, {4}});
  143. }
  144. TEST_F(X86_BENCHMARK_MULTI_THREADS, BENCHMARK_WARP_AFFINE) {
  145. constexpr size_t RUNS = 50;
  146. using BMode = param::WarpAffine::BorderMode;
  147. using IMode = param::WarpAffine::InterpolationMode;
  148. WarpAffine::Param param;
  149. param.border_val = 0.3f;
  150. param.format = param::WarpAffine::Format::NHWC;
  151. param.imode = IMode::LINEAR;
  152. param.border_mode = BMode::BORDER_CONSTANT;
  153. std::vector<SmallVector<TensorShape>> shapes;
  154. auto bench_case = [&](size_t N, size_t H, size_t W, size_t C) {
  155. SmallVector<TensorShape> shape{{N, H, W, C}, {N, 2, 3}, {N, 224, 224, C}};
  156. shapes.push_back(shape);
  157. };
  158. bench_case(1, 700, 490, 1);
  159. bench_case(1, 700, 490, 2);
  160. bench_case(1, 700, 490, 3);
  161. bench_case(1, 500, 334, 1);
  162. bench_case(1, 500, 334, 2);
  163. bench_case(1, 500, 334, 3);
  164. bench_case(1, 140, 144, 1);
  165. bench_case(1, 140, 144, 2);
  166. bench_case(1, 140, 114, 3);
  167. printf("Benchmark warp perspective\n");
  168. benchmark_impl<WarpAffine>(param, shapes, RUNS, {4, {4, 5, 6, 7}}, {1, {4}});
  169. benchmark_impl<WarpAffine>(param, shapes, RUNS, {4, {4, 5, 6, 7}}, {1, {7}});
  170. benchmark_impl<WarpAffine>(param, shapes, RUNS, {2, {4, 5}}, {1, {4}});
  171. }
  172. #endif
  173. } // namespace test
  174. } // namespace megdnn
  175. // vim: syntax=cpp.doxygen