diff --git a/paddle/fluid/framework/ir/mkldnn/cpu_quantize_squash_pass.cc b/paddle/fluid/framework/ir/mkldnn/cpu_quantize_squash_pass.cc index 3dfd82ebcfc3044dccc2943c2002947dbd72b3bb..eff9b294f7064ebad7e18fc5f24b791574d62d0c 100644 --- a/paddle/fluid/framework/ir/mkldnn/cpu_quantize_squash_pass.cc +++ b/paddle/fluid/framework/ir/mkldnn/cpu_quantize_squash_pass.cc @@ -294,6 +294,7 @@ void CPUQuantizeSquashPass::ApplyImpl(ir::Graph* graph) const { std::unordered_map nodes_keep_counter; FindNodesToKeep(graph, &nodes_keep_counter); DequantQuantSquash(graph, &nodes_keep_counter); + ConvRequantSquash(graph); ConvDequantSquash(graph); FcDequantSquash(graph); MultipleQuantizeSquash(graph); diff --git a/paddle/fluid/framework/ir/mkldnn/cpu_quantize_squash_pass_tester.cc b/paddle/fluid/framework/ir/mkldnn/cpu_quantize_squash_pass_tester.cc index 93ae9c8796aab28052ca7f11549778921145c1fd..5a364aab1c59169b95756c55bae94ba4e91fc86c 100644 --- a/paddle/fluid/framework/ir/mkldnn/cpu_quantize_squash_pass_tester.cc +++ b/paddle/fluid/framework/ir/mkldnn/cpu_quantize_squash_pass_tester.cc @@ -355,18 +355,45 @@ TEST(CpuQuantizeSquashPass, equal_scales) { // From Conv1->d->Dequant->e->Quant->f->Conv2 // First change to Conv1->d->Requant->f->Conv2 +// Then Conv1->f->Conv2 TEST(CpuQuantizeSquashPass, unequal_scales) { auto scale_out = 1.0f; auto scale1 = 1.2345f; auto scale2 = 21.0f; auto use_mkldnn = true; - // Remove 3 nodes: Dequant, Quant, e - // Insert 1 node: Requant - auto remove_nodes = 2; + // Remove 4 nodes: Dequant, Quant, e, d + auto remove_nodes = 4; CountNodeTest( BuildConvRequantProgramDesc(use_mkldnn, scale_out, scale1, scale2), remove_nodes); + + EqualScaleOutTest( + BuildConvRequantProgramDesc(use_mkldnn, scale_out, scale1, scale2), + "Conv1", scale2); +} + +// a->Conv1->b->Requant->c +// d->Conv2->e->Requant->f +// {c,f}->Concat +TEST(CpuQuantizeSquashPass, equal_scales_squash_requantize) { + // Delete both requantize op + auto scale_out = 1.0f; + auto scale = 1.2345f; + auto use_mkldnn = true; + // Remove 4 nodes: b, Requant1, e, Requant2 + auto remove_nodes = 4; + CountNodeTest( + BuildConvsRequantConcatProgramDesc(use_mkldnn, scale_out, scale, scale), + remove_nodes); + + // check equal scale conv->scale_out and requant->scale_out + EqualScaleOutTest( + BuildConvsRequantConcatProgramDesc(use_mkldnn, scale_out, scale, scale), + "Conv1", scale); + EqualScaleOutTest( + BuildConvsRequantConcatProgramDesc(use_mkldnn, scale_out, scale, scale), + "Conv2", scale); } // from diff --git a/paddle/fluid/inference/tests/api/CMakeLists.txt b/paddle/fluid/inference/tests/api/CMakeLists.txt index ef63e16ff6d60e7c351361e898aa30ed5c0a7dbd..bd43962e10daa5dc3a8c42c7a45a66195fc1586f 100644 --- a/paddle/fluid/inference/tests/api/CMakeLists.txt +++ b/paddle/fluid/inference/tests/api/CMakeLists.txt @@ -47,12 +47,17 @@ function(inference_analysis_api_int8_test_run TARGET_NAME test_binary model_dir COMMAND ${test_binary} ARGS --infer_model=${model_dir}/model --infer_data=${data_path} - --warmup_batch_size=100 + --warmup_batch_size=${WARMUP_BATCH_SIZE} --batch_size=50 --paddle_num_threads=${CPU_NUM_THREADS_ON_CI} --iterations=2) endfunction() +function(inference_analysis_api_int8_test_run_custom_warmup_batch_size TARGET_NAME test_binary model_dir data_path warmup_batch_size) + set(WARMUP_BATCH_SIZE ${warmup_batch_size}) + inference_analysis_api_int8_test_run(${TARGET_NAME} ${test_binary} ${model_dir} ${data_path}) +endfunction() + function(inference_analysis_api_object_dection_int8_test_run TARGET_NAME test_binary model_dir data_path) inference_analysis_test_run(${TARGET_NAME} COMMAND ${test_binary} @@ -268,7 +273,7 @@ if(WITH_MKLDNN) # googlenet int8 set(INT8_GOOGLENET_MODEL_DIR "${INT8_DATA_DIR}/googlenet") download_int8_data(${INT8_GOOGLENET_MODEL_DIR} "GoogleNet_int8_model.tar.gz" ) - inference_analysis_api_int8_test_run(test_analyzer_int8_googlenet ${INT8_IMG_CLASS_TEST_APP} ${INT8_GOOGLENET_MODEL_DIR} ${IMAGENET_DATA_PATH}) + inference_analysis_api_int8_test_run_custom_warmup_batch_size(test_analyzer_int8_googlenet ${INT8_IMG_CLASS_TEST_APP} ${INT8_GOOGLENET_MODEL_DIR} ${IMAGENET_DATA_PATH} 10) ### Object detection models set(PASCALVOC_DATA_PATH "${INT8_DATA_DIR}/pascalvoc_val_head_300.bin") diff --git a/paddle/fluid/inference/tests/test.cmake b/paddle/fluid/inference/tests/test.cmake index 444bab1b33df063221828fe6c1457e2af672e652..72816f6d0317600ad6bf8ffc4ad31bd1a23d7c30 100644 --- a/paddle/fluid/inference/tests/test.cmake +++ b/paddle/fluid/inference/tests/test.cmake @@ -3,6 +3,7 @@ set(INFERENCE_URL "http://paddle-inference-dist.bj.bcebos.com" CACHE STRING "inf set(INFERENCE_DEMO_INSTALL_DIR "${THIRD_PARTY_PATH}/inference_demo" CACHE STRING "A path setting inference demo download directories.") set(CPU_NUM_THREADS_ON_CI 4 CACHE STRING "Run multi-threads on CI to reduce CI time.") +set(WARMUP_BATCH_SIZE 100 CACHE STRING "Default warmup_batch_size.") function(inference_download INSTALL_DIR URL FILENAME) message(STATUS "Download inference test stuff from ${URL}/${FILENAME}") diff --git a/python/paddle/fluid/contrib/slim/tests/CMakeLists.txt b/python/paddle/fluid/contrib/slim/tests/CMakeLists.txt index 24839170922ec99ade60e9226eee9a99a6520b34..65fd4984d95e9fdf98f86a23b06d52ef477ecd8b 100644 --- a/python/paddle/fluid/contrib/slim/tests/CMakeLists.txt +++ b/python/paddle/fluid/contrib/slim/tests/CMakeLists.txt @@ -8,7 +8,7 @@ function(_inference_analysis_python_api_int8_test target model_dir data_dir file ARGS --infer_model ${model_dir}/model --infer_data ${data_dir}/data.bin --int8_model_save_path int8_models/${target} - --warmup_batch_size 100 + --warmup_batch_size ${WARMUP_BATCH_SIZE} --batch_size 50) endfunction() @@ -20,6 +20,11 @@ function(inference_analysis_python_api_int8_test_mkldnn target model_dir data_di _inference_analysis_python_api_int8_test(${target} ${model_dir} ${data_dir} ${filename} True) endfunction() +function(inference_analysis_python_api_int8_test_custom_warmup_batch_size target model_dir data_dir filename warmup_batch_size) + set(WARMUP_BATCH_SIZE ${warmup_batch_size}) + inference_analysis_python_api_int8_test(${target} ${model_dir} ${data_dir} ${filename}) +endfunction() + function(inference_qat_int8_test target model_dir data_dir test_script use_mkldnn) py_test(${target} SRCS ${test_script} ENVS FLAGS_OMP_NUM_THREADS=${CPU_NUM_THREADS_ON_CI} @@ -68,7 +73,7 @@ if(LINUX AND WITH_MKLDNN) # googlenet int8 set(INT8_GOOGLENET_MODEL_DIR "${INT8_DATA_DIR}/googlenet") - inference_analysis_python_api_int8_test(test_slim_int8_googlenet ${INT8_GOOGLENET_MODEL_DIR} ${INT8_DATA_DIR} ${MKLDNN_INT8_TEST_FILE_PATH}) + inference_analysis_python_api_int8_test_custom_warmup_batch_size(test_slim_int8_googlenet ${INT8_GOOGLENET_MODEL_DIR} ${INT8_DATA_DIR} ${MKLDNN_INT8_TEST_FILE_PATH} 10) # mobilenet int8 set(INT8_MOBILENET_MODEL_DIR "${INT8_DATA_DIR}/mobilenetv1")