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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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-2020 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(megdnn_mangle(ssprintf("Unsupported DType: %s",
  36. tensor.layout.dtype.name())));
  37. }
  38. };
  39. template <typename Argmxx>
  40. void test_argmxx(Handle *handle)
  41. {
  42. Checker<Argmxx> checker(handle);
  43. checker.set_dtype(1, dtype::Int32());
  44. using Param = typename Argmxx::Param;
  45. ArgmxxRNG rng;
  46. checker.set_rng(0, &rng);
  47. for (size_t axis = 0; axis < 4; ++axis) {
  48. Param param;
  49. param.axis = axis;
  50. checker.set_param(param).set_dtype(0, dtype::Float32()).
  51. execs({{2, 3, 4, 5}, {}});
  52. checker.set_param(param).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. {
  88. test_argmxx<Argmax>(handle_rocm());
  89. }
  90. TEST_F(ROCM, ARGMIN)
  91. {
  92. test_argmxx<Argmin>(handle_rocm());
  93. }
  94. } // namespace test
  95. } // namespace megdnn
  96. // vim: syntax=cpp.doxygen

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台