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.

mask_conv.cpp 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "test/cpu/fixture.h"
  2. #include "megdnn/oprs.h"
  3. #include "test/common/benchmarker.h"
  4. #include "test/common/checker.h"
  5. #include "test/common/mask_conv.h"
  6. #include "test/common/rng.h"
  7. #include "test/common/utils.h"
  8. using namespace megdnn;
  9. using namespace test;
  10. TEST_F(CPU, MASK_CONV) {
  11. mask_conv_test(handle());
  12. }
  13. #if MEGDNN_WITH_BENCHMARK
  14. TEST_F(CPU, MASK_CONV_BENCHMARK) {
  15. mask_conv_benchmark(handle());
  16. }
  17. #endif
  18. TEST_F(CPU, MASK_PROPAGATE) {
  19. param::MaskPropagate mask_param;
  20. auto mask_check = [&](const TensorNDArray& tensors) {
  21. auto mask_src = tensors[0];
  22. auto mask_dst = tensors[1];
  23. auto src_ptr = static_cast<float*>(megdnn_malloc(
  24. handle(), mask_src.layout.total_nr_elems() * sizeof(float)));
  25. auto src = TensorND{
  26. src_ptr,
  27. TensorLayout{
  28. mask_src.layout.reshape(
  29. {1, 1, mask_src.layout[0], mask_src.layout[1]}),
  30. dtype::Float32()}};
  31. for (size_t i = 0; i < src.layout.total_nr_elems(); ++i) {
  32. src_ptr[i] = static_cast<float>(mask_src.ptr<int>()[i]);
  33. }
  34. auto filter_ptr = static_cast<float*>(megdnn_malloc(
  35. handle(), mask_param.kernel_h * mask_param.kernel_w * sizeof(float)));
  36. auto filter = TensorND{
  37. static_cast<void*>(filter_ptr),
  38. TensorLayout{
  39. {1, 1, mask_param.kernel_h, mask_param.kernel_w},
  40. dtype::Float32()}};
  41. for (size_t i = 0; i < mask_param.kernel_h * mask_param.kernel_w; ++i) {
  42. filter_ptr[i] = 1.0;
  43. }
  44. TensorLayout dst_layout{dtype::Float32()};
  45. param::Convolution conv_param{
  46. param::Convolution::Mode::CROSS_CORRELATION,
  47. mask_param.pad_h,
  48. mask_param.pad_w,
  49. mask_param.stride_h,
  50. mask_param.stride_w,
  51. mask_param.dilate_h,
  52. mask_param.dilate_w};
  53. auto opr = handle()->create_operator<Convolution>();
  54. opr->param() = conv_param;
  55. opr->deduce_layout(src.layout, filter.layout, dst_layout);
  56. auto dst_ptr = static_cast<float*>(megdnn_malloc(
  57. handle(), mask_dst.layout.total_nr_elems() * sizeof(float)));
  58. auto dst = TensorND{dst_ptr, dst_layout};
  59. WorkspaceWrapper workspace{
  60. handle(), opr->get_workspace_in_bytes(
  61. src.layout, filter.layout, dst.layout, nullptr)};
  62. opr->exec(src, filter, dst, nullptr, workspace.workspace());
  63. for (size_t i = 0; i < dst.layout.total_nr_elems(); ++i) {
  64. mask_dst.ptr<int>()[i] = dst_ptr[i] > 0;
  65. }
  66. delete dst_ptr;
  67. delete filter_ptr;
  68. delete src_ptr;
  69. };
  70. Checker<MaskPropagate> checker(handle());
  71. auto rng = std::make_unique<BernoulliRNG>(0.5);
  72. checker.set_extra_opr_impl(mask_check)
  73. .set_dtype(0, dtype::Int32())
  74. .set_rng(0, rng.get());
  75. auto run = [&](size_t IH, size_t IW, size_t FH, size_t FW, size_t SH = 1,
  76. size_t SW = 1, size_t PH = 0, size_t PW = 0, size_t DH = 1,
  77. size_t DW = 1) {
  78. mask_param.kernel_h = FH;
  79. mask_param.kernel_w = FW;
  80. mask_param.pad_h = PH;
  81. mask_param.pad_w = PW;
  82. mask_param.stride_h = SH;
  83. mask_param.stride_w = SW;
  84. mask_param.dilate_h = DH;
  85. mask_param.dilate_w = DW;
  86. checker.set_param(mask_param);
  87. TensorShape src_shape{IH, IW}, dst_shape{};
  88. checker.execs({src_shape, dst_shape});
  89. };
  90. run(5, 5, 3, 2);
  91. run(5, 5, 2, 3, 2, 2);
  92. run(5, 5, 3, 3, 2, 2, 1, 2);
  93. run(5, 5, 3, 3, 2, 1, 1, 2);
  94. run(5, 5, 3, 3, 1, 2, 2, 2);
  95. run(24, 23, 4, 4, 1, 1, 3, 2);
  96. run(24, 23, 4, 4, 1, 1, 3, 2, 2, 2);
  97. run(24, 23, 4, 4, 1, 1, 3, 2, 2, 3);
  98. run(24, 23, 4, 4, 1, 1, 3, 2, 3, 3);
  99. }
  100. // vim: syntax=cpp.doxygen