Skip to content

Commit

Permalink
[tipc] fix test_train_infer_python/cpp (#5516)
Browse files Browse the repository at this point in the history
* add pact about

* fix doc

* fix comments

* add ptq aware

* fix

* add benchmark.png

* fix png and ptq

* fix tipc

* fix train_infer_python

* delete mkldnn trt

* fix cpp

* fix comment

* fix comment

* fix

* status_check

* fix comment

* add build cpp to prepare.sh
  • Loading branch information
LDOUBLEV authored May 18, 2022
1 parent 2aa2d44 commit 34f046e
Show file tree
Hide file tree
Showing 19 changed files with 120 additions and 291 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ cd build
cmake .. \
-DWITH_CONTRIB=OFF \
-DWITH_MKL=ON \
-DWITH_MKLDNN=ON \
-DWITH_MKLDNN=OFF \
-DWITH_TESTING=OFF \
-DCMAKE_BUILD_TYPE=Release \
-DWITH_INFERENCE_API_TEST=OFF \
Expand Down Expand Up @@ -205,9 +205,6 @@ make -j
* gpu_id:使用的GPU卡号;
* gpu_mem:显存;
* cpu_math_library_num_threads:底层科学计算库所用线程的数量;
* use_mkldnn:是否使用MKLDNN加速;
* use_tensorrt: 是否使用tensorRT进行加速;
* use_fp16:是否使用半精度浮点数进行计算,该选项仅在use_tensorrt为true时有效;
* cls_model_path:预测模型结构文件路径;
* cls_params_path:预测模型参数文件路径;
* resize_short_size:预处理时图像缩放大小;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,12 @@ class Classifier {
const std::string &params_path, const bool &use_gpu,
const int &gpu_id, const int &gpu_mem,
const int &cpu_math_library_num_threads,
const bool &use_mkldnn, const bool &use_tensorrt,
const bool &use_fp16, const int &resize_short_size,
const int &resize_short_size,
const int &crop_size) {
this->use_gpu_ = use_gpu;
this->gpu_id_ = gpu_id;
this->gpu_mem_ = gpu_mem;
this->cpu_math_library_num_threads_ = cpu_math_library_num_threads;
this->use_mkldnn_ = use_mkldnn;
this->use_tensorrt_ = use_tensorrt;
this->use_fp16_ = use_fp16;

this->resize_short_size_ = resize_short_size;
this->crop_size_ = crop_size;
Expand All @@ -70,9 +66,6 @@ class Classifier {
int gpu_id_ = 0;
int gpu_mem_ = 4000;
int cpu_math_library_num_threads_ = 4;
bool use_mkldnn_ = false;
bool use_tensorrt_ = false;
bool use_fp16_ = false;

std::vector<float> mean_ = {0.485f, 0.456f, 0.406f};
std::vector<float> scale_ = {1 / 0.229f, 1 / 0.224f, 1 / 0.225f};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ class ClsConfig {
this->cpu_math_library_num_threads =
stoi(config_map_["cpu_math_library_num_threads"]);

this->use_mkldnn = bool(stoi(config_map_["use_mkldnn"]));

this->use_tensorrt = bool(stoi(config_map_["use_tensorrt"]));
this->use_fp16 = bool(stoi(config_map_["use_fp16"]));

this->cls_model_path.assign(config_map_["cls_model_path"]);

this->cls_params_path.assign(config_map_["cls_params_path"]);
Expand All @@ -61,11 +56,6 @@ class ClsConfig {

int cpu_math_library_num_threads = 1;

bool use_mkldnn = false;

bool use_tensorrt = false;
bool use_fp16 = false;

std::string cls_model_path;

std::string cls_params_path;
Expand Down
12 changes: 0 additions & 12 deletions tutorials/mobilenetv3_prod/Step6/deploy/inference_cpp/src/cls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,8 @@ void Classifier::LoadModel(const std::string &model_path,

if (this->use_gpu_) {
config.EnableUseGpu(this->gpu_mem_, this->gpu_id_);
if (this->use_tensorrt_) {
config.EnableTensorRtEngine(
1 << 20, 1, 3,
this->use_fp16_ ? paddle_infer::Config::Precision::kHalf
: paddle_infer::Config::Precision::kFloat32,
false, false);
}
} else {
config.DisableGpu();
if (this->use_mkldnn_) {
config.EnableMKLDNN();
// cache 10 different shapes for mkldnn to avoid memory leak
config.SetMkldnnCacheCapacity(10);
}
config.SetCpuMathLibraryNumThreads(this->cpu_math_library_num_threads_);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ int main(int argc, char **argv) {

Classifier classifier(config.cls_model_path, config.cls_params_path,
config.use_gpu, config.gpu_id, config.gpu_mem,
config.cpu_math_library_num_threads, config.use_mkldnn,
config.use_tensorrt, config.use_fp16,
config.cpu_math_library_num_threads,
config.resize_short_size, config.crop_size);

double elapsed_time = 0.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ OPENCV_DIR=../opencv-3.4.7/opencv3/
LIB_DIR=../paddle_inference/
CUDA_LIB_DIR=/usr/local/cuda/lib64
CUDNN_LIB_DIR=/usr/lib64
TENSORRT_DIR=/usr/local/TensorRT-7.2.3.4

BUILD_DIR=build
rm -rf ${BUILD_DIR}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

## 1. 简介

Paddle Inference 是飞桨的原生推理库, 作用于服务器端和云端,提供高性能的推理能力。相比于直接基于预训练模型进行预测,Paddle Inference可使用MKLDNN、CUDNN、TensorRT进行预测加速,从而实现更优的推理性能。
Paddle Inference 是飞桨的原生推理库, 作用于服务器端和云端,提供高性能的推理能力。

本文档主要基于Paddle Inference的mobilenet_v3_small模型推理。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def load_predictor(self, model_file_path, params_file_path):
config.enable_use_gpu(1000, 0)
else:
config.disable_gpu()
# The thread num should not be greater than the number of cores in the CPU.
config.set_cpu_math_library_num_threads(4)

# enable memory optim
config.enable_memory_optim()
Expand Down
1 change: 1 addition & 0 deletions tutorials/mobilenetv3_prod/Step6/test_tipc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Slim训练部署:包括PACT在线量化、离线量化。
- 更多训练环境:包括Windows GPU/CPU、Linux NPU、Linux DCU等多种环境。


| 算法论文 | 模型名称 | 模型类型 | 基础<br>训练预测 | 更多<br>训练方式 | 更多<br>部署方式 | Slim<br>训练部署 | 更多<br>训练环境 |
| :--- | :--- | :----: | :--------: | :----: | :----: | :----: | :----: |
| MobileNetV3 | mobilenet_v3_small | 分类 | 支持 | 混合精度 | PYTHON 服务化部署<br>Paddle2ONNX 部署| PACT量化<br>离线量化 | Windows GPU/CPU |
Expand Down
5 changes: 3 additions & 2 deletions tutorials/mobilenetv3_prod/Step6/test_tipc/common_func.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ function status_check(){
last_status=$1 # the exit code
run_command=$2
run_log=$3
model_name=$4
if [ $last_status -eq 0 ]; then
echo -e "\033[33m Run successfully with command - ${run_command}! \033[0m" | tee -a ${run_log}
echo -e "\033[33m Run successfully with command - ${model_name} - ${run_command}! \033[0m" | tee -a ${run_log}
else
echo -e "\033[33m Run failed with command - ${run_command}! \033[0m" | tee -a ${run_log}
echo -e "\033[33m Run failed with command - ${model_name} - ${run_command}! \033[0m" | tee -a ${run_log}
fi
}

Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# model load config
model_name mobilenet_v3_small
use_gpu 0
gpu_id 0
gpu_mem 4000
cpu_math_library_num_threads 10
use_mkldnn 1
use_tensorrt 0
use_fp16 0

# cls config
cls_model_path ./deploy/inference_cpp/mobilenet_v3_small_infer/inference.pdmodel
cls_params_path ./deploy/inference_cpp/mobilenet_v3_small_infer/inference.pdiparams
resize_short_size 256
crop_size 224
crop_size 224
41 changes: 41 additions & 0 deletions tutorials/mobilenetv3_prod/Step6/test_tipc/prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,47 @@ elif [ ${MODE} = "serving_infer" ];then
wget -nc -P ./inference https://paddle-model-ecology.bj.bcebos.com/model/mobilenetv3_reprod/mobilenet_v3_small_infer.tar --no-check-certificate
cd ./inference && tar xf mobilenet_v3_small_infer.tar && cd ../
fi
elif [ ${MODE} = "cpp_infer" ];then
# wget model
wget -nc -P ./deploy/inference_cpp/ https://paddle-model-ecology.bj.bcebos.com/model/mobilenetv3_reprod/mobilenet_v3_small_infer.tar --no-check-certificate
cd ./deploy/inference_cpp/ && tar xf mobilenet_v3_small_infer.tar
wget -nc https://paddle-inference-lib.bj.bcebos.com/2.2.2/cxx_c/Linux/GPU/x86-64_gcc8.2_avx_mkl_cuda11.1_cudnn8.1.1_trt7.2.3.4/paddle_inference.tgz
tar zxf paddle_inference.tgz
wget -nc https://paddleocr.bj.bcebos.com/libs/opencv/opencv-3.4.7.tar.gz
tar zxf opencv-3.4.7.tar.gz
# build opencv
cd opencv-3.4.7/
root_path=$PWD
install_path=${root_path}/opencv3
build_dir=${root_path}/build

rm -rf ${build_dir}
mkdir ${build_dir}
cd ${build_dir}

cmake .. \
-DCMAKE_INSTALL_PREFIX=${install_path} \
-DCMAKE_BUILD_TYPE=Release \
-DBUILD_SHARED_LIBS=OFF \
-DWITH_IPP=OFF \
-DBUILD_IPP_IW=OFF \
-DWITH_LAPACK=OFF \
-DWITH_EIGEN=OFF \
-DCMAKE_INSTALL_LIBDIR=lib64 \
-DWITH_ZLIB=ON \
-DBUILD_ZLIB=ON \
-DWITH_JPEG=ON \
-DBUILD_JPEG=ON \
-DWITH_PNG=ON \
-DBUILD_PNG=ON \
-DWITH_TIFF=ON \
-DBUILD_TIFF=ON
make -j
make install
cd ../../
# build cpp
bash tools/build.sh

elif [ ${MODE} = "paddle2onnx_infer" ];then
# get data
tar -xf ./test_images/lite_data.tar
Expand Down
31 changes: 7 additions & 24 deletions tutorials/mobilenetv3_prod/Step6/test_tipc/test_inference_cpp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,45 +28,28 @@ IFS=$'\n'
lines=(${dataline})

# parser load config
use_gpu_key=$(func_parser_key_cpp "${lines[1]}")
use_gpu_value=$(func_parser_value_cpp "${lines[1]}")
use_mkldnn_key=$(func_parser_key_cpp "${lines[5]}")
use_mkldnn_value=$(func_parser_value_cpp "${lines[5]}")
use_tensorrt_key=$(func_parser_key_cpp "${lines[6]}")
use_tensorrt_value=$(func_parser_value_cpp "${lines[6]}")
use_fp16_key=$(func_parser_key_cpp "${lines[7]}")
use_fp16_value=$(func_parser_value_cpp "${lines[7]}")
model_name=$(func_parser_value_cpp "${lines[1]}")
use_gpu_key=$(func_parser_key_cpp "${lines[2]}")
use_gpu_value=$(func_parser_value_cpp "${lines[2]}")

LOG_PATH="./log/infer_cpp"
mkdir -p ${LOG_PATH}
status_log="${LOG_PATH}/results_infer_cpp.log"

function func_infer_cpp(){
# inference cpp
if test $use_gpu_value -gt 0; then
if test $use_tensorrt_value -gt 0; then
if test $use_fp16_value -gt 0; then
_save_log_path="${LOG_PATH}/infer_cpp_${use_gpu_key}_${use_tensorrt_key}_${use_fp16_key}.log"
else
_save_log_path="${LOG_PATH}/infer_cpp_${use_gpu_key}_${use_tensorrt_key}.log"
fi
else
_save_log_path="${LOG_PATH}/infer_cpp_${use_gpu_key}.log"
fi
if test $use_gpu_value -gt 0; then
_save_log_path="${LOG_PATH}/infer_cpp_use_cpu.log"
else
if test $use_mkldnn_value -gt 0; then
_save_log_path="${LOG_PATH}/infer_cpp_use_cpu_${use_mkldnn_key}.log"
else
_save_log_path="${LOG_PATH}/infer_cpp_use_cpu.log"
fi
_save_log_path="${LOG_PATH}/infer_cpp_${use_gpu_key}.log"
fi
# run infer cpp
inference_cpp_cmd="./deploy/inference_cpp/build/clas_system"
inference_cpp_img="./images/demo.jpg"
infer_cpp_full_cmd="${inference_cpp_cmd} ${FILENAME} ${inference_cpp_img} > ${_save_log_path} 2>&1 "
eval $infer_cpp_full_cmd
last_status=${PIPESTATUS[0]}
status_check $last_status "${infer_cpp_full_cmd}" "${status_log}"
status_check $last_status "${infer_cpp_full_cmd}" "${status_log}" "${model_name}"
}

echo "################### run test cpp inference ###################"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function func_inference(){
eval $command
last_status=${PIPESTATUS[0]}
eval "cat ${_save_log_path}"
status_check $last_status "${command}" "${status_log}"
status_check $last_status "${command}" "${status_log}" "${model_name}"
done
# gpu
elif [ ${use_gpu} = "True" ] || [ ${use_gpu} = "gpu" ]; then
Expand All @@ -80,7 +80,7 @@ function func_inference(){
eval $command
last_status=${PIPESTATUS[0]}
eval "cat ${_save_log_path}"
status_check $last_status "${command}" "${status_log}"
status_check $last_status "${command}" "${status_log}" "${model_name}"
done
else
echo "Does not support hardware other than CPU and GPU Currently!"
Expand All @@ -107,7 +107,7 @@ if [ ${MODE} = "whole_infer" ]; then
echo $export_cmd
# eval $export_cmd
status_export=$?
status_check $status_export "${export_cmd}" "${status_log}"
status_check $status_export "${export_cmd}" "${status_log}" "${model_name}"

save_infer_dir=${output_dir_value}
#run inference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function func_inference(){
eval $command
last_status=${PIPESTATUS[0]}
eval "cat ${_save_log_path}"
status_check $last_status "${command}" "${status_log}"
status_check $last_status "${command}" "${status_log}" "${model_name}"
done
# gpu
elif [ ${use_gpu} = "True" ] || [ ${use_gpu} = "gpu" ]; then
Expand All @@ -101,7 +101,7 @@ function func_inference(){
eval $command
last_status=${PIPESTATUS[0]}
eval "cat ${_save_log_path}"
status_check $last_status "${command}" "${status_log}"
status_check $last_status "${command}" "${status_log}" "${model_name}"
done
else
echo "Does not support hardware other than CPU and GPU Currently!"
Expand Down Expand Up @@ -131,7 +131,7 @@ if [ ${MODE} = "whole_infer" ]; then
echo $export_cmd
eval $export_cmd
status_export=$?
status_check $status_export "${export_cmd}" "${status_log}"
status_check $status_export "${export_cmd}" "${status_log}" "${model_name}"
else
save_infer_dir=${save_infer_dir}
fi
Expand Down Expand Up @@ -196,7 +196,7 @@ else
fi
# run train
eval $cmd
status_check $? "${cmd}" "${status_log}"
status_check $? "${cmd}" "${status_log}" "${model_name}"
# TODO ensure model name
set_eval_pretrain=$(func_set_params "${pretrain_model_key}" "${save_log}/${train_model_name}")

Expand All @@ -205,7 +205,7 @@ else
eval ${env}
eval_cmd="${python} ${eval_py} ${set_eval_pretrain}"
eval $eval_cmd
status_check $? "${eval_cmd}" "${status_log}"
status_check $? "${eval_cmd}" "${status_log}" "${model_name}"
fi
# run export model
if [ ${run_export} != "null" ]; then
Expand All @@ -215,7 +215,7 @@ else
set_save_infer_key=$(func_set_params "${save_infer_key}" "${save_infer_path}")
export_cmd="${python} ${run_export} ${set_export_weight} ${set_save_infer_key}"
eval $export_cmd
status_check $? "${export_cmd}" "${status_log}"
status_check $? "${export_cmd}" "${status_log}" "${model_name}"

#run inference
eval $env
Expand Down
12 changes: 0 additions & 12 deletions tutorials/tipc/infer_cpp/infer_cpp.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,8 @@ void Classifier::LoadModel(const std::string &model_path,

if (this->use_gpu_) {
config.EnableUseGpu(this->gpu_mem_, this->gpu_id_);
if (this->use_tensorrt_) {
config.EnableTensorRtEngine(
1 << 20, 1, 3,
this->use_fp16_ ? paddle_infer::Config::Precision::kHalf
: paddle_infer::Config::Precision::kFloat32,
false, false);
}
} else {
config.DisableGpu();
if (this->use_mkldnn_) {
config.EnableMKLDNN();
// cache 10 different shapes for mkldnn to avoid memory leak
config.SetMkldnnCacheCapacity(10);
}
config.SetCpuMathLibraryNumThreads(this->cpu_math_library_num_threads_);
}

Expand Down
Loading

0 comments on commit 34f046e

Please sign in to comment.