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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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-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/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. }
  50. }
  51. // large (ABC) -> (A1C) case
  52. for (auto mode : {Mode::SUM_SQR})
  53. for (auto dtype : std::vector<DType>{dtype::Int32()})
  54. for (int32_t axis : {0, 1, 2, 3}) {
  55. TensorShape shape{2, 3, 10000, 5};
  56. Param param(mode, axis);
  57. Config config(param, dtype, shape);
  58. configs.push_back(config);
  59. }
  60. // large (AB) -> (A1) case
  61. for (auto mode : {Mode::SUM_SQR})
  62. for (auto dtype : std::vector<DType>{dtype::Int32()})
  63. for (int32_t axis : {0, 1, 2, 3}) {
  64. TensorShape shape{2, 3, 5, 10000};
  65. Param param(mode, axis);
  66. Config config(param, dtype, shape);
  67. configs.push_back(config);
  68. }
  69. for (auto&& config : configs) {
  70. auto&& dtype = config.dtype;
  71. auto&& param = config.param;
  72. auto&& mode = config.param.mode;
  73. auto&& shape = config.shape;
  74. auto&& data_type = config.param.data_type;
  75. // when input/output both float16, the internal compute is float16, mode
  76. // is SUM or SUM_SQR, need set epsilon to 1e-2 to pass test
  77. if (dtype == dtype::Float16() && data_type == DataType::DEFAULT &&
  78. (mode == Mode::SUM || mode == Mode::SUM_SQR)) {
  79. checker.set_epsilon(1e-2);
  80. }
  81. checker.set_dtype(0, dtype).set_param(param).execs({shape, {}});
  82. }
  83. {
  84. static size_t N = 1 << 26;
  85. {
  86. // cpu vs naive
  87. Checker<Reduce> checker(handle());
  88. Reduce::Param param;
  89. param.axis = 0;
  90. UniformFloatRNG rng(1, 1);
  91. checker.set_param(param);
  92. checker.set_rng(0, &rng);
  93. checker.execs({{N}, {}});
  94. }
  95. {
  96. // naive vs groundtruth
  97. TensorLayout layoutN(TensorShape{N}, dtype::Float32()),
  98. layout1(TensorShape{1}, dtype::Float32());
  99. auto handle = this->handle();
  100. Tensor<float> src(handle, layoutN), dst(handle, layout1);
  101. float* ptr = src.ptr();
  102. for (size_t i = 0; i < N; ++i)
  103. ptr[i] = 1;
  104. auto opr = handle->create_operator<Reduce>();
  105. opr->param().axis = 0;
  106. auto wsize = opr->get_workspace_in_bytes(layoutN, layout1);
  107. WorkspaceWrapper workspace(handle, wsize);
  108. opr->exec(src.tensornd(), dst.tensornd(), workspace.workspace());
  109. megdnn_sync(handle);
  110. ASSERT_EQ(N, dst.ptr()[0]);
  111. }
  112. }
  113. }
  114. // vim: syntax=cpp.doxygen

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