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.

pooling.cpp 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * \file dnn/test/cpu/pooling.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/cpu/fixture.h"
  12. #include "test/common/pooling.h"
  13. #include "test/common/checker.h"
  14. #include "test/common/benchmarker.h"
  15. namespace megdnn {
  16. namespace test {
  17. TEST_F(CPU, POOLING)
  18. {
  19. auto args = pooling::get_args();
  20. using Format = param::Pooling::Format;
  21. for (auto dtype: std::vector<DType>{dtype::Int8(), dtype::Float32()})
  22. for (Format format: {Format::NCHW, Format::NHWC})
  23. for (auto &&arg: args) {
  24. auto param = arg.param;
  25. auto src = arg.ishape;
  26. Checker<Pooling> checker(handle());
  27. param.format = format;
  28. if (param.format == Format::NHWC) {
  29. src = cvt_src_or_dst_nchw2nhwc(src);
  30. }
  31. checker.set_param(param)
  32. .set_dtype(0, dtype)
  33. .set_dtype(1, dtype)
  34. .exec(TensorShapeArray{
  35. src, {}});
  36. }
  37. }
  38. TEST_F(CPU, POOLING_INT)
  39. {
  40. UniformIntRNG rng(0, 255);
  41. for (int modeflag = 0; modeflag < 2; ++modeflag) {
  42. param::Pooling param;
  43. param.mode = modeflag ? param::Pooling::Mode::AVERAGE :
  44. param::Pooling::Mode::MAX;
  45. param.window_h = param.window_w = 2;
  46. param.stride_h = param.stride_w = 2;
  47. param.pad_h = param.pad_w = 0;
  48. std::vector<size_t> sizes = {10, 12, 13, 15, 20, 63};
  49. for (size_t ih: sizes)
  50. for (size_t iw: sizes)
  51. {
  52. Checker<Pooling> checker(handle());
  53. checker.set_rng(0, &rng);
  54. checker.set_rng(1, &rng);
  55. checker.set_rng(2, &rng);
  56. checker.set_dtype(0, dtype::Int8());
  57. checker.set_dtype(1, dtype::Int8());
  58. checker.set_param(param).exec(TensorShapeArray{
  59. {2, 3, ih, iw}, {}});
  60. }
  61. }
  62. }
  63. #if MEGDNN_WITH_BENCHMARK
  64. TEST_F(CPU, BENCHMARK_POOLING_INT)
  65. {
  66. UniformIntRNG rng(0, 255);
  67. for (int modeflag = 0; modeflag < 2; ++modeflag) {
  68. param::Pooling param;
  69. if (modeflag) {
  70. param.mode = param::Pooling::Mode::MAX;
  71. std::cout << "mode=max" << std::endl;
  72. } else {
  73. param.mode = param::Pooling::Mode::AVERAGE;
  74. std::cout << "mode=avg" << std::endl;
  75. }
  76. param.window_h = param.window_w = 2;
  77. param.stride_h = param.stride_w = 2;
  78. param.pad_h = param.pad_w = 0;
  79. float time_int, time_float;
  80. {
  81. std::cout << "int: ";
  82. Benchmarker<Pooling> benchmarker(handle());
  83. benchmarker.set_dtype(0, dtype::Int8());
  84. benchmarker.set_dtype(1, dtype::Int8());
  85. benchmarker.set_rng(0, &rng);
  86. benchmarker.set_rng(1, &rng);
  87. time_int = benchmarker.set_param(param).exec({{2, 3, 640, 480}, {}});
  88. }
  89. {
  90. std::cout << "float: ";
  91. Benchmarker<Pooling> benchmarker(handle());
  92. time_float = benchmarker.set_param(param).exec({{2, 3, 640, 480}, {}});
  93. }
  94. printf("time: int=%.3fms float=%.3fms\n", time_int, time_float);
  95. }
  96. }
  97. #endif
  98. } // namespace test
  99. } // namespace megdnn
  100. // vim: syntax=cpp.doxygen

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