benchmark.h 5.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include "ultra_infer/benchmark/option.h"
  16. #include "ultra_infer/benchmark/results.h"
  17. #include "ultra_infer/core/config.h"
  18. #include "ultra_infer/utils/perf.h"
  19. #include "ultra_infer/utils/utils.h"
  20. #ifdef ENABLE_BENCHMARK
  21. #define __RUNTIME_PROFILE_LOOP_BEGIN(option, base_loop) \
  22. int __p_loop = (base_loop); \
  23. const bool __p_enable_profile = option.enable_profile; \
  24. const bool __p_include_h2d_d2h = option.include_h2d_d2h; \
  25. const int __p_repeats = option.repeats; \
  26. const int __p_warmup = option.warmup; \
  27. if (__p_enable_profile && (!__p_include_h2d_d2h)) { \
  28. __p_loop = (__p_repeats) + (__p_warmup); \
  29. FDINFO << option << std::endl; \
  30. } \
  31. TimeCounter __p_tc; \
  32. bool __p_tc_start = false; \
  33. for (int __p_i = 0; __p_i < __p_loop; ++__p_i) { \
  34. if (__p_i >= (__p_warmup) && (!__p_tc_start)) { \
  35. __p_tc.Start(); \
  36. __p_tc_start = true; \
  37. }
  38. #define __RUNTIME_PROFILE_LOOP_END(result) \
  39. } \
  40. if ((__p_enable_profile && (!__p_include_h2d_d2h))) { \
  41. if (__p_tc_start) { \
  42. __p_tc.End(); \
  43. double __p_tc_duration = __p_tc.Duration(); \
  44. result.time_of_runtime = \
  45. __p_tc_duration / static_cast<double>(__p_repeats); \
  46. } \
  47. }
  48. #define __RUNTIME_PROFILE_LOOP_H2D_D2H_BEGIN(option, base_loop) \
  49. int __p_loop_h = (base_loop); \
  50. const bool __p_enable_profile_h = option.enable_profile; \
  51. const bool __p_include_h2d_d2h_h = option.include_h2d_d2h; \
  52. const int __p_repeats_h = option.repeats; \
  53. const int __p_warmup_h = option.warmup; \
  54. if (__p_enable_profile_h && __p_include_h2d_d2h_h) { \
  55. __p_loop_h = (__p_repeats_h) + (__p_warmup_h); \
  56. FDINFO << option << std::endl; \
  57. } \
  58. TimeCounter __p_tc_h; \
  59. bool __p_tc_start_h = false; \
  60. for (int __p_i_h = 0; __p_i_h < __p_loop_h; ++__p_i_h) { \
  61. if (__p_i_h >= (__p_warmup_h) && (!__p_tc_start_h)) { \
  62. __p_tc_h.Start(); \
  63. __p_tc_start_h = true; \
  64. }
  65. #define __RUNTIME_PROFILE_LOOP_H2D_D2H_END(result) \
  66. } \
  67. if ((__p_enable_profile_h && __p_include_h2d_d2h_h)) { \
  68. if (__p_tc_start_h) { \
  69. __p_tc_h.End(); \
  70. double __p_tc_duration_h = __p_tc_h.Duration(); \
  71. result.time_of_runtime = \
  72. __p_tc_duration_h / static_cast<double>(__p_repeats_h); \
  73. } \
  74. }
  75. #else
  76. #define __RUNTIME_PROFILE_LOOP_BEGIN(option, base_loop) \
  77. for (int __p_i = 0; __p_i < (base_loop); ++__p_i) {
  78. #define __RUNTIME_PROFILE_LOOP_END(result) }
  79. #define __RUNTIME_PROFILE_LOOP_H2D_D2H_BEGIN(option, base_loop) \
  80. for (int __p_i_h = 0; __p_i_h < (base_loop); ++__p_i_h) {
  81. #define __RUNTIME_PROFILE_LOOP_H2D_D2H_END(result) }
  82. #endif