Files
FastDeploy/fastdeploy/benchmark/utils.h
DefTruth ee85a3cade [Benchmark] Add precision evaluation api from benchmark (#1310)
* [Benchmark] Init benchmark precision api

* [Benchmark] Init benchmark precision api

* [Benchmark] Add benchmark precision api

* [Benchmark] Calculate the statis of diff

* [Benchmark] Calculate the statis of diff

* [Benchmark] Calculate the statis of diff

* [Benchmark] Calculate the statis of diff

* [Benchmark] Calculate the statis of diff

* [Benchmark] Add SplitDataLine utils

* [Benchmark] Add LexSortByXY func

* [Benchmark] Add LexSortByXY func

* [Benchmark] Add LexSortDetectionResultByXY func

* [Benchmark] Add LexSortDetectionResultByXY func

* [Benchmark] Add tensor diff presicion test

* [Benchmark] fixed conflicts

* [Benchmark] fixed calc tensor diff

* fixed build bugs

* fixed ci bugs when WITH_TESTING=ON
2023-02-16 17:16:14 +08:00

140 lines
4.5 KiB
C++
Executable File

// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <memory>
#include <thread> // NOLINT
#include "fastdeploy/utils/utils.h"
#include "fastdeploy/core/fd_tensor.h"
#if defined(ENABLE_BENCHMARK) && defined(ENABLE_VISION)
#include "fastdeploy/vision/common/result.h"
#endif
namespace fastdeploy {
namespace benchmark {
#if defined(ENABLE_BENCHMARK)
/*! @brief ResourceUsageMonitor object used when to collect memory info.
*/
class FASTDEPLOY_DECL ResourceUsageMonitor {
public:
/** \brief Set sampling_interval_ms and gpu_id for ResourceUsageMonitor.
*
* \param[in] sampling_interval_ms How often to collect memory info(ms).
* \param[in] gpu_id Device(gpu) id, default 0.
*/
explicit ResourceUsageMonitor(int sampling_interval_ms, int gpu_id = 0);
~ResourceUsageMonitor() { StopInternal(); }
/// Start memory info collect
void Start();
/// Stop memory info collect
void Stop();
/// Get maximum cpu memory usage
float GetMaxCpuMem() const {
if (!is_supported_ || check_memory_thd_ == nullptr) {
return -1.0f;
}
return max_cpu_mem_;
}
/// Get maximum gpu memory usage
float GetMaxGpuMem() const {
if (!is_supported_ || check_memory_thd_ == nullptr) {
return -1.0f;
}
return max_gpu_mem_;
}
/// Get maximum gpu util
float GetMaxGpuUtil() const {
if (!is_supported_ || check_memory_thd_ == nullptr) {
return -1.0f;
}
return max_gpu_util_;
}
ResourceUsageMonitor(ResourceUsageMonitor&) = delete;
ResourceUsageMonitor& operator=(const ResourceUsageMonitor&) = delete;
ResourceUsageMonitor(ResourceUsageMonitor&&) = delete;
ResourceUsageMonitor& operator=(const ResourceUsageMonitor&&) = delete;
private:
void StopInternal();
// Get current gpu memory info
std::string GetCurrentGpuMemoryInfo(int device_id);
bool is_supported_ = false;
bool stop_signal_ = false;
const int sampling_interval_;
float max_cpu_mem_ = 0.0f; // MB
float max_gpu_mem_ = 0.0f; // MB
float max_gpu_util_ = 0.0f;
const int gpu_id_ = 0;
std::unique_ptr<std::thread> check_memory_thd_ = nullptr;
};
// Remove the ch characters at both ends of str
FASTDEPLOY_DECL std::string Strip(const std::string& str, char ch = ' ');
// Split string
FASTDEPLOY_DECL void Split(const std::string& s,
std::vector<std::string>& tokens,
char delim = ' ');
/// Diff values for precision evaluation
struct FASTDEPLOY_DECL BaseDiff {};
struct FASTDEPLOY_DECL EvalStatis {
double mean = -1.0;
double min = -1.0;
double max = -1.0;
};
struct FASTDEPLOY_DECL TensorDiff: public BaseDiff, public EvalStatis {};
#if defined(ENABLE_VISION)
struct FASTDEPLOY_DECL DetectionDiff: public BaseDiff, public EvalStatis {
EvalStatis boxes;
EvalStatis scores;
EvalStatis labels;
};
#endif // ENABLE_VISION
#endif // ENABLE_BENCHMARK
/// Utils for precision evaluation
struct FASTDEPLOY_DECL ResultManager {
#if defined(ENABLE_BENCHMARK)
/// Save & Load functions for FDTensor result.
static bool SaveFDTensor(const FDTensor& tensor, const std::string& path);
static bool LoadFDTensor(FDTensor* tensor, const std::string& path);
/// Calculate diff value between two FDTensor results.
static TensorDiff CalculateDiffStatis(FDTensor* lhs,
FDTensor* rhs);
#if defined(ENABLE_VISION)
/// Save & Load functions for basic results.
static bool SaveDetectionResult(const vision::DetectionResult& res,
const std::string& path);
static bool LoadDetectionResult(vision::DetectionResult* res,
const std::string& path);
/// Calculate diff value between two basic results.
static DetectionDiff CalculateDiffStatis(vision::DetectionResult* lhs,
vision::DetectionResult* rhs,
float score_threshold = 0.3f);
#endif // ENABLE_VISION
#endif // ENABLE_BENCHMARK
};
} // namespace benchmark
} // namespace fastdeploy