[Benchmark] avoid Result mem copy for cpp benchmark (#1203)

* avoid mem copy for cpp benchmark

* set CMAKE_BUILD_TYPE to Release
This commit is contained in:
WJJ1995
2023-02-06 10:19:44 +08:00
committed by GitHub
parent 870551f3f5
commit cfc7af2d45
3 changed files with 115 additions and 58 deletions

View File

@@ -37,7 +37,7 @@ include(${PROJECT_SOURCE_DIR}/cmake/utils.cmake)
# Set C++11 as standard for the whole project # Set C++11 as standard for the whole project
if(NOT MSVC) if(NOT MSVC)
set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "-Wno-format") set(CMAKE_CXX_FLAGS "-Wno-format -g0 -O3")
add_definitions(-D_GLIBCXX_USE_CXX11_ABI=1) add_definitions(-D_GLIBCXX_USE_CXX11_ABI=1)
endif(NOT MSVC) endif(NOT MSVC)
@@ -153,6 +153,8 @@ get_osx_architecture()
##################################### Building: FastDeploy C++ SDK ####################################### ##################################### Building: FastDeploy C++ SDK #######################################
add_definitions(-DFASTDEPLOY_LIB) add_definitions(-DFASTDEPLOY_LIB)
# set CMAKE_BUILD_TYPE to Release
add_definitions(-DCMAKE_BUILD_TYPE=Release)
# configure files before glob sources. # configure files before glob sources.
configure_file(${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/core/config.h.in ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/core/config.h) configure_file(${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/core/config.h.in ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/core/config.h)
configure_file(${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/pybind/main.cc.in ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/pybind/main.cc) configure_file(${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/pybind/main.cc.in ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/pybind/main.cc)

View File

@@ -16,11 +16,16 @@
namespace fastdeploy { namespace fastdeploy {
namespace vision { namespace vision {
void ClassifyResult::Clear() { void ClassifyResult::Free() {
std::vector<int32_t>().swap(label_ids); std::vector<int32_t>().swap(label_ids);
std::vector<float>().swap(scores); std::vector<float>().swap(scores);
} }
void ClassifyResult::Clear() {
label_ids.clear();
scores.clear();
}
std::string ClassifyResult::Str() { std::string ClassifyResult::Str() {
std::string out; std::string out;
out = "ClassifyResult(\nlabel_ids: "; out = "ClassifyResult(\nlabel_ids: ";
@@ -47,11 +52,16 @@ void Mask::Reserve(int size) { data.reserve(size); }
void Mask::Resize(int size) { data.resize(size); } void Mask::Resize(int size) { data.resize(size); }
void Mask::Clear() { void Mask::Free() {
std::vector<uint8_t>().swap(data); std::vector<uint8_t>().swap(data);
std::vector<int64_t>().swap(shape); std::vector<int64_t>().swap(shape);
} }
void Mask::Clear() {
data.clear();
shape.clear();
}
std::string Mask::Str() { std::string Mask::Str() {
std::string out = "Mask("; std::string out = "Mask(";
size_t ndim = shape.size(); size_t ndim = shape.size();
@@ -94,7 +104,7 @@ DetectionResult& DetectionResult::operator=(DetectionResult&& other) {
return *this; return *this;
} }
void DetectionResult::Clear() { void DetectionResult::Free() {
std::vector<std::array<float, 4>>().swap(boxes); std::vector<std::array<float, 4>>().swap(boxes);
std::vector<float>().swap(scores); std::vector<float>().swap(scores);
std::vector<int32_t>().swap(label_ids); std::vector<int32_t>().swap(label_ids);
@@ -102,18 +112,30 @@ void DetectionResult::Clear() {
contain_masks = false; contain_masks = false;
} }
void DetectionResult::Clear() {
boxes.clear();
scores.clear();
label_ids.clear();
masks.clear();
contain_masks = false;
}
void DetectionResult::Reserve(int size) { void DetectionResult::Reserve(int size) {
boxes.reserve(size); boxes.reserve(size);
scores.reserve(size); scores.reserve(size);
label_ids.reserve(size); label_ids.reserve(size);
if (contain_masks) {
masks.reserve(size); masks.reserve(size);
}
} }
void DetectionResult::Resize(int size) { void DetectionResult::Resize(int size) {
boxes.resize(size); boxes.resize(size);
scores.resize(size); scores.resize(size);
label_ids.resize(size); label_ids.resize(size);
if (contain_masks) {
masks.resize(size); masks.resize(size);
}
} }
std::string DetectionResult::Str() { std::string DetectionResult::Str() {
@@ -139,12 +161,18 @@ std::string DetectionResult::Str() {
return out; return out;
} }
void KeyPointDetectionResult::Clear() { void KeyPointDetectionResult::Free() {
std::vector<std::array<float, 2>>().swap(keypoints); std::vector<std::array<float, 2>>().swap(keypoints);
std::vector<float>().swap(scores); std::vector<float>().swap(scores);
num_joints = -1; num_joints = -1;
} }
void KeyPointDetectionResult::Clear() {
keypoints.clear();
scores.clear();
num_joints = -1;
}
void KeyPointDetectionResult::Reserve(int size) { keypoints.reserve(size); } void KeyPointDetectionResult::Reserve(int size) { keypoints.reserve(size); }
void KeyPointDetectionResult::Resize(int size) { keypoints.resize(size); } void KeyPointDetectionResult::Resize(int size) { keypoints.resize(size); }
@@ -155,8 +183,8 @@ std::string KeyPointDetectionResult::Str() {
out = "KeyPointDetectionResult: [x, y, conf]\n"; out = "KeyPointDetectionResult: [x, y, conf]\n";
for (size_t i = 0; i < keypoints.size(); ++i) { for (size_t i = 0; i < keypoints.size(); ++i) {
out = out + std::to_string(keypoints[i][0]) + "," + out = out + std::to_string(keypoints[i][0]) + "," +
std::to_string(keypoints[i][1]) + ", " + std::to_string(keypoints[i][1]) + ", " + std::to_string(scores[i]) +
std::to_string(scores[i]) + "\n"; "\n";
} }
out += "num_joints:" + std::to_string(num_joints) + "\n"; out += "num_joints:" + std::to_string(num_joints) + "\n";
return out; return out;
@@ -170,22 +198,22 @@ void OCRResult::Clear() {
cls_labels.clear(); cls_labels.clear();
} }
void MOTResult::Clear(){ void MOTResult::Clear() {
boxes.clear(); boxes.clear();
ids.clear(); ids.clear();
scores.clear(); scores.clear();
class_ids.clear(); class_ids.clear();
} }
std::string MOTResult::Str(){ std::string MOTResult::Str() {
std::string out; std::string out;
out = "MOTResult:\nall boxes counts: "+std::to_string(boxes.size())+"\n"; out = "MOTResult:\nall boxes counts: " + std::to_string(boxes.size()) + "\n";
out += "[xmin\tymin\txmax\tymax\tid\tscore]\n"; out += "[xmin\tymin\txmax\tymax\tid\tscore]\n";
for (size_t i = 0; i < boxes.size(); ++i) { for (size_t i = 0; i < boxes.size(); ++i) {
out = out + "["+ std::to_string(boxes[i][0]) + "\t" + out = out + "[" + std::to_string(boxes[i][0]) + "\t" +
std::to_string(boxes[i][1]) + "\t" + std::to_string(boxes[i][2]) + std::to_string(boxes[i][1]) + "\t" + std::to_string(boxes[i][2]) +
"\t" + std::to_string(boxes[i][3]) + "\t" + "\t" + std::to_string(boxes[i][3]) + "\t" + std::to_string(ids[i]) +
std::to_string(ids[i]) + "\t" + std::to_string(scores[i]) + "]\n"; "\t" + std::to_string(scores[i]) + "]\n";
} }
return out; return out;
} }
@@ -197,13 +225,20 @@ FaceDetectionResult::FaceDetectionResult(const FaceDetectionResult& res) {
landmarks_per_face = res.landmarks_per_face; landmarks_per_face = res.landmarks_per_face;
} }
void FaceDetectionResult::Clear() { void FaceDetectionResult::Free() {
std::vector<std::array<float, 4>>().swap(boxes); std::vector<std::array<float, 4>>().swap(boxes);
std::vector<float>().swap(scores); std::vector<float>().swap(scores);
std::vector<std::array<float, 2>>().swap(landmarks); std::vector<std::array<float, 2>>().swap(landmarks);
landmarks_per_face = 0; landmarks_per_face = 0;
} }
void FaceDetectionResult::Clear() {
boxes.clear();
scores.clear();
landmarks.clear();
landmarks_per_face = 0;
}
void FaceDetectionResult::Reserve(int size) { void FaceDetectionResult::Reserve(int size) {
boxes.reserve(size); boxes.reserve(size);
scores.reserve(size); scores.reserve(size);
@@ -257,23 +292,22 @@ std::string FaceDetectionResult::Str() {
return out; return out;
} }
void FaceAlignmentResult::Clear() { void FaceAlignmentResult::Free() {
std::vector<std::array<float, 2>>().swap(landmarks); std::vector<std::array<float, 2>>().swap(landmarks);
} }
void FaceAlignmentResult::Reserve(int size) { void FaceAlignmentResult::Clear() { landmarks.clear(); }
landmarks.resize(size);
}
void FaceAlignmentResult::Resize(int size) { void FaceAlignmentResult::Reserve(int size) { landmarks.resize(size); }
landmarks.resize(size);
} void FaceAlignmentResult::Resize(int size) { landmarks.resize(size); }
std::string FaceAlignmentResult::Str() { std::string FaceAlignmentResult::Str() {
std::string out; std::string out;
out = "FaceAlignmentResult: [x, y]\n"; out = "FaceAlignmentResult: [x, y]\n";
out = out + "There are " +std::to_string(landmarks.size()) + " landmarks, the top 10 are listed as below:\n"; out = out + "There are " + std::to_string(landmarks.size()) +
" landmarks, the top 10 are listed as below:\n";
int landmarks_size = landmarks.size(); int landmarks_size = landmarks.size();
size_t result_length = std::min(10, landmarks_size); size_t result_length = std::min(10, landmarks_size);
for (size_t i = 0; i < result_length; ++i) { for (size_t i = 0; i < result_length; ++i) {
@@ -355,7 +389,9 @@ FaceRecognitionResult::FaceRecognitionResult(const FaceRecognitionResult& res) {
embedding.assign(res.embedding.begin(), res.embedding.end()); embedding.assign(res.embedding.begin(), res.embedding.end());
} }
void FaceRecognitionResult::Clear() { std::vector<float>().swap(embedding); } void FaceRecognitionResult::Free() { std::vector<float>().swap(embedding); }
void FaceRecognitionResult::Clear() { embedding.clear(); }
void FaceRecognitionResult::Reserve(int size) { embedding.reserve(size); } void FaceRecognitionResult::Reserve(int size) { embedding.reserve(size); }
@@ -536,28 +572,23 @@ std::string OCRResult::Str() {
return no_result; return no_result;
} }
void HeadPoseResult::Clear() { void HeadPoseResult::Free() { std::vector<float>().swap(euler_angles); }
std::vector<float>().swap(euler_angles);
}
void HeadPoseResult::Reserve(int size) { void HeadPoseResult::Clear() { euler_angles.clear(); }
euler_angles.resize(size);
}
void HeadPoseResult::Resize(int size) { void HeadPoseResult::Reserve(int size) { euler_angles.resize(size); }
euler_angles.resize(size);
} void HeadPoseResult::Resize(int size) { euler_angles.resize(size); }
std::string HeadPoseResult::Str() { std::string HeadPoseResult::Str() {
std::string out; std::string out;
out = "HeadPoseResult: [yaw, pitch, roll]\n"; out = "HeadPoseResult: [yaw, pitch, roll]\n";
out = out + "yaw: " + std::to_string(euler_angles[0]) + "\n" + out = out + "yaw: " + std::to_string(euler_angles[0]) + "\n" + "pitch: " +
"pitch: " + std::to_string(euler_angles[1]) + "\n" + std::to_string(euler_angles[1]) + "\n" + "roll: " +
"roll: " + std::to_string(euler_angles[2]) + "\n"; std::to_string(euler_angles[2]) + "\n";
return out; return out;
} }
} // namespace vision } // namespace vision
} // namespace fastdeploy } // namespace fastdeploy

View File

@@ -51,9 +51,12 @@ struct FASTDEPLOY_DECL ClassifyResult : public BaseResult {
std::vector<float> scores; std::vector<float> scores;
ResultType type = ResultType::CLASSIFY; ResultType type = ResultType::CLASSIFY;
/// Clear result /// Clear ClassifyResult
void Clear(); void Clear();
/// Clear ClassifyResult and free the memory
void Free();
/// Copy constructor /// Copy constructor
ClassifyResult(const ClassifyResult& other) = default; ClassifyResult(const ClassifyResult& other) = default;
/// Move assignment /// Move assignment
@@ -72,9 +75,12 @@ struct FASTDEPLOY_DECL Mask : public BaseResult {
std::vector<int64_t> shape; // (H,W) ... std::vector<int64_t> shape; // (H,W) ...
ResultType type = ResultType::MASK; ResultType type = ResultType::MASK;
/// clear mask /// clear Mask result
void Clear(); void Clear();
/// Clear Mask result and free the memory
void Free();
/// Return a mutable pointer of the mask data buffer /// Return a mutable pointer of the mask data buffer
void* Data() { return data.data(); } void* Data() { return data.data(); }
@@ -117,9 +123,12 @@ struct FASTDEPLOY_DECL DetectionResult : public BaseResult {
/// Move assignment /// Move assignment
DetectionResult& operator=(DetectionResult&& other); DetectionResult& operator=(DetectionResult&& other);
/// Clear detection result /// Clear DetectionResult
void Clear(); void Clear();
/// Clear DetectionResult and free the memory
void Free();
void Reserve(int size); void Reserve(int size);
void Resize(int size); void Resize(int size);
@@ -140,9 +149,12 @@ struct FASTDEPLOY_DECL KeyPointDetectionResult : public BaseResult {
int num_joints = -1; int num_joints = -1;
ResultType type = ResultType::KEYPOINT_DETECTION; ResultType type = ResultType::KEYPOINT_DETECTION;
/// Clear detection result /// Clear KeyPointDetectionResult
void Clear(); void Clear();
/// Clear KeyPointDetectionResult and free the memory
void Free();
void Reserve(int size); void Reserve(int size);
void Resize(int size); void Resize(int size);
@@ -215,9 +227,12 @@ struct FASTDEPLOY_DECL FaceDetectionResult : public BaseResult {
FaceDetectionResult() { landmarks_per_face = 0; } FaceDetectionResult() { landmarks_per_face = 0; }
FaceDetectionResult(const FaceDetectionResult& res); FaceDetectionResult(const FaceDetectionResult& res);
/// Clear detection result /// Clear FaceDetectionResult
void Clear(); void Clear();
/// Clear FaceDetectionResult and free the memory
void Free();
void Reserve(int size); void Reserve(int size);
void Resize(int size); void Resize(int size);
@@ -233,9 +248,12 @@ struct FASTDEPLOY_DECL FaceAlignmentResult : public BaseResult {
std::vector<std::array<float, 2>> landmarks; std::vector<std::array<float, 2>> landmarks;
ResultType type = ResultType::FACE_ALIGNMENT; ResultType type = ResultType::FACE_ALIGNMENT;
/// Clear facealignment result /// Clear FaceAlignmentResult
void Clear(); void Clear();
/// Clear FaceAlignmentResult and free the memory
void Free();
void Reserve(int size); void Reserve(int size);
void Resize(int size); void Resize(int size);
@@ -292,9 +310,12 @@ struct FASTDEPLOY_DECL FaceRecognitionResult : public BaseResult {
FaceRecognitionResult() {} FaceRecognitionResult() {}
FaceRecognitionResult(const FaceRecognitionResult& res); FaceRecognitionResult(const FaceRecognitionResult& res);
/// Clear detection result /// Clear FaceRecognitionResult
void Clear(); void Clear();
/// Clear FaceRecognitionResult and free the memory
void Free();
void Reserve(int size); void Reserve(int size);
void Resize(int size); void Resize(int size);
@@ -347,9 +368,12 @@ struct FASTDEPLOY_DECL HeadPoseResult : public BaseResult {
std::vector<float> euler_angles; std::vector<float> euler_angles;
ResultType type = ResultType::HEADPOSE; ResultType type = ResultType::HEADPOSE;
/// Clear headpose result /// Clear HeadPoseResult
void Clear(); void Clear();
/// Clear HeadPoseResult and free the memory
void Free();
void Reserve(int size); void Reserve(int size);
void Resize(int size); void Resize(int size);