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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  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-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/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. bool stable_test) {
  627. megdnn_assert(src_dtype.enumv() == filter_dtype.enumv());
  628. Checker<ConvBiasForward> checker(handle, !stable_test);
  629. if (algo) {
  630. checker.set_before_exec_callback(
  631. ConvBiasAlgoChecker<ConvBiasForward>(algo));
  632. }
  633. std::unique_ptr<RNG> rng;
  634. std::unique_ptr<RNG> bias_rng;
  635. std::unique_ptr<RNG> const_rng;
  636. std::unique_ptr<RNG> zero_rng;
  637. // TODO: check range of rng
  638. if (src_dtype.enumv() == DTypeEnum::QuantizedS8) {
  639. rng = std::make_unique<UniformIntRNG>(-3, 3);
  640. const_rng = std::make_unique<UniformIntRNG>(1, 1);
  641. zero_rng = std::make_unique<UniformIntRNG>(0, 0);
  642. megdnn_assert(bias_dtype.enumv() == DTypeEnum::QuantizedS32);
  643. bias_rng = std::make_unique<UniformIntRNG>(-50, 50);
  644. checker.set_epsilon(1 + 1e-3)
  645. .set_max_avg_error(1e-1)
  646. .set_max_avg_biased_error(1e-3);
  647. } else if (src_dtype.enumv() == DTypeEnum::QuantizedS4) {
  648. rng = std::make_unique<UniformIntRNG>(-3, 3);
  649. const_rng = std::make_unique<UniformIntRNG>(1, 1);
  650. zero_rng = std::make_unique<UniformIntRNG>(0, 0);
  651. megdnn_assert(bias_dtype.enumv() == DTypeEnum::QuantizedS32);
  652. bias_rng = std::make_unique<UniformIntRNG>(-50, 50);
  653. checker.set_epsilon(1 + 1e-3)
  654. .set_max_avg_error(1e-1)
  655. .set_max_avg_biased_error(1e-3);
  656. } else if (src_dtype.enumv() == DTypeEnum::Float16) {
  657. rng = std::make_unique<NormalRNG>(2.f);
  658. megdnn_assert(bias_dtype.enumv() == DTypeEnum::Float16);
  659. bias_rng = std::make_unique<NormalRNG>(2.f);
  660. checker.set_epsilon(1e-2);
  661. } else if (src_dtype.enumv() == DTypeEnum::Float32) {
  662. rng = std::make_unique<NormalRNG>(2.f);
  663. megdnn_assert(bias_dtype.enumv() == DTypeEnum::Float32);
  664. bias_rng = std::make_unique<NormalRNG>(2.f);
  665. }
  666. using Param = param::ConvBias;
  667. using Format = Param::Format;
  668. auto get_z_shape = [&fuse_z, &format](TestArg arg) -> TensorShape {
  669. TensorShape z{};
  670. if (fuse_z) {
  671. size_t hi, wi, sh, sw, ph, pw, fh, fw;
  672. z = arg.src;
  673. size_t spatial_idx = 2;
  674. if (format == Format::NCHW4) {
  675. hi = arg.src[2];
  676. wi = arg.src[3];
  677. fh = arg.filter[2];
  678. fw = arg.filter[3];
  679. z[1] = arg.filter[0] / 4;
  680. } else if (format == Format::NCHW32) {
  681. hi = arg.src[2];
  682. wi = arg.src[3];
  683. fh = arg.filter[2];
  684. fw = arg.filter[3];
  685. z[1] = arg.filter[0] / 32;
  686. } else if (format == Format::NCHW64) {
  687. hi = arg.src[2];
  688. wi = arg.src[3];
  689. fh = arg.filter[2];
  690. fw = arg.filter[3];
  691. z[1] = arg.filter[0] / 64;
  692. } else {
  693. megdnn_assert(format == Format::CHWN4);
  694. hi = arg.src[1];
  695. wi = arg.src[2];
  696. fh = arg.filter[1];
  697. fw = arg.filter[2];
  698. z[0] = arg.filter[3] / 4;
  699. spatial_idx = 1;
  700. }
  701. sh = arg.param.stride_h;
  702. sw = arg.param.stride_w;
  703. ph = arg.param.pad_h;
  704. pw = arg.param.pad_w;
  705. size_t ho = infer_conv_shape(hi, fh, sh, ph);
  706. size_t wo = infer_conv_shape(wi, fw, sw, pw);
  707. z[spatial_idx] = ho;
  708. z[spatial_idx + 1] = wo;
  709. }
  710. return z;
  711. };
  712. megdnn_assert(rng != nullptr && bias_rng != nullptr);
  713. checker.set_rng(0, rng.get())
  714. .set_rng(1, rng.get())
  715. .set_rng(2, bias_rng.get())
  716. .set_rng(3, rng.get());
  717. if (stable_test) {
  718. checker.set_stable_check(true);
  719. checker.set_no_naive_check(true);
  720. }
  721. if (args.empty()) {
  722. std::vector<TestArg> default_args;
  723. if (format == Format::NCHW4) {
  724. default_args = get_int8_nchw4_args(3);
  725. } else if (format == Format::CHWN4) {
  726. default_args = get_int8_chwn4_args(3);
  727. }
  728. for (auto&& arg : default_args) {
  729. auto z = get_z_shape(arg);
  730. checker.set_dtype(0, src_dtype)
  731. .set_dtype(1, filter_dtype)
  732. .set_dtype(2, bias_dtype)
  733. .set_dtype(3, dst_dtype)
  734. .set_dtype(4, dst_dtype)
  735. .set_param(arg.param)
  736. .execs({arg.src, arg.filter, arg.bias, z, {}});
  737. }
  738. } else {
  739. for (auto&& arg : args) {
  740. auto z = get_z_shape(arg);
  741. checker.set_dtype(0, src_dtype)
  742. .set_dtype(1, filter_dtype)
  743. .set_dtype(2, bias_dtype)
  744. .set_dtype(3, dst_dtype)
  745. .set_dtype(4, dst_dtype)
  746. .set_param(arg.param)
  747. .execs({arg.src, arg.filter, arg.bias, z, {}});
  748. }
  749. }
  750. }
  751. #if MEGDNN_WITH_BENCHMARK
  752. std::vector<conv_bias::TestArg> get_winograd_benchmark_args(size_t kernel,
  753. size_t pack_size) {
  754. std::vector<conv_bias::TestArg> args;
  755. auto pack = [&](size_t oc, size_t ic, size_t w, size_t h, size_t kernel,
  756. size_t p) {
  757. if (ic % pack_size != 0 || oc % pack_size != 0)
  758. return;
  759. if (w + 2 * p < kernel || h + 2 * p < kernel)
  760. return;
  761. param::ConvBias param;
  762. param.stride_h = 1;
  763. param.stride_w = 1;
  764. param.pad_h = p;
  765. param.pad_w = p;
  766. args.push_back(conv_bias::TestArg{param,
  767. TensorShape{1, ic, h, w},
  768. TensorShape{oc, ic, kernel, kernel},
  769. {1, oc, 1, 1}});
  770. };
  771. for (size_t ic : {8, 16, 32, 64}) {
  772. for (size_t oc : {8, 16, 32, 64}) {
  773. pack(oc, ic, 56, 56, kernel, kernel / 2);
  774. pack(oc, ic, 128, 128, kernel, kernel / 2);
  775. pack(oc, ic, 256, 256, kernel, kernel / 2);
  776. }
  777. }
  778. //! conv in vgg16
  779. pack(512, 512, 15, 15, kernel, kernel / 2);
  780. pack(512, 256, 15, 15, kernel, kernel / 2);
  781. pack(256, 256, 29, 29, kernel, kernel / 2);
  782. pack(256, 128, 29, 29, kernel, kernel / 2);
  783. pack(128, 128, 57, 57, kernel, kernel / 2);
  784. pack(128, 64, 57, 57, kernel, kernel / 2);
  785. pack(64, 64, 123, 123, kernel, kernel / 2);
  786. pack(64, 24, 123, 123, kernel, kernel / 2);
  787. pack(24, 24, 224, 224, kernel, kernel / 2);
  788. //! conv in resnet18
  789. pack(64, 64, 56, 56, kernel, kernel / 2);
  790. pack(128, 128, 28, 28, kernel, kernel / 2);
  791. pack(256, 256, 14, 14, kernel, kernel / 2);
  792. pack(512, 512, 7, 7, kernel, kernel / 2);
  793. return args;
  794. }
  795. void benchmark_winograd(const char* algo_name, Handle* handle, size_t kernel,
  796. size_t pack_size) {
  797. auto&& args = get_winograd_benchmark_args(kernel, pack_size);
  798. using namespace conv_bias;
  799. constexpr size_t RUN = 10;
  800. Benchmarker<Convolution> benchmark(handle);
  801. benchmark.set_display(false);
  802. benchmark.set_times(RUN);
  803. Benchmarker<ConvBias> benchmark_winograd(handle);
  804. benchmark_winograd.set_display(false);
  805. benchmark_winograd.set_times(RUN);
  806. for (auto&& arg : args) {
  807. TensorLayout dst_layout;
  808. auto opr = handle->create_operator<ConvBias>();
  809. opr->param() = arg.param;
  810. opr->deduce_layout({arg.src, dtype::Float32()},
  811. {arg.filter, dtype::Float32()},
  812. {arg.bias, dtype::Float32()}, {}, dst_layout);
  813. //! dst.nr_elems * IC * FH * FW * 2
  814. float computations = dst_layout.total_nr_elems() * arg.filter[1] *
  815. arg.filter[2] * arg.filter[3] * 2.0 /
  816. (1024 * 1024 * 1024) * 1e3;
  817. param::Convolution conv_param;
  818. conv_param.pad_h = arg.param.pad_h;
  819. conv_param.pad_w = arg.param.pad_w;
  820. conv_param.stride_h = arg.param.stride_h;
  821. conv_param.stride_w = arg.param.stride_w;
  822. auto used = benchmark.set_param(conv_param)
  823. .exec({arg.src, arg.filter, {}}) /
  824. RUN;
  825. benchmark_winograd.set_param(arg.param);
  826. auto used_winograd =
  827. algo_benchmark<ConvBias>(benchmark_winograd,
  828. {arg.src, arg.filter, {}, {}, {}},
  829. algo_name) /
  830. RUN;
  831. printf("%s %s: normal: %f ms %f Gflops winograd: %f ms %f GFlops "
  832. "speedup: "
  833. "%f\n",
  834. arg.src.to_string().c_str(), arg.filter.to_string().c_str(),
  835. used, computations / used, used_winograd,
  836. computations / used_winograd, used / used_winograd);
  837. }
  838. }
  839. #endif // MEGDNN_WITH_BENCHMARK
  840. std::vector<conv_bias::TestArg> get_conv_bias_args(
  841. std::vector<size_t> kernel, size_t stride, bool no_pad, bool no_bias,
  842. bool no_nonlinemode, bool quantized_nlmod, bool only_broadcast_bias) {
  843. using namespace conv_bias;
  844. using Param = param::ConvBias;
  845. using NLMode = param::ConvBias::NonlineMode;
  846. std::vector<TestArg> args;
  847. auto pack = [&](size_t n, size_t oc, size_t ic, size_t w, size_t h,
  848. size_t kernel, size_t stride, NLMode nlmode) {
  849. Param param;
  850. param.stride_h = stride;
  851. param.stride_w = stride;
  852. if (!no_pad) {
  853. param.pad_h = kernel / 2;
  854. param.pad_w = kernel / 2;
  855. } else {
  856. param.pad_h = 0;
  857. param.pad_w = 0;
  858. }
  859. param.nonlineMode = nlmode;
  860. args.emplace_back(param, TensorShape{n, ic, h, w},
  861. TensorShape{oc, ic, kernel, kernel}, TensorShape{});
  862. if (!no_bias) {
  863. args.emplace_back(param, TensorShape{n, ic, h, w},
  864. TensorShape{oc, ic, kernel, kernel},
  865. TensorShape{1, oc, 1, 1});
  866. if (!only_broadcast_bias) {
  867. args.emplace_back(
  868. param, TensorShape{n, ic, h, w},
  869. TensorShape{oc, ic, kernel, kernel},
  870. TensorShape{
  871. n, oc,
  872. (h + 2 * param.pad_h - kernel) / stride + 1,
  873. (w + 2 * param.pad_h - kernel) / stride + 1});
  874. }
  875. }
  876. param.sparse = param::ConvBias::Sparse::GROUP;
  877. args.emplace_back(param, TensorShape{n, 2 * ic, h, w},
  878. TensorShape{2, oc, ic, kernel, kernel},
  879. TensorShape{});
  880. if (!no_bias) {
  881. if (!only_broadcast_bias) {
  882. args.emplace_back(
  883. param, TensorShape{n, 2 * ic, h, w},
  884. TensorShape{2, oc, ic, kernel, kernel},
  885. TensorShape{
  886. n, 2 * oc,
  887. (h + param.pad_h * 2 - kernel) / stride + 1,
  888. (w + param.pad_w * 2 - kernel) / stride + 1});
  889. }
  890. args.emplace_back(param, TensorShape{n, 2 * ic, h, w},
  891. TensorShape{2, oc, ic, kernel, kernel},
  892. TensorShape{1, 2 * oc, 1, 1});
  893. }
  894. };
  895. std::vector<NLMode> nonlinemode = {NLMode::IDENTITY};
  896. if (!no_nonlinemode) {
  897. nonlinemode.emplace_back(NLMode::RELU);
  898. nonlinemode.emplace_back(NLMode::H_SWISH);
  899. if (!quantized_nlmod) {
  900. nonlinemode.emplace_back(NLMode::SIGMOID);
  901. }
  902. }
  903. for (size_t n : {1, 2}) {
  904. for (auto nlmode : nonlinemode) {
  905. for (size_t ic : {1, 3, 7}) {
  906. for (size_t oc : {1, 3, 7}) {
  907. for (size_t size : {8, 16, 20}) {
  908. for (size_t kern : kernel) {
  909. pack(n, oc, ic, size, size, kern, stride, nlmode);
  910. }
  911. }
  912. }
  913. }
  914. }
  915. }
  916. return args;
  917. }
  918. std::vector<megdnn::test::conv_bias::TestArg> get_conv_bias_1x1_args(
  919. bool no_bias, bool no_nonlinemode, bool quantized_nlmod,
  920. bool only_broadcast_bias) {
  921. using namespace conv_bias;
  922. using Param = param::ConvBias;
  923. using NLMode = param::ConvBias::NonlineMode;
  924. using CONVMode = param::ConvBias::Mode;
  925. std::vector<TestArg> args;
  926. auto pack = [&](size_t n, size_t oc, size_t ic, size_t w, size_t h,
  927. size_t stride, NLMode nlmode, CONVMode convmode) {
  928. Param param;
  929. param.stride_h = stride;
  930. param.stride_w = stride;
  931. param.pad_h = 0;
  932. param.pad_w = 0;
  933. param.mode = convmode;
  934. param.nonlineMode = nlmode;
  935. args.emplace_back(param, TensorShape{n, ic, h, w},
  936. TensorShape{oc, ic, 1, 1}, TensorShape{});
  937. if (!no_bias) {
  938. args.emplace_back(param, TensorShape{n, ic, h, w},
  939. TensorShape{oc, ic, 1, 1},
  940. TensorShape{1, oc, 1, 1});
  941. if (!only_broadcast_bias) {
  942. args.emplace_back(param, TensorShape{n, ic, h, w},
  943. TensorShape{oc, ic, 1, 1},
  944. TensorShape{n, oc, (h - 1) / stride + 1,
  945. (w - 1) / stride + 1});
  946. }
  947. }
  948. param.sparse = param::ConvBias::Sparse::GROUP;
  949. args.emplace_back(param, TensorShape{n, 2 * ic, h, w},
  950. TensorShape{2, oc, ic, 1, 1}, TensorShape{});
  951. if (!no_bias) {
  952. args.emplace_back(param, TensorShape{n, 2 * ic, h, w},
  953. TensorShape{2, oc, ic, 1, 1},
  954. TensorShape{1, 2 * oc, 1, 1});
  955. if (!only_broadcast_bias) {
  956. args.emplace_back(param, TensorShape{n, 2 * ic, h, w},
  957. TensorShape{2, oc, ic, 1, 1},
  958. TensorShape{n, 2 * oc, (h - 1) / stride + 1,
  959. (w - 1) / stride + 1});
  960. }
  961. }
  962. };
  963. std::vector<NLMode> nonlinemode = {NLMode::IDENTITY};
  964. if (!no_nonlinemode) {
  965. nonlinemode.emplace_back(NLMode::RELU);
  966. nonlinemode.emplace_back(NLMode::H_SWISH);
  967. if (!quantized_nlmod) {
  968. nonlinemode.emplace_back(NLMode::SIGMOID);
  969. }
  970. }
  971. std::vector<CONVMode> convmodes{param::ConvBias::Mode::CONVOLUTION,
  972. param::ConvBias::Mode::CROSS_CORRELATION};
  973. for (size_t n : {1, 2})
  974. for (size_t oc : {1, 9, 33})
  975. for (size_t ic : {1, 16, 64})
  976. for (size_t size : {1, 7, 14, 28})
  977. for (auto nlmode : nonlinemode)
  978. for (auto convmode : convmodes) {
  979. pack(n, oc, ic, size, size, 1, nlmode, convmode);
  980. }
  981. return args;
  982. }
  983. void check_conv_bias(std::vector<conv_bias::TestArg> args, Handle* handle,
  984. const char* algo_name) {
  985. using namespace conv_bias;
  986. Checker<ConvBias> checker(handle);
  987. checker.set_before_exec_callback(
  988. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name));
  989. for (auto&& arg : args) {
  990. checker.set_param(arg.param).execs(
  991. {arg.src, arg.filter, arg.bias, {}, {}});
  992. }
  993. }
  994. void checker_conv_bias_int8x8x16(std::vector<conv_bias::TestArg> args,
  995. Handle* handle, const char* algo_name) {
  996. using namespace conv_bias;
  997. Checker<ConvBias> checker(handle);
  998. checker.set_before_exec_callback(
  999. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name));
  1000. checker.set_dtype(0, dtype::Int8());
  1001. checker.set_dtype(1, dtype::Int8());
  1002. checker.set_dtype(2, dtype::Int16());
  1003. checker.set_dtype(4, dtype::Int16());
  1004. for (auto&& arg : args) {
  1005. checker.set_param(arg.param).execs({arg.src, arg.filter, {}, {}, {}});
  1006. }
  1007. }
  1008. void check_conv_bias_preprocess(std::vector<conv_bias::TestArg> args,
  1009. Handle* handle, RNG* rng, float epsilon,
  1010. DType type0, DType type1, DType type2,
  1011. DType type3, const char* algo_name) {
  1012. using namespace conv_bias;
  1013. Checker<ConvBiasForward, OprWeightPreprocessProxy<ConvBiasForward>> checker(
  1014. handle);
  1015. checker.set_dtype(0, type0);
  1016. checker.set_dtype(1, type1);
  1017. checker.set_dtype(2, type2);
  1018. checker.set_dtype(4, type3);
  1019. checker.set_epsilon(epsilon);
  1020. if (NULL != rng) {
  1021. checker.set_rng(0, rng).set_rng(1, rng).set_rng(2, rng).set_rng(3, rng);
  1022. }
  1023. checker.set_before_exec_callback(
  1024. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name));
  1025. for (auto&& arg : args) {
  1026. checker.set_param(arg.param).execs(
  1027. {arg.src, arg.filter, arg.bias, {}, {}});
  1028. }
  1029. }
  1030. void checker_conv_bias_common(std::vector<conv_bias::TestArg> args, Handle* handle,
  1031. RNG* rng, float epsilon, DType type0, DType type1,
  1032. DType type2, DType type3, const char* algo_name) {
  1033. using namespace conv_bias;
  1034. Checker<ConvBias> checker(handle);
  1035. checker.set_before_exec_callback(
  1036. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name));
  1037. checker.set_dtype(0, type0);
  1038. checker.set_dtype(1, type1);
  1039. checker.set_dtype(2, type2);
  1040. checker.set_dtype(4, type3);
  1041. checker.set_epsilon(epsilon);
  1042. if (NULL != rng) {
  1043. checker.set_rng(0, rng).set_rng(1, rng).set_rng(2, rng).set_rng(3, rng);
  1044. }
  1045. for (auto&& arg : args) {
  1046. checker.set_param(arg.param).execs(
  1047. {arg.src, arg.filter, arg.bias, {}, {}});
  1048. }
  1049. }
  1050. void checker_conv_bias_mul_int8x8x32(std::vector<conv_bias::TestArg> args,
  1051. Handle* handle, const char* algo_name) {
  1052. using namespace conv_bias;
  1053. float epsilon = 0.001;
  1054. #if MEGDNN_ARMV7
  1055. epsilon = 1.0;
  1056. #endif
  1057. Checker<ConvBias> checker(handle);
  1058. checker.set_before_exec_callback(
  1059. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name));
  1060. checker.set_dtype(0, dtype::Int8());
  1061. checker.set_dtype(1, dtype::Int8());
  1062. checker.set_dtype(2, dtype::Int32());
  1063. checker.set_dtype(4, dtype::Int32());
  1064. checker.set_epsilon(epsilon);
  1065. for (auto&& arg : args) {
  1066. checker.set_param(arg.param).execs({arg.src, arg.filter, {}, {}, {}});
  1067. }
  1068. UniformIntRNG rng{-50, 50};
  1069. for (auto&& arg : args) {
  1070. checker.set_dtype(0, dtype::QuantizedS8(2.5f))
  1071. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1072. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1073. .set_dtype(4, dtype::QuantizedS32(6.25f))
  1074. .set_rng(0, &rng)
  1075. .set_rng(1, &rng)
  1076. .set_rng(2, &rng)
  1077. .set_param(arg.param)
  1078. .set_epsilon(epsilon)
  1079. .execs({arg.src, arg.filter, {}, {}, {}});
  1080. }
  1081. }
  1082. void checker_conv_bias_int8x8x32_preprocess(
  1083. std::vector<conv_bias::TestArg> args, Handle* handle,
  1084. const char* algo_name) {
  1085. using namespace conv_bias;
  1086. Checker<ConvBiasForward, OprWeightPreprocessProxy<ConvBiasForward>> checker(
  1087. handle);
  1088. checker.set_before_exec_callback(
  1089. conv_bias::ConvBiasAlgoChecker<ConvBias>(algo_name));
  1090. checker.set_dtype(0, dtype::Int8());
  1091. checker.set_dtype(1, dtype::Int8());
  1092. checker.set_dtype(2, dtype::Int32());
  1093. checker.set_dtype(4, dtype::Int32());
  1094. for (auto&& arg : args) {
  1095. checker.set_param(arg.param).execs({arg.src, arg.filter, {}, {}, {}});
  1096. }
  1097. UniformIntRNG rng{-50, 50};
  1098. for (auto&& arg : args) {
  1099. checker.set_dtype(0, dtype::QuantizedS8(2.5f))
  1100. .set_dtype(1, dtype::QuantizedS8(2.5f))
  1101. .set_dtype(2, dtype::QuantizedS32(6.25f))
  1102. .set_dtype(4, dtype::QuantizedS32(6.25f))
  1103. .set_rng(0, &rng)
  1104. .set_rng(1, &rng)
  1105. .set_rng(2, &rng)
  1106. .set_param(arg.param)
  1107. .execs({arg.src, arg.filter, {}, {}, {}});
  1108. }
  1109. }
  1110. std::vector<conv_bias::TestArg> get_nchw44_conv_bias_args(
  1111. std::vector<size_t> kernel_vec,
  1112. std::vector<param::ConvBias::NonlineMode> nlmode_vec,
  1113. std::vector<megdnn::BiasMode> biasmode_vec, size_t stride, bool no_pad,
  1114. bool is_input_nchw, bool is_nchw44_dot) {
  1115. using namespace conv_bias;
  1116. using NLMode = param::ConvBias::NonlineMode;
  1117. std::vector<TestArg> args;
  1118. MEGDNN_MARK_USED_VAR(no_pad);
  1119. auto pack = [&](size_t n, size_t oc, size_t ic, size_t h, size_t w,
  1120. size_t kernel, size_t stride, size_t group, NLMode nlmode,
  1121. megdnn::BiasMode bias_mode, int any_pad = -1) {
  1122. constexpr int pack_c = 4;
  1123. const size_t pad = any_pad >= 0 ? any_pad : kernel / 2;
  1124. auto oc_per_group = oc / group;
  1125. auto ic_per_group = ic / group;
  1126. bool ok_group = (oc % group == 0 && ic % group == 0) &&
  1127. oc_per_group % pack_c == 0 && oc_per_group > 0 &&
  1128. ic_per_group > 0;
  1129. bool nchw_disable = group > 1 || ic_per_group >= 4;
  1130. bool nchw44_disable = ic_per_group % pack_c != 0;
  1131. bool invalid_pad = (w + 2 * pad < kernel) || (h + 2 * pad < kernel);
  1132. if (!(ok_group) || invalid_pad) {
  1133. return;
  1134. }
  1135. if ((is_input_nchw && nchw_disable) ||
  1136. (!is_input_nchw && nchw44_disable)) {
  1137. return;
  1138. }
  1139. size_t kernel_h = kernel;
  1140. size_t kernel_w = kernel;
  1141. param::ConvBias param;
  1142. if (!is_nchw44_dot) {
  1143. param.format = param::ConvBias::Format::NCHW44;
  1144. } else {
  1145. param.format = param::ConvBias::Format::NCHW44_DOT;
  1146. }
  1147. param.stride_h = stride;
  1148. param.stride_w = stride;
  1149. param.pad_h = pad;
  1150. param.pad_w = pad;
  1151. param.nonlineMode = nlmode;
  1152. auto src_tensor_shape = TensorShape{n, ic / pack_c, h, w, pack_c};
  1153. auto weight_tensor_shape = TensorShape{
  1154. oc / pack_c, ic / pack_c, kernel_h, kernel_w, pack_c, pack_c};
  1155. auto bias_tensor_shape = TensorShape{};
  1156. if (bias_mode == megdnn::BiasMode::BROADCAST_CHANNEL_BIAS) {
  1157. bias_tensor_shape = {1, oc / pack_c, 1, 1, pack_c};
  1158. } else if (bias_mode == megdnn::BiasMode::BIAS) {
  1159. bias_tensor_shape = {n, oc / pack_c,
  1160. (h + 2 * pad - kernel) / stride + 1,
  1161. (w + 2 * pad - kernel) / stride + 1, pack_c};
  1162. }
  1163. if (group == 1) {
  1164. param.sparse = param::ConvBias::Sparse::DENSE;
  1165. } else if (group > 1 && ic / group == 1 && oc / group == 1) {
  1166. megdnn_assert(0, "not support channel wise");
  1167. param.sparse = param::ConvBias::Sparse::GROUP;
  1168. weight_tensor_shape = TensorShape{group / pack_c, 1, 1,
  1169. kernel_h, kernel_w, pack_c};
  1170. } else if (group > 1 && oc_per_group % pack_c == 0 && oc / group > 0 &&
  1171. ic_per_group % pack_c == 0 && ic / group > 0) {
  1172. param.sparse = param::ConvBias::Sparse::GROUP;
  1173. weight_tensor_shape = TensorShape{group,
  1174. oc_per_group / pack_c,
  1175. ic_per_group / pack_c,
  1176. kernel_h,
  1177. kernel_w,
  1178. pack_c,
  1179. pack_c};
  1180. }
  1181. if (is_input_nchw) {
  1182. src_tensor_shape = TensorShape{n, ic, h, w};
  1183. weight_tensor_shape =
  1184. TensorShape{oc / pack_c, kernel_h, kernel_w, ic, pack_c};
  1185. }
  1186. args.emplace_back(param, src_tensor_shape, weight_tensor_shape,
  1187. bias_tensor_shape);
  1188. };
  1189. for (auto bias : biasmode_vec)
  1190. for (auto nlmode : nlmode_vec)
  1191. for (size_t n : {1, 2})
  1192. for (size_t kernel : kernel_vec)
  1193. for (size_t oc : {4, 12})
  1194. for (size_t ic : {1, 3, 4, 12})
  1195. for (size_t h : {1, 3, 12})
  1196. for (size_t w : {1, 16, 23}) {
  1197. for (size_t group = 1;
  1198. group <=
  1199. std::min(std::min(oc, ic), 4_z);
  1200. ++group) {
  1201. if (kernel != 1 && (h == 1 || w == 1)) {
  1202. continue;
  1203. }
  1204. pack(n, oc, ic, h, w, kernel, stride,
  1205. group, nlmode, bias);
  1206. }
  1207. }
  1208. return args;
  1209. }
  1210. } // namespace conv_bias
  1211. } // namespace test
  1212. } // namespace megdnn
  1213. // vim: syntax=cpp.doxygen

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