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.

relayout_format.cpp 14 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #include "test/naive/fixture.h"
  2. #include "megdnn/oprs/nn.h"
  3. #include "test/common/checker.h"
  4. #include "test/common/random_state.h"
  5. using namespace megdnn;
  6. using namespace test;
  7. TEST_F(NAIVE, RELAYOUT_FORMAT_NCHW4_NCHW) {
  8. Checker<RelayoutFormat> checker(handle(), /* check_dispatch */ false);
  9. {
  10. auto tensor_nchw4 = TensorValue(
  11. {1, 2, 1, 2, 4}, dtype::Float32(),
  12. {1, 3, 5, 7, 2, 4, 6, 8, 9, 11, 13, 15, 10, 12, 14, 16});
  13. auto tensor_nchw = TensorValue(
  14. {1, 8, 1, 2}, dtype::Float32(),
  15. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
  16. RelayoutFormat::Param param{RelayoutFormat::Param::Mode::NCHW4_NCHW};
  17. checker.set_param(param).exect(
  18. Testcase{tensor_nchw4, {}}, Testcase{{}, tensor_nchw});
  19. }
  20. {
  21. auto tensor_nchw4 = TensorValue(
  22. {1, 2, 1, 2, 4}, dtype::Float32(),
  23. {1, 3, 5, 7, 2, 4, 6, 8, 9, 11, 13, 15, 10, 12, 14, 16});
  24. auto tensor_nchw = TensorValue(
  25. {1, 7, 1, 2}, dtype::Float32(),
  26. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14});
  27. RelayoutFormat::Param param{RelayoutFormat::Param::Mode::NCHW4_NCHW};
  28. param.oc = 7;
  29. checker.set_param(param).exect(
  30. Testcase{tensor_nchw4, {}}, Testcase{{}, tensor_nchw});
  31. }
  32. {
  33. auto tensor_nchw4 = TensorValue(
  34. {1, 2, 1, 2, 4}, dtype::Float32(),
  35. {1, 3, 5, 7, 2, 4, 6, 8, 9, 11, 13, 15, 10, 12, 14, 16});
  36. auto tensor_nchw = TensorValue(
  37. {1, 6, 1, 2}, dtype::Float32(),
  38. {1, 2, 3, 4, 5, 6, 9, 10, 11, 12, 13, 14});
  39. RelayoutFormat::Param param{RelayoutFormat::Param::Mode::NCHW4_NCHW};
  40. param.oc = 6;
  41. param.group = 2;
  42. checker.set_param(param).exect(
  43. Testcase{tensor_nchw4, {}}, Testcase{{}, tensor_nchw});
  44. }
  45. }
  46. TEST_F(NAIVE, RELAYOUT_FORMAT_NCHW_NCHW4_WEIGHT) {
  47. Checker<RelayoutFormat> checker(handle(), /* check_dispatch */ false);
  48. {
  49. auto tensor_nchw = TensorValue(
  50. {2, 2, 2, 2}, dtype::Float32(),
  51. {1, 2, 3, 4, 5, 6, 7, 8,
  52. 9, 10, 11, 12, 13, 14, 15, 16});
  53. auto tensor_nchw4 = TensorValue(
  54. {4, 1, 2, 2, 4}, dtype::Float32(),
  55. {1, 5, 0, 0, 2, 6, 0, 0, 3, 7, 0, 0, 4, 8, 0, 0,
  56. 9, 13, 0, 0, 10, 14, 0, 0, 11, 15, 0, 0, 12, 16, 0, 0,
  57. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  58. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
  59. RelayoutFormat::Param param{RelayoutFormat::Param::Mode::NCHW_NCHW4_WEIGHT};
  60. checker.set_param(param).exect(
  61. Testcase{tensor_nchw, {}}, Testcase{{}, tensor_nchw4});
  62. }
  63. {
  64. auto tensor_nchw = TensorValue(
  65. {2, 2, 1, 2, 2}, dtype::Float32(),
  66. {1, 2, 3, 4, 5, 6, 7, 8,
  67. 9, 10, 11, 12, 13, 14, 15, 16});
  68. auto tensor_nchw4 = TensorValue(
  69. {2, 4, 1, 2, 2, 4}, dtype::Float32(),
  70. {1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0,
  71. 0, 6, 0, 0, 0, 7, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  72. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  73. 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 10, 0, 0, 0, 11, 0, 0, 0,
  74. 12, 0, 0, 0, 13, 0, 0, 0, 14, 0, 0, 0, 15, 0, 0, 0, 16, 0, 0,
  75. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  76. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
  77. RelayoutFormat::Param param{RelayoutFormat::Param::Mode::NCHW_NCHW4_WEIGHT};
  78. checker.set_param(param).exect(
  79. Testcase{tensor_nchw, {}}, Testcase{{}, tensor_nchw4});
  80. }
  81. }
  82. TEST_F(NAIVE, RELAYOUT_FORMAT_NCHW_NCHW4) {
  83. Checker<RelayoutFormat> checker(handle(), /* check_dispatch */ false);
  84. {
  85. auto tensor_nchw = TensorValue(
  86. {1, 8, 1, 2}, dtype::Float32(),
  87. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
  88. auto tensor_nchw4 = TensorValue(
  89. {1, 2, 1, 2, 4}, dtype::Float32(),
  90. {1, 3, 5, 7, 2, 4, 6, 8, 9, 11, 13, 15, 10, 12, 14, 16});
  91. RelayoutFormat::Param param{RelayoutFormat::Param::Mode::NCHW_NCHW4};
  92. checker.set_param(param).exect(
  93. Testcase{tensor_nchw, {}}, Testcase{{}, tensor_nchw4});
  94. }
  95. {
  96. auto tensor_nchw = TensorValue(
  97. {1, 8, 1, 2}, dtype::Float32(),
  98. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
  99. auto tensor_nchw4 = TensorValue(
  100. {1, 4, 1, 2, 4}, dtype::Float32(),
  101. {1, 3, 0, 0, 2, 4, 0, 0, 5, 7, 0, 0, 6, 8, 0, 0,
  102. 9, 11, 0, 0, 10, 12, 0, 0, 13, 15, 0, 0, 14, 16, 0, 0});
  103. RelayoutFormat::Param param{RelayoutFormat::Param::Mode::NCHW_NCHW4};
  104. param.group = 4;
  105. checker.set_param(param).exect(
  106. Testcase{tensor_nchw, {}}, Testcase{{}, tensor_nchw4});
  107. }
  108. {
  109. auto tensor_nchw = TensorValue(
  110. {1, 6, 1, 2}, dtype::Float32(),
  111. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12});
  112. auto tensor_nchw4 = TensorValue(
  113. {1, 2, 1, 2, 4}, dtype::Float32(),
  114. {1, 3, 5, 0, 2, 4, 6, 0, 7, 9, 11, 0, 8, 10, 12, 0});
  115. RelayoutFormat::Param param{RelayoutFormat::Param::Mode::NCHW_NCHW4};
  116. param.group = 2;
  117. checker.set_param(param).exect(
  118. Testcase{tensor_nchw, {}}, Testcase{{}, tensor_nchw4});
  119. }
  120. }
  121. TEST_F(NAIVE, RELAYOUT_FORMAT_NCHW88) {
  122. Checker<RelayoutFormat> checker(handle(), /* check_dispatch */ false);
  123. {
  124. auto tensor_nchw = TensorValue(
  125. {1, 8, 1, 2}, dtype::Float32(),
  126. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
  127. auto tensor_nchw88 = TensorValue(
  128. {1, 1, 1, 2, 8}, dtype::Float32(),
  129. {1, 3, 5, 7, 9, 11, 13, 15, 2, 4, 6, 8, 10, 12, 14, 16});
  130. RelayoutFormat::Param param{RelayoutFormat::Param::Mode::NCHW_NCHW88};
  131. checker.set_param(param).exect(
  132. Testcase{tensor_nchw, {}}, Testcase{{}, tensor_nchw88});
  133. }
  134. {
  135. auto tensor_nchw = TensorValue(
  136. {2, 8, 1, 2}, dtype::Float32(),
  137. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
  138. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
  139. auto tensor_nchw88 = TensorValue(
  140. {2, 1, 1, 2, 8}, dtype::Float32(),
  141. {1, 3, 5, 7, 9, 11, 13, 15, 2, 4, 6, 8, 10, 12, 14, 16,
  142. 1, 3, 5, 7, 9, 11, 13, 15, 2, 4, 6, 8, 10, 12, 14, 16});
  143. RelayoutFormat::Param param{RelayoutFormat::Param::Mode::NCHW_NCHW88};
  144. checker.set_param(param).exect(
  145. Testcase{tensor_nchw, {}}, Testcase{{}, tensor_nchw88});
  146. }
  147. {
  148. auto tensor_nchw = TensorValue(
  149. {2, 4, 1, 2}, dtype::Float32(),
  150. {1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8});
  151. auto tensor_nchw88 = TensorValue(
  152. {2, 1, 1, 2, 8}, dtype::Float32(),
  153. {1, 3, 5, 7, 0, 0, 0, 0, 2, 4, 6, 8, 0, 0, 0, 0,
  154. 1, 3, 5, 7, 0, 0, 0, 0, 2, 4, 6, 8, 0, 0, 0, 0});
  155. RelayoutFormat::Param param{RelayoutFormat::Param::Mode::NCHW_NCHW88};
  156. checker.set_param(param).exect(
  157. Testcase{tensor_nchw, {}}, Testcase{{}, tensor_nchw88});
  158. checker.set_param(param).exec({TensorShape{1, 3, 64, 64}, {}});
  159. }
  160. {
  161. auto tensor_nchw = TensorValue(
  162. {1, 8, 1, 2}, dtype::Float32(),
  163. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
  164. auto tensor_nchw88 = TensorValue(
  165. {1, 1, 1, 2, 8}, dtype::Float32(),
  166. {1, 3, 5, 7, 9, 11, 13, 15, 2, 4, 6, 8, 10, 12, 14, 16});
  167. RelayoutFormat::Param param{RelayoutFormat::Param::Mode::NCHW88_NCHW};
  168. checker.set_param(param).exect(
  169. Testcase{tensor_nchw88, {}}, Testcase{{}, tensor_nchw});
  170. }
  171. }
  172. TEST_F(NAIVE, RELAYOUT_FORMAT_NCHW88_DENSE) {
  173. Checker<RelayoutFormat> checker(handle(), /* check_dispatch */ false);
  174. {
  175. auto tensor_oihw = TensorValue(
  176. {8, 8, 1, 1}, dtype::Float32(),
  177. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
  178. 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
  179. 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
  180. 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64});
  181. auto tensor_oihw8i8o = TensorValue(
  182. {1, 1, 1, 1, 8, 8}, dtype::Float32(),
  183. {
  184. 1, 9, 17, 25, 33, 41, 49, 57, 2, 10, 18, 26, 34, 42, 50, 58,
  185. 3, 11, 19, 27, 35, 43, 51, 59, 4, 12, 20, 28, 36, 44, 52, 60,
  186. 5, 13, 21, 29, 37, 45, 53, 61, 6, 14, 22, 30, 38, 46, 54, 62,
  187. 7, 15, 23, 31, 39, 47, 55, 63, 8, 16, 24, 32, 40, 48, 56, 64,
  188. });
  189. RelayoutFormat::Param param{
  190. RelayoutFormat::Param::Mode::NCHW_NCHW88_CONV_DENSE_WEIGHT};
  191. checker.set_param(param).exect(
  192. Testcase{tensor_oihw, {}}, Testcase{{}, tensor_oihw8i8o});
  193. }
  194. {
  195. auto tensor_oihw = TensorValue(
  196. {8, 2, 1, 1}, dtype::Float32(),
  197. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
  198. auto tensor_oihw8i8o = TensorValue(
  199. {1, 1, 1, 1, 8, 8}, dtype::Float32(),
  200. {
  201. 1, 3, 5, 7, 9, 11, 13, 15, 2, 4, 6, 8, 10, 12, 14, 16,
  202. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  203. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  204. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  205. });
  206. RelayoutFormat::Param param{
  207. RelayoutFormat::Param::Mode::NCHW_NCHW88_CONV_DENSE_WEIGHT};
  208. checker.set_param(param).exect(
  209. Testcase{tensor_oihw, {}}, Testcase{{}, tensor_oihw8i8o});
  210. }
  211. }
  212. TEST_F(NAIVE, RELAYOUT_FORMAT_NCHW88_CHAIN) {
  213. Checker<RelayoutFormat> checker(handle(), /* check_dispatch */ false);
  214. {
  215. auto tensor_goihw = TensorValue(
  216. {8, 1, 1, 1, 2}, dtype::Float32(),
  217. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
  218. auto tensor_goihw8g = TensorValue(
  219. {1, 1, 1, 1, 2, 8}, dtype::Float32(),
  220. {1, 3, 5, 7, 9, 11, 13, 15, 2, 4, 6, 8, 10, 12, 14, 16});
  221. RelayoutFormat::Param param{
  222. RelayoutFormat::Param::Mode::NCHW_NCHW88_CONV_CHAN_WEIGHT};
  223. checker.set_param(param).exect(
  224. Testcase{tensor_goihw, {}}, Testcase{{}, tensor_goihw8g});
  225. }
  226. {
  227. auto tensor_goihw =
  228. TensorValue({2, 1, 1, 1, 2}, dtype::Float32(), {1, 2, 3, 4});
  229. auto tensor_goihw8g = TensorValue(
  230. {1, 1, 1, 1, 2, 8}, dtype::Float32(),
  231. {1, 3, 0, 0, 0, 0, 0, 0, 2, 4, 0, 0, 0, 0, 0, 0});
  232. RelayoutFormat::Param param{
  233. RelayoutFormat::Param::Mode::NCHW_NCHW88_CONV_CHAN_WEIGHT};
  234. checker.set_param(param).exect(
  235. Testcase{tensor_goihw, {}}, Testcase{{}, tensor_goihw8g});
  236. }
  237. }
  238. TEST_F(NAIVE, RELAYOUT_FORMAT_NCHW88_GROUP) {
  239. Checker<RelayoutFormat> checker(handle(), /* check_dispatch */ false);
  240. {
  241. auto tensor_goihw = TensorValue(
  242. {1, 8, 8, 1, 1}, dtype::Float32(),
  243. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
  244. 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
  245. 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
  246. 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64});
  247. auto tensor_goihw8i8o = TensorValue(
  248. {1, 1, 1, 1, 1, 8, 8}, dtype::Float32(),
  249. {
  250. 1, 9, 17, 25, 33, 41, 49, 57, 2, 10, 18, 26, 34, 42, 50, 58,
  251. 3, 11, 19, 27, 35, 43, 51, 59, 4, 12, 20, 28, 36, 44, 52, 60,
  252. 5, 13, 21, 29, 37, 45, 53, 61, 6, 14, 22, 30, 38, 46, 54, 62,
  253. 7, 15, 23, 31, 39, 47, 55, 63, 8, 16, 24, 32, 40, 48, 56, 64,
  254. });
  255. RelayoutFormat::Param param{
  256. RelayoutFormat::Param::Mode::NCHW_NCHW88_CONV_GROUP_WEIGHT};
  257. checker.set_param(param).exect(
  258. Testcase{tensor_goihw, {}}, Testcase{{}, tensor_goihw8i8o});
  259. }
  260. {
  261. auto tensor_goihw = TensorValue(
  262. {1, 8, 2, 1, 1}, dtype::Float32(),
  263. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
  264. auto tensor_goihw8i8o = TensorValue(
  265. {1, 1, 1, 1, 1, 8, 8}, dtype::Float32(),
  266. {
  267. 1, 3, 5, 7, 9, 11, 13, 15, 2, 4, 6, 8, 10, 12, 14, 16,
  268. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  269. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  270. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  271. });
  272. RelayoutFormat::Param param{
  273. RelayoutFormat::Param::Mode::NCHW_NCHW88_CONV_GROUP_WEIGHT};
  274. checker.set_param(param).exect(
  275. Testcase{tensor_goihw, {}}, Testcase{{}, tensor_goihw8i8o});
  276. }
  277. {
  278. RelayoutFormat::Param param{RelayoutFormat::Param::Mode::NCHW88_NCHW};
  279. checker.set_param(param).exec({TensorShape{1, 8, 64, 64, 8}, {}});
  280. }
  281. }