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.

padding.h 18 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /**
  2. * \file dnn/test/common/padding.h
  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. #pragma once
  13. #include <cstddef>
  14. #include <iostream>
  15. #include "megdnn/basic_types.h"
  16. #include "megdnn/opr_param_defs.h"
  17. namespace megdnn {
  18. namespace test {
  19. namespace padding {
  20. struct TestArg {
  21. param::Padding param;
  22. TensorShape src;
  23. TensorShape dst;
  24. TestArg(param::Padding _param, TensorShape _src, TensorShape _dst)
  25. : param(_param), src(_src), dst(_dst) {}
  26. };
  27. inline std::vector<TestArg> get_args() {
  28. size_t src_shape_dim0 = 5;
  29. size_t src_shape_dim1 = 5;
  30. size_t src_shape_dim2 = 5;
  31. size_t src_shape_dim3 = 5;
  32. size_t src_shape_dim4 = 5;
  33. size_t src_shape_dim5 = 5;
  34. size_t src_shape_dim6 = 5;
  35. size_t dst_shape_dim0 = 8;
  36. size_t dst_shape_dim1 = 8;
  37. size_t dst_shape_dim2 = 8;
  38. size_t dst_shape_dim3 = 8;
  39. size_t dst_shape_dim4 = 8;
  40. size_t dst_shape_dim5 = 8;
  41. size_t dst_shape_dim6 = 8;
  42. std::vector<TestArg> args;
  43. param::Padding cur_param;
  44. cur_param.front_offset_dim0 = 0;
  45. cur_param.front_offset_dim1 = 0;
  46. cur_param.front_offset_dim2 = 0;
  47. cur_param.front_offset_dim3 = 0;
  48. cur_param.front_offset_dim4 = 0;
  49. cur_param.front_offset_dim5 = 0;
  50. cur_param.front_offset_dim6 = 0;
  51. cur_param.back_offset_dim0 = 0;
  52. cur_param.back_offset_dim1 = 0;
  53. cur_param.back_offset_dim2 = 0;
  54. cur_param.back_offset_dim3 = 0;
  55. cur_param.back_offset_dim4 = 0;
  56. cur_param.back_offset_dim5 = 0;
  57. cur_param.back_offset_dim6 = 0;
  58. cur_param.padding_val = 2;
  59. cur_param.front_offset_dim0 = 1;
  60. cur_param.back_offset_dim0 = 2;
  61. cur_param.padding_mode = param::Padding::PaddingMode::CONSTANT;
  62. args.emplace_back(cur_param, TensorShape{src_shape_dim0},
  63. TensorShape{dst_shape_dim0});
  64. cur_param.padding_mode = param::Padding::PaddingMode::REPLICATE;
  65. args.emplace_back(cur_param, TensorShape{src_shape_dim0},
  66. TensorShape{dst_shape_dim0});
  67. cur_param.padding_mode = param::Padding::PaddingMode::REFLECT;
  68. args.emplace_back(cur_param, TensorShape{src_shape_dim0},
  69. TensorShape{dst_shape_dim0});
  70. cur_param.front_offset_dim1 = 2;
  71. cur_param.back_offset_dim1 = 1;
  72. cur_param.padding_mode = param::Padding::PaddingMode::CONSTANT;
  73. args.emplace_back(cur_param, TensorShape{src_shape_dim0, src_shape_dim1},
  74. TensorShape{dst_shape_dim0, dst_shape_dim1});
  75. cur_param.padding_mode = param::Padding::PaddingMode::REPLICATE;
  76. args.emplace_back(cur_param, TensorShape{src_shape_dim0, src_shape_dim1},
  77. TensorShape{dst_shape_dim0, dst_shape_dim1});
  78. cur_param.padding_mode = param::Padding::PaddingMode::REFLECT;
  79. args.emplace_back(cur_param, TensorShape{src_shape_dim0, src_shape_dim1},
  80. TensorShape{dst_shape_dim0, dst_shape_dim1});
  81. cur_param.front_offset_dim2 = 1;
  82. cur_param.back_offset_dim2 = 2;
  83. cur_param.padding_mode = param::Padding::PaddingMode::CONSTANT;
  84. args.emplace_back(
  85. cur_param,
  86. TensorShape{src_shape_dim0, src_shape_dim1, src_shape_dim2},
  87. TensorShape{dst_shape_dim0, dst_shape_dim1, dst_shape_dim2});
  88. cur_param.padding_mode = param::Padding::PaddingMode::REPLICATE;
  89. args.emplace_back(
  90. cur_param,
  91. TensorShape{src_shape_dim0, src_shape_dim1, src_shape_dim2},
  92. TensorShape{dst_shape_dim0, dst_shape_dim1, dst_shape_dim2});
  93. cur_param.padding_mode = param::Padding::PaddingMode::REFLECT;
  94. args.emplace_back(
  95. cur_param,
  96. TensorShape{src_shape_dim0, src_shape_dim1, src_shape_dim2},
  97. TensorShape{dst_shape_dim0, dst_shape_dim1, dst_shape_dim2});
  98. cur_param.front_offset_dim3 = 0;
  99. cur_param.back_offset_dim3 = 3;
  100. cur_param.padding_mode = param::Padding::PaddingMode::CONSTANT;
  101. args.emplace_back(cur_param,
  102. TensorShape{src_shape_dim0, src_shape_dim1,
  103. src_shape_dim2, src_shape_dim3},
  104. TensorShape{dst_shape_dim0, dst_shape_dim1,
  105. dst_shape_dim2, dst_shape_dim3});
  106. cur_param.padding_mode = param::Padding::PaddingMode::REPLICATE;
  107. args.emplace_back(cur_param,
  108. TensorShape{src_shape_dim0, src_shape_dim1,
  109. src_shape_dim2, src_shape_dim3},
  110. TensorShape{dst_shape_dim0, dst_shape_dim1,
  111. dst_shape_dim2, dst_shape_dim3});
  112. cur_param.padding_mode = param::Padding::PaddingMode::REFLECT;
  113. args.emplace_back(cur_param,
  114. TensorShape{src_shape_dim0, src_shape_dim1,
  115. src_shape_dim2, src_shape_dim3},
  116. TensorShape{dst_shape_dim0, dst_shape_dim1,
  117. dst_shape_dim2, dst_shape_dim3});
  118. cur_param.front_offset_dim4 = 3;
  119. cur_param.back_offset_dim4 = 0;
  120. cur_param.padding_mode = param::Padding::PaddingMode::CONSTANT;
  121. args.emplace_back(
  122. cur_param,
  123. TensorShape{src_shape_dim0, src_shape_dim1, src_shape_dim2,
  124. src_shape_dim3, src_shape_dim4},
  125. TensorShape{dst_shape_dim0, dst_shape_dim1, dst_shape_dim2,
  126. dst_shape_dim3, dst_shape_dim4});
  127. cur_param.padding_mode = param::Padding::PaddingMode::REPLICATE;
  128. args.emplace_back(
  129. cur_param,
  130. TensorShape{src_shape_dim0, src_shape_dim1, src_shape_dim2,
  131. src_shape_dim3, src_shape_dim4},
  132. TensorShape{dst_shape_dim0, dst_shape_dim1, dst_shape_dim2,
  133. dst_shape_dim3, dst_shape_dim4});
  134. cur_param.padding_mode = param::Padding::PaddingMode::REFLECT;
  135. args.emplace_back(
  136. cur_param,
  137. TensorShape{src_shape_dim0, src_shape_dim1, src_shape_dim2,
  138. src_shape_dim3, src_shape_dim4},
  139. TensorShape{dst_shape_dim0, dst_shape_dim1, dst_shape_dim2,
  140. dst_shape_dim3, dst_shape_dim4});
  141. cur_param.front_offset_dim5 = 1;
  142. cur_param.back_offset_dim5 = 2;
  143. cur_param.padding_mode = param::Padding::PaddingMode::CONSTANT;
  144. args.emplace_back(
  145. cur_param,
  146. TensorShape{src_shape_dim0, src_shape_dim1, src_shape_dim2,
  147. src_shape_dim3, src_shape_dim4, src_shape_dim5},
  148. TensorShape{dst_shape_dim0, dst_shape_dim1, dst_shape_dim2,
  149. dst_shape_dim3, dst_shape_dim4, dst_shape_dim5});
  150. cur_param.padding_mode = param::Padding::PaddingMode::REPLICATE;
  151. args.emplace_back(
  152. cur_param,
  153. TensorShape{src_shape_dim0, src_shape_dim1, src_shape_dim2,
  154. src_shape_dim3, src_shape_dim4, src_shape_dim5},
  155. TensorShape{dst_shape_dim0, dst_shape_dim1, dst_shape_dim2,
  156. dst_shape_dim3, dst_shape_dim4, dst_shape_dim5});
  157. cur_param.padding_mode = param::Padding::PaddingMode::REFLECT;
  158. args.emplace_back(
  159. cur_param,
  160. TensorShape{src_shape_dim0, src_shape_dim1, src_shape_dim2,
  161. src_shape_dim3, src_shape_dim4, src_shape_dim5},
  162. TensorShape{dst_shape_dim0, dst_shape_dim1, dst_shape_dim2,
  163. dst_shape_dim3, dst_shape_dim4, dst_shape_dim5});
  164. cur_param.front_offset_dim6 = 0;
  165. cur_param.front_offset_dim6 = 3;
  166. cur_param.padding_mode = param::Padding::PaddingMode::CONSTANT;
  167. args.emplace_back(
  168. cur_param,
  169. TensorShape{src_shape_dim0, src_shape_dim1, src_shape_dim2,
  170. src_shape_dim3, src_shape_dim4, src_shape_dim5,
  171. src_shape_dim6},
  172. TensorShape{dst_shape_dim0, dst_shape_dim1, dst_shape_dim2,
  173. dst_shape_dim3, dst_shape_dim4, dst_shape_dim5,
  174. dst_shape_dim6});
  175. cur_param.padding_mode = param::Padding::PaddingMode::REPLICATE;
  176. args.emplace_back(
  177. cur_param,
  178. TensorShape{src_shape_dim0, src_shape_dim1, src_shape_dim2,
  179. src_shape_dim3, src_shape_dim4, src_shape_dim5,
  180. src_shape_dim6},
  181. TensorShape{dst_shape_dim0, dst_shape_dim1, dst_shape_dim2,
  182. dst_shape_dim3, dst_shape_dim4, dst_shape_dim5,
  183. dst_shape_dim6});
  184. cur_param.padding_mode = param::Padding::PaddingMode::REFLECT;
  185. args.emplace_back(
  186. cur_param,
  187. TensorShape{src_shape_dim0, src_shape_dim1, src_shape_dim2,
  188. src_shape_dim3, src_shape_dim4, src_shape_dim5,
  189. src_shape_dim6},
  190. TensorShape{dst_shape_dim0, dst_shape_dim1, dst_shape_dim2,
  191. dst_shape_dim3, dst_shape_dim4, dst_shape_dim5,
  192. dst_shape_dim6});
  193. return args;
  194. }
  195. inline std::vector<TestArg> get_args_backward() {
  196. size_t src_shape_dim0 = 8;
  197. size_t src_shape_dim1 = 8;
  198. size_t src_shape_dim2 = 8;
  199. size_t src_shape_dim3 = 8;
  200. size_t src_shape_dim4 = 8;
  201. size_t src_shape_dim5 = 8;
  202. size_t src_shape_dim6 = 8;
  203. size_t dst_shape_dim0 = 5;
  204. size_t dst_shape_dim1 = 5;
  205. size_t dst_shape_dim2 = 5;
  206. size_t dst_shape_dim3 = 5;
  207. size_t dst_shape_dim4 = 5;
  208. size_t dst_shape_dim5 = 5;
  209. size_t dst_shape_dim6 = 5;
  210. std::vector<TestArg> args;
  211. param::Padding cur_param;
  212. cur_param.front_offset_dim0 = 0;
  213. cur_param.front_offset_dim1 = 0;
  214. cur_param.front_offset_dim2 = 0;
  215. cur_param.front_offset_dim3 = 0;
  216. cur_param.front_offset_dim4 = 0;
  217. cur_param.front_offset_dim5 = 0;
  218. cur_param.front_offset_dim6 = 0;
  219. cur_param.back_offset_dim0 = 0;
  220. cur_param.back_offset_dim1 = 0;
  221. cur_param.back_offset_dim2 = 0;
  222. cur_param.back_offset_dim3 = 0;
  223. cur_param.back_offset_dim4 = 0;
  224. cur_param.back_offset_dim5 = 0;
  225. cur_param.back_offset_dim6 = 0;
  226. cur_param.padding_val = 2;
  227. cur_param.front_offset_dim0 = 1;
  228. cur_param.back_offset_dim0 = 2;
  229. cur_param.padding_mode = param::Padding::PaddingMode::CONSTANT;
  230. args.emplace_back(cur_param, TensorShape{src_shape_dim0},
  231. TensorShape{dst_shape_dim0});
  232. cur_param.padding_mode = param::Padding::PaddingMode::REPLICATE;
  233. args.emplace_back(cur_param, TensorShape{src_shape_dim0},
  234. TensorShape{dst_shape_dim0});
  235. cur_param.padding_mode = param::Padding::PaddingMode::REFLECT;
  236. args.emplace_back(cur_param, TensorShape{src_shape_dim0},
  237. TensorShape{dst_shape_dim0});
  238. cur_param.front_offset_dim1 = 2;
  239. cur_param.back_offset_dim1 = 1;
  240. cur_param.padding_mode = param::Padding::PaddingMode::CONSTANT;
  241. args.emplace_back(cur_param, TensorShape{src_shape_dim0, src_shape_dim1},
  242. TensorShape{dst_shape_dim0, dst_shape_dim1});
  243. cur_param.padding_mode = param::Padding::PaddingMode::REPLICATE;
  244. args.emplace_back(cur_param, TensorShape{src_shape_dim0, src_shape_dim1},
  245. TensorShape{dst_shape_dim0, dst_shape_dim1});
  246. cur_param.padding_mode = param::Padding::PaddingMode::REFLECT;
  247. args.emplace_back(cur_param, TensorShape{src_shape_dim0, src_shape_dim1},
  248. TensorShape{dst_shape_dim0, dst_shape_dim1});
  249. cur_param.front_offset_dim2 = 1;
  250. cur_param.back_offset_dim2 = 2;
  251. cur_param.padding_mode = param::Padding::PaddingMode::CONSTANT;
  252. args.emplace_back(
  253. cur_param,
  254. TensorShape{src_shape_dim0, src_shape_dim1, src_shape_dim2},
  255. TensorShape{dst_shape_dim0, dst_shape_dim1, dst_shape_dim2});
  256. cur_param.padding_mode = param::Padding::PaddingMode::REPLICATE;
  257. args.emplace_back(
  258. cur_param,
  259. TensorShape{src_shape_dim0, src_shape_dim1, src_shape_dim2},
  260. TensorShape{dst_shape_dim0, dst_shape_dim1, dst_shape_dim2});
  261. cur_param.padding_mode = param::Padding::PaddingMode::REFLECT;
  262. args.emplace_back(
  263. cur_param,
  264. TensorShape{src_shape_dim0, src_shape_dim1, src_shape_dim2},
  265. TensorShape{dst_shape_dim0, dst_shape_dim1, dst_shape_dim2});
  266. cur_param.front_offset_dim3 = 0;
  267. cur_param.back_offset_dim3 = 3;
  268. cur_param.padding_mode = param::Padding::PaddingMode::CONSTANT;
  269. args.emplace_back(cur_param,
  270. TensorShape{src_shape_dim0, src_shape_dim1,
  271. src_shape_dim2, src_shape_dim3},
  272. TensorShape{dst_shape_dim0, dst_shape_dim1,
  273. dst_shape_dim2, dst_shape_dim3});
  274. cur_param.padding_mode = param::Padding::PaddingMode::REPLICATE;
  275. args.emplace_back(cur_param,
  276. TensorShape{src_shape_dim0, src_shape_dim1,
  277. src_shape_dim2, src_shape_dim3},
  278. TensorShape{dst_shape_dim0, dst_shape_dim1,
  279. dst_shape_dim2, dst_shape_dim3});
  280. cur_param.padding_mode = param::Padding::PaddingMode::REFLECT;
  281. args.emplace_back(cur_param,
  282. TensorShape{src_shape_dim0, src_shape_dim1,
  283. src_shape_dim2, src_shape_dim3},
  284. TensorShape{dst_shape_dim0, dst_shape_dim1,
  285. dst_shape_dim2, dst_shape_dim3});
  286. cur_param.front_offset_dim4 = 3;
  287. cur_param.back_offset_dim4 =0;
  288. cur_param.padding_mode = param::Padding::PaddingMode::CONSTANT;
  289. args.emplace_back(
  290. cur_param,
  291. TensorShape{src_shape_dim0, src_shape_dim1, src_shape_dim2,
  292. src_shape_dim3, src_shape_dim4},
  293. TensorShape{dst_shape_dim0, dst_shape_dim1, dst_shape_dim2,
  294. dst_shape_dim3, dst_shape_dim4});
  295. cur_param.padding_mode = param::Padding::PaddingMode::REPLICATE;
  296. args.emplace_back(
  297. cur_param,
  298. TensorShape{src_shape_dim0, src_shape_dim1, src_shape_dim2,
  299. src_shape_dim3, src_shape_dim4},
  300. TensorShape{dst_shape_dim0, dst_shape_dim1, dst_shape_dim2,
  301. dst_shape_dim3, dst_shape_dim4});
  302. cur_param.padding_mode = param::Padding::PaddingMode::REFLECT;
  303. args.emplace_back(
  304. cur_param,
  305. TensorShape{src_shape_dim0, src_shape_dim1, src_shape_dim2,
  306. src_shape_dim3, src_shape_dim4},
  307. TensorShape{dst_shape_dim0, dst_shape_dim1, dst_shape_dim2,
  308. dst_shape_dim3, dst_shape_dim4});
  309. cur_param.front_offset_dim5 = 1;
  310. cur_param.back_offset_dim5 = 2;
  311. cur_param.padding_mode = param::Padding::PaddingMode::CONSTANT;
  312. args.emplace_back(
  313. cur_param,
  314. TensorShape{src_shape_dim0, src_shape_dim1, src_shape_dim2,
  315. src_shape_dim3, src_shape_dim4, src_shape_dim5},
  316. TensorShape{dst_shape_dim0, dst_shape_dim1, dst_shape_dim2,
  317. dst_shape_dim3, dst_shape_dim4, dst_shape_dim5});
  318. cur_param.padding_mode = param::Padding::PaddingMode::REPLICATE;
  319. args.emplace_back(
  320. cur_param,
  321. TensorShape{src_shape_dim0, src_shape_dim1, src_shape_dim2,
  322. src_shape_dim3, src_shape_dim4, src_shape_dim5},
  323. TensorShape{dst_shape_dim0, dst_shape_dim1, dst_shape_dim2,
  324. dst_shape_dim3, dst_shape_dim4, dst_shape_dim5});
  325. cur_param.padding_mode = param::Padding::PaddingMode::REFLECT;
  326. args.emplace_back(
  327. cur_param,
  328. TensorShape{src_shape_dim0, src_shape_dim1, src_shape_dim2,
  329. src_shape_dim3, src_shape_dim4, src_shape_dim5},
  330. TensorShape{dst_shape_dim0, dst_shape_dim1, dst_shape_dim2,
  331. dst_shape_dim3, dst_shape_dim4, dst_shape_dim5});
  332. cur_param.front_offset_dim6 = 0;
  333. cur_param.back_offset_dim6 = 3;
  334. cur_param.padding_mode = param::Padding::PaddingMode::CONSTANT;
  335. args.emplace_back(
  336. cur_param,
  337. TensorShape{src_shape_dim0, src_shape_dim1, src_shape_dim2,
  338. src_shape_dim3, src_shape_dim4, src_shape_dim5,
  339. src_shape_dim6},
  340. TensorShape{dst_shape_dim0, dst_shape_dim1, dst_shape_dim2,
  341. dst_shape_dim3, dst_shape_dim4, dst_shape_dim5,
  342. dst_shape_dim6});
  343. cur_param.padding_mode = param::Padding::PaddingMode::REPLICATE;
  344. args.emplace_back(
  345. cur_param,
  346. TensorShape{src_shape_dim0, src_shape_dim1, src_shape_dim2,
  347. src_shape_dim3, src_shape_dim4, src_shape_dim5,
  348. src_shape_dim6},
  349. TensorShape{dst_shape_dim0, dst_shape_dim1, dst_shape_dim2,
  350. dst_shape_dim3, dst_shape_dim4, dst_shape_dim5,
  351. dst_shape_dim6});
  352. cur_param.padding_mode = param::Padding::PaddingMode::REFLECT;
  353. args.emplace_back(
  354. cur_param,
  355. TensorShape{src_shape_dim0, src_shape_dim1, src_shape_dim2,
  356. src_shape_dim3, src_shape_dim4, src_shape_dim5,
  357. src_shape_dim6},
  358. TensorShape{dst_shape_dim0, dst_shape_dim1, dst_shape_dim2,
  359. dst_shape_dim3, dst_shape_dim4, dst_shape_dim5,
  360. dst_shape_dim6});
  361. return args;
  362. }
  363. } // namespace padding
  364. } // namespace test
  365. } // namespace megdnn

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