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 5.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * \file dnn/test/fallback/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 "test/fallback/fixture.h"
  12. #include "megdnn/oprs.h"
  13. #include "test/common/checker.h"
  14. #include "test/common/tensor.h"
  15. #include "test/common/workspace_wrapper.h"
  16. using namespace megdnn;
  17. using namespace test;
  18. TEST_F(FALLBACK, REDUCE) {
  19. using Param = Reduce::Param;
  20. using Mode = Param::Mode;
  21. using DataType = Param::DataType;
  22. Checker<Reduce> checker(handle());
  23. struct Config {
  24. Param param;
  25. DType dtype;
  26. TensorShape shape;
  27. Config(Param param, DType dtype, TensorShape shape)
  28. : param(param), dtype(dtype), shape(shape) {}
  29. };
  30. std::vector<Config> configs;
  31. // general
  32. for (auto mode :
  33. {Mode::SUM, Mode::MEAN, Mode::SUM_SQR, Mode::PRODUCT, Mode::MIN, Mode::MAX})
  34. for (auto dtype : std::vector<DType>{
  35. dtype::Float16(), dtype::Float32(), dtype::Int32(), dtype::Int16(),
  36. dtype::Int8(), dtype::Uint8()})
  37. for (int32_t axis : {0, 1, 2, 3}) {
  38. TensorShape shape{2, 3, 20, 5};
  39. Param param(mode, axis);
  40. Config config(param, dtype, shape);
  41. configs.push_back(config);
  42. if (dtype.category() == DTypeCategory::FLOAT) {
  43. Param param(mode, axis, DataType::FLOAT_O16xC32);
  44. Config config(param, dtype, shape);
  45. configs.push_back(config);
  46. param.data_type = DataType::FLOAT_O32xC32;
  47. config = Config(param, dtype, shape);
  48. configs.push_back(config);
  49. } else if (dtype == dtype::Int32()) {
  50. Param param(mode, axis, DataType::FLOAT_O32xC32);
  51. Config config(param, dtype, shape);
  52. configs.push_back(config);
  53. }
  54. }
  55. // large (ABC) -> (A1C) case
  56. for (auto mode : {Mode::SUM_SQR})
  57. for (auto dtype : std::vector<DType>{dtype::Int32()})
  58. for (int32_t axis : {0, 1, 2, 3}) {
  59. TensorShape shape{2, 3, 10000, 5};
  60. Param param(mode, axis);
  61. Config config(param, dtype, shape);
  62. configs.push_back(config);
  63. }
  64. // large (AB) -> (A1) case
  65. for (auto mode : {Mode::SUM_SQR})
  66. for (auto dtype : std::vector<DType>{dtype::Int32()})
  67. for (int32_t axis : {0, 1, 2, 3}) {
  68. TensorShape shape{2, 3, 5, 10000};
  69. Param param(mode, axis);
  70. Config config(param, dtype, shape);
  71. configs.push_back(config);
  72. }
  73. {
  74. // large reduce_mean for O16C32
  75. TensorShape shape{1, 65536, 5};
  76. Param param(Mode::MEAN, 1, DataType::FLOAT_O16xC32);
  77. Config config(param, dtype::Float16(), shape);
  78. configs.push_back(config);
  79. }
  80. for (auto&& config : configs) {
  81. auto&& dtype = config.dtype;
  82. auto&& param = config.param;
  83. auto&& mode = config.param.mode;
  84. auto&& shape = config.shape;
  85. auto&& data_type = config.param.data_type;
  86. // when input/output both float16, the internal compute is float16, mode
  87. // is SUM or SUM_SQR, need set epsilon to 1e-2 to pass test
  88. if (dtype == dtype::Float16() && data_type == DataType::DEFAULT &&
  89. (mode == Mode::SUM || mode == Mode::SUM_SQR)) {
  90. checker.set_epsilon(1e-2);
  91. }
  92. checker.set_dtype(0, dtype).set_param(param).execs({shape, {}});
  93. }
  94. {
  95. static size_t N = 1 << 26;
  96. {
  97. // cpu vs naive
  98. Checker<Reduce> checker(handle());
  99. Reduce::Param param;
  100. param.axis = 0;
  101. UniformFloatRNG rng(1, 1);
  102. checker.set_param(param);
  103. checker.set_rng(0, &rng);
  104. checker.execs({{N}, {}});
  105. }
  106. {
  107. // naive vs groundtruth
  108. TensorLayout layoutN(TensorShape{N}, dtype::Float32()),
  109. layout1(TensorShape{1}, dtype::Float32());
  110. auto handle = this->handle();
  111. Tensor<float> src(handle, layoutN), dst(handle, layout1);
  112. float* ptr = src.ptr();
  113. for (size_t i = 0; i < N; ++i)
  114. ptr[i] = 1;
  115. auto opr = handle->create_operator<Reduce>();
  116. opr->param().axis = 0;
  117. auto wsize = opr->get_workspace_in_bytes(layoutN, layout1);
  118. WorkspaceWrapper workspace(handle, wsize);
  119. opr->exec(src.tensornd(), dst.tensornd(), workspace.workspace());
  120. megdnn_sync(handle);
  121. ASSERT_EQ(N, dst.ptr()[0]);
  122. }
  123. }
  124. }
  125. // vim: syntax=cpp.doxygen

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