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.

reduce.cpp 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * \file dnn/test/rocm/reduce.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 "hcc_detail/hcc_defs_prologue.h"
  12. #include "megdnn/oprs.h"
  13. #include "test/common/checker.h"
  14. #include "test/common/rng.h"
  15. #include "test/rocm/fixture.h"
  16. using namespace megdnn;
  17. using namespace test;
  18. TEST_F(ROCM, REDUCE) {
  19. using Mode = Reduce::Param::Mode;
  20. Checker<Reduce> checker(handle_rocm());
  21. UniformFloatRNG rng(-1.0f, 1.0f);
  22. checker.set_epsilon(1e-2);
  23. checker.set_rng(0, &rng);
  24. checker.set_param({Mode::SUM, 1});
  25. // 1-step
  26. checker.execs({{2, 64, 32}, {}});
  27. // 2-step
  28. checker.execs({{2, 192, 32}, {}});
  29. // 3-step
  30. checker.execs({{2, 4333, 32}, {}});
  31. // single reduce
  32. checker.execs({{2, 1, 1}, {}});
  33. checker.execs({{2, 1 + 1, 1}, {}});
  34. checker.execs({{2, 2048 + 1, 1}, {}});
  35. checker.execs({{2, 2048 * 2048 + 1, 1}, {}});
  36. checker.execs({{2, 1 + 1, 31}, {}});
  37. checker.execs({{2, 16 + 1, 31}, {}});
  38. checker.execs({{2, 16 * 16 + 1, 31}, {}});
  39. checker.execs({{2, 16 * 16 * 16 + 1, 31}, {}});
  40. checker.execs({{2, 16 * 16 * 16 * 16 + 1, 31}, {}});
  41. checker.execs({{2, 16 * 16 * 16 * 16 * 16 + 1, 31}, {}});
  42. checker.execs({{3, 256 * 256 + 1, 2}, {}});
  43. checker.execs({{3, 128 * 128 + 1, 3}, {}});
  44. checker.execs({{3, 64 * 64 + 1, 7}, {}});
  45. checker.execs({{3, 32 * 32 + 1, 15}, {}});
  46. checker.execs({{3, 512, 500}, {}});
  47. // very large reduce
  48. checker.execs({{1, 4194304, 1}, {}});
  49. auto check = [&](Reduce::Mode mode, DType src_dtype, DType dst_dtype,
  50. Reduce::DataType data_type) {
  51. for (int32_t axis : {0, 1, 2, 3}) {
  52. if (data_type == Reduce::DataType::DEFAULT &&
  53. DNN_FLOAT16_SELECT(src_dtype == dtype::Float16(), false)) {
  54. checker.set_epsilon(1e-2);
  55. } else {
  56. checker.set_epsilon(1e-3);
  57. }
  58. Reduce::Param param{mode, axis, data_type};
  59. auto dst_shape = TensorShape{2, 3, 100, 5};
  60. dst_shape[axis] = 1;
  61. checker.set_dtype(0, src_dtype)
  62. .set_dtype(1, dst_dtype)
  63. .set_param(param)
  64. .execs({{2, 3, 100, 5}, dst_shape});
  65. }
  66. };
  67. for (auto mode :
  68. {Mode::SUM, Mode::MEAN, Mode::SUM_SQR, Mode::PRODUCT, Mode::MIN, Mode::MAX}) {
  69. for (auto dtype : std::vector<DType>{
  70. DNN_INC_FLOAT16(dtype::Float16() MEGDNN_COMMA) dtype::Float32(),
  71. dtype::Int32()}) {
  72. check(mode, dtype, dtype, Reduce::DataType::DEFAULT);
  73. }
  74. #if !MEGDNN_DISABLE_FLOAT16
  75. check(mode, dtype::Float16(), dtype::Float32(),
  76. Reduce::DataType::FLOAT_O32xC32);
  77. check(mode, dtype::Float16(), dtype::Float16(),
  78. Reduce::DataType::FLOAT_O16xC32);
  79. check(mode, dtype::Float32(), dtype::Float16(),
  80. Reduce::DataType::FLOAT_O16xC32);
  81. ASSERT_THROW(
  82. check(mode, dtype::Int32(), dtype::Float16(),
  83. Reduce::DataType::FLOAT_O16xC32),
  84. MegDNNError);
  85. ASSERT_THROW(
  86. check(mode, dtype::Float16(), dtype::Float16(),
  87. Reduce::DataType::FLOAT_IO16xC32),
  88. MegDNNError);
  89. #endif
  90. }
  91. #if !MEGDNN_DISABLE_FLOAT16
  92. {
  93. // very large reduce for I16CO32
  94. Reduce::Param param{Mode::SUM_SQR, 1, Reduce::Param::DataType::FLOAT_O32xC32};
  95. checker.set_dtype(0, dtype::Float16())
  96. .set_dtype(1, dtype::Float32())
  97. .set_param(param)
  98. .execs({{1, 4194304, 1}, {1, 1, 1}});
  99. }
  100. {
  101. // large reduce_mean for O16C32
  102. Reduce::Param param{Mode::MEAN, 1, Reduce::Param::DataType::FLOAT_O16xC32};
  103. checker.set_dtype(0, dtype::Float16())
  104. .set_dtype(1, dtype::Float16())
  105. .set_param(param)
  106. .execs({{1, 65536, 5}, {1, 1, 5}});
  107. }
  108. #endif
  109. }
  110. // vim: syntax=cpp.doxygen