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

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

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