Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[LPT] NetworkHelper::moveDequantization* fix (#33575)

Vladislav Golubevf5b2e53b9bde
diff --git a/src/common/low_precision_transformations/src/network_helper.cpp b/src/common/low_precision_transformations/src/network_helper.cpp
index 15f899d6809..0fe0420be73 100644
--- a/src/common/low_precision_transformations/src/network_helper.cpp
+++ b/src/common/low_precision_transformations/src/network_helper.cpp
@@ -1450,8 +1450,6 @@ NetworkHelper::InsertDequantizationResult NetworkHelper::moveDequantizationAfter
(NetworkHelper::getDequantization(operation, defaultPrecisions).multiplyConstant == nullptr) ||
(NetworkHelper::getDequantization(operation, defaultPrecisions).multiplyConstant.get() == dequantization.multiplyConstant.get()));
- assert(operation->get_output_size() == 1);
-
// we must have dequantization multiply
assert(dequantization.multiply != nullptr);
@@ -1498,16 +1496,14 @@ NetworkHelper::InsertDequantizationResult NetworkHelper::moveDequantizationAfter
dequantization.subtractConstant->get_element_type();
}
- parent = std::make_shared<ov::op::TypeRelaxed<ov::opset1::Subtract>>(
- element::TypeVector{ element::f32, element::f32 }, element::TypeVector{ element::f32 },
- ov::op::TemporaryReplaceOutputType(parent, element::f32).get(),
- ov::op::TemporaryReplaceOutputType(foldConvert(dequantization.subtractConstant, parentPrecision), element::f32).get());
+ parent = dequantization.subtract->clone_with_new_inputs(
+ {parent, foldConvert(dequantization.subtractConstant, parentPrecision)});
ov::copy_runtime_info({ newOperation, parent }, parent);
} else {
// Subtract constant could be changed (including a shape) before propagation in some cases
// so it's necessary to compute the shape for a subtractConvert before creating a new subtract
dequantization.subtractConvert->validate_and_infer_types();
- parent = std::make_shared<ov::opset1::Subtract>(parent, dequantization.subtractConvert);
+ parent = dequantization.subtract->clone_with_new_inputs({parent, dequantization.subtractConvert});
ov::copy_runtime_info({ newOperation, parent }, parent);
}
}
@@ -1521,19 +1517,12 @@ NetworkHelper::InsertDequantizationResult NetworkHelper::moveDequantizationAfter
dequantization.multiplyConstant->get_element_type();
}
- parent = std::make_shared<ov::op::TypeRelaxed<ov::opset1::Multiply>>(
- ov::opset1::Multiply(parent, foldConvert(dequantization.multiplyConstant, parentPrecision)),
- dequantization.multiply->get_output_element_type(0));
+ parent = dequantization.multiply->clone_with_new_inputs(
+ {parent, foldConvert(dequantization.multiplyConstant, parentPrecision)});
ov::copy_runtime_info({ newOperation, parent }, parent);
}
insertDequantizationAfter(operation, parent, newOperation);
-
- if ((!moveSubtract) && (dequantization.convert != nullptr) && (dequantization.subtract != nullptr)) {
- // issue #43088
- // NetworkHelper::optimizeElementwise(dequantization.subtract);
- }
-
return InsertDequantizationResult(newOperation, parent);
}
@@ -1593,19 +1582,14 @@ NetworkHelper::InsertDequantizationResult NetworkHelper::moveDequantizationBefor
dequantization.subtractConstant->get_element_type();
}
auto subtractConstant = subtractConstants.size() ? subtractConstants[0][i] : dequantization.subtractConstant;
- parent = std::make_shared<ov::op::TypeRelaxed<ov::opset1::Subtract>>(
- std::vector<element::Type>{element::f32, element::f32}, std::vector<element::Type>{ element::f32 },
- ov::op::TemporaryReplaceOutputType(parent, element::f32).get(),
- ov::op::TemporaryReplaceOutputType(
- subtractConstant->output(0).get_element_type() == parentPrecision ?
- subtractConstant :
- foldConvert(subtractConstant, parentPrecision), element::f32).get());
+ parent = dequantization.subtract->clone_with_new_inputs(
+ {parent, foldConvert(subtractConstant, parentPrecision)});
parent->set_friendly_name(dequantization.subtract->get_friendly_name() + "_" + std::to_string(i + 1));
} else {
// Subtract constant could be changed (including a shape) before propagation in some cases
// so it's necessary to compute the shape for a subtractConvert before creating a new subtract
dequantization.subtractConvert->validate_and_infer_types();
- parent = std::make_shared<ov::opset1::Subtract>(parent, dequantization.subtractConvert);
+ parent = dequantization.subtract->clone_with_new_inputs({parent, dequantization.subtractConvert});
}
ov::copy_runtime_info(dequantization.subtract, parent);
}
@@ -1619,19 +1603,11 @@ NetworkHelper::InsertDequantizationResult NetworkHelper::moveDequantizationBefor
", multiply dequantization constant " << multiplyConstant->get_friendly_name() << ":" << multiplyConstant->get_element_type();
}
- parent = std::make_shared<ov::op::TypeRelaxed<ov::opset1::Multiply>>(
- ov::opset1::Multiply(parent,
- multiplyConstant->output(0).get_element_type() == parentPrecision ?
- multiplyConstant :
- foldConvert(multiplyConstant->output(0), parentPrecision)),
- dequantization.multiply->get_output_element_type(0));
+ parent = dequantization.multiply->clone_with_new_inputs(
+ {parent, foldConvert(multiplyConstant->output(0), parentPrecision)});
ov::copy_runtime_info(dequantization.multiply, parent);
parent->set_friendly_name(dequantization.multiply->get_friendly_name() + "_" + std::to_string(i + 1));
}
- if ((!moveSubtract) && (dequantization.convert != nullptr) && (dequantization.subtract != nullptr)) {
- // issue #43088
- // NetworkHelper::optimizeElementwise(dequantization.subtract);
- }
newNodes.push_back(parent);
}
auto newOperation = operation->clone_with_new_inputs(ov::OutputVector(newNodes.begin(), newNodes.end()));
diff --git a/src/common/low_precision_transformations/tests/move_dequantization_after_transformation.cpp b/src/common/low_precision_transformations/tests/move_dequantization_after_transformation.cpp
index 1d0b04ecf9e..8b18d6c09d3 100644
--- a/src/common/low_precision_transformations/tests/move_dequantization_after_transformation.cpp
+++ b/src/common/low_precision_transformations/tests/move_dequantization_after_transformation.cpp
@@ -291,6 +291,20 @@ const std::vector<MoveDequantizationAfterTransformationParams> testValues = {
},
false
},
+ {
+ ov::element::u8,
+ LayerTransformation::createParamsU8I8(),
+ true,
+ true,
+ {
+ {{ov::element::f16}, {128.f}, {0.1f}}
+ },
+ {
+ {},
+ ov::element::u8,
+ {{ov::element::f16}, {128.f}, {0.1f}}
+ }
+ },
};
INSTANTIATE_TEST_SUITE_P(
diff --git a/src/common/low_precision_transformations/tests/move_fake_quantize_transformation.cpp b/src/common/low_precision_transformations/tests/move_fake_quantize_transformation.cpp
index 29ae274f885..7da9a27993b 100644
--- a/src/common/low_precision_transformations/tests/move_fake_quantize_transformation.cpp
+++ b/src/common/low_precision_transformations/tests/move_fake_quantize_transformation.cpp
@@ -321,6 +321,30 @@ const std::vector<MoveFakeQuantizeTransformationTestValues> testValues = {
{},
{},
}},
+ // F16 Q/DQ with subtract
+ {LayerTransformation::createParamsU8I8(),
+ false,
+ 1,
+ {
+ 2,
+ {},
+ {},
+ {},
+ "",
+ {256ul, {}, {0.f}, {2.55f}, {0.f}, {2.55f}},
+ {ov::element::u8},
+ {{element::f16}, {0.01f}, {0.01f}},
+ },
+ {
+ 2,
+ {{256ul, {}, {0.f}, {2.55f}, {0.f}, {2.55f}}},
+ {ov::element::u8},
+ {{element::f16}, {0.01f}, {0.01f}},
+ "",
+ {},
+ {},
+ {},
+ }},
};
INSTANTIATE_TEST_SUITE_P(smoke_LPT,