Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MLU] add ppocr c++ inference support. #14143

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion deploy/cpp_infer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ if(WITH_MKL)
if (WIN32)
set(MKLDNN_LIB ${MKLDNN_PATH}/lib/mkldnn.lib)
else ()
set(MKLDNN_LIB ${MKLDNN_PATH}/lib/libmkldnn.so.0)
set(MKLDNN_LIB ${MKLDNN_PATH}/lib/libdnnl.so.3)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is onednn related config, any reason for chaning this lib name ?

endif ()
endif()
else()
Expand Down
1 change: 1 addition & 0 deletions deploy/cpp_infer/include/args.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

// common args
DECLARE_bool(use_gpu);
DECLARE_bool(use_mlu);
DECLARE_bool(use_tensorrt);
DECLARE_int32(gpu_id);
DECLARE_int32(gpu_mem);
Expand Down
3 changes: 3 additions & 0 deletions deploy/cpp_infer/include/ocr_cls.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ namespace PaddleOCR {
class Classifier {
public:
explicit Classifier(const std::string &model_dir, const bool &use_gpu,
const bool &use_mlu,
const int &gpu_id, const int &gpu_mem,
const int &cpu_math_library_num_threads,
const bool &use_mkldnn, const double &cls_thresh,
const bool &use_tensorrt, const std::string &precision,
const int &cls_batch_num) {
this->use_gpu_ = use_gpu;
this->use_mlu_ = use_mlu;
this->gpu_id_ = gpu_id;
this->gpu_mem_ = gpu_mem;
this->cpu_math_library_num_threads_ = cpu_math_library_num_threads;
Expand All @@ -55,6 +57,7 @@ class Classifier {
std::shared_ptr<paddle_infer::Predictor> predictor_;

bool use_gpu_ = false;
bool use_mlu_ = false;
int gpu_id_ = 0;
int gpu_mem_ = 4000;
int cpu_math_library_num_threads_ = 4;
Expand Down
3 changes: 3 additions & 0 deletions deploy/cpp_infer/include/ocr_det.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace PaddleOCR {
class DBDetector {
public:
explicit DBDetector(const std::string &model_dir, const bool &use_gpu,
const bool &use_mlu,
const int &gpu_id, const int &gpu_mem,
const int &cpu_math_library_num_threads,
const bool &use_mkldnn, const std::string &limit_type,
Expand All @@ -35,6 +36,7 @@ class DBDetector {
const bool &use_dilation, const bool &use_tensorrt,
const std::string &precision) {
this->use_gpu_ = use_gpu;
this->use_mlu_ = use_mlu;
this->gpu_id_ = gpu_id;
this->gpu_mem_ = gpu_mem;
this->cpu_math_library_num_threads_ = cpu_math_library_num_threads;
Expand Down Expand Up @@ -66,6 +68,7 @@ class DBDetector {
std::shared_ptr<paddle_infer::Predictor> predictor_;

bool use_gpu_ = false;
bool use_mlu_ = false;
int gpu_id_ = 0;
int gpu_mem_ = 4000;
int cpu_math_library_num_threads_ = 4;
Expand Down
3 changes: 3 additions & 0 deletions deploy/cpp_infer/include/ocr_rec.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace PaddleOCR {
class CRNNRecognizer {
public:
explicit CRNNRecognizer(const std::string &model_dir, const bool &use_gpu,
const bool &use_mlu,
const int &gpu_id, const int &gpu_mem,
const int &cpu_math_library_num_threads,
const bool &use_mkldnn, const std::string &label_path,
Expand All @@ -33,6 +34,7 @@ class CRNNRecognizer {
const int &rec_batch_num, const int &rec_img_h,
const int &rec_img_w) {
this->use_gpu_ = use_gpu;
this->use_mlu_ = use_mlu;
this->gpu_id_ = gpu_id;
this->gpu_mem_ = gpu_mem;
this->cpu_math_library_num_threads_ = cpu_math_library_num_threads;
Expand Down Expand Up @@ -63,6 +65,7 @@ class CRNNRecognizer {
std::shared_ptr<paddle_infer::Predictor> predictor_;

bool use_gpu_ = false;
bool use_mlu_ = false;
int gpu_id_ = 0;
int gpu_mem_ = 4000;
int cpu_math_library_num_threads_ = 4;
Expand Down
4 changes: 3 additions & 1 deletion deploy/cpp_infer/include/structure_layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ namespace PaddleOCR {
class StructureLayoutRecognizer {
public:
explicit StructureLayoutRecognizer(
const std::string &model_dir, const bool &use_gpu, const int &gpu_id,
const std::string &model_dir, const bool &use_gpu, const bool &use_mlu, const int &gpu_id,
const int &gpu_mem, const int &cpu_math_library_num_threads,
const bool &use_mkldnn, const std::string &label_path,
const bool &use_tensorrt, const std::string &precision,
const double &layout_score_threshold,
const double &layout_nms_threshold) {
this->use_gpu_ = use_gpu;
this->use_mlu_ = use_mlu;
this->gpu_id_ = gpu_id;
this->gpu_mem_ = gpu_mem;
this->cpu_math_library_num_threads_ = cpu_math_library_num_threads;
Expand All @@ -54,6 +55,7 @@ class StructureLayoutRecognizer {
std::shared_ptr<paddle_infer::Predictor> predictor_;

bool use_gpu_ = false;
bool use_mlu_ = false;
int gpu_id_ = 0;
int gpu_mem_ = 4000;
int cpu_math_library_num_threads_ = 4;
Expand Down
4 changes: 3 additions & 1 deletion deploy/cpp_infer/include/structure_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ namespace PaddleOCR {
class StructureTableRecognizer {
public:
explicit StructureTableRecognizer(
const std::string &model_dir, const bool &use_gpu, const int &gpu_id,
const std::string &model_dir, const bool &use_gpu, const bool &use_mlu,const int &gpu_id,
const int &gpu_mem, const int &cpu_math_library_num_threads,
const bool &use_mkldnn, const std::string &label_path,
const bool &use_tensorrt, const std::string &precision,
const int &table_batch_num, const int &table_max_len,
const bool &merge_no_span_structure) {
this->use_gpu_ = use_gpu;
this->use_mlu_ = use_mlu;
this->gpu_id_ = gpu_id;
this->gpu_mem_ = gpu_mem;
this->cpu_math_library_num_threads_ = cpu_math_library_num_threads;
Expand All @@ -58,6 +59,7 @@ class StructureTableRecognizer {
std::shared_ptr<paddle_infer::Predictor> predictor_;

bool use_gpu_ = false;
bool use_mlu_ = false;
int gpu_id_ = 0;
int gpu_mem_ = 4000;
int cpu_math_library_num_threads_ = 4;
Expand Down
1 change: 1 addition & 0 deletions deploy/cpp_infer/src/args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

// common args
DEFINE_bool(use_gpu, false, "Infering with GPU or CPU.");
DEFINE_bool(use_mlu, false, "Infering with MLU or CPU.");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please include more informative message here, as MLU is not a well known abbreviation yet.

DEFINE_bool(use_tensorrt, false, "Whether use tensorrt.");
DEFINE_int32(gpu_id, 0, "Device id of GPU to execute.");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from the implementation, gpu_id will also be used to specify MLU ID, when being combined with use_mlu, correct ?

DEFINE_int32(gpu_mem, 4000, "GPU id when infering with GPU.");
Expand Down
4 changes: 3 additions & 1 deletion deploy/cpp_infer/src/ocr_cls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ void Classifier::LoadModel(const std::string &model_dir) {
config.EnableTunedTensorRtDynamicShape("./trt_cls_shape.txt", true);
}
}
} else {
} else if (this->use_mlu_) {
config.EnableCustomDevice("mlu", this->gpu_id_);
} else {
config.DisableGpu();
if (this->use_mkldnn_) {
config.EnableMKLDNN();
Expand Down
2 changes: 2 additions & 0 deletions deploy/cpp_infer/src/ocr_det.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ void DBDetector::LoadModel(const std::string &model_dir) {
config.EnableTunedTensorRtDynamicShape("./trt_det_shape.txt", true);
}
}
} else if (this->use_mlu_) {
config.EnableCustomDevice("mlu", this->gpu_id_);
} else {
config.DisableGpu();
if (this->use_mkldnn_) {
Expand Down
2 changes: 2 additions & 0 deletions deploy/cpp_infer/src/ocr_rec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ void CRNNRecognizer::LoadModel(const std::string &model_dir) {
config.EnableTunedTensorRtDynamicShape("./trt_rec_shape.txt", true);
}
}
} else if (this->use_mlu_) {
config.EnableCustomDevice("mlu", this->gpu_id_);
} else {
config.DisableGpu();
if (this->use_mkldnn_) {
Expand Down
6 changes: 3 additions & 3 deletions deploy/cpp_infer/src/paddleocr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace PaddleOCR {
PPOCR::PPOCR() {
if (FLAGS_det) {
this->detector_.reset(new DBDetector(
FLAGS_det_model_dir, FLAGS_use_gpu, FLAGS_gpu_id, FLAGS_gpu_mem,
FLAGS_det_model_dir, FLAGS_use_gpu, FLAGS_use_mlu, FLAGS_gpu_id, FLAGS_gpu_mem,
FLAGS_cpu_threads, FLAGS_enable_mkldnn, FLAGS_limit_type,
FLAGS_limit_side_len, FLAGS_det_db_thresh, FLAGS_det_db_box_thresh,
FLAGS_det_db_unclip_ratio, FLAGS_det_db_score_mode, FLAGS_use_dilation,
Expand All @@ -31,13 +31,13 @@ PPOCR::PPOCR() {

if (FLAGS_cls && FLAGS_use_angle_cls) {
this->classifier_.reset(new Classifier(
FLAGS_cls_model_dir, FLAGS_use_gpu, FLAGS_gpu_id, FLAGS_gpu_mem,
FLAGS_cls_model_dir, FLAGS_use_gpu, FLAGS_use_mlu, FLAGS_gpu_id, FLAGS_gpu_mem,
FLAGS_cpu_threads, FLAGS_enable_mkldnn, FLAGS_cls_thresh,
FLAGS_use_tensorrt, FLAGS_precision, FLAGS_cls_batch_num));
}
if (FLAGS_rec) {
this->recognizer_.reset(new CRNNRecognizer(
FLAGS_rec_model_dir, FLAGS_use_gpu, FLAGS_gpu_id, FLAGS_gpu_mem,
FLAGS_rec_model_dir, FLAGS_use_gpu, FLAGS_use_mlu, FLAGS_gpu_id, FLAGS_gpu_mem,
FLAGS_cpu_threads, FLAGS_enable_mkldnn, FLAGS_rec_char_dict_path,
FLAGS_use_tensorrt, FLAGS_precision, FLAGS_rec_batch_num,
FLAGS_rec_img_h, FLAGS_rec_img_w));
Expand Down
4 changes: 2 additions & 2 deletions deploy/cpp_infer/src/paddlestructure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ namespace PaddleOCR {
PaddleStructure::PaddleStructure() {
if (FLAGS_layout) {
this->layout_model_.reset(new StructureLayoutRecognizer(
FLAGS_layout_model_dir, FLAGS_use_gpu, FLAGS_gpu_id, FLAGS_gpu_mem,
FLAGS_layout_model_dir, FLAGS_use_gpu, FLAGS_use_mlu,FLAGS_gpu_id, FLAGS_gpu_mem,
FLAGS_cpu_threads, FLAGS_enable_mkldnn, FLAGS_layout_dict_path,
FLAGS_use_tensorrt, FLAGS_precision, FLAGS_layout_score_threshold,
FLAGS_layout_nms_threshold));
}
if (FLAGS_table) {
this->table_model_.reset(new StructureTableRecognizer(
FLAGS_table_model_dir, FLAGS_use_gpu, FLAGS_gpu_id, FLAGS_gpu_mem,
FLAGS_table_model_dir, FLAGS_use_gpu, FLAGS_use_mlu,FLAGS_gpu_id, FLAGS_gpu_mem,
FLAGS_cpu_threads, FLAGS_enable_mkldnn, FLAGS_table_char_dict_path,
FLAGS_use_tensorrt, FLAGS_precision, FLAGS_table_batch_num,
FLAGS_table_max_len, FLAGS_merge_no_span_structure));
Expand Down
2 changes: 2 additions & 0 deletions deploy/cpp_infer/src/structure_layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ void StructureLayoutRecognizer::LoadModel(const std::string &model_dir) {
config.EnableTunedTensorRtDynamicShape("./trt_layout_shape.txt", true);
}
}
} else if (this->use_mlu_) {
config.EnableCustomDevice("mlu", this->gpu_id_);
} else {
config.DisableGpu();
if (this->use_mkldnn_) {
Expand Down
2 changes: 2 additions & 0 deletions deploy/cpp_infer/src/structure_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ void StructureTableRecognizer::LoadModel(const std::string &model_dir) {
config.EnableTunedTensorRtDynamicShape("./trt_table_shape.txt", true);
}
}
} else if (this->use_gpu_) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be use_mlu_ ?

config.EnableCustomDevice("mlu", this->gpu_id_);
} else {
config.DisableGpu();
if (this->use_mkldnn_) {
Expand Down
Loading