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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * \file dnn/test/naive/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/naive/fixture.h"
  12. #include "megdnn/oprs/nn.h"
  13. #include "test/common/checker.h"
  14. #include "test/common/random_state.h"
  15. using namespace megdnn;
  16. using namespace test;
  17. TEST_F(NAIVE, POOLING_QUANTIZED) {
  18. using Mode = Pooling::Param::Mode;
  19. Checker<Pooling> checker(handle(), /* check_dispatch */ false);
  20. Pooling::Param param{Mode::MAX, 1, 1, 2, 2, 2, 2};
  21. auto dt = dtype::Quantized8Asymm(0.1f, (uint8_t)128);
  22. Testcase input{
  23. TensorValue({1, 1, 3, 3}, dt, {90, 136, 85, 48, 9, 226, 118, 109, 87}), {}};
  24. checker.set_param(param).exect(
  25. input, Testcase{{}, TensorValue({1, 1, 2, 2}, dt, {90, 136, 118, 226})});
  26. param = {Mode::AVERAGE, 1, 1, 2, 2, 2, 2};
  27. checker.set_param(param).exect(
  28. input, Testcase{{}, TensorValue({1, 1, 2, 2}, dt, {119, 119, 106, 108})});
  29. param = {Mode::AVERAGE_COUNT_EXCLUDE_PADDING, 1, 1, 2, 2, 2, 2};
  30. checker.set_param(param).exect(
  31. input, Testcase{{}, TensorValue({1, 1, 2, 2}, dt, {90, 111, 83, 108})});
  32. auto dt32 = dtype::QuantizedS32(0.233f);
  33. Testcase input32{
  34. TensorValue(
  35. {1, 1, 3, 3}, dt32,
  36. {12315, 10086, 10010, 12306, 23333, 19191, 9987, 12450, 12345}),
  37. {}};
  38. param = {Mode::MAX, 1, 1, 2, 2, 2, 2};
  39. checker.set_param(param).exect(
  40. input32,
  41. Testcase{
  42. {}, TensorValue({1, 1, 2, 2}, dt32, {12315, 10086, 12306, 23333})});
  43. }
  44. TEST_F(NAIVE, POOLING_QUANTIZED_Q4) {
  45. using Mode = Pooling::Param::Mode;
  46. Checker<Pooling> checker(handle(), /* check_dispatch */ false);
  47. {
  48. auto q4_dt = dtype::QuantizedS4(1.f);
  49. std::vector<int> i8_src_vec{1, 2, 3, 4, 5, 6, 7, -1, -2};
  50. std::vector<int> i8_max_dst_vec{1, 3, 7, 6};
  51. std::vector<int> i8_avg_dst_vec{0, 1, 3, 2};
  52. std::vector<int> i8_avg_exclu_dst_vec{1, 3, 6, 2};
  53. Pooling::Param param{Mode::MAX, 1, 1, 2, 2, 2, 2};
  54. Testcase input{TensorValueLowbit4({1, 1, 3, 3}, q4_dt, i8_src_vec), {}};
  55. checker.set_param(param).exect(
  56. input,
  57. Testcase{{}, TensorValueLowbit4({1, 1, 2, 2}, q4_dt, i8_max_dst_vec)});
  58. param = {Mode::AVERAGE, 1, 1, 2, 2, 2, 2};
  59. checker.set_param(param).exect(
  60. input,
  61. Testcase{{}, TensorValueLowbit4({1, 1, 2, 2}, q4_dt, i8_avg_dst_vec)});
  62. param = {Mode::AVERAGE_COUNT_EXCLUDE_PADDING, 1, 1, 2, 2, 2, 2};
  63. checker.set_param(param).exect(
  64. input,
  65. Testcase{
  66. {},
  67. TensorValueLowbit4({1, 1, 2, 2}, q4_dt, i8_avg_exclu_dst_vec)});
  68. }
  69. {
  70. auto u4_dt = dtype::Quantized4Asymm(0.1f, 3);
  71. std::vector<int> u8_src_vec{1, 2, 3, 4, 5, 6, 7, 8, 9};
  72. std::vector<int> u8_max_dst_vec{1, 3, 7, 9};
  73. std::vector<int> u8_avg_dst_vec{3, 3, 4, 7};
  74. std::vector<int> u8_avg_exclu_dst_vec{1, 3, 6, 7};
  75. Pooling::Param param{Mode::MAX, 1, 1, 2, 2, 2, 2};
  76. Testcase input{TensorValueLowbit4({1, 1, 3, 3}, u4_dt, u8_src_vec), {}};
  77. checker.set_param(param).exect(
  78. input,
  79. Testcase{{}, TensorValueLowbit4({1, 1, 2, 2}, u4_dt, u8_max_dst_vec)});
  80. param = {Mode::AVERAGE, 1, 1, 2, 2, 2, 2};
  81. checker.set_param(param).exect(
  82. input,
  83. Testcase{{}, TensorValueLowbit4({1, 1, 2, 2}, u4_dt, u8_avg_dst_vec)});
  84. param = {Mode::AVERAGE_COUNT_EXCLUDE_PADDING, 1, 1, 2, 2, 2, 2};
  85. checker.set_param(param).exect(
  86. input,
  87. Testcase{
  88. {},
  89. TensorValueLowbit4({1, 1, 2, 2}, u4_dt, u8_avg_exclu_dst_vec)});
  90. }
  91. }
  92. TEST_F(NAIVE, POOLING_INT_AVERAGE) {
  93. using Mode = Pooling::Param::Mode;
  94. Checker<Pooling> checker(handle(), /* check_dispatch */ false);
  95. auto dt = dtype::Int8();
  96. Pooling::Param param = {Mode::AVERAGE, 0, 0, 1, 1, 2, 2};
  97. Testcase input_positive{
  98. TensorValue(
  99. {1, 1, 3, 3}, dt, {127, 127, 127, 127, 127, 127, 127, 127, 127}),
  100. {}};
  101. Testcase input_negative{
  102. TensorValue(
  103. {1, 1, 3, 3}, dt,
  104. {-127, -127, -127, -127, -127, -127, -127, -127, -127}),
  105. {}};
  106. checker.set_param(param).exect(
  107. input_positive,
  108. Testcase{{}, TensorValue({1, 1, 2, 2}, dt, {127, 127, 127, 127})});
  109. checker.set_param(param).exect(
  110. input_negative,
  111. Testcase{{}, TensorValue({1, 1, 2, 2}, dt, {-127, -127, -127, -127})});
  112. param = {Mode::AVERAGE_COUNT_EXCLUDE_PADDING, 0, 0, 1, 1, 2, 2};
  113. checker.set_param(param).exect(
  114. input_positive,
  115. Testcase{{}, TensorValue({1, 1, 2, 2}, dt, {127, 127, 127, 127})});
  116. checker.set_param(param).exect(
  117. input_negative,
  118. Testcase{{}, TensorValue({1, 1, 2, 2}, dt, {-127, -127, -127, -127})});
  119. }
  120. // vim: syntax=cpp.doxygen

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