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.

argmxx.cpp 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "test/rocm/fixture.h"
  2. #include "megdnn/oprs.h"
  3. #include "test/common/checker.h"
  4. #include "test/common/rng.h"
  5. namespace {
  6. using namespace megdnn;
  7. using namespace test;
  8. class ArgmxxRNG final : public RNG {
  9. public:
  10. void gen(const TensorND& tensor) override {
  11. auto offset = tensor.layout.span().low_elem;
  12. auto nr_elems = tensor.layout.span().dist_elem();
  13. #define cb(DType) \
  14. if (tensor.layout.dtype == DType()) { \
  15. using ctype = typename DTypeTrait<DType>::ctype; \
  16. auto ptr = tensor.ptr<ctype>(); \
  17. for (size_t i = 0; i < nr_elems; ++i) { \
  18. ptr[offset + i] = i; \
  19. } \
  20. COMPAT_RANDOM(ptr + offset, ptr + offset + nr_elems); \
  21. return; \
  22. }
  23. MEGDNN_FOREACH_COMPUTING_DTYPE_FLOAT(cb);
  24. #undef cb
  25. megdnn_throw(ssprintf("Unsupported DType: %s", tensor.layout.dtype.name()));
  26. }
  27. };
  28. template <typename Argmxx>
  29. void test_argmxx(Handle* handle) {
  30. Checker<Argmxx> checker(handle);
  31. checker.set_dtype(1, dtype::Int32());
  32. using Param = typename Argmxx::Param;
  33. ArgmxxRNG rng;
  34. checker.set_rng(0, &rng);
  35. for (size_t axis = 0; axis < 4; ++axis) {
  36. Param param;
  37. param.axis = axis;
  38. checker.set_param(param)
  39. .set_dtype(0, dtype::Float32())
  40. .execs({{2, 3, 4, 5}, {}});
  41. checker.set_param(param)
  42. .set_dtype(0, dtype::Float16())
  43. .execs({{2, 3, 4, 5}, {}});
  44. }
  45. checker.set_dtype(0, dtype::Float32());
  46. Param param;
  47. param.axis = 1;
  48. checker.set_param(param);
  49. // 1-step
  50. checker.execs({{2, 64, 32}, {}});
  51. // 2-step
  52. checker.execs({{2, 192, 32}, {}});
  53. // 3-step
  54. checker.execs({{2, 4333, 32}, {}});
  55. // single reduce
  56. checker.execs({{2, 1, 1}, {}});
  57. checker.execs({{2, 1 + 1, 1}, {}});
  58. checker.execs({{2, 2048 + 1, 1}, {}});
  59. checker.execs({{2, 2048 * 2048 + 1, 1}, {}});
  60. checker.execs({{2, 1 + 1, 31}, {}});
  61. checker.execs({{2, 16 + 1, 31}, {}});
  62. checker.execs({{2, 16 * 16 + 1, 31}, {}});
  63. checker.execs({{2, 16 * 16 * 16 + 1, 31}, {}});
  64. checker.execs({{2, 16 * 16 * 16 * 16 + 1, 31}, {}});
  65. checker.execs({{3, 256 * 256 + 1, 2}, {}});
  66. checker.execs({{3, 128 * 128 + 1, 3}, {}});
  67. checker.execs({{3, 64 * 64 + 1, 7}, {}});
  68. checker.execs({{3, 32 * 32 + 1, 15}, {}});
  69. checker.execs({{3, 512, 500}, {}});
  70. // very large reduce
  71. checker.execs({{1, 4194304, 1}, {}});
  72. }
  73. } // anonymous namespace
  74. namespace megdnn {
  75. namespace test {
  76. TEST_F(ROCM, ARGMAX) {
  77. test_argmxx<Argmax>(handle_rocm());
  78. }
  79. TEST_F(ROCM, ARGMIN) {
  80. test_argmxx<Argmin>(handle_rocm());
  81. }
  82. } // namespace test
  83. } // namespace megdnn
  84. // vim: syntax=cpp.doxygen