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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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-2020 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 "test/naive/fixture.h"
  12. #include "megdnn/oprs/nn.h"
  13. #include "test/common/checker.h"
  14. #include "test/common/random_state.h"
  15. using namespace megdnn;
  16. using namespace test;
  17. TEST_F(NAIVE, RELAYOUT_FORMAT_NCHW88) {
  18. Checker<RelayoutFormat> checker(handle(), /* check_dispatch */ false);
  19. {
  20. auto tensor_nchw = TensorValue(
  21. {1, 8, 1, 2}, dtype::Float32(),
  22. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
  23. auto tensor_nchw88 = TensorValue(
  24. {1, 1, 1, 2, 8}, dtype::Float32(),
  25. {1, 3, 5, 7, 9, 11, 13, 15, 2, 4, 6, 8, 10, 12, 14, 16});
  26. RelayoutFormat::Param param{RelayoutFormat::Param::Mode::NCHW_NCHW88};
  27. checker.set_param(param).exect(Testcase{tensor_nchw, {}},
  28. Testcase{{}, tensor_nchw88});
  29. }
  30. {
  31. auto tensor_nchw = TensorValue(
  32. {2, 8, 1, 2}, dtype::Float32(),
  33. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
  34. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
  35. auto tensor_nchw88 = TensorValue(
  36. {2, 1, 1, 2, 8}, dtype::Float32(),
  37. {1, 3, 5, 7, 9, 11, 13, 15, 2, 4, 6, 8, 10, 12, 14, 16,
  38. 1, 3, 5, 7, 9, 11, 13, 15, 2, 4, 6, 8, 10, 12, 14, 16});
  39. RelayoutFormat::Param param{RelayoutFormat::Param::Mode::NCHW_NCHW88};
  40. checker.set_param(param).exect(Testcase{tensor_nchw, {}},
  41. Testcase{{}, tensor_nchw88});
  42. }
  43. {
  44. auto tensor_nchw =
  45. TensorValue({2, 4, 1, 2}, dtype::Float32(),
  46. {1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8});
  47. auto tensor_nchw88 =
  48. TensorValue({2, 1, 1, 2, 8}, dtype::Float32(),
  49. {1, 3, 5, 7, 0, 0, 0, 0, 2, 4, 6, 8, 0, 0, 0, 0,
  50. 1, 3, 5, 7, 0, 0, 0, 0, 2, 4, 6, 8, 0, 0, 0, 0});
  51. RelayoutFormat::Param param{RelayoutFormat::Param::Mode::NCHW_NCHW88};
  52. checker.set_param(param).exect(Testcase{tensor_nchw, {}},
  53. Testcase{{}, tensor_nchw88});
  54. checker.set_param(param).exec({TensorShape{1, 3, 64, 64}, {}});
  55. }
  56. {
  57. auto tensor_nchw = TensorValue(
  58. {1, 8, 1, 2}, dtype::Float32(),
  59. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
  60. auto tensor_nchw88 = TensorValue(
  61. {1, 1, 1, 2, 8}, dtype::Float32(),
  62. {1, 3, 5, 7, 9, 11, 13, 15, 2, 4, 6, 8, 10, 12, 14, 16});
  63. RelayoutFormat::Param param{RelayoutFormat::Param::Mode::NCHW88_NCHW};
  64. checker.set_param(param).exect(Testcase{tensor_nchw88, {}},
  65. Testcase{{}, tensor_nchw});
  66. }
  67. }
  68. TEST_F(NAIVE, RELAYOUT_FORMAT_NCHW88_DENSE) {
  69. Checker<RelayoutFormat> checker(handle(), /* check_dispatch */ false);
  70. {
  71. auto tensor_oihw =
  72. TensorValue({8, 8, 1, 1}, dtype::Float32(),
  73. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
  74. 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
  75. 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  76. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
  77. 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64});
  78. auto tensor_oihw8i8o = TensorValue(
  79. {1, 1, 1, 1, 8, 8}, dtype::Float32(),
  80. {
  81. 1, 9, 17, 25, 33, 41, 49, 57, 2, 10, 18, 26, 34,
  82. 42, 50, 58, 3, 11, 19, 27, 35, 43, 51, 59, 4, 12,
  83. 20, 28, 36, 44, 52, 60, 5, 13, 21, 29, 37, 45, 53,
  84. 61, 6, 14, 22, 30, 38, 46, 54, 62, 7, 15, 23, 31,
  85. 39, 47, 55, 63, 8, 16, 24, 32, 40, 48, 56, 64,
  86. });
  87. RelayoutFormat::Param param{
  88. RelayoutFormat::Param::Mode::NCHW_NCHW88_CONV_DENSE_WEIGHT};
  89. checker.set_param(param).exect(Testcase{tensor_oihw, {}},
  90. Testcase{{}, tensor_oihw8i8o});
  91. }
  92. {
  93. auto tensor_oihw = TensorValue(
  94. {8, 2, 1, 1}, dtype::Float32(),
  95. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
  96. auto tensor_oihw8i8o = TensorValue(
  97. {1, 1, 1, 1, 8, 8}, dtype::Float32(),
  98. {
  99. 1, 3, 5, 7, 9, 11, 13, 15, 2, 4, 6, 8, 10, 12, 14, 16,
  100. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  101. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  102. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  103. });
  104. RelayoutFormat::Param param{
  105. RelayoutFormat::Param::Mode::NCHW_NCHW88_CONV_DENSE_WEIGHT};
  106. checker.set_param(param).exect(Testcase{tensor_oihw, {}},
  107. Testcase{{}, tensor_oihw8i8o});
  108. }
  109. }
  110. TEST_F(NAIVE, RELAYOUT_FORMAT_NCHW88_CHAIN) {
  111. Checker<RelayoutFormat> checker(handle(), /* check_dispatch */ false);
  112. {
  113. auto tensor_goihw = TensorValue(
  114. {8, 1, 1, 1, 2}, dtype::Float32(),
  115. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
  116. auto tensor_goihw8g = TensorValue(
  117. {1, 1, 1, 1, 2, 8}, dtype::Float32(),
  118. {1, 3, 5, 7, 9, 11, 13, 15, 2, 4, 6, 8, 10, 12, 14, 16});
  119. RelayoutFormat::Param param{
  120. RelayoutFormat::Param::Mode::NCHW_NCHW88_CONV_CHAN_WEIGHT};
  121. checker.set_param(param).exect(Testcase{tensor_goihw, {}},
  122. Testcase{{}, tensor_goihw8g});
  123. }
  124. {
  125. auto tensor_goihw =
  126. TensorValue({2, 1, 1, 1, 2}, dtype::Float32(), {1, 2, 3, 4});
  127. auto tensor_goihw8g =
  128. TensorValue({1, 1, 1, 1, 2, 8}, dtype::Float32(),
  129. {1, 3, 0, 0, 0, 0, 0, 0, 2, 4, 0, 0, 0, 0, 0, 0});
  130. RelayoutFormat::Param param{
  131. RelayoutFormat::Param::Mode::NCHW_NCHW88_CONV_CHAN_WEIGHT};
  132. checker.set_param(param).exect(Testcase{tensor_goihw, {}},
  133. Testcase{{}, tensor_goihw8g});
  134. }
  135. }
  136. TEST_F(NAIVE, RELAYOUT_FORMAT_NCHW88_GROUP) {
  137. Checker<RelayoutFormat> checker(handle(), /* check_dispatch */ false);
  138. {
  139. auto tensor_goihw =
  140. TensorValue({1, 8, 8, 1, 1}, dtype::Float32(),
  141. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
  142. 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
  143. 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  144. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
  145. 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64});
  146. auto tensor_goihw8i8o = TensorValue(
  147. {1, 1, 1, 1, 1, 8, 8}, dtype::Float32(),
  148. {
  149. 1, 9, 17, 25, 33, 41, 49, 57, 2, 10, 18, 26, 34,
  150. 42, 50, 58, 3, 11, 19, 27, 35, 43, 51, 59, 4, 12,
  151. 20, 28, 36, 44, 52, 60, 5, 13, 21, 29, 37, 45, 53,
  152. 61, 6, 14, 22, 30, 38, 46, 54, 62, 7, 15, 23, 31,
  153. 39, 47, 55, 63, 8, 16, 24, 32, 40, 48, 56, 64,
  154. });
  155. RelayoutFormat::Param param{
  156. RelayoutFormat::Param::Mode::NCHW_NCHW88_CONV_GROUP_WEIGHT};
  157. checker.set_param(param).exect(Testcase{tensor_goihw, {}},
  158. Testcase{{}, tensor_goihw8i8o});
  159. }
  160. {
  161. auto tensor_goihw = TensorValue(
  162. {1, 8, 2, 1, 1}, dtype::Float32(),
  163. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16});
  164. auto tensor_goihw8i8o = TensorValue(
  165. {1, 1, 1, 1, 1, 8, 8}, dtype::Float32(),
  166. {
  167. 1, 3, 5, 7, 9, 11, 13, 15, 2, 4, 6, 8, 10, 12, 14, 16,
  168. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  169. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  170. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  171. });
  172. RelayoutFormat::Param param{
  173. RelayoutFormat::Param::Mode::NCHW_NCHW88_CONV_GROUP_WEIGHT};
  174. checker.set_param(param).exect(Testcase{tensor_goihw, {}},
  175. Testcase{{}, tensor_goihw8i8o});
  176. }
  177. {
  178. RelayoutFormat::Param param{RelayoutFormat::Param::Mode::NCHW88_NCHW};
  179. checker.set_param(param).exec({TensorShape{1, 8, 64, 64, 8}, {}});
  180. }
  181. }

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