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.0 kB

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