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.

local_share.cpp 45 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. /**
  2. * \file dnn/test/cuda/local_share.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 implied.
  10. */
  11. #include "megdnn/oprs/nn.h"
  12. #include "src/common/utils.h"
  13. #include "test/common/checker.h"
  14. #include "test/common/convolution.h"
  15. #include "test/common/tensor.h"
  16. #include "test/common/workspace_wrapper.h"
  17. #include "test/cuda/benchmark.h"
  18. #include "test/cuda/fixture.h"
  19. #include "test/cuda/utils.h"
  20. using namespace megdnn;
  21. using namespace test;
  22. namespace {
  23. struct LocalShareArgs {
  24. size_t b, c, f, p, s, h, w, sg;
  25. };
  26. std::vector<LocalShareArgs> get_local_share_conv_1x1_args_lar_bs() {
  27. std::vector<LocalShareArgs> ret;
  28. // clang-format off
  29. for (size_t b : {32, 64}) {
  30. for (size_t c : {32, 16, 8}) {
  31. for (size_t f : {1}) {
  32. for (int p : {0}) {
  33. for (size_t s : {1, 2}) {
  34. for (size_t h : {8, 16}) {
  35. for (size_t w : {2, 3, 4, 5, 6, 7, 8, 9, 10, 16, 24, 32, 33}) {
  36. for (size_t sg : {3, 2}) {
  37. size_t ho = infer_conv_shape(h, f, s, p);
  38. size_t wo = infer_conv_shape(w, f, s, p);
  39. if (ho % sg != 0 || wo % sg != 0)
  40. continue;
  41. ret.emplace_back(LocalShareArgs{b, c, f, static_cast<size_t>(p),
  42. s, h, w, sg});
  43. } } } } } } } }
  44. // clang-format on
  45. return ret;
  46. }
  47. std::vector<LocalShareArgs> get_local_share_conv_3x3_args_lar_bs() {
  48. std::vector<LocalShareArgs> ret;
  49. // clang-format off
  50. for (size_t b : {32, 64}) {
  51. for (size_t c : {32, 16, 8}) {
  52. for (size_t f : {3}) {
  53. for (int p : {static_cast<int>(f / 2), 0}) {
  54. for (size_t s : {1, 2}) {
  55. for (size_t h : {8, 16}) {
  56. for (size_t w : {3, 4, 5, 6, 7, 8, 9, 10, 16, 24, 32, 33}) {
  57. for (size_t sg : {3, 2}) {
  58. size_t ho = infer_conv_shape(h, f, s, p);
  59. size_t wo = infer_conv_shape(w, f, s, p);
  60. if (ho % sg != 0 || wo % sg != 0)
  61. continue;
  62. ret.emplace_back(LocalShareArgs{b, c, f, static_cast<size_t>(p),
  63. s, h, w, sg});
  64. } } } } } } } }
  65. // clang-format on
  66. return ret;
  67. }
  68. std::vector<LocalShareArgs> get_local_share_conv_5x5_args_lar_bs() {
  69. std::vector<LocalShareArgs> ret;
  70. // clang-format off
  71. for (size_t b : {32, 64}) {
  72. for (size_t c : {32, 16, 8}) {
  73. for (size_t f : {5}) {
  74. for (int p : {static_cast<int>(f / 2), 0}) {
  75. for (size_t s : {1, 2}) {
  76. for (size_t h : {8, 16}) {
  77. for (size_t w : {8, 9, 10, 16, 24, 32, 33}) {
  78. for (size_t sg : {3, 2}) {
  79. size_t ho = infer_conv_shape(h, f, s, p);
  80. size_t wo = infer_conv_shape(w, f, s, p);
  81. if (ho % sg != 0 || wo % sg != 0)
  82. continue;
  83. ret.emplace_back(LocalShareArgs{b, c, f, static_cast<size_t>(p), s,
  84. h, w, sg});
  85. } } } } } } } }
  86. // clang-format on
  87. return ret;
  88. }
  89. std::vector<LocalShareArgs> get_local_share_conv_7x7_args_lar_bs() {
  90. std::vector<LocalShareArgs> ret;
  91. // clang-format off
  92. for (size_t b : {32, 64}) {
  93. for (size_t c : {32, 16, 8}) {
  94. for (size_t f : {7}) {
  95. for (int p : {static_cast<int>(f / 2), 0}) {
  96. for (size_t s : {1, 2}) {
  97. for (size_t h : {8, 16}) {
  98. for (size_t w : {8, 9, 10, 16, 24, 32, 33}) {
  99. for (size_t sg : {3, 2}) {
  100. size_t ho = infer_conv_shape(h, f, s, p);
  101. size_t wo = infer_conv_shape(w, f, s, p);
  102. if (ho % sg != 0 || wo % sg != 0)
  103. continue;
  104. ret.emplace_back(LocalShareArgs{b, c, f, static_cast<size_t>(p), s,
  105. h, w, sg});
  106. } } } } } } } }
  107. // clang-format on
  108. return ret;
  109. }
  110. std::vector<LocalShareArgs> get_local_share_conv_small_image(size_t kernel_size) {
  111. size_t f = kernel_size;
  112. std::vector<LocalShareArgs> ret;
  113. // clang-format off
  114. for (size_t b : {8, 16, 32, 48, 64}) {
  115. for (size_t c : {8, 16, 32, 48, 64, 96, 128}) {
  116. for (int p : {static_cast<int>(f / 2), 0}) {
  117. for (size_t s : {1, 2}) {
  118. for (size_t h : {12}) {
  119. for (size_t w : {12}) {
  120. for (size_t sg : {3, 2}) {
  121. size_t ho = infer_conv_shape(h, f, s, p);
  122. size_t wo = infer_conv_shape(w, f, s, p);
  123. if (ho % sg != 0 || wo % sg != 0)
  124. continue;
  125. ret.emplace_back(LocalShareArgs{b, c, f, static_cast<size_t>(p), s,
  126. h, w, sg});
  127. } } } } } } }
  128. // clang-format on
  129. return ret;
  130. }
  131. std::vector<LocalShareArgs> get_local_share_conv_small_image() {
  132. std::vector<LocalShareArgs> ret = get_local_share_conv_small_image(3);
  133. auto ret1 = get_local_share_conv_small_image(5);
  134. auto ret2 = get_local_share_conv_small_image(7);
  135. ret.insert(ret.begin(), ret1.begin(), ret1.end());
  136. ret.insert(ret.begin(), ret2.begin(), ret2.end());
  137. return ret;
  138. }
  139. void test_local_share_bwd_data_implicit_gemm(size_t kernel_size, Handle* handle) {
  140. Checker<LocalShareBackwardData> checker(handle);
  141. bool require_algo = false;
  142. checker.set_before_exec_callback(AlgoChecker<LocalShareBackwardData>(
  143. "LOCAL_SHARE_IMPLICIT_GEMM", &require_algo));
  144. using Param = LocalShare::Param;
  145. ConstValue const_0{0};
  146. auto args = get_local_share_conv_small_image(kernel_size);
  147. for (auto&& arg : args) {
  148. static_cast<void>(arg);
  149. size_t b = arg.b, c = arg.c, f = arg.f, p = arg.p, s = arg.s, h = arg.h,
  150. w = arg.w, sg = arg.sg;
  151. size_t ho = infer_conv_shape(h, f, s, p), wo = infer_conv_shape(w, f, s, p);
  152. Param param;
  153. param.stride_h = param.stride_w = s;
  154. param.pad_h = param.pad_w = p;
  155. param.spatial_groups_h = param.spatial_groups_w = sg;
  156. checker.set_param(param);
  157. checker.set_rng(2, &const_0);
  158. TensorShape diff{b, c, ho, wo}, filter{sg, sg, 4, f, f, c}, grad{b, 4, h, w};
  159. checker.execs({filter, diff, grad});
  160. diff = TensorShape{b, c, ho, wo}, filter = TensorShape{sg, sg, 8, f, f, c};
  161. grad = {b, 8, h, w};
  162. checker.exec({filter, diff, grad});
  163. }
  164. }
  165. } // namespace
  166. TEST_F(CUDA, LOCAL_SHARE_FORWARD_1x1_LAR_BS) {
  167. require_compute_capability(6, 0);
  168. Checker<LocalShare> checker(handle_cuda());
  169. bool require_algo = false;
  170. checker.set_before_exec_callback(AlgoChecker<LocalShare>(
  171. "LOCAL_SHARE_CHWN_BATCH_SIZE_AWARE", &require_algo));
  172. using Param = LocalShare::Param;
  173. auto args = get_local_share_conv_1x1_args_lar_bs();
  174. for (auto&& arg : args) {
  175. size_t b = arg.b, c = arg.c, f = arg.f, p = arg.p, s = arg.s, h = arg.h,
  176. w = arg.w, sg = arg.sg;
  177. Param param;
  178. param.stride_h = param.stride_w = s;
  179. param.pad_h = param.pad_w = p;
  180. param.spatial_groups_h = param.spatial_groups_w = sg;
  181. checker.set_param(param);
  182. TensorShape src{b, 4, h, w}, filter{sg, sg, 4, f, f, c};
  183. checker.execs({src, filter, {}});
  184. src = TensorShape{b, 8, h, w}, filter = TensorShape{sg, sg, 8, f, f, c};
  185. checker.exec({src, filter, {}});
  186. }
  187. }
  188. TEST_F(CUDA, LOCAL_SHARE_FORWARD_3x3_LAR_BS) {
  189. require_compute_capability(6, 0);
  190. Checker<LocalShare> checker(handle_cuda());
  191. bool require_algo = false;
  192. checker.set_before_exec_callback(AlgoChecker<LocalShare>(
  193. "LOCAL_SHARE_CHWN_BATCH_SIZE_AWARE", &require_algo));
  194. using Param = LocalShare::Param;
  195. auto args = get_local_share_conv_3x3_args_lar_bs();
  196. ConstValue const_1{1};
  197. for (auto&& arg : args) {
  198. size_t b = arg.b, c = arg.c, f = arg.f, p = arg.p, s = arg.s, h = arg.h,
  199. w = arg.w, sg = arg.sg;
  200. Param param;
  201. param.stride_h = param.stride_w = s;
  202. param.pad_h = param.pad_w = p;
  203. param.spatial_groups_h = param.spatial_groups_w = sg;
  204. checker.set_param(param);
  205. TensorShape src{b, 4, h, w}, filter{sg, sg, 4, f, f, c};
  206. checker.execs({src, filter, {}});
  207. src = TensorShape{b, 8, h, w}, filter = TensorShape{sg, sg, 8, f, f, c};
  208. checker.exec({src, filter, {}});
  209. }
  210. }
  211. TEST_F(CUDA, LOCAL_SHARE_FORWARD_5x5_LAR_BS) {
  212. require_compute_capability(6, 0);
  213. Checker<LocalShare> checker(handle_cuda());
  214. bool require_algo = false;
  215. checker.set_before_exec_callback(AlgoChecker<LocalShare>(
  216. "LOCAL_SHARE_CHWN_BATCH_SIZE_AWARE", &require_algo));
  217. using Param = LocalShare::Param;
  218. auto args = get_local_share_conv_5x5_args_lar_bs();
  219. for (auto&& arg : args) {
  220. size_t b = arg.b, c = arg.c, f = arg.f, p = arg.p, s = arg.s, h = arg.h,
  221. w = arg.w, sg = arg.sg;
  222. Param param;
  223. param.stride_h = param.stride_w = s;
  224. param.pad_h = param.pad_w = p;
  225. param.spatial_groups_h = param.spatial_groups_w = sg;
  226. checker.set_param(param);
  227. TensorShape src{b, 4, h, w}, filter{sg, sg, 4, f, f, c};
  228. checker.execs({src, filter, {}});
  229. src = TensorShape{b, 8, h, w}, filter = TensorShape{sg, sg, 8, f, f, c};
  230. checker.exec({src, filter, {}});
  231. }
  232. }
  233. TEST_F(CUDA, LOCAL_SHARE_FORWARD_7x7_LAR_BS) {
  234. require_compute_capability(6, 0);
  235. Checker<LocalShare> checker(handle_cuda());
  236. bool require_algo = false;
  237. checker.set_before_exec_callback(AlgoChecker<LocalShare>(
  238. "LOCAL_SHARE_CHWN_BATCH_SIZE_AWARE", &require_algo));
  239. using Param = LocalShare::Param;
  240. auto args = get_local_share_conv_7x7_args_lar_bs();
  241. for (auto&& arg : args) {
  242. size_t b = arg.b, c = arg.c, f = arg.f, p = arg.p, s = arg.s, h = arg.h,
  243. w = arg.w, sg = arg.sg;
  244. Param param;
  245. param.stride_h = param.stride_w = s;
  246. param.pad_h = param.pad_w = p;
  247. param.spatial_groups_h = param.spatial_groups_w = sg;
  248. checker.set_param(param);
  249. TensorShape src{b, 4, h, w}, filter{sg, sg, 4, f, f, c};
  250. checker.execs({src, filter, {}});
  251. src = TensorShape{b, 8, h, w}, filter = TensorShape{sg, sg, 8, f, f, c};
  252. checker.exec({src, filter, {}});
  253. }
  254. }
  255. TEST_F(CUDA, LOCAL_SHARE_BATCHED_MATMUL) {
  256. Checker<LocalShare> checker(handle_cuda());
  257. bool require_algo = false;
  258. checker.set_before_exec_callback(
  259. AlgoChecker<LocalShare>("LOCAL_SHARE_BATCHED_MATMUL", &require_algo));
  260. using Param = LocalShare::Param;
  261. auto args = convolution::get_args();
  262. for (size_t sg : {2, 3}) {
  263. for (auto&& arg : args) {
  264. if (arg.param.sparse != LocalShare::Param::Sparse::DENSE)
  265. continue;
  266. if (arg.param.format != LocalShare::Param::Format::NCHW)
  267. continue;
  268. if (arg.param.dilate_h != 1 || arg.param.dilate_w != 1)
  269. continue;
  270. Param param;
  271. param.sparse = arg.param.sparse;
  272. param.stride_h = arg.param.stride_h, param.stride_w = arg.param.stride_w;
  273. param.pad_h = arg.param.pad_h, param.pad_w = arg.param.pad_w;
  274. param.dilate_h = arg.param.dilate_h, param.dilate_w = arg.param.dilate_w;
  275. param.spatial_groups_h = param.spatial_groups_w = sg;
  276. size_t ho = infer_conv_shape(
  277. arg.src[2], arg.filter[2], param.stride_h, param.pad_h);
  278. size_t wo = infer_conv_shape(
  279. arg.src[3], arg.filter[3], param.stride_w, param.pad_w);
  280. if (ho % sg != 0 || wo % sg != 0)
  281. continue;
  282. TensorShape filter{
  283. sg, sg, arg.filter[1], arg.filter[2], arg.filter[3], arg.filter[0]};
  284. checker.set_param(param);
  285. checker.exec({arg.src, filter, {}});
  286. }
  287. }
  288. }
  289. TEST_F(CUDA, GROUP_LOCAL_SHARE_BATCHED_MATMUL) {
  290. Checker<LocalShare> checker(handle_cuda());
  291. bool require_algo = false;
  292. checker.set_before_exec_callback(
  293. AlgoChecker<LocalShare>("LOCAL_SHARE_BATCHED_MATMUL", &require_algo));
  294. using Param = LocalShare::Param;
  295. auto args = convolution::get_args();
  296. for (size_t sg : {2, 3}) {
  297. for (auto&& arg : args) {
  298. if (arg.param.sparse != LocalShare::Param::Sparse::DENSE)
  299. continue;
  300. if (arg.param.format != LocalShare::Param::Format::NCHW)
  301. continue;
  302. if (arg.param.dilate_h != 1 || arg.param.dilate_w != 1)
  303. continue;
  304. if (arg.filter.ndim != 4)
  305. continue;
  306. Param param;
  307. param.sparse = Param::Sparse::GROUP;
  308. param.stride_h = arg.param.stride_h, param.stride_w = arg.param.stride_w;
  309. param.pad_h = arg.param.pad_h, param.pad_w = arg.param.pad_w;
  310. param.dilate_h = arg.param.dilate_h, param.dilate_w = arg.param.dilate_w;
  311. param.spatial_groups_h = param.spatial_groups_w = sg;
  312. size_t ho = infer_conv_shape(
  313. arg.src[2], arg.filter[2], param.stride_h, param.pad_h);
  314. size_t wo = infer_conv_shape(
  315. arg.src[3], arg.filter[3], param.stride_w, param.pad_w);
  316. if (ho % sg != 0 || wo % sg != 0)
  317. continue;
  318. size_t nr_groups = 3;
  319. TensorShape filter{
  320. nr_groups, sg, sg, arg.filter[1], arg.filter[2],
  321. arg.filter[3], arg.filter[0]};
  322. TensorShape src{arg.src[0], arg.src[1] * nr_groups, arg.src[2], arg.src[3]};
  323. checker.set_param(param);
  324. checker.exec({src, filter, {}});
  325. }
  326. }
  327. }
  328. TEST_F(CUDA, LOCAL_SHARE_FORWARD_SMALL_IMAGE_GENERAL) {
  329. require_compute_capability(6, 0);
  330. Checker<LocalShare> checker(handle_cuda());
  331. bool require_algo = false;
  332. checker.set_before_exec_callback(AlgoChecker<LocalShare>(
  333. "LOCAL_SHARE_CHWN_BATCH_SIZE_AWARE_SMALL_IMAGE", &require_algo));
  334. using Param = LocalShare::Param;
  335. auto args = convolution::get_args();
  336. for (size_t sg : {2, 3}) {
  337. for (auto&& arg : args) {
  338. if (arg.param.sparse != LocalShare::Param::Sparse::DENSE)
  339. continue;
  340. if (arg.param.format != LocalShare::Param::Format::NCHW)
  341. continue;
  342. if (arg.param.dilate_h != 1 || arg.param.dilate_w != 1)
  343. continue;
  344. Param param;
  345. param.stride_h = arg.param.stride_h, param.stride_w = arg.param.stride_w;
  346. param.pad_h = arg.param.pad_h, param.pad_w = arg.param.pad_w;
  347. param.dilate_h = arg.param.dilate_h, param.dilate_w = arg.param.dilate_w;
  348. param.spatial_groups_h = param.spatial_groups_w = sg;
  349. size_t ho = infer_conv_shape(
  350. arg.src[2], arg.filter[2], param.stride_h, param.pad_h);
  351. size_t wo = infer_conv_shape(
  352. arg.src[3], arg.filter[3], param.stride_w, param.pad_w);
  353. if (ho % sg != 0 || wo % sg != 0)
  354. continue;
  355. arg.filter[1] = arg.filter[1] + (4 - arg.filter[1] % 4);
  356. arg.src[1] = arg.filter[1];
  357. TensorShape filter{
  358. sg, sg, arg.filter[1], arg.filter[2], arg.filter[3], arg.filter[0]};
  359. checker.set_param(param);
  360. checker.exec({arg.src, filter, {}});
  361. }
  362. }
  363. }
  364. TEST_F(CUDA, LOCAL_SHARE_FORWARD_SMALL_IMAGE_SPECIAL) {
  365. require_compute_capability(6, 0);
  366. Checker<LocalShare> checker(handle_cuda());
  367. bool require_algo = false;
  368. checker.set_before_exec_callback(AlgoChecker<LocalShare>(
  369. "LOCAL_SHARE_CHWN_BATCH_SIZE_AWARE_SMALL_IMAGE", &require_algo));
  370. using Param = LocalShare::Param;
  371. auto args = get_local_share_conv_small_image();
  372. for (auto&& arg : args) {
  373. size_t b = arg.b, c = arg.c, f = arg.f, p = arg.p, s = arg.s, h = arg.h,
  374. w = arg.w, sg = arg.sg;
  375. Param param;
  376. param.stride_h = param.stride_w = s;
  377. param.pad_h = param.pad_w = p;
  378. param.spatial_groups_h = param.spatial_groups_w = sg;
  379. checker.set_param(param);
  380. TensorShape src{b, 4, h, w}, filter{sg, sg, 4, f, f, c};
  381. checker.execs({src, filter, {}});
  382. src = TensorShape{b, 8, h, w}, filter = TensorShape{sg, sg, 8, f, f, c};
  383. checker.exec({src, filter, {}});
  384. }
  385. }
  386. TEST_F(CUDA, LOCAL_SHARE_BWD_DATA_IMPLICIT_GEMM_GENERAL) {
  387. require_compute_capability(6, 0);
  388. Checker<LocalShareBackwardData> checker(handle_cuda());
  389. bool require_algo = false;
  390. checker.set_before_exec_callback(AlgoChecker<LocalShareBackwardData>(
  391. "LOCAL_SHARE_IMPLICIT_GEMM", &require_algo));
  392. using Param = LocalShare::Param;
  393. auto args = convolution::get_args();
  394. ConstValue const_0{0};
  395. for (size_t sg : {2, 3}) {
  396. for (auto&& arg : args) {
  397. if (arg.param.sparse != LocalShare::Param::Sparse::DENSE)
  398. continue;
  399. if (arg.param.format != LocalShare::Param::Format::NCHW)
  400. continue;
  401. if (arg.param.dilate_h != 1 || arg.param.dilate_w != 1)
  402. continue;
  403. Param param;
  404. param.stride_h = arg.param.stride_h, param.stride_w = arg.param.stride_w;
  405. param.pad_h = arg.param.pad_h, param.pad_w = arg.param.pad_w;
  406. param.dilate_h = arg.param.dilate_h, param.dilate_w = arg.param.dilate_w;
  407. param.spatial_groups_h = param.spatial_groups_w = sg;
  408. size_t ho = infer_conv_shape(
  409. arg.src[2], arg.filter[2], param.stride_h, param.pad_h);
  410. size_t wo = infer_conv_shape(
  411. arg.src[3], arg.filter[3], param.stride_w, param.pad_w);
  412. if (ho % sg != 0 || wo % sg != 0)
  413. continue;
  414. arg.filter[0] = arg.filter[0] + (4 - arg.filter[0] % 4);
  415. TensorShape filter{
  416. sg, sg, arg.filter[1], arg.filter[2], arg.filter[3], arg.filter[0]};
  417. TensorShape diff{arg.src[0], arg.filter[0], ho, wo};
  418. checker.set_param(param);
  419. checker.set_rng(2, &const_0);
  420. checker.exec({filter, diff, arg.src});
  421. }
  422. }
  423. }
  424. TEST_F(CUDA, LOCAL_SHARE_BWD_DATA_IMPLICIT_GEMM_SPECIAL_PART1) {
  425. require_compute_capability(6, 0);
  426. test_local_share_bwd_data_implicit_gemm(3, handle_cuda());
  427. }
  428. TEST_F(CUDA, LOCAL_SHARE_BWD_DATA_IMPLICIT_GEMM_SPECIAL_PART2) {
  429. require_compute_capability(6, 0);
  430. test_local_share_bwd_data_implicit_gemm(5, handle_cuda());
  431. }
  432. TEST_F(CUDA, LOCAL_SHARE_BWD_DATA_IMPLICIT_GEMM_SPECIAL_PART3) {
  433. require_compute_capability(6, 0);
  434. test_local_share_bwd_data_implicit_gemm(7, handle_cuda());
  435. }
  436. TEST_F(CUDA, LOCAL_SHARE_BWD_DATA_BATCHED_MATMUL) {
  437. Checker<LocalShareBackwardData> checker(handle_cuda());
  438. bool require_algo = false;
  439. checker.set_before_exec_callback(AlgoChecker<LocalShareBackwardData>(
  440. "LOCAL_SHARE_BATCHED_MATMUL", &require_algo));
  441. using Param = LocalShare::Param;
  442. auto args = convolution::get_args();
  443. ConstValue const_0{0};
  444. for (size_t sg : {2, 3}) {
  445. for (auto&& arg : args) {
  446. if (arg.param.sparse != LocalShare::Param::Sparse::DENSE)
  447. continue;
  448. if (arg.param.format != LocalShare::Param::Format::NCHW)
  449. continue;
  450. if (arg.param.dilate_h != 1 || arg.param.dilate_w != 1)
  451. continue;
  452. Param param;
  453. param.stride_h = arg.param.stride_h, param.stride_w = arg.param.stride_w;
  454. param.pad_h = arg.param.pad_h, param.pad_w = arg.param.pad_w;
  455. param.dilate_h = arg.param.dilate_h, param.dilate_w = arg.param.dilate_w;
  456. param.spatial_groups_h = param.spatial_groups_w = sg;
  457. size_t ho = infer_conv_shape(
  458. arg.src[2], arg.filter[2], param.stride_h, param.pad_h);
  459. size_t wo = infer_conv_shape(
  460. arg.src[3], arg.filter[3], param.stride_w, param.pad_w);
  461. if (ho % sg != 0 || wo % sg != 0)
  462. continue;
  463. TensorShape filter{
  464. sg, sg, arg.filter[1], arg.filter[2], arg.filter[3], arg.filter[0]};
  465. TensorShape diff{arg.src[0], arg.filter[0], ho, wo};
  466. checker.set_rng(2, &const_0);
  467. checker.set_param(param);
  468. checker.exec({filter, diff, arg.src});
  469. }
  470. }
  471. }
  472. TEST_F(CUDA, GROUP_LOCAL_SHARE_BWD_DATA_BATCHED_MATMUL) {
  473. Checker<LocalShareBackwardData> checker(handle_cuda());
  474. bool require_algo = false;
  475. checker.set_before_exec_callback(AlgoChecker<LocalShareBackwardData>(
  476. "LOCAL_SHARE_BATCHED_MATMUL", &require_algo));
  477. using Param = LocalShare::Param;
  478. auto args = convolution::get_args();
  479. ConstValue const_0{0};
  480. for (size_t sg : {2, 3}) {
  481. for (auto&& arg : args) {
  482. if (arg.param.sparse != LocalShare::Param::Sparse::DENSE)
  483. continue;
  484. if (arg.param.format != LocalShare::Param::Format::NCHW)
  485. continue;
  486. if (arg.param.dilate_h != 1 || arg.param.dilate_w != 1)
  487. continue;
  488. Param param;
  489. param.sparse = Param::Sparse::GROUP;
  490. param.stride_h = arg.param.stride_h, param.stride_w = arg.param.stride_w;
  491. param.pad_h = arg.param.pad_h, param.pad_w = arg.param.pad_w;
  492. param.dilate_h = arg.param.dilate_h, param.dilate_w = arg.param.dilate_w;
  493. param.spatial_groups_h = param.spatial_groups_w = sg;
  494. size_t ho = infer_conv_shape(
  495. arg.src[2], arg.filter[2], param.stride_h, param.pad_h);
  496. size_t wo = infer_conv_shape(
  497. arg.src[3], arg.filter[3], param.stride_w, param.pad_w);
  498. if (ho % sg != 0 || wo % sg != 0)
  499. continue;
  500. size_t nr_groups = 3;
  501. TensorShape filter{
  502. nr_groups, sg, sg, arg.filter[1], arg.filter[2],
  503. arg.filter[3], arg.filter[0]};
  504. TensorShape diff{arg.src[0], arg.filter[0] * nr_groups, ho, wo};
  505. TensorShape grad{
  506. arg.src[0], arg.src[1] * nr_groups, arg.src[2], arg.src[3]};
  507. checker.set_rng(2, &const_0);
  508. checker.set_param(param);
  509. checker.exec({filter, diff, grad});
  510. }
  511. }
  512. }
  513. TEST_F(CUDA, LOCAL_SHARE_BWD_FILTER_IMPLICIT_GEMM_GENERAL) {
  514. require_compute_capability(6, 0);
  515. Checker<LocalShareBackwardFilter> checker(handle_cuda());
  516. bool require_algo = false;
  517. checker.set_before_exec_callback(AlgoChecker<LocalShareBackwardFilter>(
  518. "LOCAL_SHARE_IMPLICIT_GEMM", &require_algo));
  519. using Param = LocalShare::Param;
  520. auto args = convolution::get_args();
  521. ConstValue const_0{0};
  522. for (size_t sg : {2, 3}) {
  523. for (auto&& arg : args) {
  524. if (arg.param.sparse != LocalShare::Param::Sparse::DENSE)
  525. continue;
  526. if (arg.param.format != LocalShare::Param::Format::NCHW)
  527. continue;
  528. if (arg.param.dilate_h != 1 || arg.param.dilate_w != 1)
  529. continue;
  530. Param param;
  531. param.stride_h = arg.param.stride_h, param.stride_w = arg.param.stride_w;
  532. param.pad_h = arg.param.pad_h, param.pad_w = arg.param.pad_w;
  533. param.dilate_h = arg.param.dilate_h, param.dilate_w = arg.param.dilate_w;
  534. param.spatial_groups_h = param.spatial_groups_w = sg;
  535. size_t ho = infer_conv_shape(
  536. arg.src[2], arg.filter[2], param.stride_h, param.pad_h);
  537. size_t wo = infer_conv_shape(
  538. arg.src[3], arg.filter[3], param.stride_w, param.pad_w);
  539. if (ho % sg != 0 || wo % sg != 0)
  540. continue;
  541. arg.src[0] = arg.src[0] + (4 - arg.src[0] % 4);
  542. TensorShape grad{
  543. sg, sg, arg.filter[1], arg.filter[2], arg.filter[3], arg.filter[0]};
  544. TensorShape diff{arg.src[0], arg.filter[0], ho, wo};
  545. checker.set_param(param);
  546. checker.set_rng(2, &const_0);
  547. checker.exec({arg.src, diff, grad});
  548. }
  549. }
  550. }
  551. TEST_F(CUDA, LOCAL_SHARE_BWD_FILTER_IMPLICIT_GEMM_SPECIAL) {
  552. require_compute_capability(6, 0);
  553. Checker<LocalShareBackwardFilter> checker(handle_cuda());
  554. bool require_algo = false;
  555. checker.set_before_exec_callback(AlgoChecker<LocalShareBackwardFilter>(
  556. "LOCAL_SHARE_IMPLICIT_GEMM", &require_algo));
  557. using Param = LocalShare::Param;
  558. ConstValue const_0{0};
  559. auto args = get_local_share_conv_small_image();
  560. for (auto&& arg : args) {
  561. static_cast<void>(arg);
  562. size_t b = arg.b, c = arg.c, f = arg.f, p = arg.p, s = arg.s, h = arg.h,
  563. w = arg.w, sg = arg.sg;
  564. size_t ho = infer_conv_shape(h, f, s, p), wo = infer_conv_shape(w, f, s, p);
  565. Param param;
  566. param.stride_h = param.stride_w = s;
  567. param.pad_h = param.pad_w = p;
  568. param.spatial_groups_h = param.spatial_groups_w = sg;
  569. checker.set_param(param);
  570. checker.set_rng(2, &const_0);
  571. TensorShape diff{b, c, ho, wo}, grad{sg, sg, 4, f, f, c}, src{b, 4, h, w};
  572. checker.execs({src, diff, grad});
  573. src = {b, 8, h, w};
  574. diff = TensorShape{b, c, ho, wo}, grad = TensorShape{sg, sg, 8, f, f, c};
  575. checker.exec({src, diff, grad});
  576. }
  577. }
  578. TEST_F(CUDA, LOCAL_SHARE_BWD_FILTER_BATCHED_MATMUL) {
  579. Checker<LocalShareBackwardFilter> checker(handle_cuda());
  580. bool require_algo = false;
  581. checker.set_before_exec_callback(AlgoChecker<LocalShareBackwardFilter>(
  582. "LOCAL_SHARE_BATCHED_MATMUL", &require_algo));
  583. using Param = LocalShare::Param;
  584. auto args = convolution::get_args();
  585. ConstValue const_0{0};
  586. for (size_t sg : {2, 3}) {
  587. for (auto&& arg : args) {
  588. if (arg.param.sparse != LocalShare::Param::Sparse::DENSE)
  589. continue;
  590. if (arg.param.format != LocalShare::Param::Format::NCHW)
  591. continue;
  592. if (arg.param.dilate_h != 1 || arg.param.dilate_w != 1)
  593. continue;
  594. Param param;
  595. param.stride_h = arg.param.stride_h, param.stride_w = arg.param.stride_w;
  596. param.pad_h = arg.param.pad_h, param.pad_w = arg.param.pad_w;
  597. param.dilate_h = arg.param.dilate_h, param.dilate_w = arg.param.dilate_w;
  598. param.spatial_groups_h = param.spatial_groups_w = sg;
  599. size_t ho = infer_conv_shape(
  600. arg.src[2], arg.filter[2], param.stride_h, param.pad_h);
  601. size_t wo = infer_conv_shape(
  602. arg.src[3], arg.filter[3], param.stride_w, param.pad_w);
  603. if (ho % sg != 0 || wo % sg != 0)
  604. continue;
  605. TensorShape grad{
  606. sg, sg, arg.filter[1], arg.filter[2], arg.filter[3], arg.filter[0]};
  607. TensorShape diff{arg.src[0], arg.filter[0], ho, wo};
  608. checker.set_rng(2, &const_0);
  609. checker.set_param(param);
  610. checker.exec({arg.src, diff, grad});
  611. }
  612. }
  613. }
  614. TEST_F(CUDA, GROUP_LOCAL_SHARE_BWD_FILTER_BATCHED_MATMUL) {
  615. Checker<LocalShareBackwardFilter> checker(handle_cuda());
  616. bool require_algo = false;
  617. checker.set_before_exec_callback(AlgoChecker<LocalShareBackwardFilter>(
  618. "LOCAL_SHARE_BATCHED_MATMUL", &require_algo));
  619. using Param = LocalShare::Param;
  620. auto args = convolution::get_args();
  621. ConstValue const_0{0};
  622. for (size_t sg : {2, 3}) {
  623. for (auto&& arg : args) {
  624. if (arg.param.sparse != LocalShare::Param::Sparse::DENSE)
  625. continue;
  626. if (arg.param.format != LocalShare::Param::Format::NCHW)
  627. continue;
  628. if (arg.param.dilate_h != 1 || arg.param.dilate_w != 1)
  629. continue;
  630. Param param;
  631. param.sparse = Param::Sparse::GROUP;
  632. param.stride_h = arg.param.stride_h, param.stride_w = arg.param.stride_w;
  633. param.pad_h = arg.param.pad_h, param.pad_w = arg.param.pad_w;
  634. param.dilate_h = arg.param.dilate_h, param.dilate_w = arg.param.dilate_w;
  635. param.spatial_groups_h = param.spatial_groups_w = sg;
  636. size_t ho = infer_conv_shape(
  637. arg.src[2], arg.filter[2], param.stride_h, param.pad_h);
  638. size_t wo = infer_conv_shape(
  639. arg.src[3], arg.filter[3], param.stride_w, param.pad_w);
  640. if (ho % sg != 0 || wo % sg != 0)
  641. continue;
  642. size_t nr_groups = 3;
  643. TensorShape grad{
  644. nr_groups, sg, sg, arg.filter[1], arg.filter[2],
  645. arg.filter[3], arg.filter[0]};
  646. TensorShape diff{arg.src[0], arg.filter[0] * nr_groups, ho, wo};
  647. TensorShape src{arg.src[0], arg.src[1] * nr_groups, arg.src[2], arg.src[3]};
  648. checker.set_rng(2, &const_0);
  649. checker.set_param(param);
  650. checker.exec({src, diff, grad});
  651. }
  652. }
  653. }
  654. #if MEGDNN_WITH_BENCHMARK
  655. TEST_F(CUDA, BENCHMARK_LOCAL_SHARE_BWD_FILTER) {
  656. CUBenchmarker<LocalShareBackwardFilter> bencher(handle_cuda());
  657. size_t RUNS = 1000;
  658. bencher.set_display(false).set_times(RUNS);
  659. std::unique_ptr<OprProxy<LocalShareBackwardFilter>> proxy{
  660. new OprProxy<LocalShareBackwardFilter>{true}};
  661. bencher.set_proxy(proxy);
  662. LocalShare::Param param;
  663. NormalRNG rng;
  664. auto run = [&](size_t batch, size_t ic, size_t ih, size_t iw, size_t oc, size_t f,
  665. size_t s, size_t sg) {
  666. param.pad_h = f / 2;
  667. param.pad_w = f / 2;
  668. param.stride_h = s;
  669. param.stride_w = s;
  670. param.spatial_groups_h = sg;
  671. param.spatial_groups_w = sg;
  672. TensorShape src = {batch, ic, ih, iw}, grad = {sg, sg, ic, f, f, oc};
  673. size_t ho = infer_conv_shape(ih, f, s, f / 2);
  674. size_t wo = infer_conv_shape(iw, f, s, f / 2);
  675. TensorShape diff = {batch, oc, ho, wo};
  676. float flo = 2.0 * batch * oc * ho * wo * ic * f * f / (1e12);
  677. bencher.set_param(param)
  678. .set_dtype(0, dtype::Float32())
  679. .set_dtype(1, dtype::Float32())
  680. .set_dtype(2, dtype::Float32())
  681. .set_rng(0, &rng)
  682. .set_rng(1, &rng);
  683. bencher.proxy()->target_execution_policy.algo.reset();
  684. auto time_in_ms = bencher.execs({src, diff, grad}) / RUNS;
  685. printf("src=%s, diff=%s, grad=%s, float32: %.2fms "
  686. "%.2fTFlops\n",
  687. src.to_string().c_str(), diff.to_string().c_str(),
  688. grad.to_string().c_str(), time_in_ms, (flo / (time_in_ms * 1e-3)));
  689. };
  690. // stride = 1
  691. run(32, 128, 24, 24, 128, 1, 1, 3);
  692. run(32, 256, 12, 12, 256, 1, 1, 3);
  693. // stride = 2
  694. run(32, 256, 12, 12, 512, 1, 2, 3);
  695. run(32, 512, 6, 6, 1024, 1, 2, 3);
  696. // stride = 1
  697. run(32, 128, 24, 24, 128, 3, 1, 3);
  698. run(32, 256, 12, 12, 256, 3, 1, 3);
  699. // stride = 2
  700. run(32, 128, 24, 24, 256, 3, 2, 3);
  701. run(32, 256, 12, 12, 512, 3, 2, 3);
  702. // stride = 1
  703. run(64, 128, 24, 24, 128, 1, 1, 3);
  704. run(64, 256, 12, 12, 256, 1, 1, 3);
  705. // stride = 2
  706. run(64, 256, 12, 12, 512, 1, 2, 3);
  707. run(64, 512, 6, 6, 1024, 1, 2, 3);
  708. // stride = 1
  709. run(64, 128, 24, 24, 128, 3, 1, 3);
  710. run(64, 256, 12, 12, 256, 3, 1, 3);
  711. // stride = 2
  712. run(64, 128, 24, 24, 256, 3, 2, 3);
  713. run(64, 256, 12, 12, 512, 3, 2, 3);
  714. }
  715. TEST_F(CUDA, BENCHMARK_GROUP_LOCAL_SHARE_FORWARD) {
  716. CUBenchmarker<LocalShare> bencher(handle_cuda());
  717. size_t RUNS = 1000;
  718. bencher.set_display(false).set_times(RUNS);
  719. std::unique_ptr<OprProxy<LocalShareForward>> proxy{
  720. new OprProxy<LocalShareForward>{true}};
  721. bencher.set_proxy(proxy);
  722. LocalShare::Param param;
  723. NormalRNG rng;
  724. auto run = [&](size_t batch, size_t ic, size_t ih, size_t iw, size_t oc, size_t f,
  725. size_t s, size_t sg) {
  726. param.pad_h = f / 2;
  727. param.pad_w = f / 2;
  728. param.stride_h = s;
  729. param.stride_w = s;
  730. param.spatial_groups_h = sg;
  731. param.spatial_groups_w = sg;
  732. param.sparse = LocalShare::Param::Sparse::GROUP;
  733. TensorShape src = {1, batch * ic, ih, iw},
  734. filter = {batch, sg, sg, ic, f, f, oc};
  735. size_t ho = infer_conv_shape(ih, f, s, f / 2);
  736. size_t wo = infer_conv_shape(iw, f, s, f / 2);
  737. float flo = 2.0 * batch * oc * ho * wo * ic * f * f / (1e12);
  738. bencher.set_param(param)
  739. .set_dtype(0, dtype::Float32())
  740. .set_dtype(1, dtype::Float32())
  741. .set_dtype(2, dtype::Float32())
  742. .set_rng(0, &rng)
  743. .set_rng(1, &rng);
  744. bencher.proxy()->target_execution_policy.algo.reset();
  745. auto time_in_ms = bencher.execs({src, filter, {}}) / RUNS;
  746. ;
  747. printf("src=%s, filter=%s, float32: %.2fms %.2fTFlops\n",
  748. src.to_string().c_str(), filter.to_string().c_str(), time_in_ms,
  749. (flo / (time_in_ms * 1e-3)));
  750. };
  751. // stride = 1
  752. run(32, 128, 24, 24, 128, 1, 1, 3);
  753. run(32, 256, 12, 12, 256, 1, 1, 3);
  754. // stride = 2
  755. run(32, 256, 12, 12, 512, 1, 2, 3);
  756. run(32, 512, 6, 6, 1024, 1, 2, 3);
  757. // stride = 1
  758. run(64, 128, 24, 24, 128, 1, 1, 3);
  759. run(64, 256, 12, 12, 256, 1, 1, 3);
  760. // stride = 2
  761. run(64, 256, 12, 12, 512, 1, 2, 3);
  762. run(64, 512, 6, 6, 1024, 1, 2, 3);
  763. }
  764. TEST_F(CUDA, BENCHMARK_LOCAL_SHARE_BWD_DATA) {
  765. CUBenchmarker<LocalShareBackwardData> bencher(handle_cuda());
  766. size_t RUNS = 1000;
  767. bencher.set_display(false).set_times(RUNS);
  768. std::unique_ptr<OprProxy<LocalShareBackwardData>> proxy{
  769. new OprProxy<LocalShareBackwardData>{true}};
  770. bencher.set_proxy(proxy);
  771. LocalShare::Param param;
  772. NormalRNG rng;
  773. auto run = [&](size_t batch, size_t ic, size_t ih, size_t iw, size_t oc, size_t f,
  774. size_t s, size_t sg) {
  775. param.pad_h = f / 2;
  776. param.pad_w = f / 2;
  777. param.stride_h = s;
  778. param.stride_w = s;
  779. param.spatial_groups_h = sg;
  780. param.spatial_groups_w = sg;
  781. TensorShape grad = {batch, ic, ih, iw}, filter = {sg, sg, ic, f, f, oc};
  782. size_t ho = infer_conv_shape(ih, f, s, f / 2);
  783. size_t wo = infer_conv_shape(iw, f, s, f / 2);
  784. TensorShape diff = {batch, oc, ho, wo};
  785. float flo = 2.0 * batch * oc * ho * wo * ic * f * f / (1e12);
  786. bencher.set_param(param)
  787. .set_dtype(0, dtype::Float32())
  788. .set_dtype(1, dtype::Float32())
  789. .set_dtype(2, dtype::Float32())
  790. .set_rng(0, &rng)
  791. .set_rng(1, &rng);
  792. bencher.proxy()->target_execution_policy.algo.reset();
  793. auto time_in_ms = bencher.execs({filter, diff, grad}) / RUNS;
  794. printf("filter=%s, diff=%s, grad=%s, float32: %.2fms "
  795. "%.2fTFlops\n",
  796. filter.to_string().c_str(), diff.to_string().c_str(),
  797. grad.to_string().c_str(), time_in_ms, (flo / (time_in_ms * 1e-3)));
  798. };
  799. // stride = 1
  800. run(32, 128, 24, 24, 128, 1, 1, 3);
  801. run(32, 256, 12, 12, 256, 1, 1, 3);
  802. // stride = 2
  803. run(32, 256, 12, 12, 512, 1, 2, 3);
  804. run(32, 512, 6, 6, 1024, 1, 2, 3);
  805. // stride = 1
  806. run(32, 128, 24, 24, 128, 3, 1, 3);
  807. run(32, 256, 12, 12, 256, 3, 1, 3);
  808. // stride = 2
  809. run(32, 128, 24, 24, 256, 3, 2, 3);
  810. run(32, 256, 12, 12, 512, 3, 2, 3);
  811. // stride = 1
  812. run(64, 128, 24, 24, 128, 1, 1, 3);
  813. run(64, 256, 12, 12, 256, 1, 1, 3);
  814. // stride = 2
  815. run(64, 256, 12, 12, 512, 1, 2, 3);
  816. run(64, 512, 6, 6, 1024, 1, 2, 3);
  817. // stride = 1
  818. run(64, 128, 24, 24, 128, 3, 1, 3);
  819. run(64, 256, 12, 12, 256, 3, 1, 3);
  820. // stride = 2
  821. run(64, 128, 24, 24, 256, 3, 2, 3);
  822. run(64, 256, 12, 12, 512, 3, 2, 3);
  823. }
  824. TEST_F(CUDA, BENCHMARK_LOCAL_SHARE_FORWARD_BOTTLENECK) {
  825. CUBenchmarker<LocalShare> bencher(handle_cuda());
  826. CUBenchmarker<Convolution> bencher_conv(handle_cuda());
  827. size_t RUNS = 1000;
  828. bencher.set_display(false).set_times(RUNS);
  829. std::unique_ptr<OprProxy<LocalShareForward>> proxy{
  830. new OprProxy<LocalShareForward>{true}};
  831. bencher.set_proxy(proxy);
  832. bencher_conv.set_display(false).set_times(RUNS);
  833. std::unique_ptr<OprProxy<Convolution>> conv_proxy{new OprProxy<Convolution>{true}};
  834. bencher_conv.set_proxy(conv_proxy);
  835. LocalShare::Param param;
  836. Convolution::Param conv_param;
  837. NormalRNG rng;
  838. auto run = [&](size_t batch, size_t ic, size_t ih, size_t iw, size_t oc, size_t f,
  839. size_t s, size_t sg) {
  840. param.pad_h = f / 2;
  841. param.pad_w = f / 2;
  842. param.stride_h = s;
  843. param.stride_w = s;
  844. param.spatial_groups_h = sg;
  845. param.spatial_groups_w = sg;
  846. conv_param.pad_h = f / 2;
  847. conv_param.pad_w = f / 2;
  848. conv_param.stride_h = s;
  849. conv_param.stride_w = s;
  850. TensorShape src = {batch, ic, ih, iw}, filter = {sg, sg, ic, f, f, oc};
  851. size_t ho = infer_conv_shape(ih, f, s, f / 2);
  852. size_t wo = infer_conv_shape(iw, f, s, f / 2);
  853. float flo = 2.0 * batch * oc * ho * wo * ic * f * f / (1e12);
  854. bencher.set_param(param)
  855. .set_dtype(0, dtype::Float32())
  856. .set_dtype(1, dtype::Float32())
  857. .set_dtype(2, dtype::Float32())
  858. .set_rng(0, &rng)
  859. .set_rng(1, &rng);
  860. bencher.proxy()->target_execution_policy.algo.reset();
  861. auto time_in_ms = bencher.execs({src, filter, {}}) / RUNS;
  862. bencher_conv.set_param(conv_param);
  863. bencher_conv.proxy()->target_execution_policy.algo.reset();
  864. auto time_in_ms_conv = bencher_conv.execs({src, {oc, ic, f, f}, {}}) / RUNS;
  865. printf("src=%s, filter=%s, float32: %.2fms %.2fTFlops, "
  866. "conv(float32): %.2fms %.2fTFlops, local_share/conv=%.2f\n",
  867. src.to_string().c_str(), filter.to_string().c_str(), time_in_ms,
  868. (flo / (time_in_ms * 1e-3)), time_in_ms_conv,
  869. (flo / (time_in_ms_conv * 1e-3)), time_in_ms / time_in_ms_conv);
  870. };
  871. // stride = 1
  872. run(32, 128, 24, 24, 128, 1, 1, 3);
  873. run(32, 256, 12, 12, 256, 1, 1, 3);
  874. // stride = 2
  875. run(32, 256, 12, 12, 512, 1, 2, 3);
  876. run(32, 512, 6, 6, 1024, 1, 2, 3);
  877. // stride = 1
  878. run(32, 128, 24, 24, 128, 3, 1, 3);
  879. run(32, 256, 12, 12, 256, 3, 1, 3);
  880. // stride = 2
  881. run(32, 128, 24, 24, 256, 3, 2, 3);
  882. run(32, 256, 12, 12, 512, 3, 2, 3);
  883. // stride = 1
  884. run(64, 128, 24, 24, 128, 1, 1, 3);
  885. run(64, 256, 12, 12, 256, 1, 1, 3);
  886. // stride = 2
  887. run(64, 256, 12, 12, 512, 1, 2, 3);
  888. run(64, 512, 6, 6, 1024, 1, 2, 3);
  889. // stride = 1
  890. run(64, 128, 24, 24, 128, 3, 1, 3);
  891. run(64, 256, 12, 12, 256, 3, 1, 3);
  892. // stride = 2
  893. run(64, 128, 24, 24, 256, 3, 2, 3);
  894. run(64, 256, 12, 12, 512, 3, 2, 3);
  895. }
  896. TEST_F(CUDA, BENCHMARK_LOCAL_SHARE_FORWARD_FROM_RESEARCH) {
  897. CUBenchmarker<LocalShare> bencher(handle_cuda());
  898. CUBenchmarker<Convolution> bencher_conv(handle_cuda());
  899. size_t RUNS = 1000;
  900. bencher.set_display(false).set_times(RUNS);
  901. std::unique_ptr<OprProxy<LocalShareForward>> proxy{
  902. new OprProxy<LocalShareForward>{true}};
  903. bencher.set_proxy(proxy);
  904. bencher_conv.set_display(false).set_times(RUNS);
  905. std::unique_ptr<OprProxy<Convolution>> conv_proxy{new OprProxy<Convolution>{true}};
  906. bencher_conv.set_proxy(conv_proxy);
  907. LocalShare::Param param;
  908. Convolution::Param conv_param;
  909. NormalRNG rng;
  910. auto run = [&](size_t batch, size_t ic, size_t ih, size_t iw, size_t oc, size_t f,
  911. size_t s, size_t sg) {
  912. param.pad_h = f / 2;
  913. param.pad_w = f / 2;
  914. param.stride_h = s;
  915. param.stride_w = s;
  916. param.spatial_groups_h = sg;
  917. param.spatial_groups_w = sg;
  918. conv_param.pad_h = f / 2;
  919. conv_param.pad_w = f / 2;
  920. conv_param.stride_h = s;
  921. conv_param.stride_w = s;
  922. TensorShape src = {batch, ic, ih, iw}, filter = {sg, sg, ic, f, f, oc};
  923. size_t ho = infer_conv_shape(ih, f, s, f / 2);
  924. size_t wo = infer_conv_shape(iw, f, s, f / 2);
  925. float flo = 2.0 * batch * oc * ho * wo * ic * f * f / (1e12);
  926. bencher.set_param(param)
  927. .set_dtype(0, dtype::Float32())
  928. .set_dtype(1, dtype::Float32())
  929. .set_dtype(2, dtype::Float32())
  930. .set_rng(0, &rng)
  931. .set_rng(1, &rng);
  932. bencher.proxy()->target_execution_policy.algo.reset();
  933. auto time_in_ms = bencher.execs({src, filter, {}}) / RUNS;
  934. bencher_conv.set_param(conv_param);
  935. bencher_conv.proxy()->target_execution_policy.algo.reset();
  936. auto time_in_ms_conv = bencher_conv.execs({src, {oc, ic, f, f}, {}}) / RUNS;
  937. printf("src=%s, filter=%s, float32: %.2fms %.2fTFlops, "
  938. "conv(float32): %.2fms %.2fTFlops, local_share/conv=%.2f\n",
  939. src.to_string().c_str(), filter.to_string().c_str(), time_in_ms,
  940. (flo / (time_in_ms * 1e-3)), time_in_ms_conv,
  941. (flo / (time_in_ms_conv * 1e-3)), time_in_ms / time_in_ms_conv);
  942. };
  943. // stride = 1
  944. run(64, 128, 24, 24, 128, 1, 1, 3);
  945. run(64, 256, 12, 12, 256, 1, 1, 3);
  946. run(64, 512, 6, 6, 512, 1, 1, 3);
  947. run(64, 1024, 3, 3, 1024, 1, 1, 3);
  948. // stride = 2
  949. run(64, 128, 24, 24, 256, 1, 2, 3);
  950. run(64, 256, 12, 12, 512, 1, 2, 3);
  951. run(64, 512, 6, 6, 1024, 1, 2, 3);
  952. // stride = 1
  953. run(64, 128, 24, 24, 128, 3, 1, 3);
  954. run(64, 256, 12, 12, 256, 3, 1, 3);
  955. run(64, 512, 6, 6, 512, 3, 1, 3);
  956. run(64, 1024, 3, 3, 1024, 3, 1, 3);
  957. // stride = 2
  958. run(64, 128, 24, 24, 256, 3, 2, 3);
  959. run(64, 256, 12, 12, 512, 3, 2, 3);
  960. run(64, 512, 6, 6, 1024, 3, 2, 3);
  961. }
  962. TEST_F(CUDA, BENCHMARK_LOCAL_SHARE_FORWARD) {
  963. require_compute_capability(6, 0);
  964. CUBenchmarker<LocalShare> bencher(handle_cuda());
  965. CUBenchmarker<Convolution> bencher_conv(handle_cuda());
  966. size_t RUNS = 200;
  967. bencher.set_display(false).set_times(RUNS);
  968. std::unique_ptr<OprProxy<LocalShareForward>> proxy{
  969. new OprProxy<LocalShareForward>{true}};
  970. bencher.set_proxy(proxy);
  971. bencher_conv.set_display(false).set_times(RUNS);
  972. std::unique_ptr<OprProxy<Convolution>> conv_proxy{new OprProxy<Convolution>{true}};
  973. bencher_conv.set_proxy(conv_proxy);
  974. LocalShare::Param param;
  975. Convolution::Param conv_param;
  976. NormalRNG rng;
  977. auto run = [&](size_t batch, size_t ic, size_t ih, size_t iw, size_t oc, size_t f,
  978. size_t s, size_t sg) {
  979. param.pad_h = f / 2;
  980. param.pad_w = f / 2;
  981. param.stride_h = s;
  982. param.stride_w = s;
  983. param.spatial_groups_h = sg;
  984. param.spatial_groups_w = sg;
  985. conv_param.pad_h = f / 2;
  986. conv_param.pad_w = f / 2;
  987. conv_param.stride_h = s;
  988. conv_param.stride_w = s;
  989. TensorShape src = {batch, ic, ih, iw}, filter = {sg, sg, ic, f, f, oc};
  990. size_t ho = infer_conv_shape(ih, f, s, f / 2);
  991. size_t wo = infer_conv_shape(iw, f, s, f / 2);
  992. float flo = 2.0 * batch * oc * ho * wo * ic * f * f / (1e12);
  993. bencher.set_param(param)
  994. .set_dtype(0, dtype::Float32())
  995. .set_dtype(1, dtype::Float32())
  996. .set_dtype(2, dtype::Float32())
  997. .set_rng(0, &rng)
  998. .set_rng(1, &rng);
  999. bencher.proxy()->target_execution_policy.algo.reset();
  1000. auto time_in_ms = bencher.execs({src, filter, {}}) / RUNS;
  1001. bencher_conv.set_param(conv_param);
  1002. bencher_conv.proxy()->target_execution_policy.algo.reset();
  1003. auto time_in_ms_conv = bencher_conv.execs({src, {oc, ic, f, f}, {}}) / RUNS;
  1004. printf("src=%s, filter=%s, float32: %.2fms %.2fTFlops, "
  1005. "conv(float32): %.2fms %.2fTFlops, local_share/conv=%.2f\n",
  1006. src.to_string().c_str(), filter.to_string().c_str(), time_in_ms,
  1007. (flo / (time_in_ms * 1e-3)), time_in_ms_conv,
  1008. (flo / (time_in_ms_conv * 1e-3)), time_in_ms / time_in_ms_conv);
  1009. };
  1010. run(64, 256, 48, 48, 256, 7, 1, 3);
  1011. run(64, 128, 24, 24, 128, 7, 1, 3);
  1012. run(64, 256, 12, 12, 256, 7, 1, 3);
  1013. run(64, 512, 6, 6, 512, 7, 1, 3);
  1014. run(64, 256, 48, 48, 256, 5, 1, 3);
  1015. run(64, 128, 24, 24, 128, 5, 1, 3);
  1016. run(64, 256, 12, 12, 256, 5, 1, 3);
  1017. run(64, 512, 6, 6, 512, 5, 1, 3);
  1018. run(32, 64, 96, 96, 256, 7, 2, 3);
  1019. run(32, 128, 24, 24, 128, 7, 2, 3);
  1020. run(32, 256, 12, 12, 256, 7, 2, 3);
  1021. run(32, 64, 96, 96, 256, 5, 2, 3);
  1022. run(32, 128, 24, 24, 128, 5, 2, 3);
  1023. run(32, 256, 12, 12, 256, 5, 2, 3);
  1024. }
  1025. #endif
  1026. // vim: syntax=cpp.doxygen