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.

benchmark.cpp 8.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * \file dnn/test/cuda/benchmark.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/cuda/fixture.h"
  12. #include "test/common/tensor.h"
  13. #include "test/common/timer.h"
  14. #include "megdnn/oprs.h"
  15. #include "test/common/workspace_wrapper.h"
  16. #include "test/common/benchmarker.h"
  17. #include "src/cuda/utils.h"
  18. namespace megdnn {
  19. namespace test {
  20. #if MEGDNN_WITH_BENCHMARK
  21. TEST_F(CUDA, BENCHMARK_CONVOLUTION_8X8X32)
  22. {
  23. if (cuda::current_device_prop().major < 6) {
  24. printf("Skip CUDA.BENCHMARK_CONVOLUTION_8X8X32 test as current device"
  25. "doesn't support\n");
  26. return;
  27. }
  28. using Param = param::Convolution;
  29. auto run_1x1 = [&](size_t N, size_t OC, size_t IC, size_t H, size_t W) {
  30. Benchmarker<Convolution> benchmarker(handle_cuda());
  31. Param param_base;
  32. Param param_float = param_base, param_int = param_base;
  33. param_int.format = Param::Format::NHWC;
  34. TensorShape src_float{N, IC, H, W}, filter_float{OC, IC, 1, 1};
  35. TensorShape src_int{N, H, W, IC}, filter_int{OC, 1, 1, IC};
  36. benchmarker.set_display(false);
  37. auto time_in_ms_float = benchmarker.set_param(param_float)
  38. .set_dtype(0, dtype::Float32())
  39. .set_dtype(1, dtype::Float32())
  40. .execs({src_float, filter_float, {}});
  41. auto time_in_ms_int = benchmarker.set_param(param_int)
  42. .set_dtype(0, dtype::Int8())
  43. .set_dtype(1, dtype::Int8())
  44. .execs({src_int, filter_int, {}});
  45. std::cout << "1x1: N=" << N << " OC=" << OC << " IC=" << IC
  46. << " H=" << H << " W=" << W
  47. << " time_float=" << time_in_ms_float << "ms"
  48. << " time_int=" << time_in_ms_int << "ms" << std::endl;
  49. };
  50. auto run_chanwise = [&](size_t N, size_t C, size_t H, size_t W,
  51. size_t F) {
  52. size_t P = F/2;
  53. Benchmarker<Convolution> benchmarker(handle_cuda());
  54. Param param_base;
  55. param_base.pad_h = param_base.pad_w = P;
  56. param_base.sparse = Param::Sparse::GROUP;
  57. Param param_float = param_base;
  58. Param param_int = param_base;
  59. param_int.format = Param::Format::NHWC;
  60. TensorShape src_float{N, C, H, W}, filter_float{C, 1, 1, F, F};
  61. TensorShape src_int{N, H, W, C}, filter_int{C, 1, F, F, 1};
  62. benchmarker.set_display(false);
  63. auto time_in_ms_float = benchmarker.set_param(param_float)
  64. .set_dtype(0, dtype::Float32())
  65. .set_dtype(1, dtype::Float32())
  66. .execs({src_float, filter_float, {}});
  67. auto time_in_ms_int = benchmarker.set_param(param_int)
  68. .set_dtype(0, dtype::Int8())
  69. .set_dtype(1, dtype::Int8())
  70. .execs({src_int, filter_int, {}});
  71. std::cout << "chanwise: N=" << N << " C=" << C
  72. << " H=" << H << " W=" << W << " F=" << F
  73. << " time_float=" << time_in_ms_float << "ms"
  74. << " time_int=" << time_in_ms_int << "ms" << std::endl;
  75. };
  76. run_chanwise(1, 384, 56, 56, 3);
  77. run_1x1(1, 32, 32, 56, 56);
  78. run_1x1(1, 256, 256, 7, 7);
  79. }
  80. TEST_F(CUDA, BENCHMARK_REDUCE)
  81. {
  82. auto run = [&](size_t A, size_t B, size_t C) {
  83. Tensor<> src(handle_cuda(), TensorLayout({A, B, C}, dtype::Float32())),
  84. dst(handle_cuda(), TensorLayout({A, 1, C}, dtype::Float32()));
  85. auto opr = handle_cuda()->create_operator<Reduce>();
  86. opr->param().axis = 1;
  87. WorkspaceWrapper workspace(handle_cuda(), opr->get_workspace_in_bytes(
  88. src.layout(), dst.layout()));
  89. opr->exec(src.tensornd(), dst.tensornd(), workspace.workspace());
  90. Timer timer;
  91. megcoreSynchronize(handle_cuda()->megcore_computing_handle());
  92. timer.start();
  93. for (size_t i = 0; i < 10; ++i)
  94. opr->exec(src.tensornd(), dst.tensornd(), workspace.workspace());
  95. megcoreSynchronize(handle_cuda()->megcore_computing_handle());
  96. timer.stop();
  97. float time_in_us = timer.get_time_in_us();
  98. std::cout << "src = " << A << "x" << B << "x" << C << std::endl
  99. << "time = " << time_in_us / 1e3 << "ms" << std::endl;
  100. };
  101. run(65536, 64, 1);
  102. run(1, 268435455, 1);
  103. run(256, 1048575, 1);
  104. run(1, 1048575, 256);
  105. run(256, 4095, 256);
  106. }
  107. TEST_F(CUDA, BENCHMARK_BATCHED_MATRIX_MUL)
  108. {
  109. auto run = [&](size_t b, size_t m, size_t n, size_t k) {
  110. Tensor<> A(handle_cuda(), TensorLayout({b, m, k}, dtype::Float32()));
  111. Tensor<> B(handle_cuda(), TensorLayout({b, k, n}, dtype::Float32()));
  112. Tensor<> C(handle_cuda(), TensorLayout({b, m, n}, dtype::Float32()));
  113. auto opr = handle_cuda()->create_operator<BatchedMatrixMul>();
  114. WorkspaceWrapper workspace(handle_cuda(), opr->get_workspace_in_bytes(
  115. A.layout(), B.layout(), C.layout()));
  116. opr->exec(A.tensornd(), B.tensornd(), C.tensornd(),
  117. workspace.workspace());
  118. Timer timer;
  119. megcoreSynchronize(handle_cuda()->megcore_computing_handle());
  120. timer.start();
  121. opr->exec(A.tensornd(), B.tensornd(), C.tensornd(),
  122. workspace.workspace());
  123. megcoreSynchronize(handle_cuda()->megcore_computing_handle());
  124. timer.stop();
  125. float time_in_s = timer.get_time_in_us() / 1e6;
  126. float flo = b*m*n*k*2;
  127. float gflops = flo / time_in_s / 1e9;
  128. std::cout << "time_in_s = " << time_in_s << '\n'
  129. << "flo = " << flo << '\n'
  130. << "gflops = " << gflops << std::endl;
  131. };
  132. run(256, 256, 256, 256);
  133. }
  134. TEST_F(CUDA, BENCHMARK_MATRIX_MUL)
  135. {
  136. auto run = [&](size_t m, size_t n, size_t k) {
  137. Tensor<> A(handle_cuda(), TensorLayout({m, k}, dtype::Float32()));
  138. Tensor<> B(handle_cuda(), TensorLayout({k, n}, dtype::Float32()));
  139. Tensor<> C(handle_cuda(), TensorLayout({m, n}, dtype::Float32()));
  140. auto opr = handle_cuda()->create_operator<MatrixMul>();
  141. WorkspaceWrapper workspace(handle_cuda(), opr->get_workspace_in_bytes(
  142. A.layout(), B.layout(), C.layout()));
  143. opr->exec(A.tensornd(), B.tensornd(), C.tensornd(),
  144. workspace.workspace());
  145. Timer timer;
  146. megcoreSynchronize(handle_cuda()->megcore_computing_handle());
  147. timer.start();
  148. opr->exec(A.tensornd(), B.tensornd(), C.tensornd(),
  149. workspace.workspace());
  150. megcoreSynchronize(handle_cuda()->megcore_computing_handle());
  151. timer.stop();
  152. float time_in_s = timer.get_time_in_us() / 1e6;
  153. float flo = m*n*k*2;
  154. float gflops = flo / time_in_s / 1e9;
  155. std::cout << "time_in_s = " << time_in_s << '\n'
  156. << "flo = " << flo << '\n'
  157. << "gflops = " << gflops << std::endl;
  158. };
  159. run(4096, 4096, 4096);
  160. }
  161. TEST_F(CUDA, BENCHMARK_LOCAL)
  162. {
  163. auto run = [&](size_t N, size_t IC, size_t IH, size_t IW,
  164. size_t OC, size_t OH, size_t OW, size_t FH, size_t FW) {
  165. Tensor<> src(handle_cuda(), TensorLayout({N, IC, IH, IW},
  166. dtype::Float32()));
  167. Tensor<> filter(handle_cuda(), TensorLayout({OH, OW, IC, FH, FW, OC},
  168. dtype::Float32()));
  169. Tensor<> dst(handle_cuda(), TensorLayout({N, OC, OH, OW},
  170. dtype::Float32()));
  171. auto opr = handle_cuda()->create_operator<Local>();
  172. WorkspaceWrapper workspace(handle_cuda(), opr->get_workspace_in_bytes(
  173. src.layout(), filter.layout(), dst.layout()));
  174. opr->exec(src.tensornd(), filter.tensornd(), dst.tensornd(),
  175. workspace.workspace());
  176. Timer timer;
  177. megcoreSynchronize(handle_cuda()->megcore_computing_handle());
  178. timer.start();
  179. opr->exec(src.tensornd(), filter.tensornd(), dst.tensornd(),
  180. workspace.workspace());
  181. megcoreSynchronize(handle_cuda()->megcore_computing_handle());
  182. timer.stop();
  183. float time_in_us = timer.get_time_in_us();
  184. std::cout << "time = " << time_in_us << "us" << std::endl;
  185. };
  186. run(32, 64, 7, 7, 64, 5, 5, 3, 3);
  187. }
  188. #endif
  189. } // namespace test
  190. } // namespace megdnn
  191. // vim: syntax=cpp.doxygen

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