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.

accuracy_shake.cpp 9.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. * \file dnn/test/cuda/accuracy_shake.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
  10. * implied.
  11. */
  12. #include "megdnn/dtype.h"
  13. #include "megdnn/opr_param_defs.h"
  14. #include "megdnn/oprs.h"
  15. #include "test/common/accuracy_shake_checker.h"
  16. #include "test/common/rng.h"
  17. #include "test/cuda/fixture.h"
  18. #include "test/cuda/utils.h"
  19. namespace megdnn {
  20. namespace test {
  21. TEST_F(CUDA, SHAKE_CONV_BIAS_FORWARD) {
  22. require_compute_capability(6, 1);
  23. AccuracyShakeChecker<ConvBiasForward> checker(handle_cuda());
  24. NormalRNG default_rng;
  25. checker.set_dtype(0, dtype::Float32())
  26. .set_dtype(1, dtype::Float32())
  27. .set_dtype(2, dtype::Float32())
  28. .set_rng(0, &default_rng)
  29. .set_rng(1, &default_rng);
  30. // convolution
  31. checker.exec({{64, 16, 32, 32}, {64, 16, 3, 3}, {}, {}, {}});
  32. // convbias without z
  33. checker.exec({{64, 16, 32, 32}, {64, 16, 3, 3}, {1, 64, 1, 1}, {}, {}});
  34. // convbias with z
  35. checker.exec(
  36. {{64, 16, 32, 32}, {64, 16, 3, 3}, {1, 64, 1, 1}, {64, 64, 30, 30}, {}});
  37. ConvBias::Param param;
  38. // FIXME currently group conv cannot get the attribute of it's subopr, so we
  39. // just ignore group conv here.
  40. }
  41. TEST_F(CUDA, SHAKE_CONV_BIAS_FORWARD_QS8_NCHW) {
  42. require_compute_capability(6, 1);
  43. AccuracyShakeChecker<ConvBiasForward> checker(handle_cuda());
  44. UniformIntRNG int_rng{-128, 127};
  45. checker.set_dtype(0, dtype::QuantizedS8(2.5f))
  46. .set_dtype(1, dtype::QuantizedS8(2.5f))
  47. .set_dtype(2, dtype::QuantizedS32(6.25f))
  48. .set_dtype(3, dtype::QuantizedS8(0.25f))
  49. .set_dtype(4, dtype::QuantizedS8(0.25f))
  50. .set_rng(0, &int_rng)
  51. .set_rng(1, &int_rng)
  52. .set_rng(2, &int_rng)
  53. .set_rng(3, &int_rng);
  54. // convolution
  55. checker.exec({{64, 16, 32, 32}, {64, 16, 3, 3}, {}, {}, {}});
  56. // convbias without z
  57. checker.exec({{64, 16, 32, 32}, {64, 16, 3, 3}, {1, 64, 1, 1}, {}, {}});
  58. // convbias with z
  59. checker.exec(
  60. {{64, 16, 32, 32}, {64, 16, 3, 3}, {1, 64, 1, 1}, {64, 64, 30, 30}, {}});
  61. // group
  62. ConvBias::Param param;
  63. param.sparse = ConvBias::Param::Sparse::GROUP;
  64. checker.set_param(param);
  65. checker.exec({{64, 16, 32, 32}, {2, 32, 8, 3, 3}, {}, {}, {}});
  66. checker.exec({{64, 16, 32, 32}, {2, 32, 8, 3, 3}, {1, 64, 1, 1}, {}, {}});
  67. checker.exec(
  68. {{64, 16, 32, 32}, {2, 32, 8, 3, 3}, {1, 64, 1, 1}, {64, 64, 30, 30}, {}});
  69. }
  70. TEST_F(CUDA, SHAKE_CONV_BIAS_FORWARD_QS8_NHWC) {
  71. require_compute_capability(6, 1);
  72. UniformIntRNG int_rng{-50, 50};
  73. AccuracyShakeChecker<ConvBiasForward> checker(handle_cuda());
  74. ConvBias::Param param;
  75. param.format = ConvBias::Param::Format::NHWC;
  76. checker.set_dtype(0, dtype::QuantizedS8(2.5f))
  77. .set_dtype(1, dtype::QuantizedS8(2.5f))
  78. .set_dtype(2, dtype::QuantizedS32(6.25f))
  79. .set_dtype(4, dtype::QuantizedS8(60.25f))
  80. .set_rng(0, &int_rng)
  81. .set_rng(1, &int_rng)
  82. .set_rng(2, &int_rng)
  83. .set_param(param);
  84. checker.exec({{20, 32, 32, 4}, {24, 1, 1, 4}, {1, 1, 1, 24}, {}, {}});
  85. param.sparse = ConvBias::Param::Sparse::GROUP;
  86. checker.set_param(param).exec(
  87. {{20, 32, 32, 16}, {4, 4, 1, 1, 4}, {1, 1, 1, 16}, {}, {}});
  88. }
  89. TEST_F(CUDA, SHAKE_CONV_BIAS_FORWARD_QS8_NCHWX) {
  90. using Format = ConvBias::Param::Format;
  91. require_compute_capability(7, 5);
  92. AccuracyShakeChecker<ConvBiasForward> checker(handle_cuda());
  93. UniformIntRNG int_rng{-5, 5};
  94. UniformFloatRNG float_rng{-50, 50};
  95. checker.set_dtype(0, dtype::QuantizedS8(1.2f))
  96. .set_dtype(1, dtype::QuantizedS8(1.3f))
  97. .set_dtype(2, dtype::QuantizedS32(1.2 * 1.3f))
  98. .set_dtype(3, dtype::QuantizedS8(1.3f))
  99. .set_dtype(4, dtype::QuantizedS8(1.3f))
  100. .set_rng(0, &int_rng)
  101. .set_rng(1, &int_rng)
  102. .set_rng(2, &int_rng)
  103. .set_rng(3, &int_rng);
  104. auto run = [&](const TensorShapeArray& shapes, const Format& format) {
  105. ConvBias::Param param;
  106. param.format = format;
  107. checker.set_param(param).exec({shapes[0], shapes[1], shapes[2], {}, {}});
  108. };
  109. run({{20, 2, 24, 24, 4}, {24, 2, 3, 3, 4}, {1, 6, 1, 1, 4}}, Format::NCHW4);
  110. run({{20, 1, 24, 24, 32}, {64, 1, 3, 3, 32}, {1, 2, 1, 1, 32}}, Format::NCHW32);
  111. run({{16, 4, 23, 40, 4}, {32, 4, 3, 3, 4}, {1, 1, 1, 1, 32}}, Format::NCHW4_NCHW32);
  112. checker.set_dtype(0, dtype::QuantizedS8(1.9980618f))
  113. .set_dtype(1, dtype::QuantizedS8(1.9980927f))
  114. .set_dtype(2, dtype::Float32())
  115. .set_dtype(3, dtype::Float32())
  116. .set_dtype(4, dtype::Float32())
  117. .set_rng(0, &int_rng)
  118. .set_rng(1, &int_rng)
  119. .set_rng(2, &float_rng)
  120. .set_rng(3, &float_rng);
  121. run({{16, 4, 92, 160, 4}, {20, 4, 3, 3, 4}, {1, 20, 1, 1}}, Format::NCHW4_NCHW);
  122. }
  123. TEST_F(CUDA, SHAKE_MATRIX_MUL_FORWARD) {
  124. AccuracyShakeChecker<MatrixMul> checker(handle_cuda());
  125. checker.set_dtype(0, dtype::Float32())
  126. .set_dtype(1, dtype::Float32())
  127. .set_dtype(2, dtype::Float32())
  128. .exec({{50, 100}, {100, 60}, {}});
  129. }
  130. TEST_F(CUDA, SHAKE_BATCH_CONV_BIAS_QS8) {
  131. require_compute_capability(6, 1);
  132. AccuracyShakeChecker<BatchConvBiasForward> checker(handle_cuda());
  133. UniformIntRNG const_rng{1, 1};
  134. UniformIntRNG rng{-5, 5};
  135. UniformIntRNG bias_rng{-50, 50};
  136. checker.set_rng(0, &rng)
  137. .set_rng(1, &rng)
  138. .set_rng(2, &rng)
  139. .set_rng(3, &rng)
  140. .set_dtype(0, dtype::QuantizedS8{1.2f})
  141. .set_dtype(1, dtype::QuantizedS8{1.3f})
  142. .set_dtype(2, dtype::QuantizedS32{1.2f * 1.3f})
  143. .set_dtype(3, dtype::QuantizedS8{1.1f})
  144. .set_dtype(4, dtype::QuantizedS8{1.1f});
  145. param::BatchConvBias param;
  146. param.pad_h = 2, param.pad_w = 1;
  147. param.stride_h = 1, param.stride_w = 2;
  148. param.format = param::BatchConvBias::Format::NCHW4;
  149. checker.set_param(param).exec(
  150. {{32, 4, 24, 24, 4}, {32, 32, 4, 1, 1, 4}, {1, 8, 1, 1, 4}, {}, {}});
  151. }
  152. TEST_F(CUDA, SHAKE_BATCHED_MATRIX_MUL) {
  153. AccuracyShakeChecker<BatchedMatrixMul> checker(handle_cuda());
  154. UniformIntRNG int_rng{-127, 127};
  155. NormalRNG default_rng;
  156. checker.set_dtype(0, dtype::QuantizedS8(1.2f))
  157. .set_dtype(1, dtype::QuantizedS8(1.3f))
  158. .set_dtype(2, {})
  159. .set_rng(0, &int_rng)
  160. .set_rng(1, &int_rng);
  161. checker.exec({{20, 424, 368}, {20, 368, 256}, {20, 424, 256}});
  162. checker.set_dtype(0, dtype::Float32())
  163. .set_dtype(1, dtype::Float32())
  164. .set_dtype(2, dtype::Float32())
  165. .set_rng(0, &default_rng)
  166. .set_rng(1, &default_rng);
  167. checker.exec({{20, 424, 368}, {20, 368, 256}, {20, 424, 256}});
  168. }
  169. TEST_F(CUDA, SHAKE_CONVOLUTION3D_FORWARD) {
  170. AccuracyShakeChecker<Convolution3DForward> checker(handle_cuda());
  171. NormalRNG default_rng;
  172. float scale = 1.0f / sqrt(5);
  173. UniformFloatRNG rng(scale, 2 * scale);
  174. param::Convolution3D param;
  175. param.mode = param::Convolution3D::Mode::CROSS_CORRELATION;
  176. param.stride_d = param.stride_h = param.stride_w = 2;
  177. param.pad_d = param.pad_h = param.pad_w = 0;
  178. param.dilate_d = param.dilate_h = param.dilate_w = 1;
  179. checker.set_dtype(0, dtype::Float32())
  180. .set_dtype(1, dtype::Float32())
  181. .set_rng(0, &default_rng)
  182. .set_rng(1, &default_rng)
  183. .set_param(param)
  184. .exec({{20, 5, 12, 12, 16}, {5, 5, 3, 3, 3}, {}});
  185. }
  186. TEST_F(CUDA, SHAKE_LOCAL_SHARE) {
  187. AccuracyShakeChecker<LocalShare> checker(handle_cuda());
  188. using Param = LocalShare::Param;
  189. Param param;
  190. param.spatial_groups_h = param.spatial_groups_w = 3;
  191. checker.set_param(param);
  192. checker.exec({{20, 16, 32, 32}, {3, 3, 16, 3, 3, 64}, {}});
  193. }
  194. TEST_F(CUDA, SHAKE_CONVOLUTION_BACKWARD_DATA) {
  195. AccuracyShakeChecker<ConvolutionBackwardData> checker(handle_cuda());
  196. NormalRNG default_rng;
  197. checker.set_dtype(0, dtype::Float32())
  198. .set_dtype(1, dtype::Float32())
  199. .set_rng(0, &default_rng)
  200. .set_rng(1, &default_rng);
  201. checker.exec({{8, 16, 3, 3}, {64, 8, 5, 5}, {64, 16, 7, 7}});
  202. // FIXME currently group conv cannot get the attribute of it's subopr, so we
  203. // just ignore group conv here.
  204. }
  205. TEST_F(CUDA, SHAKE_CONVOLUTION_BACKWARD_FILTER) {
  206. AccuracyShakeChecker<ConvolutionBackwardFilter> checker(handle_cuda());
  207. NormalRNG default_rng;
  208. checker.set_dtype(0, dtype::Float32())
  209. .set_dtype(1, dtype::Float32())
  210. .set_rng(0, &default_rng)
  211. .set_rng(1, &default_rng);
  212. checker.exec({{2, 64, 7, 7}, {2, 32, 5, 5}, {32, 64, 3, 3}});
  213. // FIXME currently group conv cannot get the attribute of it's subopr, so we
  214. // just ignore group conv here.
  215. }
  216. } // namespace test
  217. } // namespace megdnn
  218. // vim: syntax=cpp.doxygen