Skip to content

Commit ae80df9

Browse files
author
joanna.wozna.intel
authored
Fix elementwise_add quantization (#34820)
* Remove force_fp32_output from elementwise_add quantization * Fix cpu_quantize_placement test * Review related changes
1 parent 9cb6565 commit ae80df9

File tree

4 files changed

+18
-22
lines changed

4 files changed

+18
-22
lines changed

paddle/fluid/framework/ir/graph_pattern_detector.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2249,9 +2249,9 @@ PDNode *patterns::MultipleQuantize::operator()() {
22492249
PDNode *patterns::QuantizePlacement::operator()(
22502250
const std::unordered_set<std::string> &quantize_enabled_op_types) {
22512251
std::unordered_set<std::string> supported_op_types =
2252-
std::unordered_set<std::string>(
2253-
{"concat", "conv2d", "elementwise_add", "fc", "matmul", "pool2d",
2254-
"prior_box", "relu", "reshape2", "transpose2", "fusion_gru"});
2252+
std::unordered_set<std::string>({"concat", "conv2d", "elementwise_add",
2253+
"fc", "matmul", "pool2d", "prior_box",
2254+
"reshape2", "transpose2", "fusion_gru"});
22552255
if (!quantize_enabled_op_types.empty()) {
22562256
supported_op_types = quantize_enabled_op_types;
22572257
}

paddle/fluid/framework/ir/mkldnn/cpu_quantize_pass.cc

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,8 @@ void CPUQuantizePass::QuantizeElementwiseAdd(Graph* graph) const {
770770
GET_IR_NODE_FROM_SUBGRAPH(elementwise_add_out, elementwise_add_out,
771771
elementwise_add_pattern);
772772

773-
if (!AreScalesPresentForNodes({elementwise_add_x, elementwise_add_y})) {
773+
if (!AreScalesPresentForNodes(
774+
{elementwise_add_x, elementwise_add_y, elementwise_add_out})) {
774775
LogCannotQuantizeOp(elementwise_add_op);
775776
return;
776777
}
@@ -793,16 +794,12 @@ void CPUQuantizePass::QuantizeElementwiseAdd(Graph* graph) const {
793794
QuantizeInput(g, elementwise_add_op, elementwise_add_y, "Y", input_y_scale,
794795
is_y_unsigned, "Scale_y");
795796

796-
// if quantization scale is missing for output tensor, return fp32 data
797-
if (AreScalesPresentForNodes({elementwise_add_out})) {
798-
bool is_output_unsigned{false};
799-
auto output_scale =
800-
GetScaleValueForNode(elementwise_add_out, &is_output_unsigned);
801-
DequantizeOutput(g, elementwise_add_op, elementwise_add_out, "Out",
802-
output_scale, is_output_unsigned, "Scale_out");
803-
} else {
804-
elementwise_add_op->Op()->SetAttr("force_fp32_output", true);
805-
}
797+
bool is_output_unsigned{false};
798+
auto output_scale =
799+
GetScaleValueForNode(elementwise_add_out, &is_output_unsigned);
800+
801+
DequantizeOutput(g, elementwise_add_op, elementwise_add_out, "Out",
802+
output_scale, is_output_unsigned, "Scale_out");
806803

807804
++quantize_elementwise_add_count;
808805
};

paddle/fluid/framework/ir/mkldnn/cpu_quantize_pass_tester.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -854,13 +854,12 @@ TEST(CpuQuantizePass, elementwise_add) {
854854

855855
TEST(CpuQuantizePass, elementwise_add_output_scale_missing) {
856856
int elementwise_add_count = 1;
857-
int quant_count = 2;
857+
int quant_count = 0;
858858
int dequant_count = 2;
859-
// 2 Quant + 2 IN
860-
int added_nodes_count = 4;
859+
int added_nodes_count = 0;
861860
MainTestElementwiseAdd(BuildProgramDescElementwiseAdd(),
862861
elementwise_add_count, quant_count, dequant_count,
863-
added_nodes_count, 2.0f * 127, true);
862+
added_nodes_count, 1.f, true);
864863
}
865864

866865
TEST(CpuQuantizePass, elementwise_add_unsigned_and_signed_input) {

paddle/fluid/framework/ir/mkldnn/cpu_quantize_placement_pass_tester.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,13 @@ TEST(QuantizerPlacementPass, enabled_conv_excluded_one) {
131131
}
132132

133133
TEST(QuantizerPlacementPass, empty_list) {
134-
// all operators quantized
135-
MainTest({}, {}, 6);
134+
// all operators except relu should be quantized
135+
MainTest({}, {}, 5);
136136
}
137137

138138
TEST(QuantizerPlacementPass, default_attr_value) {
139-
// all operators quantized
140-
DefaultAttrTest(6);
139+
// all operators except relu should be quantized
140+
DefaultAttrTest(5);
141141
}
142142

143143
} // namespace ir

0 commit comments

Comments
 (0)