未验证 提交 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()() { ...@@ -2249,9 +2249,9 @@ PDNode *patterns::MultipleQuantize::operator()() {
PDNode *patterns::QuantizePlacement::operator()( PDNode *patterns::QuantizePlacement::operator()(
const std::unordered_set<std::string> &quantize_enabled_op_types) { const std::unordered_set<std::string> &quantize_enabled_op_types) {
std::unordered_set<std::string> supported_op_types = std::unordered_set<std::string> supported_op_types =
std::unordered_set<std::string>( std::unordered_set<std::string>({"concat", "conv2d", "elementwise_add",
{"concat", "conv2d", "elementwise_add", "fc", "matmul", "pool2d", "fc", "matmul", "pool2d", "prior_box",
"prior_box", "relu", "reshape2", "transpose2", "fusion_gru"}); "reshape2", "transpose2", "fusion_gru"});
if (!quantize_enabled_op_types.empty()) { if (!quantize_enabled_op_types.empty()) {
supported_op_types = quantize_enabled_op_types; supported_op_types = quantize_enabled_op_types;
} }
......
...@@ -770,7 +770,8 @@ void CPUQuantizePass::QuantizeElementwiseAdd(Graph* graph) const { ...@@ -770,7 +770,8 @@ void CPUQuantizePass::QuantizeElementwiseAdd(Graph* graph) const {
GET_IR_NODE_FROM_SUBGRAPH(elementwise_add_out, elementwise_add_out, GET_IR_NODE_FROM_SUBGRAPH(elementwise_add_out, elementwise_add_out,
elementwise_add_pattern); 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); LogCannotQuantizeOp(elementwise_add_op);
return; return;
} }
...@@ -793,16 +794,12 @@ void CPUQuantizePass::QuantizeElementwiseAdd(Graph* graph) const { ...@@ -793,16 +794,12 @@ void CPUQuantizePass::QuantizeElementwiseAdd(Graph* graph) const {
QuantizeInput(g, elementwise_add_op, elementwise_add_y, "Y", input_y_scale, QuantizeInput(g, elementwise_add_op, elementwise_add_y, "Y", input_y_scale,
is_y_unsigned, "Scale_y"); is_y_unsigned, "Scale_y");
// if quantization scale is missing for output tensor, return fp32 data bool is_output_unsigned{false};
if (AreScalesPresentForNodes({elementwise_add_out})) { auto output_scale =
bool is_output_unsigned{false}; GetScaleValueForNode(elementwise_add_out, &is_output_unsigned);
auto output_scale =
GetScaleValueForNode(elementwise_add_out, &is_output_unsigned); DequantizeOutput(g, elementwise_add_op, elementwise_add_out, "Out",
DequantizeOutput(g, elementwise_add_op, elementwise_add_out, "Out", output_scale, is_output_unsigned, "Scale_out");
output_scale, is_output_unsigned, "Scale_out");
} else {
elementwise_add_op->Op()->SetAttr("force_fp32_output", true);
}
++quantize_elementwise_add_count; ++quantize_elementwise_add_count;
}; };
......
...@@ -854,13 +854,12 @@ TEST(CpuQuantizePass, elementwise_add) { ...@@ -854,13 +854,12 @@ TEST(CpuQuantizePass, elementwise_add) {
TEST(CpuQuantizePass, elementwise_add_output_scale_missing) { TEST(CpuQuantizePass, elementwise_add_output_scale_missing) {
int elementwise_add_count = 1; int elementwise_add_count = 1;
int quant_count = 2; int quant_count = 0;
int dequant_count = 2; int dequant_count = 2;
// 2 Quant + 2 IN int added_nodes_count = 0;
int added_nodes_count = 4;
MainTestElementwiseAdd(BuildProgramDescElementwiseAdd(), MainTestElementwiseAdd(BuildProgramDescElementwiseAdd(),
elementwise_add_count, quant_count, dequant_count, 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) { TEST(CpuQuantizePass, elementwise_add_unsigned_and_signed_input) {
......
...@@ -131,13 +131,13 @@ TEST(QuantizerPlacementPass, enabled_conv_excluded_one) { ...@@ -131,13 +131,13 @@ TEST(QuantizerPlacementPass, enabled_conv_excluded_one) {
} }
TEST(QuantizerPlacementPass, empty_list) { TEST(QuantizerPlacementPass, empty_list) {
// all operators quantized // all operators except relu should be quantized
MainTest({}, {}, 6); MainTest({}, {}, 5);
} }
TEST(QuantizerPlacementPass, default_attr_value) { TEST(QuantizerPlacementPass, default_attr_value) {
// all operators quantized // all operators except relu should be quantized
DefaultAttrTest(6); DefaultAttrTest(5);
} }
} // namespace ir } // namespace ir
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册