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

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

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