Skip to content

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

[LPT] NormalizeDequantization correction (#26490)

Vladislav Golubev3e2f1eb30817
diff --git a/src/common/low_precision_transformations/src/network_helper.cpp b/src/common/low_precision_transformations/src/network_helper.cpp
index 95958bd67f2..1aebfb111d7 100644
--- a/src/common/low_precision_transformations/src/network_helper.cpp
+++ b/src/common/low_precision_transformations/src/network_helper.cpp
@@ -1293,8 +1293,19 @@ FakeQuantizeDequantization NetworkHelper::normalizeDequantization(FakeQuantizeDe
return dequantization;
}
- // task: 79740
- if (dequantization.multiply != nullptr && ov::as_type_ptr<ov::opset1::Constant>(dequantization.multiply->get_input_node_shared_ptr(0))) {
+ // Note: Dequantization operations might have converts between DQ constant and eltwise
+ auto is_branch_const = [](const ov::Output<ov::Node>& in) {
+ auto node = in.get_node_shared_ptr();
+ return (ov::is_type<ov::opset1::Constant>(node)) ||
+ (ov::is_type<ov::opset1::Convert>(node) && ov::is_type<ov::opset1::Constant>(node->get_input_node_shared_ptr(0)));
+ };
+
+ // Eltwise is unnormalized if it has constant 0th input and non-constant 1st input
+ auto unnormalized_eltwise = [&is_branch_const](const std::shared_ptr<ov::Node>& eltwise) {
+ return eltwise != nullptr && is_branch_const(eltwise->input_value(0)) && !is_branch_const(eltwise->input_value(1));
+ };
+
+ if (unnormalized_eltwise(dequantization.multiply)) {
const auto leftParent = dequantization.multiply->input_value(0);
const auto rightParent = dequantization.multiply->input_value(1);
std::shared_ptr<ov::opset1::Multiply> normalized_multiply = ov::as_type_ptr<ov::opset1::Multiply>(
@@ -1302,7 +1313,7 @@ FakeQuantizeDequantization NetworkHelper::normalizeDequantization(FakeQuantizeDe
replace_node(dequantization.multiply, normalized_multiply);
dequantization.multiply = normalized_multiply;
}
- if (dequantization.subtract != nullptr && ov::as_type_ptr<ov::opset1::Constant>(dequantization.subtract->get_input_node_shared_ptr(0))) {
+ if (unnormalized_eltwise(dequantization.subtract)) {
const auto leftParent = dequantization.subtract->input_value(0);
const auto rightParent = dequantization.subtract->input_value(1);
std::shared_ptr<ov::opset1::Subtract> normalized_subtract = ov::as_type_ptr<ov::opset1::Subtract>(
diff --git a/src/common/low_precision_transformations/tests/normalize_dequantization_transformation.cpp b/src/common/low_precision_transformations/tests/normalize_dequantization_transformation.cpp
index 3937a9a0e59..4ed67c7297b 100644
--- a/src/common/low_precision_transformations/tests/normalize_dequantization_transformation.cpp
+++ b/src/common/low_precision_transformations/tests/normalize_dequantization_transformation.cpp
@@ -36,6 +36,7 @@ public:
};
TestTransformationParams params;
ov::Shape inputShape;
+ bool constantPath;
Actual actual;
Expected expected;
};
@@ -48,7 +49,8 @@ public:
actualFunction = ov::builder::subgraph::NormalizeDequantizationFunction::getOriginal(
testValues.actual.precisionBeforeDequantization,
testValues.inputShape,
- testValues.actual.dequantization);
+ testValues.actual.dequantization,
+ testValues.constantPath);
const auto targetNode = actualFunction->get_output_op(0)->get_input_node_shared_ptr(0);
const auto dequantization = ov::pass::low_precision::NetworkHelper::getDequantization(targetNode);
@@ -57,7 +59,8 @@ public:
referenceFunction = ov::builder::subgraph::NormalizeDequantizationFunction::getOriginal(
testValues.expected.precisionBeforeDequantization,
testValues.inputShape,
- testValues.expected.dequantization);
+ testValues.expected.dequantization,
+ testValues.constantPath);
}
static std::string getTestCaseName(testing::TestParamInfo<NormalizeDequantizationTestValues> obj) {
@@ -80,10 +83,14 @@ TEST_P(NormalizeDequantizationTransformation, CompareFunctions) {
ASSERT_TRUE(res.first) << res.second;
}
+using Subtract = ov::builder::subgraph::DequantizationOperations::Subtract;
+using Multiply = ov::builder::subgraph::DequantizationOperations::Multiply;
+
const std::vector<NormalizeDequantizationTestValues> testValues = {
{
LayerTransformation::createParamsU8I8(),
{ 1, 3, 16, 16 },
+ false,
{
ov::element::f32,
{
@@ -104,6 +111,7 @@ const std::vector<NormalizeDequantizationTestValues> testValues = {
{
LayerTransformation::createParamsU8I8(),
{ 1, 3, 16, 16 },
+ false,
{
ov::element::i32,
{
@@ -124,6 +132,7 @@ const std::vector<NormalizeDequantizationTestValues> testValues = {
{
LayerTransformation::createParamsU8I8(),
{ 1, 3, 16, 16 },
+ false,
{
ov::element::u32,
{
@@ -144,6 +153,7 @@ const std::vector<NormalizeDequantizationTestValues> testValues = {
{
LayerTransformation::createParamsU8I8().setUpdatePrecisions(true),
{ 1, 3, 16, 16 },
+ false,
{
ov::element::u32,
{
@@ -161,6 +171,69 @@ const std::vector<NormalizeDequantizationTestValues> testValues = {
}
},
},
+ {
+ LayerTransformation::createParamsU8I8(),
+ { 1, 3, 16, 16 },
+ true,
+ {
+ ov::element::f32,
+ {
+ {},
+ { {7.f}, ov::element::f32, { 1, 3, 16, 16 }, true, 1 },
+ { {10.f}, ov::element::f32, { 1, 3, 16, 16 }, true, 1 }
+ },
+ },
+ {
+ ov::element::f32,
+ {
+ {},
+ { {7.f}, ov::element::f32, { 1, 3, 16, 16 }, true, 1 },
+ { {10.0f}, ov::element::f32, { 1, 3, 16, 16 }, true, 1 }
+ }
+ },
+ },
+ {
+ LayerTransformation::createParamsU8I8(),
+ { 1, 3, 16, 16 },
+ true,
+ {
+ ov::element::i8,
+ {
+ {ov::element::f32},
+ Subtract({7.f}, ov::element::f32, { 1, 3, 16, 16 }).setConstantPrecision(ov::element::f16).setAddConvert(true),
+ Multiply({10.f}, ov::element::f32, { 1, 3, 16, 16 })
+ },
+ },
+ {
+ ov::element::i8,
+ {
+ {ov::element::f32},
+ Subtract({7.f}, ov::element::f32, { 1, 3, 16, 16 }).setConstantPrecision(ov::element::f16).setAddConvert(true),
+ Multiply({10.f}, ov::element::f32, { 1, 3, 16, 16 })
+ },
+ },
+ },
+ {
+ LayerTransformation::createParamsU8I8(),
+ { 1, 3, 16, 16 },
+ true,
+ {
+ ov::element::f32,
+ {
+ {},
+ Subtract({7.f}, ov::element::f32, { 1, 3, 16, 16 }).setConstantPrecision(ov::element::f16).setAddConvert(true),
+ Multiply({10.f}, ov::element::f32, { 1, 3, 16, 16 })
+ },
+ },
+ {
+ ov::element::f32,
+ {
+ {},
+ Subtract({7.f}, ov::element::f32, { 1, 3, 16, 16 }).setConstantPrecision(ov::element::f16).setAddConvert(true),
+ Multiply({10.f}, ov::element::f32, { 1, 3, 16, 16 })
+ },
+ },
+ },
};
INSTANTIATE_TEST_SUITE_P(
diff --git a/src/tests/ov_helpers/ov_lpt_models/include/ov_lpt_models/normalize_dequantization.hpp b/src/tests/ov_helpers/ov_lpt_models/include/ov_lpt_models/normalize_dequantization.hpp
index 20a3b961030..43f0fbd4f50 100644
--- a/src/tests/ov_helpers/ov_lpt_models/include/ov_lpt_models/normalize_dequantization.hpp
+++ b/src/tests/ov_helpers/ov_lpt_models/include/ov_lpt_models/normalize_dequantization.hpp
@@ -16,7 +16,8 @@ public:
static std::shared_ptr<ov::Model> getOriginal(
const ov::element::Type precision,
const ov::Shape& inputShape,
- const ov::builder::subgraph::DequantizationOperations dequantization);
+ const ov::builder::subgraph::DequantizationOperations dequantization,
+ bool constant_path);
};
} // namespace subgraph
diff --git a/src/tests/ov_helpers/ov_lpt_models/src/normalize_dequantization.cpp b/src/tests/ov_helpers/ov_lpt_models/src/normalize_dequantization.cpp
index 72e0db50e3b..eeb1977afbe 100644
--- a/src/tests/ov_helpers/ov_lpt_models/src/normalize_dequantization.cpp
+++ b/src/tests/ov_helpers/ov_lpt_models/src/normalize_dequantization.cpp
@@ -4,6 +4,7 @@
#include "ov_lpt_models/normalize_dequantization.hpp"
+#include "common_test_utils/node_builders/constant.hpp"
#include "ov_lpt_models/common/builders.hpp"
#include "ov_ops/type_relaxed.hpp"
@@ -14,9 +15,17 @@ namespace subgraph {
std::shared_ptr<ov::Model> NormalizeDequantizationFunction::getOriginal(
const ov::element::Type precision,
const ov::Shape& inputShape,
- const ov::builder::subgraph::DequantizationOperations dequantization) {
- const auto input = std::make_shared<ov::op::v0::Parameter>(precision, inputShape);
-
+ const ov::builder::subgraph::DequantizationOperations dequantization,
+ bool constant_path) {
+ std::shared_ptr<ov::Node> input;
+ ov::ParameterVector params;
+ if (constant_path) {
+ input = ov::test::utils::make_constant(precision, inputShape);
+ } else {
+ auto param = std::make_shared<ov::op::v0::Parameter>(precision, inputShape);
+ params.push_back(param);
+ input = param;
+ }
const auto deq = makeDequantization(input, dequantization);
const auto op =
@@ -30,7 +39,7 @@ namespace subgraph {
return std::make_shared<ov::Model>(
ov::ResultVector{ std::make_shared<ov::opset1::Result>(targetOp) },
- ov::ParameterVector{ input },
+ params,
"NormalizeDequantizationFunction");
}