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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  1. /**
  2. * \file dnn/test/common/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
  10. * implied.
  11. */
  12. #include "test/common/conv_bias.h"
  13. #include "megdnn/opr_param_defs.h"
  14. #include "src/common/utils.h"
  15. #include "test/common/benchmarker.h"
  16. namespace megdnn {
  17. namespace test {
  18. namespace conv_bias {
  19. namespace {
  20. void convert_arg_from_nchw4_to_chwn4(TestArg& arg) {
  21. arg.param.format = param::ConvBias::Format::CHWN4;
  22. arg.src = TensorShape{arg.src[1], arg.src[2], arg.src[3], arg.src[0], 4};
  23. arg.filter = TensorShape{arg.filter[1], arg.filter[2], arg.filter[3],
  24. arg.filter[0], 4};
  25. arg.bias =
  26. TensorShape{arg.bias[1], arg.bias[2], arg.bias[3], arg.bias[0], 4};
  27. }
  28. } // namespace
  29. std::vector<TestArg> get_args() {
  30. std::vector<TestArg> args;
  31. param::ConvBias cur_param;
  32. using NLMode = param::ConvBias::NonlineMode;
  33. // clang-format off
  34. for (auto nlmode :
  35. {NLMode::IDENTITY, NLMode::RELU, NLMode::SIGMOID, NLMode::H_SWISH}) {
  36. for (size_t i : {9, 63}) {
  37. cur_param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  38. cur_param.nonlineMode = nlmode;
  39. // fallback case
  40. args.emplace_back(cur_param, TensorShape{10, 1, i, i},
  41. TensorShape{1, 1, 8, 8}, TensorShape{1, 1, 1, 1});
  42. args.emplace_back(cur_param, TensorShape{10, 4, i, i},
  43. TensorShape{3, 4, 4, 4}, TensorShape{1, 3, 1, 1});
  44. cur_param.mode = param::ConvBias::Mode::CONVOLUTION;
  45. args.emplace_back(cur_param, TensorShape{10, 4, i, i},
  46. TensorShape{1, 4, 3, 3}, TensorShape{1, 1, 1, 1});
  47. args.emplace_back(cur_param, TensorShape{1, 4, i, i},
  48. TensorShape{5, 4, 3, 3}, TensorShape{1, 5, 1, 1});
  49. } }
  50. // clang-format on
  51. return args;
  52. }
  53. std::vector<TestArg> get_chanwise_args() {
  54. std::vector<TestArg> args;
  55. param::ConvBias cur_param;
  56. using NLMode = param::ConvBias::NonlineMode;
  57. cur_param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  58. cur_param.sparse = ConvBias::Param::Sparse::GROUP;
  59. for (auto nlmode :
  60. {NLMode::IDENTITY, NLMode::RELU, NLMode::SIGMOID, NLMode::H_SWISH}) {
  61. cur_param.nonlineMode = nlmode;
  62. // simple case
  63. for (uint32_t s : {1, 2})
  64. for (uint32_t p : {0, 1, 2, 3})
  65. for (size_t f : {2, 3, 5, 7})
  66. for (size_t ocpg : {1, 3}) {
  67. cur_param.pad_h = cur_param.pad_w = p;
  68. cur_param.stride_h = cur_param.stride_w = s;
  69. args.emplace_back(cur_param, TensorShape{2, 3, 16, 16},
  70. TensorShape{3, ocpg, 1, f, f},
  71. TensorShape{1, 3 * ocpg, 1, 1});
  72. }
  73. args.emplace_back(cur_param, TensorShape{32, 12, 20, 10},
  74. TensorShape{12, 2, 1, 4, 5},
  75. TensorShape{1, 24, 1, 1});
  76. // padding larger than kern
  77. args.emplace_back(cur_param, TensorShape{32, 12, 20, 10},
  78. TensorShape{12, 2, 1, 4, 5},
  79. TensorShape{1, 24, 1, 1});
  80. }
  81. return args;
  82. }
  83. std::vector<TestArg> get_args_1x1() {
  84. std::vector<TestArg> args;
  85. param::ConvBias cur_param;
  86. using NLMode = param::ConvBias::NonlineMode;
  87. for (auto nlmode :
  88. {NLMode::IDENTITY, NLMode::RELU, NLMode::SIGMOID, NLMode::H_SWISH}) {
  89. cur_param.nonlineMode = nlmode;
  90. for (size_t i : {16, 19}) {
  91. cur_param.mode = param::ConvBias::Mode::CONVOLUTION;
  92. args.emplace_back(cur_param, TensorShape{2, 20, i, i + 1},
  93. TensorShape{30, 20, 1, 1},
  94. TensorShape{1, 30, 1, 1});
  95. cur_param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  96. args.emplace_back(cur_param, TensorShape{2, 20, i, i + 1},
  97. TensorShape{30, 20, 1, 1},
  98. TensorShape{1, 30, 1, 1});
  99. }
  100. }
  101. return args;
  102. }
  103. std::vector<TestArg> get_winograd_args(size_t kernel_size) {
  104. std::vector<TestArg> args;
  105. param::ConvBias cur_param;
  106. using NLMode = param::ConvBias::NonlineMode;
  107. // clang-format off
  108. for (auto nlmode :
  109. {NLMode::IDENTITY, NLMode::RELU, NLMode::SIGMOID, NLMode::H_SWISH}) {
  110. for (size_t ic : {1, 3, 4, 7}) {
  111. for (size_t oc : {1, 3, 4, 7}) {
  112. for (size_t i : {9, 63}) {
  113. cur_param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  114. cur_param.nonlineMode = nlmode;
  115. cur_param.sparse = param::ConvBias::Sparse::DENSE;
  116. cur_param.pad_h = cur_param.pad_w = 0;
  117. //! no bias
  118. args.emplace_back(cur_param, TensorShape{1, ic, i, i},
  119. TensorShape{oc, ic, kernel_size, kernel_size},
  120. TensorShape{});
  121. //! bias
  122. args.emplace_back(
  123. cur_param, TensorShape{2, ic, i, i},
  124. TensorShape{oc, ic, kernel_size, kernel_size},
  125. TensorShape{2, oc, (i + cur_param.pad_h * 2 - kernel_size) + 1,
  126. (i + cur_param.pad_w * 2 - kernel_size) + 1});
  127. //! bias channel
  128. args.emplace_back(cur_param, TensorShape{2, ic, i, i},
  129. TensorShape{oc, ic, kernel_size, kernel_size},
  130. TensorShape{1, oc, 1, 1});
  131. cur_param.sparse = param::ConvBias::Sparse::GROUP;
  132. args.emplace_back(
  133. cur_param, TensorShape{2, 2 * ic, i, i},
  134. TensorShape{2, oc, ic, kernel_size, kernel_size},
  135. TensorShape{2, 2 * oc,
  136. (i + cur_param.pad_h * 2 - kernel_size) + 1,
  137. (i + cur_param.pad_w * 2 - kernel_size) + 1});
  138. args.emplace_back(cur_param, TensorShape{2, 2 * ic, i, i},
  139. TensorShape{2, oc, ic, kernel_size, kernel_size},
  140. TensorShape{1, 2 * oc, 1, 1});
  141. } } } }
  142. // clang-format on
  143. //! test for multi-thread OC parallel
  144. for (size_t i : {9, 63}) {
  145. cur_param.sparse = param::ConvBias::Sparse::DENSE;
  146. cur_param.pad_h = cur_param.pad_w = 1;
  147. args.emplace_back(cur_param, TensorShape{1, 8, i, i},
  148. TensorShape{128, 8, kernel_size, kernel_size},
  149. TensorShape{1, 128, 1, 1});
  150. args.emplace_back(cur_param, TensorShape{2, 8, i, i},
  151. TensorShape{128, 8, kernel_size, kernel_size},
  152. TensorShape{1, 128, 1, 1});
  153. cur_param.sparse = param::ConvBias::Sparse::GROUP;
  154. args.emplace_back(cur_param, TensorShape{2, 2 * 8, i, i},
  155. TensorShape{2, 128, 8, kernel_size, kernel_size},
  156. TensorShape{1, 2 * 128, 1, 1});
  157. }
  158. return args;
  159. }
  160. std::vector<TestArg> get_winograd_mk_packed_args(size_t pack_size) {
  161. std::vector<TestArg> args;
  162. param::ConvBias cur_param;
  163. using NLMode = param::ConvBias::NonlineMode;
  164. // clang-format off
  165. for (auto nlmode :
  166. {NLMode::IDENTITY, NLMode::RELU, NLMode::SIGMOID, NLMode::H_SWISH}) {
  167. for (size_t ic : {pack_size, 2 * pack_size}) {
  168. for (size_t oc : {pack_size, 2 * pack_size}) {
  169. for (size_t i : {9, 63}) {
  170. cur_param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  171. cur_param.nonlineMode = nlmode;
  172. cur_param.sparse = param::ConvBias::Sparse::DENSE;
  173. cur_param.pad_h = cur_param.pad_w = 1;
  174. args.emplace_back(cur_param, TensorShape{1, pack_size, 3, 3},
  175. TensorShape{pack_size, pack_size, 3, 3},
  176. TensorShape{1, pack_size, 1, 1});
  177. //! no bias
  178. args.emplace_back(cur_param, TensorShape{2, ic, i, i},
  179. TensorShape{oc, ic, 3, 3}, TensorShape{});
  180. //! bias
  181. args.emplace_back(cur_param, TensorShape{2, ic, i, i},
  182. TensorShape{oc, ic, 3, 3}, TensorShape{2, oc, i, i});
  183. //! bias channel
  184. args.emplace_back(cur_param, TensorShape{2, ic, i, i},
  185. TensorShape{oc, ic, 3, 3}, TensorShape{1, oc, 1, 1});
  186. cur_param.sparse = param::ConvBias::Sparse::GROUP;
  187. args.emplace_back(cur_param, TensorShape{2, 2 * ic, i, i},
  188. TensorShape{2, oc, ic, 3, 3},
  189. TensorShape{2, 2 * oc, i, i});
  190. args.emplace_back(cur_param, TensorShape{2, 2 * ic, i, i},
  191. TensorShape{2, oc, ic, 3, 3},
  192. TensorShape{1, 2 * oc, 1, 1});
  193. } } } }
  194. // clang-format on
  195. //! test for multi-thread OC parallel
  196. for (size_t i : {9, 63}) {
  197. cur_param.sparse = param::ConvBias::Sparse::DENSE;
  198. cur_param.pad_h = cur_param.pad_w = 1;
  199. args.emplace_back(cur_param, TensorShape{1, 8, i, i},
  200. TensorShape{128, 8, 3, 3}, TensorShape{1, 128, 1, 1});
  201. args.emplace_back(cur_param, TensorShape{2, 8, i, i},
  202. TensorShape{128, 8, 3, 3}, TensorShape{1, 128, 1, 1});
  203. cur_param.sparse = param::ConvBias::Sparse::GROUP;
  204. args.emplace_back(cur_param, TensorShape{2, 2 * 8, i, i},
  205. TensorShape{2, 128, 8, 3, 3},
  206. TensorShape{1, 2 * 128, 1, 1});
  207. }
  208. return args;
  209. }
  210. std::vector<TestArg> get_quantized_winograd_mk_packed_args(
  211. size_t pack_size, bool compute_float32) {
  212. std::vector<TestArg> args;
  213. param::ConvBias cur_param;
  214. using NLMode = param::ConvBias::NonlineMode;
  215. // clang-format off
  216. for (auto nlmode : {NLMode::IDENTITY, NLMode::RELU}) {
  217. for (size_t ic : {pack_size, 2 * pack_size}) {
  218. for (size_t oc : {pack_size, 2 * pack_size}) {
  219. for (size_t i : {9, 63}) {
  220. cur_param.mode = param::ConvBias::Mode::CROSS_CORRELATION;
  221. cur_param.nonlineMode = nlmode;
  222. cur_param.sparse = param::ConvBias::Sparse::DENSE;
  223. cur_param.pad_h = cur_param.pad_w = 1;
  224. if(compute_float32){
  225. cur_param.compute_mode = param::ConvBias::ComputeMode::FLOAT32;
  226. }
  227. args.emplace_back(cur_param, TensorShape{1, pack_size, 3, 3},
  228. TensorShape{pack_size, pack_size, 3, 3},
  229. TensorShape{1, pack_size, 1, 1});
  230. //! no bias
  231. args.emplace_back(cur_param, TensorShape{2, ic, i, i},
  232. TensorShape{oc, ic, 3, 3}, TensorShape{});
  233. //! bias
  234. args.emplace_back(cur_param, TensorShape{2, ic, i, i},
  235. TensorShape{oc, ic, 3, 3}, TensorShape{2, oc, i, i});
  236. //! bias channel
  237. args.emplace_back(cur_param, TensorShape{2, ic, i, i},
  238. TensorShape{oc, ic, 3, 3}, TensorShape{1, oc, 1, 1});
  239. cur_param.sparse = param::ConvBias::Sparse::GROUP;
  240. args.emplace_back(cur_param, TensorShape{2, 2 * ic, i, i},
  241. TensorShape{2, oc, ic, 3, 3},
  242. TensorShape{2, 2 * oc, i, i});
  243. args.emplace_back(cur_param, TensorShape{2, 2 * ic, i, i},
  244. TensorShape{2, oc, ic, 3, 3},
  245. TensorShape{1, 2 * oc, 1, 1});
  246. } } } }
  247. // clang-format on
  248. //! test for multi-thread OC parallel
  249. for (size_t i : {9, 63}) {
  250. cur_param.sparse = param::ConvBias::Sparse::DENSE;
  251. cur_param.pad_h = cur_param.pad_w = 1;
  252. args.emplace_back(cur_param, TensorShape{1, 8, i, i},
  253. TensorShape{128, 8, 3, 3}, TensorShape{1, 128, 1, 1});
  254. args.emplace_back(cur_param, TensorShape{2, 8, i, i},
  255. TensorShape{128, 8, 3, 3}, TensorShape{1, 128, 1, 1});
  256. cur_param.sparse = param::ConvBias::Sparse::GROUP;
  257. args.emplace_back(cur_param, TensorShape{2, 2 * 8, i, i},
  258. TensorShape{2, 128, 8, 3, 3},
  259. TensorShape{1, 2 * 128, 1, 1});
  260. }
  261. return args;
  262. }
  263. std::vector<TestArg> get_quantized_args_with_nlmode(
  264. param::ConvBias::NonlineMode nlmode) {
  265. std::vector<TestArg> args;
  266. param::ConvBias cur_param;
  267. // clang-format off
  268. for (auto mode : {param::ConvBias::Mode::CROSS_CORRELATION,
  269. param::ConvBias::Mode::CONVOLUTION}) {
  270. for (size_t ic : {1, 2, 3, 4, 5, 7}) {
  271. for (size_t oc : {1, 2, 3, 4, 5, 7}) {
  272. for (size_t i : {9, 63}) {
  273. cur_param.mode = mode;
  274. cur_param.nonlineMode = nlmode;
  275. cur_param.sparse = param::ConvBias::Sparse::DENSE;
  276. cur_param.pad_h = cur_param.pad_w = 1;
  277. //! no bias
  278. args.emplace_back(cur_param, TensorShape{2, ic, i, i},
  279. TensorShape{oc, ic, 3, 3}, TensorShape{});
  280. //! bias
  281. args.emplace_back(cur_param, TensorShape{2, ic, i, i},
  282. TensorShape{oc, ic, 3, 3}, TensorShape{2, oc, i, i});
  283. //! bias channel
  284. args.emplace_back(cur_param, TensorShape{2, ic, i, i},
  285. TensorShape{oc, ic, 3, 3}, TensorShape{1, oc, 1, 1});
  286. cur_param.sparse = param::ConvBias::Sparse::GROUP;
  287. args.emplace_back(cur_param, TensorShape{2, 2 * ic, i, i},
  288. TensorShape{2, oc, ic, 3, 3},
  289. TensorShape{2, 2 * oc, i, i});
  290. args.emplace_back(cur_param, TensorShape{2, 2 * ic, i, i},
  291. TensorShape{2, oc, ic, 3, 3},
  292. TensorShape{1, 2 * oc, 1, 1});
  293. cur_param.sparse = param::ConvBias::Sparse::DENSE;
  294. cur_param.pad_h = cur_param.pad_w = 0;
  295. args.emplace_back(cur_param, TensorShape{2, ic, i, i},
  296. TensorShape{oc, ic, 1, 1}, TensorShape{});
  297. } } } }
  298. // clang-format on
  299. return args;
  300. }
  301. std::vector<TestArg> get_quantized_args() {
  302. using NLMode = param::ConvBias::NonlineMode;
  303. auto arg_p1 = get_quantized_args_with_nlmode(NLMode::IDENTITY),
  304. arg_p2 = get_quantized_args_with_nlmode(NLMode::RELU),
  305. arg_p3 = get_quantized_args_with_nlmode(NLMode::H_SWISH);
  306. std::vector<TestArg> args;
  307. args.insert(args.end(), arg_p1.begin(), arg_p1.end());
  308. args.insert(args.end(), arg_p2.begin(), arg_p2.end());
  309. args.insert(args.end(), arg_p3.begin(), arg_p3.end());
  310. return args;
  311. }
  312. std::vector<TestArg> get_int8_nchw4_args(size_t kernel_size) {
  313. std::vector<TestArg> args;
  314. param::ConvBias cur_param;
  315. using NLMode = param::ConvBias::NonlineMode;
  316. // clang-format off
  317. for (auto nlmode : {NLMode::IDENTITY, NLMode::RELU}) {
  318. for (auto mode : {param::ConvBias::Mode::CROSS_CORRELATION}) {
  319. for (size_t b : {64, 16}) {
  320. for (size_t ic : {16, 32}) {
  321. for (size_t oc : {16, 32}) {
  322. for (size_t h : {8}) {
  323. for (size_t w : {8, 11}) {
  324. for (int p : {0, static_cast<int>(kernel_size / 2)}) {
  325. for (size_t s : {2, 1}) {
  326. if (kernel_size == 7) {
  327. b = std::min(b, 32_z);
  328. }
  329. size_t f = kernel_size;
  330. cur_param.mode = mode;
  331. cur_param.nonlineMode = nlmode;
  332. cur_param.format = param::ConvBias::Format::NCHW4;
  333. cur_param.sparse = param::ConvBias::Sparse::DENSE;
  334. cur_param.pad_h = cur_param.pad_w = p;
  335. cur_param.stride_h = cur_param.stride_w = s;
  336. //! bias channel
  337. args.emplace_back(cur_param, TensorShape{b, ic / 4, h, w, 4},
  338. TensorShape{oc, ic / 4, f, f, 4},
  339. TensorShape{1, oc / 4, 1, 1, 4});
  340. } } } } } } } } }
  341. // clang-format on
  342. return args;
  343. }
  344. std::vector<TestArg> get_int8_nchw44_args(size_t kernel_size, size_t pack_size,
  345. bool compute_float32,
  346. bool group_mode) {
  347. std::vector<TestArg> args;
  348. param::ConvBias cur_param;
  349. megdnn_assert(pack_size > 0, "not support pack_size");
  350. megdnn_assert(kernel_size > 0, "not support kernel_size");
  351. using NLMode = param::ConvBias::NonlineMode;
  352. // clang-format off
  353. for (auto nlmode : {NLMode::IDENTITY, NLMode::RELU}) {
  354. for (auto mode : {param::ConvBias::Mode::CROSS_CORRELATION}) {
  355. for (size_t b : {1,2}) {
  356. for (size_t ic : {8,16}) {
  357. for (size_t oc : {8,16}) {
  358. for (size_t h : {9,23}) {
  359. for (size_t w : {9,23}) {
  360. for (int p : {0, static_cast<int>(kernel_size / 2)}) {
  361. for (size_t s : {1}) {
  362. if (kernel_size == 7) {
  363. b = std::min(b, 32_z);
  364. }
  365. size_t f = kernel_size;
  366. cur_param.mode = mode;
  367. cur_param.nonlineMode = nlmode;
  368. if (pack_size == 4){
  369. cur_param.format = param::ConvBias::Format::NCHW44;
  370. } else if(pack_size == 8){
  371. cur_param.format = param::ConvBias::Format::NCHW88;
  372. }
  373. if(compute_float32){
  374. cur_param.compute_mode =
  375. param::ConvBias::ComputeMode::FLOAT32;
  376. }
  377. cur_param.sparse = param::ConvBias::Sparse::DENSE;
  378. cur_param.pad_h = cur_param.pad_w = p;
  379. cur_param.stride_h = cur_param.stride_w = s;
  380. if (!group_mode) {
  381. //! no bias
  382. args.emplace_back(cur_param,
  383. TensorShape{b, ic / pack_size, h, w, pack_size},
  384. TensorShape{oc / pack_size, ic / pack_size, f, f,
  385. pack_size, pack_size},
  386. TensorShape{});
  387. //! bias channel
  388. args.emplace_back(cur_param,
  389. TensorShape{b, ic / pack_size, h, w, pack_size},
  390. TensorShape{oc / pack_size, ic / pack_size, f, f,
  391. pack_size, pack_size},
  392. TensorShape{1, oc / pack_size, 1, 1, pack_size});
  393. //! bias
  394. args.emplace_back(
  395. cur_param, TensorShape{b, ic / pack_size, h, w, pack_size},
  396. TensorShape{oc / pack_size, ic / pack_size, f, f, pack_size,
  397. pack_size},
  398. TensorShape{b, oc / pack_size, (h - f + 2 * p) / s + 1,
  399. (w - f + 2 * p) / s + 1, pack_size});
  400. } else {
  401. cur_param.sparse = param::ConvBias::Sparse::GROUP;
  402. args.emplace_back(
  403. cur_param,
  404. TensorShape{2, 2 * ic / pack_size, h, w, pack_size},
  405. TensorShape{2, oc / pack_size, ic / pack_size, 3, 3,
  406. pack_size, pack_size},
  407. TensorShape{2, 2 * oc / pack_size, (h - f + 2 * p) / s + 1,
  408. (w - f + 2 * p) / s + 1, pack_size});
  409. args.emplace_back(
  410. cur_param,
  411. TensorShape{2, 2 * ic / pack_size, h, w, pack_size},
  412. TensorShape{2, oc / pack_size, ic / pack_size, f, f,
  413. pack_size, pack_size},
  414. TensorShape{1, 2 * oc / pack_size, 1, 1, pack_size});
  415. args.emplace_back(
  416. cur_param,
  417. TensorShape{2, 2 * ic / pack_size, h, w, pack_size},
  418. TensorShape{2, oc / pack_size, ic / pack_size, f, f,
  419. pack_size, pack_size},
  420. TensorShape{});
  421. }
  422. } } } } } } } } }
  423. // clang-format on
  424. return args;
  425. }
  426. std::vector<TestArg> get_int8_nchw4_args_check_bounds(size_t kernel_size) {
  427. std::vector<TestArg> args;
  428. param::ConvBias cur_param;
  429. using NLMode = param::ConvBias::NonlineMode;
  430. // clang-format off
  431. for (auto nlmode : {NLMode::IDENTITY, NLMode::RELU}) {
  432. for (auto mode : {param::ConvBias::Mode::CROSS_CORRELATION}) {
  433. for (size_t b : {7, 8, 4, 1}) {
  434. for (size_t ic : {16, 32}) {
  435. for (size_t oc : {16, 8, 4}) {
  436. for (size_t h : {8}) {
  437. for (size_t w : {8, 11}) {
  438. for (int p : {static_cast<int>(kernel_size / 2), 0}) {
  439. for (size_t s : {1, 2}) {
  440. size_t f = kernel_size;
  441. cur_param.mode = mode;
  442. cur_param.nonlineMode = nlmode;
  443. cur_param.format = param::ConvBias::Format::NCHW4;
  444. cur_param.sparse = param::ConvBias::Sparse::DENSE;
  445. cur_param.pad_h = cur_param.pad_w = p;
  446. cur_param.stride_h = cur_param.stride_w = s;
  447. //! bias channel
  448. args.emplace_back(cur_param, TensorShape{b, ic / 4, h, w, 4},
  449. TensorShape{oc, ic / 4, f, f, 4},
  450. TensorShape{1, oc / 4, 1, 1, 4});
  451. } } } } } } } } }
  452. // clang-format on
  453. return args;
  454. }
  455. std::vector<TestArg> get_int8_nchw4_args_small_batch(size_t kernel_size) {
  456. std::vector<TestArg> args;
  457. param::ConvBias cur_param;
  458. using NLMode = param::ConvBias::NonlineMode;
  459. // clang-format off
  460. for (auto nlmode : {NLMode::IDENTITY, NLMode::RELU}) {
  461. for (auto mode : {param::ConvBias::Mode::CROSS_CORRELATION}) {
  462. for (size_t b : {12, 8, 4}) {
  463. for (size_t ic : {16, 32}) {
  464. for (size_t oc : {16, 8, 4}) {
  465. for (size_t h : {8}) {
  466. for (size_t w : {8, 9, 10, 11, 12, 13, 14, 15, 16}) {
  467. for (int p : {static_cast<int>(kernel_size / 2), 0}) {
  468. for (size_t s : {1, 2}) {
  469. size_t f = kernel_size;
  470. cur_param.mode = mode;
  471. cur_param.nonlineMode = nlmode;
  472. cur_param.format = param::ConvBias::Format::NCHW4;
  473. cur_param.sparse = param::ConvBias::Sparse::DENSE;
  474. cur_param.pad_h = cur_param.pad_w = p;
  475. cur_param.stride_h = cur_param.stride_w = s;
  476. //! bias channel
  477. args.emplace_back(cur_param, TensorShape{b, ic / 4, h, w, 4},
  478. TensorShape{oc, ic / 4, f, f, 4},
  479. TensorShape{1, oc / 4, 1, 1, 4});
  480. } } } } } } } } }
  481. // clang-format on
  482. return args;
  483. }
  484. std::vector<TestArg> get_int8_nchw4_small_channel_args(size_t kernel_size) {
  485. std::vector<TestArg> args;
  486. param::ConvBias cur_param;
  487. using NLMode = param::ConvBias::NonlineMode;
  488. // clang-format off
  489. for (auto nlmode : {NLMode::IDENTITY, NLMode::RELU}) {
  490. for (auto mode : {param::ConvBias::Mode::CROSS_CORRELATION}) {
  491. for (size_t b : {64, 16}) {
  492. for (size_t ic : {4, 12}) {
  493. for (size_t oc : {128, 32}) {
  494. for (size_t h : {8}) {
  495. for (size_t w : {8, 11}) {
  496. for (int p : {static_cast<int>(kernel_size / 2), 0}) {
  497. for (size_t s : {1, 2}) {
  498. size_t f = kernel_size;
  499. cur_param.mode = mode;
  500. cur_param.nonlineMode = nlmode;
  501. cur_param.format =
  502. param::ConvBias::Format::NCHW4;
  503. cur_param.sparse =
  504. param::ConvBias::Sparse::DENSE;
  505. cur_param.pad_h = cur_param.pad_w = p;
  506. cur_param.stride_h =
  507. cur_param.stride_w = s;
  508. //! bias channel
  509. args.emplace_back(
  510. cur_param,
  511. TensorShape{b, ic / 4, h, w, 4},
  512. TensorShape{oc, ic / 4, f, f,
  513. 4},
  514. TensorShape{1, oc / 4, 1, 1,
  515. 4});
  516. } } } } } } } } }
  517. // clang-format on
  518. return args;
  519. }
  520. std::vector<TestArg> get_int8_nchw4_small_channel_args_check_bounds(
  521. size_t kernel_size) {
  522. std::vector<TestArg> args;
  523. param::ConvBias cur_param;
  524. using NLMode = param::ConvBias::NonlineMode;
  525. // clang-format off
  526. for (auto nlmode : {NLMode::IDENTITY, NLMode::RELU}) {
  527. for (auto mode : {param::ConvBias::Mode::CROSS_CORRELATION}) {
  528. for (size_t b : {8, 7, 4, 1}) {
  529. for (size_t ic : {4, 12}) {
  530. for (size_t oc : {16, 8, 12, 4}) {
  531. for (size_t h : {8}) {
  532. for (size_t w : {8, 11}) {
  533. for (int p : {static_cast<int>(kernel_size / 2), 0}) {
  534. for (size_t s : {1, 2}) {
  535. size_t f = kernel_size;
  536. cur_param.mode = mode;
  537. cur_param.nonlineMode = nlmode;
  538. cur_param.format = param::ConvBias::Format::NCHW4;
  539. cur_param.sparse = param::ConvBias::Sparse::DENSE;
  540. cur_param.pad_h = cur_param.pad_w = p;
  541. cur_param.stride_h = cur_param.stride_w = s;
  542. //! bias channel
  543. args.emplace_back(cur_param, TensorShape{b, ic / 4, h, w, 4},
  544. TensorShape{oc, ic / 4, f, f, 4},
  545. TensorShape{1, oc / 4, 1, 1, 4});
  546. } } } } } } } } }
  547. // clang-format on
  548. return args;
  549. }
  550. std::vector<TestArg> get_int8_chwn4_args(size_t kernel_size) {
  551. auto args = get_int8_nchw4_args(kernel_size);
  552. for (auto& arg : args) {
  553. convert_arg_from_nchw4_to_chwn4(arg);
  554. }
  555. return args;
  556. }
  557. std::vector<TestArg> get_int8_chwn4_args_check_bounds(size_t kernel_size) {
  558. auto args = get_int8_nchw4_args_check_bounds(kernel_size);
  559. for (auto& arg : args) {
  560. convert_arg_from_nchw4_to_chwn4(arg);
  561. }
  562. return args;
  563. }
  564. std::vector<TestArg> get_int8_chwn4_small_channel_args(size_t kernel_size) {
  565. auto args = get_int8_nchw4_small_channel_args(kernel_size);
  566. for (auto& arg : args) {
  567. convert_arg_from_nchw4_to_chwn4(arg);
  568. }
  569. return args;
  570. }
  571. std::vector<TestArg> get_int8_chwn4_small_channel_args_check_bounds(
  572. size_t kernel_size) {
  573. auto args = get_int8_nchw4_small_channel_args_check_bounds(kernel_size);
  574. for (auto& arg : args) {
  575. convert_arg_from_nchw4_to_chwn4(arg);
  576. }
  577. return args;
  578. }
  579. std::vector<TestArg> get_int8_chwn4_args_small_batch(size_t kernel_size) {
  580. auto args = get_int8_nchw4_args_small_batch(kernel_size);
  581. for (auto& arg : args) {
  582. convert_arg_from_nchw4_to_chwn4(arg);
  583. }
  584. return args;
  585. }
  586. std::vector<TestArg> get_int8_nchw4_tensorcore_args(size_t kernel_size) {
  587. std::vector<TestArg> args;
  588. param::ConvBias cur_param;
  589. using NLMode = param::ConvBias::NonlineMode;
  590. // clang-format off
  591. for (auto nlmode : {NLMode::IDENTITY, NLMode::RELU}) {
  592. for (auto mode : {param::ConvBias::Mode::CROSS_CORRELATION}) {
  593. size_t b = 64, oc = 128;
  594. for (size_t ic : {32, 64}) {
  595. for (size_t h : {8}) {
  596. for (size_t w : {11}) {
  597. for (int p : {static_cast<int>(kernel_size / 2), 0}) {
  598. for (size_t s : {1, 2}) {
  599. size_t f = kernel_size;
  600. cur_param.mode = mode;
  601. cur_param.nonlineMode = nlmode;
  602. cur_param.format = param::ConvBias::Format::NCHW4;
  603. cur_param.sparse = param::ConvBias::Sparse::DENSE;
  604. cur_param.pad_h = cur_param.pad_w = p;
  605. cur_param.stride_h = cur_param.stride_w = s;
  606. //! bias channel
  607. args.emplace_back(cur_param, TensorShape{b, ic / 4, h, w, 4},
  608. TensorShape{oc, ic / 4, f, f, 4},
  609. TensorShape{1, oc / 4, 1, 1, 4});
  610. } } } } }
  611. } }
  612. // clang-format on
  613. return args;
  614. }
  615. std::vector<TestArg> get_int8_chwn4_tensorcore_args(size_t kernel_size) {
  616. auto args = get_int8_nchw4_tensorcore_args(kernel_size);
  617. for (auto& arg : args) {
  618. convert_arg_from_nchw4_to_chwn4(arg);
  619. }
  620. return args;
  621. }
  622. void check_conv_bias(DType src_dtype, DType filter_dtype, DType bias_dtype,
  623. DType dst_dtype, Handle* handle, const char* algo,
  624. param::ConvBias::Format format,
  625. const std::vector<TestArg>& args, bool fuse_z) {
  626. megdnn_assert(src_dtype.enumv() == filter_dtype.enumv());
  627. Checker<ConvBiasForward> checker(handle);
  628. if (algo) {
  629. checker.set_before_exec_callback(
  630. ConvBiasAlgoChecker<ConvBiasForward>(algo));
  631. }
  632. std::unique_ptr<RNG> rng;
  633. std::unique_ptr<RNG> bias_rng;
  634. std::unique_ptr<RNG> const_rng;
  635. std::unique_ptr<RNG> zero_rng;
  636. // TODO: check range of rng
  637. if (src_dtype.enumv() == DTypeEnum::QuantizedS8) {
  638. rng = std::make_unique<UniformIntRNG>(-3, 3);
  639. const_rng = std::make_unique<UniformIntRNG>(1, 1);
  640. zero_rng = std::make_unique<UniformIntRNG>(0, 0);
  641. megdnn_assert(bias_dtype.enumv() == DTypeEnum::QuantizedS32);
  642. bias_rng = std::make_unique<UniformIntRNG>(-50, 50);
  643. checker.set_epsilon(1 + 1e-3)
  644. .set_max_avg_error(1e-1)
  645. .set_max_avg_biased_error(1e-3);
  646. } else if (src_dtype.enumv() == DTypeEnum::Float16) {
  647. rng = std::make_unique<NormalRNG>(2.f);
  648. megdnn_assert(bias_dtype.enumv() == DTypeEnum::Float16);
  649. bias_rng = std::make_unique<NormalRNG>(2.f);
  650. checker.set_epsilon(1e-2);
  651. } else if (src_dtype.enumv() == DTypeEnum::Float32) {
  652. rng = std::make_unique<NormalRNG>(2.f);
  653. megdnn_assert(bias_dtype.enumv() == DTypeEnum::Float32);
  654. bias_rng = std::make_unique<NormalRNG>(2.f);
  655. }
  656. using Param = param::ConvBias;
  657. using Format = Param::Format;
  658. auto get_z_shape = [&fuse_z, &format](TestArg arg) -> TensorShape {
  659. TensorShape z{};
  660. if (fuse_z) {
  661. size_t hi, wi, sh, sw, ph, pw, fh, fw;
  662. z = arg.src;
  663. size_t spatial_idx = 2;
  664. if (format == Format::NCHW4) {
  665. hi = arg.src[2];
  666. wi = arg.src[3];
  667. fh = arg.filter[2];
  668. fw = arg.filter[3];
  669. z[1] = arg.filter[0] / 4;
  670. } else if (format == Format::NCHW32) {
  671. hi = arg.src[2];
  672. wi = arg.src[3];
  673. fh = arg.filter[2];
  674. fw = arg.filter[3];
  675. z[1] = arg.filter[0] / 32;
  676. } else {
  677. megdnn_assert(format == Format::CHWN4);
  678. hi = arg.src[1];
  679. wi = arg.src[2];
  680. fh = arg.filter[1];
  681. fw = arg.filter[2];
  682. z[0] = arg.filter[3] / 4;
  683. spatial_idx = 1;
  684. }
  685. sh = arg.param.stride_h;
  686. sw = arg.param.stride_w;
  687. ph = arg.param.pad_h;
  688. pw = arg.param.pad_w;
  689. size_t ho = infer_conv_shape(hi, fh, sh, ph);
  690. size_t wo = infer_conv_shape(wi, fw, sw, pw);
  691. z[spatial_idx] = ho;
  692. z[spatial_idx + 1] = wo;
  693. }
  694. return z;
  695. };
  696. megdnn_assert(rng != nullptr && bias_rng != nullptr);
  697. checker.set_rng(0, rng.get())
  698. .set_rng(1, rng.get())
  699. .set_rng(2, bias_rng.get())
  700. .set_rng(3, rng.get());
  701. if (args.empty()) {
  702. std::vector<TestArg> default_args;
  703. if (format == Format::NCHW4) {
  704. default_args = get_int8_nchw4_args(3);
  705. } else if (format == Format::CHWN4) {
  706. default_args = get_int8_chwn4_args(3);
  707. }
  708. for (auto&& arg : default_args) {
  709. auto z = get_z_shape(arg);
  710. checker.set_dtype(0, src_dtype)
  711. .set_dtype(1, filter_dtype)
  712. .set_dtype(2, bias_dtype)
  713. .set_dtype(3, dst_dtype)
  714. .set_dtype(4, dst_dtype)
  715. .set_param(arg.param)
  716. .execs({arg.src, arg.filter, arg.bias, z, {}});
  717. }
  718. } else {
  719. for (auto&& arg : args) {
  720. auto z = get_z_shape(arg);
  721. checker.set_dtype(0, src_dtype)
  722. .set_dtype(1, filter_dtype)
  723. .set_dtype(2, bias_dtype)
  724. .set_dtype(3, dst_dtype)
  725. .set_dtype(4, dst_dtype)
  726. .set_param(arg.param)
  727. .execs({arg.src, arg.filter, arg.bias, z, {}});
  728. }
  729. }
  730. }
  731. #if MEGDNN_WITH_BENCHMARK
  732. std::vector<conv_bias::TestArg> get_winograd_benchmark_args(size_t kernel,
  733. size_t pack_size) {
  734. std::vector<conv_bias::TestArg> args;
  735. auto pack = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  736. size_t p) {
  737. if (ic % pack_size != 0 || oc % pack_size != 0)
  738. return;
  739. if (w + 2 * p < kernel || h + 2 * p < kernel)
  740. return;
  741. param::ConvBias param;
  742. param.stride_h = 1;
  743. param.stride_w = 1;
  744. param.pad_h = p;
  745. param.pad_w = p;
  746. args.push_back(conv_bias::TestArg{param,
  747. TensorShape{1, ic, h, w},
  748. TensorShape{oc, ic, kernel, kernel},
  749. {1, oc, 1, 1}});
  750. };
  751. for (size_t ic : {8, 16, 32, 64}) {
  752. for (size_t oc : {8, 16, 32, 64}) {
  753. pack(oc, ic, 56, 56, kernel, kernel / 2);
  754. pack(oc, ic, 128, 128, kernel, kernel / 2);
  755. pack(oc, ic, 256, 256, kernel, kernel / 2);
  756. }
  757. }
  758. //! conv in vgg16
  759. pack(512, 512, 15, 15, kernel, kernel / 2);
  760. pack(512, 256, 15, 15, kernel, kernel / 2);
  761. pack(256, 256, 29, 29, kernel, kernel / 2);
  762. pack(256, 128, 29, 29, kernel, kernel / 2);
  763. pack(128, 128, 57, 57, kernel, kernel / 2);
  764. pack(128, 64, 57, 57, kernel, kernel / 2);
  765. pack(64, 64, 123, 123, kernel, kernel / 2);
  766. pack(64, 24, 123, 123, kernel, kernel / 2);
  767. pack(24, 24, 224, 224, kernel, kernel / 2);
  768. //! conv in resnet18
  769. pack(64, 64, 56, 56, kernel, kernel / 2);
  770. pack(128, 128, 28, 28, kernel, kernel / 2);
  771. pack(256, 256, 14, 14, kernel, kernel / 2);
  772. pack(512, 512, 7, 7, kernel, kernel / 2);
  773. return args;
  774. }
  775. void benchmark_winograd(const char* algo_name, Handle* handle, size_t kernel,
  776. size_t pack_size) {
  777. auto&& args = get_winograd_benchmark_args(kernel, pack_size);
  778. using namespace conv_bias;
  779. constexpr size_t RUN = 10;
  780. Benchmarker<Convolution> benchmark(handle);
  781. benchmark.set_display(false);
  782. benchmark.set_times(RUN);
  783. Benchmarker<ConvBias> benchmark_winograd(handle);
  784. benchmark_winograd.set_display(false);
  785. benchmark_winograd.set_times(RUN);
  786. for (auto&& arg : args) {
  787. TensorLayout dst_layout;
  788. auto opr = handle->create_operator<ConvBias>();
  789. opr->param() = arg.param;
  790. opr->deduce_layout({arg.src, dtype::Float32()},
  791. {arg.filter, dtype::Float32()},
  792. {arg.bias, dtype::Float32()}, {}, dst_layout);
  793. //! dst.nr_elems * IC * FH * FW * 2
  794. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  795. arg.filter[2] * arg.filter[3] * 2.0 /
  796. (1024 * 1024 * 1024) * 1e3;
  797. param::Convolution conv_param;
  798. conv_param.pad_h = arg.param.pad_h;
  799. conv_param.pad_w = arg.param.pad_w;
  800. conv_param.stride_h = arg.param.stride_h;
  801. conv_param.stride_w = arg.param.stride_w;
  802. auto used = benchmark.set_param(conv_param)
  803. .exec({arg.src, arg.filter, {}}) /
  804. RUN;
  805. benchmark_winograd.set_param(arg.param);
  806. auto used_winograd =
  807. algo_benchmark<ConvBias>(benchmark_winograd,
  808. {arg.src, arg.filter, {}, {}, {}},
  809. algo_name) /
  810. RUN;
  811. printf("%s %s: normal: %f ms %f Gflops winograd: %f ms %f GFlops "
  812. "speedup: "
  813. "%f\n",
  814. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  815. used, computations / used, used_winograd,
  816. computations / used_winograd, used / used_winograd);
  817. }
  818. }
  819. #endif // MEGDNN_WITH_BENCHMARK
  820. std::vector<conv_bias::TestArg> get_conv_bias_args(
  821. std::vector<size_t> kernel, size_t stride, bool no_pad, bool no_bias,
  822. bool no_nonlinemode, bool quantized_nlmod, bool only_broadcast_bias) {
  823. using namespace conv_bias;
  824. using Param = param::ConvBias;
  825. using NLMode = param::ConvBias::NonlineMode;
  826. std::vector<TestArg> args;
  827. auto pack = [&](size_t n, size_t oc, size_t ic, size_t w, size_t h,
  828. size_t kernel, size_t stride, NLMode nlmode) {
  829. Param param;
  830. param.stride_h = stride;
  831. param.stride_w = stride;
  832. if (!no_pad) {
  833. param.pad_h = kernel / 2;
  834. param.pad_w = kernel / 2;
  835. } else {
  836. param.pad_h = 0;
  837. param.pad_w = 0;
  838. }
  839. param.nonlineMode = nlmode;
  840. args.emplace_back(param, TensorShape{n, ic, h, w},
  841. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  842. if (!no_bias) {
  843. args.emplace_back(param, TensorShape{n, ic, h, w},
  844. TensorShape{oc, ic, kernel, kernel},
  845. TensorShape{1, oc, 1, 1});
  846. if (!only_broadcast_bias) {
  847. args.emplace_back(
  848. param, TensorShape{n, ic, h, w},
  849. TensorShape{oc, ic, kernel, kernel},
  850. TensorShape{
  851. n, oc,
  852. (h + 2 * param.pad_h - kernel) / stride + 1,
  853. (w + 2 * param.pad_h - kernel) / stride + 1});
  854. }
  855. }
  856. param.sparse = param::ConvBias::Sparse::GROUP;
  857. args.emplace_back(param, TensorShape{n, 2 * ic, h, w},
  858. TensorShape{2, oc, ic, kernel, kernel},
  859. TensorShape{});
  860. if (!no_bias) {
  861. if (!only_broadcast_bias) {
  862. args.emplace_back(
  863. param, TensorShape{n, 2 * ic, h, w},
  864. TensorShape{2, oc, ic, kernel, kernel},
  865. TensorShape{
  866. n, 2 * oc,
  867. (h + param.pad_h * 2 - kernel) / stride + 1,
  868. (w + param.pad_w * 2 - kernel) / stride + 1});
  869. }
  870. args.emplace_back(param, TensorShape{n, 2 * ic, h, w},
  871. TensorShape{2, oc, ic, kernel, kernel},
  872. TensorShape{1, 2 * oc, 1, 1});
  873. }
  874. };
  875. std::vector<NLMode> nonlinemode = {NLMode::IDENTITY};
  876. if (!no_nonlinemode) {
  877. nonlinemode.emplace_back(NLMode::RELU);
  878. nonlinemode.emplace_back(NLMode::H_SWISH);
  879. if (!quantized_nlmod) {
  880. nonlinemode.emplace_back(NLMode::SIGMOID);
  881. }
  882. }
  883. for (size_t n : {1, 2}) {
  884. for (auto nlmode : nonlinemode) {
  885. for (size_t ic : {1, 3, 7}) {
  886. for (size_t oc : {1, 3, 7}) {
  887. for (size_t size : {8, 16, 20}) {
  888. for (size_t kern : kernel) {
  889. pack(n, oc, ic, size, size, kern, stride, nlmode);
  890. }
  891. }
  892. }
  893. }
  894. }
  895. }
  896. return args;
  897. }
  898. std::vector<megdnn::test::conv_bias::TestArg> get_conv_bias_1x1_args(
  899. bool no_bias, bool no_nonlinemode, bool quantized_nlmod,
  900. bool only_broadcast_bias) {
  901. using namespace conv_bias;
  902. using Param = param::ConvBias;
  903. using NLMode = param::ConvBias::NonlineMode;
  904. using CONVMode = param::ConvBias::Mode;
  905. std::vector<TestArg> args;
  906. auto pack = [&](size_t n, size_t oc, size_t ic, size_t w, size_t h,
  907. size_t stride, NLMode nlmode, CONVMode convmode) {
  908. Param param;
  909. param.stride_h = stride;
  910. param.stride_w = stride;
  911. param.pad_h = 0;
  912. param.pad_w = 0;
  913. param.mode = convmode;
  914. param.nonlineMode = nlmode;
  915. args.emplace_back(param, TensorShape{n, ic, h, w},
  916. TensorShape{oc, ic, 1, 1}, TensorShape{});
  917. if (!no_bias) {
  918. args.emplace_back(param, TensorShape{n, ic, h, w},
  919. TensorShape{oc, ic, 1, 1},
  920. TensorShape{1, oc, 1, 1});
  921. if (!only_broadcast_bias) {
  922. args.emplace_back(param, TensorShape{n, ic, h, w},
  923. TensorShape{oc, ic, 1, 1},
  924. TensorShape{n, oc, (h - 1) / stride + 1,
  925. (w - 1) / stride + 1});
  926. }
  927. }
  928. param.sparse = param::ConvBias::Sparse::GROUP;
  929. args.emplace_back(param, TensorShape{n, 2 * ic, h, w},
  930. TensorShape{2, oc, ic, 1, 1}, TensorShape{});
  931. if (!no_bias) {
  932. args.emplace_back(param, TensorShape{n, 2 * ic, h, w},
  933. TensorShape{2, oc, ic, 1, 1},
  934. TensorShape{1, 2 * oc, 1, 1});
  935. if (!only_broadcast_bias) {
  936. args.emplace_back(param, TensorShape{n, 2 * ic, h, w},
  937. TensorShape{2, oc, ic, 1, 1},
  938. TensorShape{n, 2 * oc, (h - 1) / stride + 1,
  939. (w - 1) / stride + 1});
  940. }
  941. }
  942. };
  943. std::vector<NLMode> nonlinemode = {NLMode::IDENTITY};
  944. if (!no_nonlinemode) {
  945. nonlinemode.emplace_back(NLMode::RELU);
  946. nonlinemode.emplace_back(NLMode::H_SWISH);
  947. if (!quantized_nlmod) {
  948. nonlinemode.emplace_back(NLMode::SIGMOID);
  949. }
  950. }
  951. std::vector<CONVMode> convmodes{param::ConvBias::Mode::CONVOLUTION,
  952. param::ConvBias::Mode::CROSS_CORRELATION};
  953. for (size_t n : {1, 2})
  954. for (size_t oc : {1, 9, 33})
  955. for (size_t ic : {1, 16, 64})
  956. for (size_t size : {1, 7, 14, 28})
  957. for (auto nlmode : nonlinemode)
  958. for (auto convmode : convmodes) {
  959. pack(n, oc, ic, size, size, 1, nlmode, convmode);
  960. }
  961. return args;
  962. }
  963. void check_conv_bias(std::vector<conv_bias::TestArg> args, Handle* handle,
  964. const char* algo_name) {
  965. using namespace conv_bias;
  966. Checker<ConvBias> checker(handle);
  967. checker.set_before_exec_callback(
  968. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name));
  969. for (auto&& arg : args) {
  970. checker.set_param(arg.param).execs(
  971. {arg.src, arg.filter, arg.bias, {}, {}});
  972. }
  973. }
  974. void checker_conv_bias_int8x8x16(std::vector<conv_bias::TestArg> args,
  975. Handle* handle, const char* algo_name) {
  976. using namespace conv_bias;
  977. Checker<ConvBias> checker(handle);
  978. checker.set_before_exec_callback(
  979. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name));
  980. checker.set_dtype(0, dtype::Int8());
  981. checker.set_dtype(1, dtype::Int8());
  982. checker.set_dtype(2, dtype::Int16());
  983. checker.set_dtype(4, dtype::Int16());
  984. for (auto&& arg : args) {
  985. checker.set_param(arg.param).execs({arg.src, arg.filter, {}, {}, {}});
  986. }
  987. }
  988. void check_conv_bias_preprocess(std::vector<conv_bias::TestArg> args,
  989. Handle* handle, RNG* rng, float epsilon,
  990. DType type0, DType type1, DType type2,
  991. DType type3, const char* algo_name) {
  992. using namespace conv_bias;
  993. Checker<ConvBiasForward, OprWeightPreprocessProxy<ConvBiasForward>> checker(
  994. handle);
  995. checker.set_dtype(0, type0);
  996. checker.set_dtype(1, type1);
  997. checker.set_dtype(2, type2);
  998. checker.set_dtype(4, type3);
  999. checker.set_epsilon(epsilon);
  1000. if (NULL != rng) {
  1001. checker.set_rng(0, rng).set_rng(1, rng).set_rng(2, rng).set_rng(3, rng);
  1002. }
  1003. checker.set_before_exec_callback(
  1004. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name));
  1005. for (auto&& arg : args) {
  1006. checker.set_param(arg.param).execs(
  1007. {arg.src, arg.filter, arg.bias, {}, {}});
  1008. }
  1009. }
  1010. void winograd_algo_extra_impl(const TensorNDArray& tensors, uint32_t m,
  1011. param::ConvBias param, Handle* handle,
  1012. param::MatrixMul::Format format) {
  1013. megdnn_assert(param.format == param::ConvBias::Format::NCHW ||
  1014. param.format == param::ConvBias::Format::NCHW44);
  1015. auto winograd_preprocess_opr =
  1016. handle->create_operator<WinogradFilterPreprocess>();
  1017. winograd_preprocess_opr->param().output_block_size = m;
  1018. winograd_preprocess_opr->param().format = format;
  1019. winograd_preprocess_opr->param().compute_mode = param.compute_mode;
  1020. TensorLayout filter_transform_layout;
  1021. winograd_preprocess_opr->deduce_layout(tensors[1].layout,
  1022. filter_transform_layout);
  1023. size_t winograd_preprocess_workspace_in_bytes =
  1024. winograd_preprocess_opr->get_workspace_in_bytes(
  1025. tensors[1].layout, filter_transform_layout);
  1026. auto conv_bias_opr = handle->create_operator<ConvBias>();
  1027. conv_bias_opr->param() = param;
  1028. if (param.format == param::ConvBias::Format::NCHW) {
  1029. conv_bias_opr->param().format = param::ConvBias::Format::NCHW_WINOGRAD;
  1030. } else {
  1031. conv_bias_opr->param().format =
  1032. param::ConvBias::Format::NCHW44_WINOGRAD;
  1033. }
  1034. conv_bias_opr->param().output_block_size = m;
  1035. size_t conv_bias_workspace_in_bytes = conv_bias_opr->get_workspace_in_bytes(
  1036. tensors[0].layout, filter_transform_layout, tensors[2].layout,
  1037. tensors[3].layout, tensors[4].layout, nullptr);
  1038. WorkspaceBundle wb(nullptr, {filter_transform_layout.span().dist_byte(),
  1039. conv_bias_workspace_in_bytes,
  1040. winograd_preprocess_workspace_in_bytes});
  1041. wb.set(malloc(wb.total_size_in_bytes()));
  1042. TensorND filter_transform_tensor(wb.get(0),
  1043. std::move(filter_transform_layout));
  1044. winograd_preprocess_opr->exec(tensors[1], filter_transform_tensor,
  1045. wb.get_workspace(2));
  1046. conv_bias_opr->exec(tensors[0], filter_transform_tensor, tensors[2],
  1047. tensors[3], tensors[4], nullptr, wb.get_workspace(1));
  1048. free(wb.ptr());
  1049. };
  1050. } // namespace conv_bias
  1051. } // namespace test
  1052. } // namespace megdnn
  1053. // vim: syntax=cpp.doxygen

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