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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * \file dnn/test/cuda/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/cuda/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. }
  32. MEGDNN_FOREACH_COMPUTING_DTYPE(cb);
  33. #undef cb
  34. }
  35. };
  36. template <typename Argmxx>
  37. void test_argmxx(Handle *handle)
  38. {
  39. Checker<Argmxx> checker(handle);
  40. checker.set_dtype(1, dtype::Int32());
  41. using Param = typename Argmxx::Param;
  42. ArgmxxRNG rng;
  43. checker.set_rng(0, &rng);
  44. for (size_t axis = 0; axis < 4; ++axis) {
  45. Param param;
  46. param.axis = axis;
  47. checker.set_param(param).set_dtype(0, dtype::Float32()).
  48. execs({{2, 3, 4, 5}, {}});
  49. checker.set_param(param).set_dtype(0, dtype::Float16()).
  50. execs({{2, 3, 4, 5}, {}});
  51. checker.set_param(param).set_dtype(0, dtype::Int32()).
  52. execs({{2, 3, 4, 5}, {}});
  53. checker.set_param(param).set_dtype(0, dtype::Int16()).
  54. execs({{2, 3, 4, 5}, {}});
  55. checker.set_param(param).set_dtype(0, dtype::Int8()).
  56. execs({{2, 3, 4, 5}, {}});
  57. checker.set_param(param).set_dtype(0, dtype::Uint8()).
  58. execs({{2, 3, 4, 5}, {}});
  59. }
  60. checker.set_dtype(0, dtype::Float32());
  61. Param param;
  62. param.axis = 1;
  63. checker.set_param(param);
  64. // 1-step
  65. checker.execs({{2, 64, 32}, {}});
  66. // 2-step
  67. checker.execs({{2, 192, 32}, {}});
  68. // 3-step
  69. checker.execs({{2, 4333, 32}, {}});
  70. // single reduce
  71. checker.execs({{2, 1, 1}, {}});
  72. checker.execs({{2, 1+1, 1}, {}});
  73. checker.execs({{2, 2048+1, 1}, {}});
  74. checker.execs({{2, 2048*2048+1, 1}, {}});
  75. checker.execs({{2, 1+1, 31}, {}});
  76. checker.execs({{2, 16+1, 31}, {}});
  77. checker.execs({{2, 16*16+1, 31}, {}});
  78. checker.execs({{2, 16*16*16+1, 31}, {}});
  79. checker.execs({{2, 16*16*16*16+1, 31}, {}});
  80. checker.execs({{3, 256*256+1, 2}, {}});
  81. checker.execs({{3, 128*128+1, 3}, {}});
  82. checker.execs({{3, 64*64+1, 7}, {}});
  83. checker.execs({{3, 32*32+1, 15}, {}});
  84. checker.execs({{3, 512, 500}, {}});
  85. // very large reduce
  86. checker.execs({{1, 4194304, 1}, {}});
  87. }
  88. } // anonymous namespace
  89. namespace megdnn {
  90. namespace test {
  91. TEST_F(CUDA, ARGMAX)
  92. {
  93. test_argmxx<Argmax>(handle_cuda());
  94. }
  95. TEST_F(CUDA, ARGMIN)
  96. {
  97. test_argmxx<Argmin>(handle_cuda());
  98. }
  99. } // namespace test
  100. } // namespace megdnn
  101. // vim: syntax=cpp.doxygen

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