Skip to content

Commit eae8f4e

Browse files
iducnjiweibo
andauthored
[Inference] [unittest] Inference unit tests rely on dynamic libraries (#24743) (#26008)
Co-authored-by: Wilber <[email protected]>
1 parent ac347fc commit eae8f4e

17 files changed

+231
-39
lines changed

cmake/generic.cmake

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,7 @@ function(cc_test_run TARGET_NAME)
382382
set(multiValueArgs COMMAND ARGS)
383383
cmake_parse_arguments(cc_test "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
384384
add_test(NAME ${TARGET_NAME}
385-
COMMAND ${cc_test_COMMAND}
386-
ARGS ${cc_test_ARGS}
385+
COMMAND ${cc_test_COMMAND} ${cc_test_ARGS}
387386
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
388387
set_property(TEST ${TARGET_NAME} PROPERTY ENVIRONMENT FLAGS_cpu_deterministic=true)
389388
set_property(TEST ${TARGET_NAME} PROPERTY ENVIRONMENT FLAGS_init_allocated_mem=true)

paddle/fluid/inference/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@ if(WITH_TESTING)
6363
endif()
6464
endif()
6565

66-
if(NOT ON_INFER)
67-
return()
68-
endif()
69-
7066
set(SHARED_INFERENCE_SRCS
7167
io.cc
7268
${CMAKE_CURRENT_SOURCE_DIR}/../framework/data_feed.cc

paddle/fluid/inference/api/CMakeLists.txt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,21 @@ cc_library(analysis_predictor SRCS analysis_predictor.cc ${mkldnn_quantizer_src}
4545
cc_test(test_paddle_inference_api SRCS api_tester.cc DEPS paddle_inference_api)
4646

4747
if(WITH_TESTING)
48-
inference_base_test(test_api_impl SRCS api_impl_tester.cc DEPS ${inference_deps}
49-
ARGS --word2vec_dirname=${WORD2VEC_MODEL_DIR} --book_dirname=${PYTHON_TESTS_DIR}/book)
48+
if (NOT APPLE AND NOT WIN32)
49+
inference_base_test(test_api_impl SRCS api_impl_tester.cc DEPS paddle_fluid_shared
50+
ARGS --word2vec_dirname=${WORD2VEC_MODEL_DIR} --book_dirname=${PYTHON_TESTS_DIR}/book)
51+
else()
52+
inference_base_test(test_api_impl SRCS api_impl_tester.cc DEPS ${inference_deps}
53+
ARGS --word2vec_dirname=${WORD2VEC_MODEL_DIR} --book_dirname=${PYTHON_TESTS_DIR}/book)
54+
endif()
5055
set_tests_properties(test_api_impl PROPERTIES DEPENDS test_image_classification)
5156
set_tests_properties(test_api_impl PROPERTIES LABELS "RUN_TYPE=DIST")
5257
endif()
53-
cc_test(test_analysis_predictor SRCS analysis_predictor_tester.cc DEPS analysis_predictor benchmark ${inference_deps}
54-
ARGS --dirname=${WORD2VEC_MODEL_DIR})
58+
59+
if (NOT APPLE AND NOT WIN32)
60+
cc_test(test_analysis_predictor SRCS analysis_predictor_tester.cc DEPS paddle_fluid_shared
61+
ARGS --dirname=${WORD2VEC_MODEL_DIR})
62+
else()
63+
cc_test(test_analysis_predictor SRCS analysis_predictor_tester.cc DEPS analysis_predictor benchmark ${inference_deps}
64+
ARGS --dirname=${WORD2VEC_MODEL_DIR})
65+
endif()

paddle/fluid/inference/tests/api/CMakeLists.txt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
set(INFERENCE_EXTRA_DEPS paddle_inference_api paddle_fluid_api ir_pass_manager analysis_predictor benchmark)
1+
if (NOT APPLE AND NOT WIN32)
2+
set(INFERENCE_EXTRA_DEPS paddle_fluid_shared)
3+
else()
4+
set(INFERENCE_EXTRA_DEPS paddle_inference_api paddle_fluid_api ir_pass_manager analysis_predictor benchmark)
5+
endif()
26

37
if(WITH_GPU AND TENSORRT_FOUND)
4-
set(INFERENCE_EXTRA_DEPS ${INFERENCE_EXTRA_DEPS} analysis ${analysis_deps} ir_pass_manager analysis_predictor)
8+
set(INFERENCE_EXTRA_DEPS ${INFERENCE_EXTRA_DEPS} analysis ${analysis_deps})
59
endif()
610

711
function(download_data install_dir data_file)
@@ -33,13 +37,13 @@ endfunction()
3337

3438
function(inference_analysis_api_test target install_dir filename)
3539
inference_analysis_test(${target} SRCS ${filename}
36-
EXTRA_DEPS ${INFERENCE_EXTRA_DEPS} benchmark
40+
EXTRA_DEPS ${INFERENCE_EXTRA_DEPS}
3741
ARGS --infer_model=${install_dir}/model --infer_data=${install_dir}/data.txt --refer_result=${install_dir}/result.txt)
3842
endfunction()
3943

4044
function(inference_analysis_api_test_build TARGET_NAME filename)
4145
inference_analysis_test_build(${TARGET_NAME} SRCS ${filename}
42-
EXTRA_DEPS ${INFERENCE_EXTRA_DEPS} benchmark)
46+
EXTRA_DEPS ${INFERENCE_EXTRA_DEPS})
4347
endfunction()
4448

4549
function(inference_analysis_api_int8_test_run TARGET_NAME test_binary model_dir data_path)
@@ -49,7 +53,7 @@ function(inference_analysis_api_int8_test_run TARGET_NAME test_binary model_dir
4953
--infer_data=${data_path}
5054
--warmup_batch_size=${WARMUP_BATCH_SIZE}
5155
--batch_size=50
52-
--paddle_num_threads=${CPU_NUM_THREADS_ON_CI}
56+
--cpu_num_threads=${CPU_NUM_THREADS_ON_CI}
5357
--iterations=2)
5458
endfunction()
5559

@@ -65,7 +69,7 @@ function(inference_analysis_api_object_dection_int8_test_run TARGET_NAME test_bi
6569
--infer_data=${data_path}
6670
--warmup_batch_size=10
6771
--batch_size=300
68-
--paddle_num_threads=${CPU_NUM_THREADS_ON_CI}
72+
--cpu_num_threads=${CPU_NUM_THREADS_ON_CI}
6973
--iterations=1)
7074
endfunction()
7175

@@ -88,7 +92,7 @@ function(inference_analysis_api_qat_test_run TARGET_NAME test_binary fp32_model_
8892
--int8_model=${int8_model_dir}
8993
--infer_data=${data_path}
9094
--batch_size=50
91-
--paddle_num_threads=${CPU_NUM_THREADS_ON_CI}
95+
--cpu_num_threads=${CPU_NUM_THREADS_ON_CI}
9296
--with_accuracy_layer=false
9397
--iterations=2)
9498
endfunction()
@@ -167,7 +171,7 @@ set(ERNIE_INSTALL_DIR "${INFERENCE_DEMO_INSTALL_DIR}/Ernie_Large")
167171
download_model_and_data(${ERNIE_INSTALL_DIR} "Ernie_large_model.tar.gz" "Ernie_large_data.txt.tar.gz" "Ernie_large_result.txt.tar.gz")
168172
download_result(${ERNIE_INSTALL_DIR} "Ernie_large_result.txt.tar.gz")
169173
inference_analysis_test(test_analyzer_ernie_large SRCS analyzer_ernie_tester.cc
170-
EXTRA_DEPS ${INFERENCE_EXTRA_DEPS} benchmark
174+
EXTRA_DEPS ${INFERENCE_EXTRA_DEPS}
171175
ARGS --infer_model=${ERNIE_INSTALL_DIR}/model --infer_data=${ERNIE_INSTALL_DIR}/data.txt --refer_result=${ERNIE_INSTALL_DIR}/result.txt --ernie_large=true)
172176

173177
# text_classification
@@ -186,7 +190,7 @@ download_model_and_data(${TRANSFORMER_INSTALL_DIR} "temp%2Ftransformer_model.tar
186190
inference_analysis_test(test_analyzer_transformer SRCS analyzer_transformer_tester.cc
187191
EXTRA_DEPS ${INFERENCE_EXTRA_DEPS}
188192
ARGS --infer_model=${TRANSFORMER_INSTALL_DIR}/model --infer_data=${TRANSFORMER_INSTALL_DIR}/data.txt --batch_size=8
189-
--paddle_num_threads=${CPU_NUM_THREADS_ON_CI})
193+
--cpu_num_threads=${CPU_NUM_THREADS_ON_CI})
190194

191195
# ocr
192196
set(OCR_INSTALL_DIR "${INFERENCE_DEMO_INSTALL_DIR}/ocr")
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/* Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include <gtest/gtest.h>
16+
#include <fstream>
17+
#include <iostream>
18+
#include "paddle/fluid/inference/tests/api/tester_helper.h"
19+
20+
DEFINE_string(infer_shape, "", "data shape file");
21+
DEFINE_int32(sample, 20, "number of sample");
22+
23+
namespace paddle {
24+
namespace inference {
25+
namespace analysis {
26+
27+
struct Record {
28+
std::vector<float> data;
29+
std::vector<int32_t> shape;
30+
};
31+
32+
Record ProcessALine(const std::string &line, const std::string &shape_line) {
33+
VLOG(3) << "process a line";
34+
std::vector<std::string> columns;
35+
36+
Record record;
37+
std::vector<std::string> data_strs;
38+
split(line, ' ', &data_strs);
39+
for (auto &d : data_strs) {
40+
record.data.push_back(std::stof(d));
41+
}
42+
43+
std::vector<std::string> shape_strs;
44+
split(shape_line, ' ', &shape_strs);
45+
for (auto &s : shape_strs) {
46+
record.shape.push_back(std::stoi(s));
47+
}
48+
return record;
49+
}
50+
51+
void SetConfig(AnalysisConfig *cfg) {
52+
cfg->SetModel(FLAGS_infer_model + "/model", FLAGS_infer_model + "/params");
53+
cfg->DisableGpu();
54+
cfg->SwitchIrDebug();
55+
cfg->SwitchSpecifyInputNames(false);
56+
cfg->SetCpuMathLibraryNumThreads(FLAGS_cpu_num_threads);
57+
}
58+
59+
void SetInput(std::vector<std::vector<PaddleTensor>> *inputs,
60+
const std::string &line, const std::string &shape_line) {
61+
auto record = ProcessALine(line, shape_line);
62+
63+
PaddleTensor input;
64+
input.shape = record.shape;
65+
input.dtype = PaddleDType::FLOAT32;
66+
size_t input_size = record.data.size() * sizeof(float);
67+
input.data.Resize(input_size);
68+
memcpy(input.data.data(), record.data.data(), input_size);
69+
std::vector<PaddleTensor> input_slots;
70+
input_slots.assign({input});
71+
(*inputs).emplace_back(input_slots);
72+
}
73+
74+
void profile(int cache_capacity = 1) {
75+
AnalysisConfig cfg;
76+
SetConfig(&cfg);
77+
cfg.EnableMKLDNN();
78+
cfg.SetMkldnnCacheCapacity(cache_capacity);
79+
80+
std::vector<std::vector<PaddleTensor>> outputs;
81+
std::vector<std::vector<PaddleTensor>> input_slots_all;
82+
83+
Timer run_timer;
84+
double elapsed_time = 0;
85+
86+
int num_times = FLAGS_repeat;
87+
int sample = FLAGS_sample;
88+
auto predictor = CreatePaddlePredictor<AnalysisConfig>(cfg);
89+
outputs.resize(sample);
90+
91+
std::vector<std::thread> threads;
92+
93+
std::ifstream file(FLAGS_infer_data);
94+
std::ifstream infer_file(FLAGS_infer_shape);
95+
std::string line;
96+
std::string shape_line;
97+
98+
for (int i = 0; i < sample; i++) {
99+
threads.emplace_back([&, i]() {
100+
std::getline(file, line);
101+
std::getline(infer_file, shape_line);
102+
SetInput(&input_slots_all, line, shape_line);
103+
104+
run_timer.tic();
105+
predictor->Run(input_slots_all[0], &outputs[0], FLAGS_batch_size);
106+
elapsed_time += run_timer.toc();
107+
});
108+
threads[0].join();
109+
threads.clear();
110+
std::vector<std::vector<PaddleTensor>>().swap(input_slots_all);
111+
}
112+
file.close();
113+
infer_file.close();
114+
115+
auto batch_latency = elapsed_time / (sample * num_times);
116+
PrintTime(FLAGS_batch_size, num_times, FLAGS_num_threads, 0, batch_latency,
117+
sample, VarType::FP32);
118+
}
119+
120+
#ifdef PADDLE_WITH_MKLDNN
121+
TEST(Analyzer_detect, profile_mkldnn) {
122+
profile(5 /* cache_capacity */);
123+
profile(10 /* cache_capacity */);
124+
}
125+
#endif
126+
127+
} // namespace analysis
128+
} // namespace inference
129+
} // namespace paddle

paddle/fluid/inference/tests/api/analyzer_ernie_tester.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ void SetConfig(AnalysisConfig *cfg, bool use_mkldnn = false,
143143
}
144144
cfg->SwitchSpecifyInputNames();
145145
cfg->SwitchIrOptim();
146-
cfg->SetCpuMathLibraryNumThreads(FLAGS_paddle_num_threads);
146+
cfg->SetCpuMathLibraryNumThreads(FLAGS_cpu_num_threads);
147147
}
148148

149149
void profile(bool use_mkldnn = false, bool use_gpu = false) {

paddle/fluid/inference/tests/api/analyzer_image_classification_tester.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void SetConfig(AnalysisConfig *cfg) {
2727
cfg->DisableGpu();
2828
cfg->SwitchIrOptim();
2929
cfg->SwitchSpecifyInputNames();
30-
cfg->SetCpuMathLibraryNumThreads(FLAGS_paddle_num_threads);
30+
cfg->SetCpuMathLibraryNumThreads(FLAGS_cpu_num_threads);
3131
}
3232

3333
void SetInput(std::vector<std::vector<PaddleTensor>> *inputs) {
@@ -40,7 +40,7 @@ void SetOptimConfig(AnalysisConfig *cfg) {
4040
cfg->DisableGpu();
4141
cfg->SwitchIrOptim();
4242
cfg->SwitchSpecifyInputNames();
43-
cfg->SetCpuMathLibraryNumThreads(FLAGS_paddle_num_threads);
43+
cfg->SetCpuMathLibraryNumThreads(FLAGS_cpu_num_threads);
4444
}
4545

4646
// Easy for profiling independently.

paddle/fluid/inference/tests/api/analyzer_int8_image_classification_tester.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void SetConfig(AnalysisConfig *cfg) {
2626
cfg->DisableGpu();
2727
cfg->SwitchIrOptim();
2828
cfg->SwitchSpecifyInputNames();
29-
cfg->SetCpuMathLibraryNumThreads(FLAGS_paddle_num_threads);
29+
cfg->SetCpuMathLibraryNumThreads(FLAGS_cpu_num_threads);
3030
cfg->EnableMKLDNN();
3131
}
3232

paddle/fluid/inference/tests/api/analyzer_int8_object_detection_tester.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void SetConfig(AnalysisConfig *cfg) {
2727
cfg->DisableGpu();
2828
cfg->SwitchIrOptim(true);
2929
cfg->SwitchSpecifyInputNames(false);
30-
cfg->SetCpuMathLibraryNumThreads(FLAGS_paddle_num_threads);
30+
cfg->SetCpuMathLibraryNumThreads(FLAGS_cpu_num_threads);
3131
cfg->EnableMKLDNN();
3232
}
3333

paddle/fluid/inference/tests/api/analyzer_pyramid_dnn_tester.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ void SetConfig(AnalysisConfig *cfg) {
107107
cfg->DisableGpu();
108108
cfg->SwitchSpecifyInputNames();
109109
cfg->SwitchIrOptim();
110-
cfg->SetCpuMathLibraryNumThreads(FLAGS_paddle_num_threads);
110+
cfg->SetCpuMathLibraryNumThreads(FLAGS_cpu_num_threads);
111111
if (FLAGS_zero_copy) {
112112
cfg->SwitchUseFeedFetchOps(false);
113113
}

0 commit comments

Comments
 (0)