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

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