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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 : {Mode::SUM, Mode::MEAN, Mode::SUM_SQR, Mode::PRODUCT,
  33. Mode::MIN, Mode::MAX})
  34. for (auto dtype : std::vector<DType>{dtype::Float16(), dtype::Float32(),
  35. 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. for (auto&& config : configs) {
  74. auto&& dtype = config.dtype;
  75. auto&& param = config.param;
  76. auto&& mode = config.param.mode;
  77. auto&& shape = config.shape;
  78. auto&& data_type = config.param.data_type;
  79. // when input/output both float16, the internal compute is float16, mode
  80. // is SUM or SUM_SQR, need set epsilon to 1e-2 to pass test
  81. if (dtype == dtype::Float16() && data_type == DataType::DEFAULT &&
  82. (mode == Mode::SUM || mode == Mode::SUM_SQR)) {
  83. checker.set_epsilon(1e-2);
  84. }
  85. checker.set_dtype(0, dtype).set_param(param).execs({shape, {}});
  86. }
  87. {
  88. static size_t N = 1 << 26;
  89. {
  90. // cpu vs naive
  91. Checker<Reduce> checker(handle());
  92. Reduce::Param param;
  93. param.axis = 0;
  94. UniformFloatRNG rng(1, 1);
  95. checker.set_param(param);
  96. checker.set_rng(0, &rng);
  97. checker.execs({{N}, {}});
  98. }
  99. {
  100. // naive vs groundtruth
  101. TensorLayout layoutN(TensorShape{N}, dtype::Float32()),
  102. layout1(TensorShape{1}, dtype::Float32());
  103. auto handle = this->handle();
  104. Tensor<float> src(handle, layoutN), dst(handle, layout1);
  105. float* ptr = src.ptr();
  106. for (size_t i = 0; i < N; ++i)
  107. ptr[i] = 1;
  108. auto opr = handle->create_operator<Reduce>();
  109. opr->param().axis = 0;
  110. auto wsize = opr->get_workspace_in_bytes(layoutN, layout1);
  111. WorkspaceWrapper workspace(handle, wsize);
  112. opr->exec(src.tensornd(), dst.tensornd(), workspace.workspace());
  113. megdnn_sync(handle);
  114. ASSERT_EQ(N, dst.ptr()[0]);
  115. }
  116. }
  117. }
  118. // vim: syntax=cpp.doxygen

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