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

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