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

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