未验证 提交 ae80df91 编写于 作者: J joanna.wozna.intel 提交者: GitHub

Fix elementwise_add quantization (#34820)

* Remove force_fp32_output from elementwise_add quantization

* Fix cpu_quantize_placement test

* Review related changes
上级 9cb65653
......@@ -2249,9 +2249,9 @@ PDNode *patterns::MultipleQuantize::operator()() {
PDNode *patterns::QuantizePlacement::operator()(
const std::unordered_set<std::string> &quantize_enabled_op_types) {
std::unordered_set<std::string> supported_op_types =
std::unordered_set<std::string>(
{"concat", "conv2d", "elementwise_add", "fc", "matmul", "pool2d",
"prior_box", "relu", "reshape2", "transpose2", "fusion_gru"});
std::unordered_set<std::string>({"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;
}
......
......@@ -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;
};
......
......@@ -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) {
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册