Skip to content

Commit

Permalink
[Benchmark] Support PaddleClas cpp benchmark (PaddlePaddle#1324)
Browse files Browse the repository at this point in the history
* add GPL lisence

* add GPL-3.0 lisence

* add GPL-3.0 lisence

* add GPL-3.0 lisence

* support yolov8

* add pybind for yolov8

* add yolov8 readme

* add cpp benchmark

* add cpu and gpu mem

* public part split

* add runtime mode

* fixed bugs

* add cpu_thread_nums

* deal with comments

* deal with comments

* deal with comments

* rm useless code

* add FASTDEPLOY_DECL

* add FASTDEPLOY_DECL

* fixed for windows

* mv rss to pss

* mv rss to pss

* Update utils.cc

* use thread to collect mem

* Add ResourceUsageMonitor

* rm useless code

* fixed bug

* fixed typo

* update ResourceUsageMonitor

* fixed bug

* fixed bug

* add note for ResourceUsageMonitor

* deal with comments

* add macros

* deal with comments

* deal with comments

* deal with comments

* re-lint

* rm pmap and use mem api

* rm pmap and use mem api

* add mem api

* Add PrintBenchmarkInfo func

* Add PrintBenchmarkInfo func

* Add PrintBenchmarkInfo func

* deal with comments

* fixed enable_paddle_to_trt

* add log for paddle_trt

* support ppcls benchmark

* use new trt option api

* update benchmark info

* simplify benchmark.cc

* simplify benchmark.cc

* deal with comments

---------

Co-authored-by: DefTruth <[email protected]>
  • Loading branch information
wjj19950828 and DefTruth authored Feb 15, 2023
1 parent 6a2cfde commit da94fc4
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 35 deletions.
3 changes: 3 additions & 0 deletions benchmark/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ include_directories(${FASTDEPLOY_INCS})

add_executable(benchmark_yolov5 ${PROJECT_SOURCE_DIR}/benchmark_yolov5.cc)
add_executable(benchmark_ppyolov8 ${PROJECT_SOURCE_DIR}/benchmark_ppyolov8.cc)
add_executable(benchmark_ppcls ${PROJECT_SOURCE_DIR}/benchmark_ppcls.cc)

if(UNIX AND (NOT APPLE) AND (NOT ANDROID))
target_link_libraries(benchmark_yolov5 ${FASTDEPLOY_LIBS} gflags pthread)
target_link_libraries(benchmark_ppyolov8 ${FASTDEPLOY_LIBS} gflags pthread)
target_link_libraries(benchmark_ppcls ${FASTDEPLOY_LIBS} gflags pthread)
else()
target_link_libraries(benchmark_yolov5 ${FASTDEPLOY_LIBS} gflags)
target_link_libraries(benchmark_ppyolov8 ${FASTDEPLOY_LIBS} gflags)
target_link_libraries(benchmark_ppcls ${FASTDEPLOY_LIBS} gflags pthread)
endif()
36 changes: 36 additions & 0 deletions benchmark/cpp/benchmark_ppcls.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2023 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.

#include "flags.h"
#include "macros.h"
#include "option.h"

int main(int argc, char* argv[]) {
// Initialization
auto option = fastdeploy::RuntimeOption();
if (!CreateRuntimeOption(&option, argc, argv, true)) {
return -1;
}
auto im = cv::imread(FLAGS_image);
// Set max_batch_size 1 for best performance
option.trt_option.max_batch_size = 1;
auto model_file = FLAGS_model + sep + "inference.pdmodel";
auto params_file = FLAGS_model + sep + "inference.pdiparams";
auto config_file = FLAGS_model + sep + "inference_cls.yaml";
auto model_ppcls = fastdeploy::vision::classification::PaddleClasModel(
model_file, params_file, config_file, option);
fastdeploy::vision::ClassifyResult res;
BENCHMARK_MODEL(model_ppcls, model_ppcls.Predict(im, &res))
return 0;
}
11 changes: 4 additions & 7 deletions benchmark/cpp/benchmark_ppyolov8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "macros.h"
#include "flags.h"
#include "macros.h"
#include "option.h"

int main(int argc, char* argv[]) {
google::ParseCommandLineFlags(&argc, &argv, true);
auto im = cv::imread(FLAGS_image);
// Initialization
auto option = fastdeploy::RuntimeOption();
if (!CreateRuntimeOption(&option)) {
PrintUsage();
return false;
if (!CreateRuntimeOption(&option, argc, argv, true)) {
return -1;
}
PrintBenchmarkInfo();
auto im = cv::imread(FLAGS_image);
auto model_file = FLAGS_model + sep + "model.pdmodel";
auto params_file = FLAGS_model + sep + "model.pdiparams";
auto config_file = FLAGS_model + sep + "infer_cfg.yml";
Expand Down
13 changes: 5 additions & 8 deletions benchmark/cpp/benchmark_yolov5.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "macros.h"
#include "flags.h"
#include "macros.h"
#include "option.h"

int main(int argc, char* argv[]) {
google::ParseCommandLineFlags(&argc, &argv, true);
auto im = cv::imread(FLAGS_image);
// Initialization
auto option = fastdeploy::RuntimeOption();
if (!CreateRuntimeOption(&option)) {
PrintUsage();
return false;
if (!CreateRuntimeOption(&option, argc, argv, true)) {
return -1;
}
PrintBenchmarkInfo();
auto im = cv::imread(FLAGS_image);
auto model_yolov5 =
fastdeploy::vision::detection::YOLOv5(FLAGS_model, "", option);
fastdeploy::vision::DetectionResult res;
Expand All @@ -34,4 +31,4 @@ int main(int argc, char* argv[]) {
cv::imwrite("vis_result.jpg", vis_im);
std::cout << "Visualized result saved in ./vis_result.jpg" << std::endl;
return 0;
}
}
13 changes: 8 additions & 5 deletions benchmark/cpp/flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
#pragma once

#include "gflags/gflags.h"
#include "fastdeploy/benchmark/utils.h"

#ifdef WIN32
const char sep = '\\';
static const char sep = '\\';
#else
const char sep = '/';
static const char sep = '/';
#endif

DEFINE_string(model, "", "Directory of the inference model.");
Expand All @@ -44,7 +45,7 @@ DEFINE_bool(
collect_memory_info, false, "Whether to collect memory info");
DEFINE_int32(sampling_interval, 50, "How often to collect memory info(ms).");

void PrintUsage() {
static void PrintUsage() {
std::cout << "Usage: infer_demo --model model_path --image img_path --device "
"[cpu|gpu|xpu] --backend "
"[default|ort|paddle|ov|trt|paddle_trt|lite] "
Expand All @@ -55,7 +56,7 @@ void PrintUsage() {
std::cout << "Default value of use_fp16: false" << std::endl;
}

void PrintBenchmarkInfo() {
static void PrintBenchmarkInfo() {
// Get model name
std::vector<std::string> model_names;
fastdeploy::benchmark::Split(FLAGS_model, model_names, sep);
Expand All @@ -76,7 +77,9 @@ void PrintBenchmarkInfo() {
ss << "device_id: " << FLAGS_device_id << std::endl;
}
ss << "backend: " << FLAGS_backend << std::endl;
ss << "cpu_thread_nums: " << FLAGS_cpu_thread_nums << std::endl;
if (FLAGS_device == "cpu") {
ss << "cpu_thread_nums: " << FLAGS_cpu_thread_nums << std::endl;
}
ss << "use_fp16: " << FLAGS_use_fp16 << std::endl;
ss << "collect_memory_info: " << FLAGS_collect_memory_info << std::endl;
if (FLAGS_collect_memory_info) {
Expand Down
19 changes: 13 additions & 6 deletions benchmark/cpp/option.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

#include "fastdeploy/vision.h"

static bool CreateRuntimeOption(fastdeploy::RuntimeOption* option) {
static bool CreateRuntimeOption(fastdeploy::RuntimeOption* option,
int argc, char* argv[], bool remove_flags) {
google::ParseCommandLineFlags(&argc, &argv, remove_flags);
if (FLAGS_profile_mode == "runtime") {
option->EnableProfiling(FLAGS_include_h2d_d2h, FLAGS_repeat, FLAGS_warmup);
}
Expand All @@ -29,17 +31,19 @@ static bool CreateRuntimeOption(fastdeploy::RuntimeOption* option) {
} else if (FLAGS_backend == "trt" || FLAGS_backend == "paddle_trt") {
option->UseTrtBackend();
if (FLAGS_backend == "paddle_trt") {
option->EnablePaddleToTrt();
option->UsePaddleInferBackend();
option->paddle_infer_option.enable_trt = true;
}
if (FLAGS_use_fp16) {
option->EnableTrtFP16();
option->trt_option.enable_fp16 = true;
}
} else if (FLAGS_backend == "default") {
return true;
} else {
std::cout << "While inference with GPU, only support "
"default/ort/paddle/trt/paddle_trt now, "
<< FLAGS_backend << " is not supported." << std::endl;
PrintUsage();
return false;
}
} else if (FLAGS_device == "cpu") {
Expand All @@ -53,14 +57,15 @@ static bool CreateRuntimeOption(fastdeploy::RuntimeOption* option) {
} else if (FLAGS_backend == "lite") {
option->UsePaddleLiteBackend();
if (FLAGS_use_fp16) {
option->EnableLiteFP16();
option->paddle_lite_option.enable_fp16 = true;
}
} else if (FLAGS_backend == "default") {
return true;
} else {
std::cout << "While inference with CPU, only support "
"default/ort/ov/paddle/lite now, "
<< FLAGS_backend << " is not supported." << std::endl;
PrintUsage();
return false;
}
} else if (FLAGS_device == "xpu") {
Expand All @@ -72,21 +77,23 @@ static bool CreateRuntimeOption(fastdeploy::RuntimeOption* option) {
} else if (FLAGS_backend == "lite") {
option->UsePaddleLiteBackend();
if (FLAGS_use_fp16) {
option->EnableLiteFP16();
option->paddle_lite_option.enable_fp16 = true;
}
} else if (FLAGS_backend == "default") {
return true;
} else {
std::cout << "While inference with XPU, only support "
"default/ort/paddle/lite now, "
<< FLAGS_backend << " is not supported." << std::endl;
PrintUsage();
return false;
}
} else {
std::cerr << "Only support device CPU/GPU/XPU now, " << FLAGS_device
<< " is not supported." << std::endl;
PrintUsage();
return false;
}

PrintBenchmarkInfo();
return true;
}
5 changes: 4 additions & 1 deletion benchmark/python/benchmark_ppcls.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ def build_option(args):
elif backend in ["trt", "paddle_trt"]:
option.use_trt_backend()
if backend == "paddle_trt":
option.enable_paddle_to_trt()
option.use_paddle_infer_backend()
option.paddle_infer_option.enable_trt = True
# Set max_batch_size 1 for best performance
option.trt_option.max_batch_size = 1
if enable_trt_fp16:
option.enable_trt_fp16()
elif backend == "default":
Expand Down
3 changes: 2 additions & 1 deletion benchmark/python/benchmark_ppdet.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ def build_option(args):
elif backend in ["trt", "paddle_trt"]:
option.use_trt_backend()
if backend == "paddle_trt":
option.enable_paddle_to_trt()
option.use_paddle_infer_backend()
option.paddle_infer_option.enable_trt = True
if enable_trt_fp16:
option.enable_trt_fp16()
elif backend == "default":
Expand Down
5 changes: 3 additions & 2 deletions benchmark/python/benchmark_ppocr.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ def build_option(args):
elif backend in ["trt", "paddle_trt"]:
option.use_trt_backend()
if backend == "paddle_trt":
option.enable_paddle_trt_collect_shape()
option.enable_paddle_to_trt()
option.paddle_infer_option.collect_trt_shape = True
option.use_paddle_infer_backend()
option.paddle_infer_option.enable_trt = True
if enable_trt_fp16:
option.enable_trt_fp16()
elif backend == "default":
Expand Down
5 changes: 3 additions & 2 deletions benchmark/python/benchmark_ppseg.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ def build_option(args):
option.set_trt_input_shape("x", [1, 3, 192, 192],
[1, 3, 192, 192], [1, 3, 192, 192])
if backend == "paddle_trt":
option.enable_paddle_trt_collect_shape()
option.enable_paddle_to_trt()
option.paddle_infer_option.collect_trt_shape = True
option.use_paddle_infer_backend()
option.paddle_infer_option.enable_trt = True
if enable_trt_fp16:
option.enable_trt_fp16()
elif backend == "default":
Expand Down
5 changes: 3 additions & 2 deletions benchmark/python/benchmark_uie.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ def build_option(args):
else:
option.use_trt_backend()
if args.backend == 'paddle_trt':
option.enable_paddle_to_trt()
option.enable_paddle_trt_collect_shape()
option.paddle_infer_option.collect_trt_shape = True
option.use_paddle_infer_backend()
option.paddle_infer_option.enable_trt = True
trt_file = os.path.join(args.model_dir, "infer.trt")
option.set_trt_input_shape(
'input_ids',
Expand Down
3 changes: 2 additions & 1 deletion benchmark/python/benchmark_yolo.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ def build_option(args):
elif backend in ["trt", "paddle_trt"]:
option.use_trt_backend()
if backend == "paddle_trt":
option.enable_paddle_to_trt()
option.use_paddle_infer_backend()
option.paddle_infer_option.enable_trt = True
if enable_trt_fp16:
option.enable_trt_fp16()
elif backend == "default":
Expand Down

0 comments on commit da94fc4

Please sign in to comment.