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

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

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