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.

padding.cpp 8.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #include "test/common/padding.h"
  2. #include "megdnn/oprs.h"
  3. #include "test/common/checker.h"
  4. #include "test/common/rng.h"
  5. #include "test/cuda/benchmark.h"
  6. #include "test/cuda/fixture.h"
  7. using namespace megdnn;
  8. using namespace test;
  9. TEST_F(CUDA, PADDING) {
  10. std::vector<padding::TestArg> args = padding::get_args();
  11. Checker<Padding> checker(handle_cuda());
  12. UniformIntNonZeroRNG rng(1, 9);
  13. for (auto&& arg : args) {
  14. checker.set_param(arg.param)
  15. .set_rng(0, &rng)
  16. .set_dtype(0, dtype::Int8())
  17. .set_dtype(1, dtype::Int8())
  18. .execs({arg.src, arg.dst});
  19. }
  20. }
  21. TEST_F(CUDA, PADDING_BACKWARD) {
  22. std::vector<padding::TestArg> args = padding::get_args_backward();
  23. Checker<PaddingBackward> checker(handle_cuda());
  24. UniformFloatRNG rng(1, 9);
  25. for (auto&& arg : args) {
  26. checker.set_param(arg.param)
  27. .set_rng(0, &rng)
  28. .set_dtype(0, dtype::Float32())
  29. .set_dtype(1, dtype::Float32())
  30. .execs({arg.src, arg.dst});
  31. }
  32. }
  33. TEST_F(CUDA, PADDING_REFLECT) {
  34. Checker<Padding> checker(handle_cuda(), false);
  35. param::Padding param;
  36. param.padding_val = 10;
  37. param.padding_mode = param::Padding::PaddingMode::REFLECT;
  38. param.front_offset_dim0 = 2;
  39. param.front_offset_dim1 = 0;
  40. param.front_offset_dim2 = 0;
  41. param.front_offset_dim3 = 0;
  42. param.front_offset_dim4 = 0;
  43. param.front_offset_dim5 = 0;
  44. param.front_offset_dim6 = 0;
  45. param.back_offset_dim0 = 3;
  46. param.back_offset_dim1 = 0;
  47. param.back_offset_dim2 = 0;
  48. param.back_offset_dim3 = 0;
  49. param.back_offset_dim4 = 0;
  50. param.back_offset_dim5 = 0;
  51. param.back_offset_dim6 = 0;
  52. checker.set_param(param).exect(
  53. Testcase{TensorValue({5}, dtype::Int8(), {1, 2, 3, 4, 5}), {}},
  54. Testcase{
  55. {},
  56. TensorValue({10}, dtype::Int8(), {3, 2, 1, 2, 3, 4, 5, 4, 3, 2})});
  57. }
  58. TEST_F(CUDA, PADDING_REFLECT2) {
  59. Checker<Padding> checker(handle_cuda(), false);
  60. param::Padding param;
  61. param.padding_val = 10;
  62. param.padding_mode = param::Padding::PaddingMode::REFLECT;
  63. param.front_offset_dim0 = 1;
  64. param.front_offset_dim1 = 2;
  65. param.front_offset_dim2 = 0;
  66. param.front_offset_dim3 = 0;
  67. param.front_offset_dim4 = 0;
  68. param.front_offset_dim5 = 0;
  69. param.front_offset_dim6 = 0;
  70. param.back_offset_dim0 = 1;
  71. param.back_offset_dim1 = 2;
  72. param.back_offset_dim2 = 0;
  73. param.back_offset_dim3 = 0;
  74. param.back_offset_dim4 = 0;
  75. param.back_offset_dim5 = 0;
  76. param.back_offset_dim6 = 0;
  77. checker.set_param(param).exect(
  78. Testcase{
  79. TensorValue({3, 3}, dtype::Int8(), {3, 5, 1, 3, 6, 1, 4, 7, 9}),
  80. {}},
  81. Testcase{{}, TensorValue({5, 7}, dtype::Int8(), {1, 6, 3, 6, 1, 6, 3, 1, 5,
  82. 3, 5, 1, 5, 3, 1, 6, 3, 6,
  83. 1, 6, 3, 9, 7, 4, 7, 9, 7,
  84. 4, 1, 6, 3, 6, 1, 6, 3})});
  85. }
  86. TEST_F(CUDA, PADDING_REFLECT2_QUANTIZED) {
  87. Checker<Padding> checker(handle_cuda(), false);
  88. param::Padding param;
  89. param.padding_mode = param::Padding::PaddingMode::REFLECT;
  90. param.front_offset_dim0 = 2;
  91. param.front_offset_dim1 = 1;
  92. param.front_offset_dim2 = 0;
  93. param.front_offset_dim3 = 0;
  94. param.front_offset_dim4 = 0;
  95. param.front_offset_dim5 = 0;
  96. param.front_offset_dim6 = 0;
  97. param.back_offset_dim0 = 0;
  98. param.back_offset_dim1 = 2;
  99. param.back_offset_dim2 = 0;
  100. param.back_offset_dim3 = 0;
  101. param.back_offset_dim4 = 0;
  102. param.back_offset_dim5 = 0;
  103. param.back_offset_dim6 = 0;
  104. checker.set_param(param).exect(
  105. Testcase{
  106. TensorValue(
  107. {3, 3}, dtype::QuantizedS8(), {1, 2, 3, 4, 5, 6, 7, 8, 9}),
  108. {}},
  109. Testcase{{}, TensorValue({5, 6}, dtype::QuantizedS8(), {8, 7, 8, 9, 8, 7, 5,
  110. 4, 5, 6, 5, 4, 2, 1,
  111. 2, 3, 2, 1, 5, 4, 5,
  112. 6, 5, 4, 8, 7, 8, 9,
  113. 8, 7})});
  114. }
  115. TEST_F(CUDA, PADDING_REPLICATE) {
  116. Checker<Padding> checker(handle_cuda(), false);
  117. param::Padding param;
  118. param.padding_val = 10;
  119. param.padding_mode = param::Padding::PaddingMode::REPLICATE;
  120. param.front_offset_dim0 = 1;
  121. param.front_offset_dim1 = 0;
  122. param.front_offset_dim2 = 0;
  123. param.front_offset_dim3 = 0;
  124. param.front_offset_dim4 = 0;
  125. param.front_offset_dim5 = 0;
  126. param.front_offset_dim6 = 0;
  127. param.back_offset_dim0 = 2;
  128. param.back_offset_dim1 = 0;
  129. param.back_offset_dim2 = 0;
  130. param.back_offset_dim3 = 0;
  131. param.back_offset_dim4 = 0;
  132. param.back_offset_dim5 = 0;
  133. param.back_offset_dim6 = 0;
  134. checker.set_param(param).exect(
  135. Testcase{TensorValue({9}, dtype::Int8(), {1, 2, 3, 4, 5, 6, 7, 8, 9}), {}},
  136. Testcase{
  137. {},
  138. TensorValue(
  139. {12}, dtype::Int8(),
  140. {1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 9})});
  141. }
  142. TEST_F(CUDA, PADDING_REPLICATE2) {
  143. Checker<Padding> checker(handle_cuda(), false);
  144. param::Padding param;
  145. param.padding_val = 10;
  146. param.padding_mode = param::Padding::PaddingMode::REPLICATE;
  147. param.front_offset_dim0 = 2;
  148. param.front_offset_dim1 = 1;
  149. param.front_offset_dim2 = 0;
  150. param.front_offset_dim3 = 0;
  151. param.front_offset_dim4 = 0;
  152. param.front_offset_dim5 = 0;
  153. param.front_offset_dim6 = 0;
  154. param.back_offset_dim0 = 0;
  155. param.back_offset_dim1 = 3;
  156. param.back_offset_dim2 = 0;
  157. param.back_offset_dim3 = 0;
  158. param.back_offset_dim4 = 0;
  159. param.back_offset_dim5 = 0;
  160. param.back_offset_dim6 = 0;
  161. checker.set_param(param).exect(
  162. Testcase{
  163. TensorValue({3, 3}, dtype::Int8(), {1, 2, 3, 4, 5, 6, 7, 8, 9}),
  164. {}},
  165. Testcase{{}, TensorValue({5, 7}, dtype::Int8(), {1, 1, 2, 3, 3, 3, 3, 1, 1,
  166. 2, 3, 3, 3, 3, 1, 1, 2, 3,
  167. 3, 3, 3, 4, 4, 5, 6, 6, 6,
  168. 6, 7, 7, 8, 9, 9, 9, 9})});
  169. }
  170. #if MEGDNN_WITH_BENCHMARK
  171. TEST_F(CUDA, BENCHMARK_PADDING_CONSTANT) {
  172. using Param = Padding::Param;
  173. auto run = [&](const TensorShapeArray& shapes, Param param) {
  174. CUBenchmarker<PaddingForward> benchmarker(handle_cuda());
  175. benchmarker.set_param(param);
  176. benchmarker.set_dtype(0, dtype::Int8()).set_dtype(1, dtype::Int8());
  177. for (auto&& shape : shapes) {
  178. double memaccess =
  179. double(TensorLayout(shape, dtype::Int8()).span().dist_byte()) *
  180. 2e-6;
  181. auto time_ms = benchmarker.execs({shape, {}});
  182. printf("execute %s, time %.4f ms, %.4f GB/s\n", shape.to_string().c_str(),
  183. time_ms, memaccess / time_ms);
  184. }
  185. };
  186. printf("mode -> constant; dtype -> int8\n");
  187. {
  188. TensorShapeArray shapes = {
  189. {16, 3, 736, 1280},
  190. };
  191. Param param;
  192. param.padding_mode = param::Padding::PaddingMode::CONSTANT;
  193. param.front_offset_dim1 = 1;
  194. run(shapes, param);
  195. }
  196. printf("mode -> replicate; dtype -> int8\n");
  197. {
  198. TensorShapeArray shapes = {
  199. {16, 3, 736, 1280},
  200. };
  201. Param param;
  202. param.padding_mode = param::Padding::PaddingMode::REPLICATE;
  203. param.front_offset_dim1 = 1;
  204. run(shapes, param);
  205. }
  206. printf("mode -> reflect; dtype -> int8\n");
  207. {
  208. TensorShapeArray shapes = {
  209. {16, 3, 736, 1280},
  210. };
  211. Param param;
  212. param.padding_mode = param::Padding::PaddingMode::REFLECT;
  213. param.front_offset_dim1 = 1;
  214. run(shapes, param);
  215. }
  216. }
  217. #endif