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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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_REPLICATE) {
  98. Checker<Padding> checker(handle_cuda(), false);
  99. param::Padding param;
  100. param.padding_val = 10;
  101. param.padding_mode = param::Padding::PaddingMode::REPLICATE;
  102. param.front_offset_dim0 = 1;
  103. param.front_offset_dim1 = 0;
  104. param.front_offset_dim2 = 0;
  105. param.front_offset_dim3 = 0;
  106. param.front_offset_dim4 = 0;
  107. param.front_offset_dim5 = 0;
  108. param.front_offset_dim6 = 0;
  109. param.back_offset_dim0 = 2;
  110. param.back_offset_dim1 = 0;
  111. param.back_offset_dim2 = 0;
  112. param.back_offset_dim3 = 0;
  113. param.back_offset_dim4 = 0;
  114. param.back_offset_dim5 = 0;
  115. param.back_offset_dim6 = 0;
  116. checker.set_param(param).exect(
  117. Testcase{TensorValue({9}, dtype::Int8(), {1, 2, 3, 4, 5, 6, 7, 8, 9}), {}},
  118. Testcase{
  119. {},
  120. TensorValue(
  121. {12}, dtype::Int8(),
  122. {1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 9})});
  123. }
  124. TEST_F(CUDA, PADDING_REPLICATE2) {
  125. Checker<Padding> checker(handle_cuda(), false);
  126. param::Padding param;
  127. param.padding_val = 10;
  128. param.padding_mode = param::Padding::PaddingMode::REPLICATE;
  129. param.front_offset_dim0 = 2;
  130. param.front_offset_dim1 = 1;
  131. param.front_offset_dim2 = 0;
  132. param.front_offset_dim3 = 0;
  133. param.front_offset_dim4 = 0;
  134. param.front_offset_dim5 = 0;
  135. param.front_offset_dim6 = 0;
  136. param.back_offset_dim0 = 0;
  137. param.back_offset_dim1 = 3;
  138. param.back_offset_dim2 = 0;
  139. param.back_offset_dim3 = 0;
  140. param.back_offset_dim4 = 0;
  141. param.back_offset_dim5 = 0;
  142. param.back_offset_dim6 = 0;
  143. checker.set_param(param).exect(
  144. Testcase{
  145. TensorValue({3, 3}, dtype::Int8(), {1, 2, 3, 4, 5, 6, 7, 8, 9}),
  146. {}},
  147. Testcase{{}, TensorValue({5, 7}, dtype::Int8(), {1, 1, 2, 3, 3, 3, 3, 1, 1,
  148. 2, 3, 3, 3, 3, 1, 1, 2, 3,
  149. 3, 3, 3, 4, 4, 5, 6, 6, 6,
  150. 6, 7, 7, 8, 9, 9, 9, 9})});
  151. }
  152. // #if MEGDNN_WITH_BENCHMARK
  153. TEST_F(CUDA, BENCHMARK_PADDING_CONSTANT) {
  154. using Param = Padding::Param;
  155. auto run = [&](const TensorShapeArray& shapes, Param param) {
  156. CUBenchmarker<PaddingForward> benchmarker(handle_cuda());
  157. benchmarker.set_param(param);
  158. benchmarker.set_dtype(0, dtype::Int8()).set_dtype(1, dtype::Int8());
  159. for (auto&& shape : shapes) {
  160. double memaccess =
  161. double(TensorLayout(shape, dtype::Int8()).span().dist_byte()) *
  162. 2e-6;
  163. auto time_ms = benchmarker.execs({shape, {}});
  164. printf("execute %s, time %.4f ms, %.4f GB/s\n", shape.to_string().c_str(),
  165. time_ms, memaccess / time_ms);
  166. }
  167. };
  168. printf("mode -> constant; dtype -> int8\n");
  169. {
  170. TensorShapeArray shapes = {
  171. {16, 3, 736, 1280},
  172. };
  173. Param param;
  174. param.padding_mode = param::Padding::PaddingMode::CONSTANT;
  175. param.front_offset_dim1 = 1;
  176. run(shapes, param);
  177. }
  178. printf("mode -> replicate; dtype -> int8\n");
  179. {
  180. TensorShapeArray shapes = {
  181. {16, 3, 736, 1280},
  182. };
  183. Param param;
  184. param.padding_mode = param::Padding::PaddingMode::REPLICATE;
  185. param.front_offset_dim1 = 1;
  186. run(shapes, param);
  187. }
  188. printf("mode -> reflect; dtype -> int8\n");
  189. {
  190. TensorShapeArray shapes = {
  191. {16, 3, 736, 1280},
  192. };
  193. Param param;
  194. param.padding_mode = param::Padding::PaddingMode::REFLECT;
  195. param.front_offset_dim1 = 1;
  196. run(shapes, param);
  197. }
  198. }
  199. // #endif