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_affine.cpp 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * \file dnn/test/cuda/warp_affine.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/warp_affine.h"
  12. #include "include/megdnn/thin/small_vector.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. // FIXME test WARP_PERSPECTIVE_CV failed here
  19. #if 0
  20. TEST_F(CUDA, WARP_AFFINE_CV)
  21. {
  22. using namespace warp_affine;
  23. std::vector<TestArg> args = get_cv_args();
  24. Checker<WarpAffine> checker(handle_cuda());
  25. for (auto &&arg: args) {
  26. if (arg.src[3] == 2) continue;
  27. checker.set_param(arg.param)
  28. .set_epsilon(1 + 1e-3)
  29. .set_dtype(0, dtype::Uint8())
  30. .set_dtype(1, dtype::Float32())
  31. .set_dtype(2, dtype::Uint8())
  32. .execs({arg.src, arg.trans, arg.dst});
  33. }
  34. for (auto &&arg: args) {
  35. if (arg.src[3] == 2) continue;
  36. checker.set_param(arg.param)
  37. .set_dtype(0, dtype::Float32())
  38. .set_dtype(1, dtype::Float32())
  39. .set_dtype(2, dtype::Float32())
  40. .execs({arg.src, arg.trans, arg.dst});
  41. }
  42. }
  43. #endif
  44. TEST_F(CUDA, WARP_AFFINE) {
  45. //! NCHW
  46. for (auto dtype : std::vector<DType>{dtype::Float32()}) {
  47. for (auto bmode :
  48. {WarpAffine::BorderMode::WRAP, WarpAffine::BorderMode::REFLECT,
  49. WarpAffine::BorderMode::CONSTANT, WarpAffine::BorderMode::REPLICATE,
  50. WarpAffine::BorderMode::CONSTANT}) {
  51. Checker<WarpAffine> checker(handle_cuda());
  52. NormalRNG rng;
  53. checker.set_rng(1, &rng);
  54. WarpAffine::Param param;
  55. param.border_val = 0.3f;
  56. param.border_mode = bmode;
  57. param.imode = param::WarpAffine::InterpolationMode::LINEAR;
  58. param.format = param::WarpAffine::Format::NCHW;
  59. checker.set_param(param);
  60. checker.set_dtype(0, dtype);
  61. checker.set_dtype(1, dtype);
  62. checker.set_dtype(2, dtype);
  63. checker.execs({{2, 3, 10, 11}, {2, 2, 3}, {2, 3, 11, 12}});
  64. checker.execs({{22, 3, 10, 11}, {22, 2, 3}, {22, 3, 11, 12}});
  65. }
  66. }
  67. //! NHWC
  68. for (auto dtype : std::vector<DType>{dtype::Float32()}) {
  69. for (auto bmode :
  70. {WarpAffine::BorderMode::WRAP, WarpAffine::BorderMode::REFLECT,
  71. WarpAffine::BorderMode::CONSTANT, WarpAffine::BorderMode::REPLICATE,
  72. WarpAffine::BorderMode::CONSTANT}) {
  73. Checker<WarpAffine> checker(handle_cuda());
  74. NormalRNG rng;
  75. checker.set_rng(1, &rng);
  76. WarpAffine::Param param;
  77. param.format = param::WarpAffine::Format::NHWC;
  78. param.border_val = 0.3f;
  79. param.border_mode = bmode;
  80. param.imode = param::WarpAffine::InterpolationMode::LINEAR;
  81. checker.set_param(param);
  82. checker.set_dtype(0, dtype);
  83. checker.set_dtype(1, dtype);
  84. checker.set_dtype(2, dtype);
  85. checker.execs({{2, 3, 10, 11}, {2, 2, 3}, {2, 12, 11, 11}});
  86. checker.execs({{22, 3, 10, 12}, {22, 2, 3}, {22, 3, 11, 12}});
  87. }
  88. }
  89. }
  90. #if MEGDNN_WITH_BENCHMARK
  91. TEST_F(CUDA, WARP_AFFINE_BENCHMARK) {
  92. const size_t RUNS = 50;
  93. Benchmarker<WarpAffine> benchmark(handle_cuda());
  94. benchmark.set_display(false);
  95. benchmark.set_times(RUNS);
  96. using Param = param::WarpAffine;
  97. Param param;
  98. auto run = [&benchmark, &param](TensorShape src, TensorShape mat, TensorShape dst) {
  99. benchmark.set_param(param);
  100. auto used = benchmark.execs({src, mat, dst});
  101. printf("src: %s dst: %s used: %.2f ms %.2f Gflops\n", src.to_string().c_str(),
  102. dst.to_string().c_str(), used,
  103. //! 8 mul + 3 add
  104. 11 * dst.total_nr_elems() / (used / RUNS) / 1e6);
  105. };
  106. run({1, 100, 100, 1}, {1, 2, 3}, {1, 112, 112, 1});
  107. run({512, 100, 100, 1}, {512, 2, 3}, {512, 112, 112, 1});
  108. }
  109. #endif
  110. } // namespace test
  111. } // namespace megdnn
  112. // vim: syntax=cpp.doxygen