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

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

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台