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

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