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

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

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