utils.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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/core/fd_tensor.h"
  16. #include "ultra_infer/utils/utils.h"
  17. #include <memory>
  18. #include <thread> // NOLINT
  19. #include <unordered_map>
  20. #if defined(ENABLE_BENCHMARK) && defined(ENABLE_VISION)
  21. #include "ultra_infer/vision/common/result.h"
  22. #endif
  23. namespace ultra_infer {
  24. namespace benchmark {
  25. #if defined(ENABLE_BENCHMARK)
  26. /*! @brief ResourceUsageMonitor object used when to collect memory info.
  27. */
  28. class ULTRAINFER_DECL ResourceUsageMonitor {
  29. public:
  30. /** \brief Set sampling_interval_ms and gpu_id for ResourceUsageMonitor.
  31. *
  32. * \param[in] sampling_interval_ms How often to collect memory info(ms).
  33. * \param[in] gpu_id Device(gpu) id, default 0.
  34. */
  35. explicit ResourceUsageMonitor(int sampling_interval_ms, int gpu_id = 0);
  36. ~ResourceUsageMonitor() { StopInternal(); }
  37. /// Start memory info collect
  38. void Start();
  39. /// Stop memory info collect
  40. void Stop();
  41. /// Get maximum cpu memory usage
  42. float GetMaxCpuMem() const {
  43. if (!is_supported_ || check_memory_thd_ == nullptr) {
  44. return -1.0f;
  45. }
  46. return max_cpu_mem_;
  47. }
  48. /// Get maximum gpu memory usage
  49. float GetMaxGpuMem() const {
  50. if (!is_supported_ || check_memory_thd_ == nullptr) {
  51. return -1.0f;
  52. }
  53. return max_gpu_mem_;
  54. }
  55. /// Get maximum gpu util
  56. float GetMaxGpuUtil() const {
  57. if (!is_supported_ || check_memory_thd_ == nullptr) {
  58. return -1.0f;
  59. }
  60. return max_gpu_util_;
  61. }
  62. ResourceUsageMonitor(ResourceUsageMonitor &) = delete;
  63. ResourceUsageMonitor &operator=(const ResourceUsageMonitor &) = delete;
  64. ResourceUsageMonitor(ResourceUsageMonitor &&) = delete;
  65. ResourceUsageMonitor &operator=(const ResourceUsageMonitor &&) = delete;
  66. private:
  67. void StopInternal();
  68. // Get current gpu memory info
  69. std::string GetCurrentGpuMemoryInfo(int device_id);
  70. bool is_supported_ = false;
  71. bool stop_signal_ = false;
  72. const int sampling_interval_;
  73. float max_cpu_mem_ = 0.0f; // MB
  74. float max_gpu_mem_ = 0.0f; // MB
  75. float max_gpu_util_ = 0.0f;
  76. const int gpu_id_ = 0;
  77. std::unique_ptr<std::thread> check_memory_thd_ = nullptr;
  78. };
  79. // Remove the ch characters at both ends of str
  80. ULTRAINFER_DECL std::string Strip(const std::string &str, char ch = ' ');
  81. // Split string
  82. ULTRAINFER_DECL void Split(const std::string &s,
  83. std::vector<std::string> &tokens, char delim = ' ');
  84. /// Diff values for precision evaluation
  85. struct ULTRAINFER_DECL BaseDiff {};
  86. struct ULTRAINFER_DECL EvalStatis {
  87. double mean = -1.0;
  88. double min = -1.0;
  89. double max = -1.0;
  90. };
  91. struct ULTRAINFER_DECL TensorDiff : public BaseDiff {
  92. EvalStatis data;
  93. };
  94. #if defined(ENABLE_VISION)
  95. struct ULTRAINFER_DECL DetectionDiff : public BaseDiff {
  96. EvalStatis boxes;
  97. EvalStatis scores;
  98. EvalStatis labels;
  99. };
  100. struct ULTRAINFER_DECL ClassifyDiff : public BaseDiff {
  101. EvalStatis scores;
  102. EvalStatis labels;
  103. };
  104. struct ULTRAINFER_DECL SegmentationDiff : public BaseDiff {
  105. EvalStatis scores;
  106. EvalStatis labels;
  107. };
  108. struct ULTRAINFER_DECL OCRDetDiff : public BaseDiff {
  109. EvalStatis boxes;
  110. };
  111. struct ULTRAINFER_DECL MattingDiff : public BaseDiff {
  112. EvalStatis alpha;
  113. EvalStatis foreground;
  114. };
  115. #endif // ENABLE_VISION
  116. #endif // ENABLE_BENCHMARK
  117. /// Utils for precision evaluation
  118. struct ULTRAINFER_DECL ResultManager {
  119. #if defined(ENABLE_BENCHMARK)
  120. /// Save & Load functions for FDTensor result.
  121. static bool SaveFDTensor(const FDTensor &tensor, const std::string &path);
  122. static bool LoadFDTensor(FDTensor *tensor, const std::string &path);
  123. /// Calculate diff value between two FDTensor results.
  124. static TensorDiff CalculateDiffStatis(const FDTensor &lhs,
  125. const FDTensor &rhs);
  126. /// Save Benchmark data
  127. static void SaveBenchmarkResult(const std::string &res,
  128. const std::string &path);
  129. /// Load Benchmark config
  130. static bool LoadBenchmarkConfig(
  131. const std::string &path,
  132. std::unordered_map<std::string, std::string> *config_info);
  133. /// Get Input Shapes
  134. static std::vector<std::vector<int32_t>>
  135. GetInputShapes(const std::string &raw_shapes);
  136. /// Get Input Names
  137. static std::vector<std::string> GetInputNames(const std::string &raw_names);
  138. /// Get Input Dtypes
  139. static std::vector<FDDataType> GetInputDtypes(const std::string &raw_dtypes);
  140. /// Split string
  141. static std::vector<std::string> SplitStr(const std::string &raw_str,
  142. char delim = ':');
  143. #if defined(ENABLE_VISION)
  144. /// Save & Load functions for basic results.
  145. static bool SaveDetectionResult(const vision::DetectionResult &res,
  146. const std::string &path);
  147. static bool LoadDetectionResult(vision::DetectionResult *res,
  148. const std::string &path);
  149. static bool SaveClassifyResult(const vision::ClassifyResult &res,
  150. const std::string &path);
  151. static bool LoadClassifyResult(vision::ClassifyResult *res,
  152. const std::string &path);
  153. static bool SaveSegmentationResult(const vision::SegmentationResult &res,
  154. const std::string &path);
  155. static bool LoadSegmentationResult(vision::SegmentationResult *res,
  156. const std::string &path);
  157. static bool SaveOCRDetResult(const std::vector<std::array<int, 8>> &res,
  158. const std::string &path);
  159. static bool LoadOCRDetResult(std::vector<std::array<int, 8>> *res,
  160. const std::string &path);
  161. static bool SaveMattingResult(const vision::MattingResult &res,
  162. const std::string &path);
  163. static bool LoadMattingResult(vision::MattingResult *res,
  164. const std::string &path);
  165. /// Calculate diff value between two basic results.
  166. static DetectionDiff CalculateDiffStatis(const vision::DetectionResult &lhs,
  167. const vision::DetectionResult &rhs,
  168. const float &score_threshold = 0.3f);
  169. static ClassifyDiff CalculateDiffStatis(const vision::ClassifyResult &lhs,
  170. const vision::ClassifyResult &rhs);
  171. static SegmentationDiff
  172. CalculateDiffStatis(const vision::SegmentationResult &lhs,
  173. const vision::SegmentationResult &rhs);
  174. static OCRDetDiff
  175. CalculateDiffStatis(const std::vector<std::array<int, 8>> &lhs,
  176. const std::vector<std::array<int, 8>> &rhs);
  177. static MattingDiff CalculateDiffStatis(const vision::MattingResult &lhs,
  178. const vision::MattingResult &rhs);
  179. #endif // ENABLE_VISION
  180. #endif // ENABLE_BENCHMARK
  181. };
  182. } // namespace benchmark
  183. } // namespace ultra_infer