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.

conv_bias.cpp 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #include "test/aarch64/fixture.h"
  2. #include "src/fallback/conv_bias/common.h"
  3. #include "test/common/benchmarker.h"
  4. #include "test/common/checker.h"
  5. #include "test/common/conv_bias.h"
  6. #include "test/common/rng.h"
  7. #include "test/common/task_record_check.h"
  8. #include "test/common/tensor.h"
  9. namespace megdnn {
  10. namespace test {
  11. std::vector<conv_bias::TestArg> get_conv_bias_args(
  12. std::vector<size_t> kernel, size_t stride) {
  13. using namespace conv_bias;
  14. using Param = param::ConvBias;
  15. using NLMode = param::ConvBias::NonlineMode;
  16. std::vector<TestArg> args;
  17. auto pack = [&](size_t n, size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  18. size_t stride, NLMode nonline_mode) {
  19. Param param;
  20. param.stride_h = stride;
  21. param.stride_w = stride;
  22. param.pad_h = kernel == 1 ? 0 : kernel / 2;
  23. param.pad_w = kernel == 1 ? 0 : kernel / 2;
  24. param.nonlineMode = nonline_mode;
  25. //! no bias
  26. args.emplace_back(
  27. param, TensorShape{n, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  28. TensorShape{});
  29. //! bias broadcast channle
  30. args.emplace_back(
  31. param, TensorShape{n, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  32. TensorShape{1, oc, 1, 1});
  33. //! bias
  34. args.emplace_back(
  35. param, TensorShape{n, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  36. TensorShape{
  37. n, oc, (h + 2 * param.pad_h - kernel) / stride + 1,
  38. (w + 2 * param.pad_h - kernel) / stride + 1});
  39. };
  40. for (auto nlmode : {NLMode::IDENTITY, NLMode::RELU, NLMode::SIGMOID}) {
  41. for (size_t n : {1, 2}) {
  42. for (size_t ic : {1, 2, 3, 4, 8}) {
  43. for (size_t oc : {1, 2, 3, 4, 8}) {
  44. for (size_t size : {1, 2, 3, 4, 8, 24}) {
  45. for (size_t k : kernel) {
  46. pack(n, oc, ic, size + 24, size + 24, k, stride, nlmode);
  47. }
  48. }
  49. }
  50. }
  51. }
  52. }
  53. return args;
  54. }
  55. void checker_conv_bias(
  56. std::vector<conv_bias::TestArg> args, Handle* handle, const char* algo_name) {
  57. using namespace conv_bias;
  58. Checker<ConvBias> checker(handle);
  59. checker.set_before_exec_callback(
  60. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name));
  61. for (auto&& arg : args) {
  62. checker.set_param(arg.param).execs({arg.src, arg.filter, arg.bias, {}, {}});
  63. }
  64. }
  65. TEST_F(AARCH64_MULTI_THREADS, CONVBIAS_DIRECT_FP32_STR2) {
  66. check_conv_bias(
  67. conv_bias::get_conv_bias_args({2, 3, 5, 7}, 2, false, false, false),
  68. handle(), "ARMV8F32STRD2");
  69. }
  70. TEST_F(AARCH64_MULTI_THREADS, CONVBIAS_RECORD) {
  71. auto args = conv_bias::get_conv_bias_args({2, 3, 5, 7}, 2, false, false, false);
  72. TaskRecordChecker<ConvBias> checker(0);
  73. for (auto&& arg : args) {
  74. checker.set_param(arg.param).execs({arg.src, arg.filter, arg.bias, {}, {}});
  75. }
  76. }
  77. #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
  78. void checker_conv_bias_fp16(
  79. std::vector<conv_bias::TestArg> args, Handle* handle, const char* algo_name,
  80. float epsilon) {
  81. using namespace conv_bias;
  82. Checker<ConvBias> checker(handle);
  83. checker.set_before_exec_callback(
  84. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name));
  85. checker.set_epsilon(epsilon);
  86. checker.set_dtype(0, dtype::Float16())
  87. .set_dtype(1, dtype::Float16())
  88. .set_dtype(2, dtype::Float16())
  89. .set_dtype(4, dtype::Float16());
  90. NormalRNG rng(1.f);
  91. checker.set_rng(0, &rng).set_rng(1, &rng);
  92. for (auto&& arg : args) {
  93. checker.set_param(arg.param).execs({arg.src, arg.filter, arg.bias, {}, {}});
  94. }
  95. }
  96. TEST_F(AARCH64_MULTI_THREADS, CONVBIAS_DIRECT_FP16_STR2) {
  97. NormalRNG rng(1);
  98. checker_conv_bias_f16(
  99. conv_bias::get_conv_bias_args({2, 3, 5}, 2, false, false, false), handle(),
  100. rng, "ARMV8F16STRD2", 0.04);
  101. }
  102. #endif
  103. #if MEGDNN_WITH_BENCHMARK
  104. std::vector<conv_bias::TestArg> get_conv_bias_benchmaker_args(
  105. std::vector<size_t> kernel, size_t stride) {
  106. using namespace conv_bias;
  107. using Param = param::ConvBias;
  108. using NLMode = param::ConvBias::NonlineMode;
  109. std::vector<TestArg> args;
  110. auto pack = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  111. size_t stride, NLMode nonline_mode) {
  112. Param param;
  113. param.stride_h = stride;
  114. param.stride_w = stride;
  115. param.pad_h = kernel == 1 ? 0 : kernel / 2;
  116. param.pad_w = kernel == 1 ? 0 : kernel / 2;
  117. param.nonlineMode = nonline_mode;
  118. //! no bias
  119. args.emplace_back(
  120. param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  121. TensorShape{});
  122. //! bias broadcast channle
  123. args.emplace_back(
  124. param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  125. TensorShape{1, oc, 1, 1});
  126. //! bias
  127. args.emplace_back(
  128. param, TensorShape{1, ic, h, w}, TensorShape{oc, ic, kernel, kernel},
  129. TensorShape{
  130. 1, oc, (h + 2 * param.pad_h - kernel) / stride + 1,
  131. (w + 2 * param.pad_w - kernel) / stride + 1});
  132. };
  133. for (auto nlmode : {NLMode::IDENTITY, NLMode::RELU, NLMode::SIGMOID}) {
  134. for (size_t k : kernel) {
  135. for (size_t ic : {3, 6, 12, 24}) {
  136. for (size_t oc : {3, 6, 12, 24}) {
  137. for (size_t size : {4, 7, 8, 14, 16, 17, 28, 32, 34, 64, 112}) {
  138. pack(oc, ic, size, size, k, stride, nlmode);
  139. }
  140. }
  141. }
  142. }
  143. }
  144. return args;
  145. }
  146. void benchmarker_conv_bias(
  147. std::vector<conv_bias::TestArg> args, Handle* handle, const char* algo_name,
  148. const char* cmp_algo_name) {
  149. using namespace conv_bias;
  150. constexpr size_t N = 10;
  151. Benchmarker<ConvBias> benchmark_float(handle);
  152. benchmark_float
  153. .set_before_exec_callback(
  154. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name))
  155. .set_times(N)
  156. .set_display(false);
  157. #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
  158. Benchmarker<ConvBias> benchmark_float16(handle);
  159. benchmark_float16
  160. .set_before_exec_callback(
  161. conv_bias::ConvBiasAlgoChecker<ConvBias>(cmp_algo_name))
  162. .set_times(N)
  163. .set_dtype(0, dtype::Float16())
  164. .set_dtype(1, dtype::Float16())
  165. .set_dtype(2, dtype::Float16())
  166. .set_dtype(4, dtype::Float16())
  167. .set_display(false);
  168. #endif
  169. for (auto&& arg : args) {
  170. TensorLayout dst_layout;
  171. auto opr = handle->create_operator<ConvBias>();
  172. opr->param() = arg.param;
  173. opr->deduce_layout(
  174. {arg.src, dtype::Float32()}, {arg.filter, dtype::Float32()},
  175. {arg.bias, dtype::Float32()}, {}, dst_layout);
  176. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  177. arg.filter[2] * arg.filter[3] * 2.0 /
  178. (1024 * 1024 * 1024) * 1e3; // GFLOPS
  179. printf("filter n: %zu c: %zu h:%zu w:%zu ", arg.filter[0], arg.filter[1],
  180. arg.filter[2], arg.filter[3]);
  181. printf("input c: %zu h:%zu w:%zu \n", arg.src[1], arg.src[2], arg.src[3]);
  182. auto time32 = benchmark_float.set_param(arg.param).execs(
  183. {arg.src, arg.filter, arg.bias, {}, {}}) /
  184. N;
  185. #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
  186. auto time16 = benchmark_float16.set_param(arg.param).execs(
  187. {arg.src, arg.filter, arg.bias, {}, {}}) /
  188. N;
  189. printf("---------------------------------fp32 flops: %.3f Gflops fp16 "
  190. "flops %.3f Gflops speedup: %f\n",
  191. computations / time32, computations / time16, time32 / time16);
  192. #else
  193. printf("---------------------------------fp32 flops: %.3f Gflops\n",
  194. computations / time32);
  195. #endif
  196. }
  197. }
  198. TEST_F(AARCH64, BENCHMARK_CONVBIAS_STRIDE2_FP32_FP16) {
  199. benchmarker_conv_bias(
  200. get_conv_bias_benchmaker_args({2, 3, 5, 7}, 2), handle(), "ARMV8F32STRD2",
  201. "ARMV8F16STRD2");
  202. }
  203. TEST_F(AARCH64, BENCHMARK_CONVBIAS) {
  204. constexpr size_t RUNS = 10;
  205. param::ConvBias param;
  206. param.stride_h = 1;
  207. param.stride_w = 1;
  208. Benchmarker<ConvBias> benchmarker_int(handle());
  209. benchmarker_int.set_times(RUNS)
  210. .set_dtype(0, dtype::QuantizedS8(2.5f))
  211. .set_dtype(1, dtype::QuantizedS8(2.5f))
  212. .set_dtype(2, dtype::QuantizedS32(6.25f))
  213. .set_dtype(4, dtype::QuantizedS8(40.25f))
  214. .set_display(false);
  215. Benchmarker<ConvBias> benchmarker_float(handle());
  216. benchmarker_float.set_display(false).set_times(RUNS);
  217. auto run = [&](size_t N, size_t IC, size_t OC, size_t H, size_t W, size_t FS) {
  218. TensorShape src({N, IC, H, W}), filter({OC, IC, FS, FS}), bias({N, OC, H, W}),
  219. dst({N, OC, H, W});
  220. param.pad_h = FS / 2;
  221. param.pad_w = FS / 2;
  222. auto int_used =
  223. benchmarker_int.set_param(param).exec({src, filter, bias, {}, dst}) /
  224. RUNS;
  225. auto float_used =
  226. benchmarker_float.set_param(param).exec({src, filter, bias, {}, dst}) /
  227. RUNS;
  228. float computations = IC * (FS * FS + 1) * dst.total_nr_elems() * 2 * 1e-6;
  229. printf("run: %s %s %s->%s \nfloat: %f ms %f Gflops int: %f ms "
  230. "%f Gflops speedup: %f\n",
  231. src.to_string().c_str(), filter.to_string().c_str(),
  232. bias.to_string().c_str(), dst.to_string().c_str(), float_used,
  233. computations / float_used, int_used, computations / int_used,
  234. float_used / int_used);
  235. };
  236. run(1, 128, 128, 32, 32, 3);
  237. for (size_t IC : {1, 4, 8, 16, 32, 64}) {
  238. for (size_t OC : {1, 4, 8, 16, 32, 64}) {
  239. for (size_t size : {7, 14, 28, 56}) {
  240. for (size_t FS : {1, 3, 5}) {
  241. run(1, IC, OC, size, size, FS);
  242. }
  243. }
  244. }
  245. }
  246. }
  247. #endif
  248. } // namespace test
  249. } // namespace megdnn
  250. // vim: syntax=cpp.doxygen