diff --git a/paddle/fluid/framework/ir/graph_pattern_detector.cc b/paddle/fluid/framework/ir/graph_pattern_detector.cc index 70e48755dcd1e33812cfbfdc60a639df510abfcd..b4c94010e480a79b54531a7f3e1bff6723cec2cb 100644 --- a/paddle/fluid/framework/ir/graph_pattern_detector.cc +++ b/paddle/fluid/framework/ir/graph_pattern_detector.cc @@ -2249,9 +2249,9 @@ PDNode *patterns::MultipleQuantize::operator()() { PDNode *patterns::QuantizePlacement::operator()( const std::unordered_set &quantize_enabled_op_types) { std::unordered_set supported_op_types = - std::unordered_set( - {"concat", "conv2d", "elementwise_add", "fc", "matmul", "pool2d", - "prior_box", "relu", "reshape2", "transpose2", "fusion_gru"}); + std::unordered_set({"concat", "conv2d", "elementwise_add", + "fc", "matmul", "pool2d", "prior_box", + "reshape2", "transpose2", "fusion_gru"}); if (!quantize_enabled_op_types.empty()) { supported_op_types = quantize_enabled_op_types; } diff --git a/paddle/fluid/framework/ir/mkldnn/cpu_quantize_pass.cc b/paddle/fluid/framework/ir/mkldnn/cpu_quantize_pass.cc index 3c06c9ee41d2a2b314bec1d656d33387a3aaf73d..f50cd0a01d204dcce4935d93e3dd45554a654b82 100644 --- a/paddle/fluid/framework/ir/mkldnn/cpu_quantize_pass.cc +++ b/paddle/fluid/framework/ir/mkldnn/cpu_quantize_pass.cc @@ -770,7 +770,8 @@ void CPUQuantizePass::QuantizeElementwiseAdd(Graph* graph) const { GET_IR_NODE_FROM_SUBGRAPH(elementwise_add_out, elementwise_add_out, elementwise_add_pattern); - if (!AreScalesPresentForNodes({elementwise_add_x, elementwise_add_y})) { + if (!AreScalesPresentForNodes( + {elementwise_add_x, elementwise_add_y, elementwise_add_out})) { LogCannotQuantizeOp(elementwise_add_op); return; } @@ -793,16 +794,12 @@ void CPUQuantizePass::QuantizeElementwiseAdd(Graph* graph) const { QuantizeInput(g, elementwise_add_op, elementwise_add_y, "Y", input_y_scale, is_y_unsigned, "Scale_y"); - // if quantization scale is missing for output tensor, return fp32 data - if (AreScalesPresentForNodes({elementwise_add_out})) { - bool is_output_unsigned{false}; - auto output_scale = - GetScaleValueForNode(elementwise_add_out, &is_output_unsigned); - DequantizeOutput(g, elementwise_add_op, elementwise_add_out, "Out", - output_scale, is_output_unsigned, "Scale_out"); - } else { - elementwise_add_op->Op()->SetAttr("force_fp32_output", true); - } + bool is_output_unsigned{false}; + auto output_scale = + GetScaleValueForNode(elementwise_add_out, &is_output_unsigned); + + DequantizeOutput(g, elementwise_add_op, elementwise_add_out, "Out", + output_scale, is_output_unsigned, "Scale_out"); ++quantize_elementwise_add_count; }; diff --git a/paddle/fluid/framework/ir/mkldnn/cpu_quantize_pass_tester.cc b/paddle/fluid/framework/ir/mkldnn/cpu_quantize_pass_tester.cc index adb431fdb097f54d5d476065aa6915b66ac71299..6fcea6a66cc5d18729d000131a87687ea7c9ed6c 100644 --- a/paddle/fluid/framework/ir/mkldnn/cpu_quantize_pass_tester.cc +++ b/paddle/fluid/framework/ir/mkldnn/cpu_quantize_pass_tester.cc @@ -854,13 +854,12 @@ TEST(CpuQuantizePass, elementwise_add) { TEST(CpuQuantizePass, elementwise_add_output_scale_missing) { int elementwise_add_count = 1; - int quant_count = 2; + int quant_count = 0; int dequant_count = 2; - // 2 Quant + 2 IN - int added_nodes_count = 4; + int added_nodes_count = 0; MainTestElementwiseAdd(BuildProgramDescElementwiseAdd(), elementwise_add_count, quant_count, dequant_count, - added_nodes_count, 2.0f * 127, true); + added_nodes_count, 1.f, true); } TEST(CpuQuantizePass, elementwise_add_unsigned_and_signed_input) { diff --git a/paddle/fluid/framework/ir/mkldnn/cpu_quantize_placement_pass_tester.cc b/paddle/fluid/framework/ir/mkldnn/cpu_quantize_placement_pass_tester.cc index 761defc25ff5c89b740ccd5adff7d613beccd9d4..daf913bf7d80d174f753a4303c12f6c86319420b 100644 --- a/paddle/fluid/framework/ir/mkldnn/cpu_quantize_placement_pass_tester.cc +++ b/paddle/fluid/framework/ir/mkldnn/cpu_quantize_placement_pass_tester.cc @@ -131,13 +131,13 @@ TEST(QuantizerPlacementPass, enabled_conv_excluded_one) { } TEST(QuantizerPlacementPass, empty_list) { - // all operators quantized - MainTest({}, {}, 6); + // all operators except relu should be quantized + MainTest({}, {}, 5); } TEST(QuantizerPlacementPass, default_attr_value) { - // all operators quantized - DefaultAttrTest(6); + // all operators except relu should be quantized + DefaultAttrTest(5); } } // namespace ir