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.

algo_chooser.cpp 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /**
  2. * \file src/opr/test/algo_chooser.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 implied.
  10. */
  11. #include "megbrain/comp_node_env.h"
  12. #include "megbrain/opr/blas.h"
  13. #include "megbrain/opr/dnn/convolution.h"
  14. #include "megbrain/test/autocheck.h"
  15. #include "megbrain/test/helper.h"
  16. #include "megbrain/test/megdnn_helper.h"
  17. #include "megbrain/serialization/serializer.h"
  18. #include "megbrain/opr/basic_arith.h"
  19. #include "megbrain/gopt/inference.h"
  20. #include "megbrain/opr/tensor_manip.h"
  21. #include "megdnn/oprs/base.h"
  22. #include "megdnn/dtype.h"
  23. #include <cmath>
  24. #include <random>
  25. #include <utility>
  26. using namespace mgb;
  27. namespace {
  28. #if MGB_CUDA
  29. #if MGB_ENABLE_FASTRUN
  30. template <typename MgbOpr, int arith>
  31. struct GraphMaker;
  32. template <typename MgbOpr>
  33. struct GraphMaker<MgbOpr, 2> {
  34. SymbolVar operator()(const std::array<cg::SymbolVar, 2>& inputs,
  35. typename MgbOpr::Param& param,
  36. typename MgbOpr::ExecutionPolicy& policy) {
  37. return MgbOpr::make(inputs[0], inputs[1], param, policy);
  38. }
  39. };
  40. template <>
  41. struct GraphMaker<opr::ConvolutionBackwardData, 2> {
  42. SymbolVar operator()(
  43. const std::array<cg::SymbolVar, 2>& inputs,
  44. opr::ConvolutionBackwardData::Param& param,
  45. opr::ConvolutionBackwardData::ExecutionPolicy& policy) {
  46. return opr::ConvolutionBackwardData::make_deconv(inputs[0], inputs[1],
  47. param, policy);
  48. }
  49. };
  50. template <>
  51. struct GraphMaker<opr::Convolution3DBackwardData, 2> {
  52. SymbolVar operator()(
  53. const std::array<cg::SymbolVar, 2>& inputs,
  54. opr::Convolution3DBackwardData::Param& param,
  55. opr::Convolution3DBackwardData::ExecutionPolicy& policy) {
  56. return opr::Convolution3DBackwardData::make_deconv(inputs[0], inputs[1],
  57. param, policy);
  58. }
  59. };
  60. template <typename MgbOpr>
  61. struct GraphMaker<MgbOpr, 3> {
  62. SymbolVar operator()(const std::array<cg::SymbolVar, 3>& inputs,
  63. typename MgbOpr::Param& param,
  64. typename MgbOpr::ExecutionPolicy& policy) {
  65. return MgbOpr::make(inputs[0], inputs[1], inputs[2], param, policy, {});
  66. }
  67. };
  68. template <typename MgbOpr>
  69. struct GraphMaker<MgbOpr, 4> {
  70. SymbolVar operator()(const std::array<cg::SymbolVar, 4>& inputs,
  71. typename MgbOpr::Param& param,
  72. typename MgbOpr::ExecutionPolicy& policy) {
  73. return MgbOpr::make(inputs[0], inputs[1], inputs[2], inputs[3], param,
  74. policy, {});
  75. }
  76. };
  77. template <typename MgbOpr>
  78. struct GraphMaker<MgbOpr, 5> {
  79. SymbolVar operator()(const std::array<cg::SymbolVar, 5>& inputs,
  80. typename MgbOpr::Param& param,
  81. typename MgbOpr::ExecutionPolicy& policy) {
  82. return MgbOpr::make(inputs[0], inputs[1], inputs[2], inputs[3],
  83. inputs[4], param, policy, {});
  84. }
  85. };
  86. template <typename MgbOpr, int arith, typename dtype = dtype::Float32>
  87. void test_fastrun_opr(std::array<TensorShape, arith> inps0,
  88. std::array<TensorShape, arith> inps1,
  89. size_t expect_nr_cache_set_inp0 = 0,
  90. size_t expect_nr_cache_set_inp1 = 0,
  91. typename MgbOpr::Param param = {}) {
  92. using Policy = opr::Convolution::ExecutionPolicy;
  93. using S = Policy::Strategy;
  94. using InputGenerator = std::function<void(HostTensorND & dest)>;
  95. using ShapeInpArray = std::array<TensorShape, arith>;
  96. using CacheMem = std::pair<const void*, size_t>;
  97. auto on_get = [](const std::string&, const void*, size_t, const void*,
  98. size_t) {};
  99. std::vector<std::pair<CacheMem, CacheMem>> cache_set_history;
  100. auto on_set = [&cache_set_history](const std::string&, const void* key,
  101. size_t key_size, const void* val,
  102. size_t val_size) {
  103. cache_set_history.emplace_back(std::make_pair(key, key_size),
  104. std::make_pair(val, val_size));
  105. };
  106. PersistentCacheHook cache_hook{on_get, on_set};
  107. CompNode comp_node = CompNode::load("xpu0");
  108. GraphMaker<MgbOpr, arith> graph_maker;
  109. auto run = [&param, &comp_node, &graph_maker](
  110. const std::shared_ptr<cg::ComputingGraph>& graph,
  111. const ShapeInpArray& shapes) {
  112. std::array<InputGenerator, arith> inputs_generator;
  113. std::array<std::shared_ptr<HostTensorND>, arith> inputs;
  114. for (size_t i = 0; i < arith; ++i) {
  115. inputs[i] = std::make_shared<HostTensorND>(comp_node,
  116. dtype());
  117. }
  118. HostTensorGenerator<dtype> gen_host;
  119. for (size_t i = 0; i < arith; ++i) {
  120. inputs[i]->resize(shapes[i]);
  121. *inputs[i] = *gen_host(inputs[i]->shape(), comp_node);
  122. mgb_assert(inputs[i]->shape().eq_shape(shapes[i]));
  123. }
  124. std::array<cg::SymbolVar, arith> sym_in;
  125. for (size_t i = 0; i < arith; ++i) {
  126. // to trigger graph trans
  127. sym_in[i] = opr::Host2DeviceCopy::make(*graph, inputs[i],
  128. ssprintf("inp%zu", i));
  129. }
  130. Policy policy;
  131. policy.strategy = S::PROFILE;
  132. auto out = graph_maker(sym_in, param, policy);
  133. std::unique_ptr<cg::AsyncExecutable> func =
  134. graph->compile({{out, {}}});
  135. func->execute();
  136. };
  137. std::shared_ptr<cg::ComputingGraph> fastrun_ignore_batchsize_graph =
  138. ComputingGraph::make();
  139. fastrun_ignore_batchsize_graph->options()
  140. .fast_run_config.shared_batch_size = 20;
  141. run(fastrun_ignore_batchsize_graph, inps0);
  142. size_t nr_set_inp0 = cache_set_history.size();
  143. if (expect_nr_cache_set_inp0) {
  144. ASSERT_EQ(cache_set_history.size(), expect_nr_cache_set_inp0);
  145. }
  146. run(fastrun_ignore_batchsize_graph, inps1);
  147. size_t nr_set_total = expect_nr_cache_set_inp1 + nr_set_inp0;
  148. ASSERT_EQ(cache_set_history.size(), nr_set_total);
  149. }
  150. TEST(TestOprDNN, FastrunIgnoreBatchSizeConvolution) {
  151. REQUIRE_GPU(1);
  152. test_fastrun_opr<opr::Convolution, 2>(
  153. {TensorShape{12, 3, 36, 36}, TensorShape{4, 3, 3, 3}},
  154. {TensorShape{1, 3, 36, 36}, TensorShape{4, 3, 3, 3}});
  155. test_fastrun_opr<opr::ConvolutionBackwardData, 2>(
  156. {TensorShape{12, 4, 23, 29}, TensorShape{4, 5, 3, 2}},
  157. {TensorShape{2, 4, 23, 29}, TensorShape{4, 5, 3, 2}});
  158. test_fastrun_opr<opr::ConvolutionBackwardFilter, 3>(
  159. {TensorShape{12, 4, 23, 29}, TensorShape{12, 5, 21, 28},
  160. TensorShape{5, 4, 3, 2}},
  161. {TensorShape{2, 4, 23, 29}, TensorShape{2, 5, 21, 28},
  162. TensorShape{5, 4, 3, 2}});
  163. }
  164. TEST(TestOprDNN, FastrunIgnoreBatchSizeConvBias) {
  165. REQUIRE_GPU(1);
  166. test_fastrun_opr<opr::ConvBias, 3>(
  167. {TensorShape{20, 16, 50, 50}, TensorShape{24, 16, 3, 3},
  168. TensorShape{1, 24, 1, 1}},
  169. {TensorShape{1, 16, 50, 50}, TensorShape{24, 16, 3, 3},
  170. TensorShape{1, 24, 1, 1}});
  171. }
  172. TEST(TestOprDNN, FastrunIgnoreBatchSizeConvolution3D) {
  173. REQUIRE_GPU(1);
  174. test_fastrun_opr<opr::Convolution3D, 2>(
  175. {TensorShape{8, 4, 12, 13, 14}, TensorShape{4, 4, 3, 3, 3}},
  176. {TensorShape{3, 4, 12, 13, 14}, TensorShape{4, 4, 3, 3, 3}});
  177. test_fastrun_opr<opr::Convolution3DBackwardData, 2>(
  178. {TensorShape{14, 5, 12, 12, 16}, TensorShape{5, 5, 3, 3, 3}},
  179. {TensorShape{4, 5, 12, 12, 16}, TensorShape{5, 5, 3, 3, 3}});
  180. test_fastrun_opr<opr::Convolution3DBackwardFilter, 3>(
  181. {TensorShape{64, 16, 18, 18, 18}, TensorShape{64, 16, 18, 18, 18},
  182. TensorShape{16, 16, 1, 1, 1}},
  183. {TensorShape{4, 16, 18, 18, 18}, TensorShape{4, 16, 18, 18, 18},
  184. TensorShape{16, 16, 1, 1, 1}});
  185. }
  186. TEST(TestOprDNN, FastrunIgnoreBatchSizeLocalShare) {
  187. REQUIRE_GPU(1);
  188. opr::LocalShare::Param local_share_param;
  189. local_share_param.mode = opr::LocalShare::Param::Mode::CROSS_CORRELATION;
  190. local_share_param.pad_h = local_share_param.pad_w = 1;
  191. local_share_param.stride_h = local_share_param.stride_w = 1;
  192. local_share_param.spatial_groups_h = local_share_param.spatial_groups_w = 2;
  193. test_fastrun_opr<opr::LocalShareForward, 2>(
  194. {TensorShape{32, 2, 23, 23}, TensorShape{2, 2, 2, 2, 2, 7}},
  195. {TensorShape{3, 2, 23, 23}, TensorShape{2, 2, 2, 2, 2, 7}}, 0, 0,
  196. local_share_param);
  197. test_fastrun_opr<opr::LocalShareBackwardData, 3>(
  198. {TensorShape{3, 3, 128, 1, 1, 128}, TensorShape{32, 128, 24, 24},
  199. TensorShape{32, 128, 24, 24}},
  200. {TensorShape{3, 3, 128, 1, 1, 128}, TensorShape{2, 128, 24, 24},
  201. TensorShape{2, 128, 24, 24}});
  202. test_fastrun_opr<opr::LocalShareBackwardFilter, 3>(
  203. {TensorShape{12, 3, 36, 36}, TensorShape{12, 4, 35, 35},
  204. TensorShape{3, 3, 3, 3, 3, 4}},
  205. {TensorShape{4, 3, 36, 36}, TensorShape{4, 4, 35, 35},
  206. TensorShape{3, 3, 3, 3, 3, 4}});
  207. }
  208. TEST(TestOprDNN, FastrunIgnoreBatchSizeDeformableConv) {
  209. REQUIRE_GPU(1);
  210. test_fastrun_opr<opr::DeformableConvForward, 4>(
  211. {TensorShape{12, 6, 20, 20}, TensorShape{6, 6, 3, 3},
  212. TensorShape{12, 18, 18, 18}, TensorShape{12, 9, 18, 18}},
  213. {TensorShape{4, 6, 20, 20}, TensorShape{6, 6, 3, 3},
  214. TensorShape{4, 18, 18, 18}, TensorShape{4, 9, 18, 18}});
  215. test_fastrun_opr<opr::DeformableConvBackwardData, 5>(
  216. {TensorShape{12, 6, 20, 20}, TensorShape{6, 6, 3, 3},
  217. TensorShape{12, 18, 18, 18}, TensorShape{12, 9, 18, 18},
  218. TensorShape{12, 6, 18, 18}},
  219. {TensorShape{4, 6, 20, 20},
  220. TensorShape{6, 6, 3, 3},
  221. TensorShape{4, 18, 18, 18},
  222. TensorShape{4, 9, 18, 18},
  223. TensorShape{4, 6, 18, 18}});
  224. test_fastrun_opr<opr::DeformableConvBackwardFilter, 5>(
  225. {TensorShape{12, 6, 20, 20}, TensorShape{6, 6, 3, 3},
  226. TensorShape{12, 18, 18, 18}, TensorShape{12, 9, 18, 18},
  227. TensorShape{12, 6, 18, 18}},
  228. {TensorShape{4, 6, 20, 20}, TensorShape{6, 6, 3, 3},
  229. TensorShape{4, 18, 18, 18}, TensorShape{4, 9, 18, 18},
  230. TensorShape{4, 6, 18, 18}});
  231. }
  232. TEST(TestOprDNN, FastrunIgnoreBatchSizeMatrixMul) {
  233. REQUIRE_GPU(1);
  234. //! fastrun_shared_batch_size == 20
  235. //! {20(12), 12(1)}, {12(12), 20(1)} -> {20(12), 20(1)} origin
  236. //! {12(10), 20(1)}, {12(12), 20(1)} -> {20(12), 20(1)} transA
  237. //! {12(10), 20(1)}, {20(12), 12(1)} -> {20(12), 20(1)} transA, transB
  238. //! {20(12), 12(1)}, {20(12), 12(1)} -> {20(12), 20(1)} transB
  239. //!
  240. //! {20(12), 12(1)}, {12(12), 20(1)} -> {20(12), 20(1)} origin duplicate
  241. //! {12(4), 20(1)}, {12(12), 20(1)} -> {20(12), 20(1)} transA
  242. //! {12(4), 20(1)}, {20(12), 12(1)} -> {20(12), 20(1)} transA, transB
  243. //! {20(12), 12(1)}, {20(12), 12(1)} -> {20(12), 20(1)} transB duplicate
  244. test_fastrun_opr<opr::MatrixMul, 2>(
  245. {TensorShape{10, 12}, TensorShape{12, 12}},
  246. {TensorShape{4, 12}, TensorShape{12, 12}}, 4, 2);
  247. }
  248. TEST(TestOprDNN, FastrunIgnoreBatchSizeBatchedMatrixMul) {
  249. REQUIRE_GPU(1);
  250. //! fastrun_shared_batch_size == 20
  251. //! {20(48), 6(8), 8(1)}, {20(32), 8(4), 4(1)} -> {20(24), 6(4), 4(1)} origin
  252. //! {20(48), 8(6), 6(1)}, {20(32), 8(4), 4(1)} -> {20(24), 6(4), 4(1)} transA
  253. //! {20(48), 8(6), 6(1)}, {20(32), 4(8), 8(1)} -> {20(24), 6(4), 4(1)} transA, transB
  254. //! {20(48), 6(8), 8(1)}, {20(32), 4(8), 8(1)} -> {20(24), 6(4), 4(1)} transB
  255. //!
  256. //! {20(48), 6(8), 8(1)}, {20(32), 8(4), 4(1)} -> {20(24), 6(4), 4(1)} origin duplicate
  257. //! {20(48), 8(6), 6(1)}, {20(32), 8(4), 4(1)} -> {20(24), 6(4), 4(1)} transA duplicate
  258. //! {20(48), 8(6), 6(1)}, {20(32), 4(8), 8(1)} -> {20(24), 6(4), 4(1)} transA, transB duplicate
  259. //! {20(48), 6(8), 8(1)}, {20(32), 4(8), 8(1)} -> {20(24), 6(4), 4(1)} transB duplicate
  260. test_fastrun_opr<opr::BatchedMatrixMul, 2>(
  261. {TensorShape{12, 6, 8}, TensorShape{12, 8, 4}},
  262. {TensorShape{4, 6, 8}, TensorShape{4, 8, 4}});
  263. }
  264. #endif // MGB_ENABLE_FASTRUN
  265. #endif // MGB_CUDA
  266. } // anonymous namespace
  267. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}

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