From 3099d9d47c2abb10118937ff6d6110d6f418e4a3 Mon Sep 17 00:00:00 2001 From: "joanna.wozna.intel" Date: Sat, 25 Jan 2020 15:17:15 +0100 Subject: [PATCH] Restore requantize squash (#22399) --- .../ir/mkldnn/cpu_quantize_squash_pass.cc | 1 + .../mkldnn/cpu_quantize_squash_pass_tester.cc | 33 +++++++++++++++++-- .../fluid/inference/tests/api/CMakeLists.txt | 9 +++-- paddle/fluid/inference/tests/test.cmake | 1 + .../fluid/contrib/slim/tests/CMakeLists.txt | 9 +++-- 5 files changed, 46 insertions(+), 7 deletions(-) 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 3dfd82ebcfc..eff9b294f70 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 93ae9c8796a..5a364aab1c5 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 ef63e16ff6d..bd43962e10d 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 444bab1b33d..72816f6d031 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 24839170922..65fd4984d95 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") -- GitLab