Skip to content

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

Allow ConvertQuantizeDequantize to handle mixed fp32/fp16 float precision (#35941)

Mikhail Dvoretckiicd7e2dbca590
diff --git a/src/common/low_precision_transformations/src/qdq_stripping.cpp b/src/common/low_precision_transformations/src/qdq_stripping.cpp
index 95cd3968fd2..566d54cae70 100644
--- a/src/common/low_precision_transformations/src/qdq_stripping.cpp
+++ b/src/common/low_precision_transformations/src/qdq_stripping.cpp
@@ -28,7 +28,9 @@
#include "openvino/op/fake_quantize.hpp"
#include "openvino/op/gather.hpp"
#include "openvino/op/less.hpp"
+#include "openvino/op/logical_or.hpp"
#include "openvino/op/matmul.hpp"
+#include "openvino/op/maximum.hpp"
#include "openvino/op/multiply.hpp"
#include "openvino/op/mvn.hpp"
#include "openvino/op/reshape.hpp"
@@ -205,18 +207,41 @@ bool FQStrippingTransformation::run_on_model(const std::shared_ptr<ov::Model>& f
auto fq_ranges_are_the_same = [](const std::shared_ptr<ov::op::v0::FakeQuantize>& fq) {
auto equal_with_threshold = [](const ov::Output<ov::Node>& val1, const ov::Output<ov::Node>& val2) {
- auto diff = std::make_shared<ov::op::v1::Subtract>(val1, val2);
- auto abs_diff = std::make_shared<ov::op::v0::Abs>(diff);
- auto eps = ov::op::v0::Constant::create(val1.get_element_type(), {}, {1e-6f});
- auto is_less = ov::util::get_constant_from_source(std::make_shared<ov::op::v1::Less>(abs_diff, eps));
-
auto all_true = [](const std::shared_ptr<ov::op::v0::Constant>& c) {
auto v = c->get_vector<bool>();
return std::all_of(v.begin(), v.end(), [](bool b) {
return b;
});
};
- return is_less && all_true(is_less);
+ if (val1.get_element_type() != val2.get_element_type()) {
+ auto abs1 = std::make_shared<ov::op::v0::Abs>(val1);
+ auto cast = std::make_shared<ov::op::v0::Convert>(val2, val1.get_element_type());
+ auto abs2 = std::make_shared<ov::op::v0::Abs>(cast);
+ auto diff = std::make_shared<ov::op::v1::Subtract>(val1, cast);
+ auto abs_diff = std::make_shared<ov::op::v0::Abs>(diff);
+ auto max_abs = std::make_shared<ov::op::v1::Maximum>(abs1, abs2);
+
+ // In the mixed precision case, an absolute difference check may not be applicable due to
+ // stored precision differences. Therefore, a mixed check is used, with the error criteria
+ // being an absolute error of over 1e-6 (effective for small values) and a relative error
+ // of over 1% (effective for large values)
+ auto eps = ov::op::v0::Constant::create(val1.get_element_type(), {}, {1e-6f});
+ auto relative_eps = ov::op::v0::Constant::create(val1.get_element_type(), {}, {1e-2f});
+ auto relative_eps_full = std::make_shared<ov::op::v1::Multiply>(max_abs, relative_eps);
+ auto is_less_absolute = std::make_shared<ov::op::v1::Less>(abs_diff, eps);
+ auto is_less_relative = std::make_shared<ov::op::v1::Less>(abs_diff, relative_eps_full);
+ auto is_less = ov::util::get_constant_from_source(
+ std::make_shared<ov::op::v1::LogicalOr>(is_less_absolute, is_less_relative));
+
+ return is_less && all_true(is_less);
+ } else {
+ auto diff = std::make_shared<ov::op::v1::Subtract>(val1, val2);
+ auto abs_diff = std::make_shared<ov::op::v0::Abs>(diff);
+ auto eps = ov::op::v0::Constant::create(val1.get_element_type(), {}, {1e-6f});
+ auto is_less = ov::util::get_constant_from_source(std::make_shared<ov::op::v1::Less>(abs_diff, eps));
+
+ return is_less && all_true(is_less);
+ }
};
return equal_with_threshold(fq->input_value(1), fq->input_value(3)) &&
diff --git a/src/common/low_precision_transformations/tests/subgraph/src/qdq_stripping.cpp b/src/common/low_precision_transformations/tests/subgraph/src/qdq_stripping.cpp
index dd50625f1d4..637c605bdbb 100644
--- a/src/common/low_precision_transformations/tests/subgraph/src/qdq_stripping.cpp
+++ b/src/common/low_precision_transformations/tests/subgraph/src/qdq_stripping.cpp
@@ -33,7 +33,7 @@ public:
TransformationTestsF::SetUp();
const auto& [need_weights_adjustment, quantization_precision] = GetParam();
manager.register_pass<ov::pass::SharedOpOptimization>();
- manager.register_pass<ov::pass::ConvertQuantizeDequantize>(TypeVector{i16, u16}, TypeVector{f32});
+ manager.register_pass<ov::pass::ConvertQuantizeDequantize>(TypeVector{i16, u16});
manager.register_pass<ov::pass::low_precision::FQStrippingTransformation>(std::set<size_t>{65536},
need_weights_adjustment);
}
@@ -87,3 +87,38 @@ INSTANTIATE_TEST_SUITE_P(smoke_LPT,
QDQStrippingTest,
Combine(Bool(), Values(ov::element::u16, ov::element::i16)),
QDQStrippingTest::getTestCaseName);
+
+class QDQStrippingTestMixed : public TransformationTestsF, public WithParamInterface<QDQStrippingTestParams> {
+public:
+ static std::string getTestCaseName(const ::testing::TestParamInfo<QDQStrippingTestParams>& info) {
+ const auto& [need_weights_adjustment, quantization_precision] = info.param;
+ return std::string(need_weights_adjustment ? "WeightsAdjusted" : "WeightsOriginal") + "_" +
+ quantization_precision.get_type_name();
+ }
+
+ QDQStrippingTestMixed() : TransformationTestsF() {
+ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);
+ }
+
+ void SetUp() override {
+ using namespace ov::element;
+ TransformationTestsF::SetUp();
+ const auto& [need_weights_adjustment, quantization_precision] = GetParam();
+ manager.register_pass<ov::pass::SharedOpOptimization>();
+ manager.register_pass<ov::pass::ConvertQuantizeDequantize>(TypeVector{i8, u8});
+ manager.register_pass<ov::pass::low_precision::FQStrippingTransformation>(std::set<size_t>{256},
+ need_weights_adjustment);
+ }
+};
+
+TEST_P(QDQStrippingTestMixed, MixedPrecision) {
+ const auto& [need_weights_adjustment, quantization_precision] = GetParam();
+ const auto input_shape = ov::PartialShape{1, 3, 8, 8};
+ model = QDQStrippingFunction::build_mixed_precision_pattern(input_shape, quantization_precision);
+ model_ref = QDQStrippingFunction::build_mixed_precision_pattern_ref(input_shape, need_weights_adjustment);
+}
+
+INSTANTIATE_TEST_SUITE_P(smoke_LPT,
+ QDQStrippingTestMixed,
+ Combine(Bool(), Values(ov::element::u8, ov::element::i8)),
+ QDQStrippingTestMixed::getTestCaseName);
diff --git a/src/common/transformations/include/transformations/common_optimizations/convert_quantize_dequantize.hpp b/src/common/transformations/include/transformations/common_optimizations/convert_quantize_dequantize.hpp
index 85b676d9ef1..7a33b838054 100644
--- a/src/common/transformations/include/transformations/common_optimizations/convert_quantize_dequantize.hpp
+++ b/src/common/transformations/include/transformations/common_optimizations/convert_quantize_dequantize.hpp
@@ -24,10 +24,11 @@ class TRANSFORMATIONS_API ConvertQuantizeDequantize;
* @brief ConvertQuantizeDequantize transformation replaces following graph:
* FakeQuantize->Convert->Convert->Subtract->Multiply with a single FakeQuantize.
* Restrictions:
- * - quantized data type must be i8 or u8
- * - 'levels' attribute to FakeQuantize must be equal to 256
- * - (output_low, output_high) must be (-128, 127) or (0, 256) (depends on sign of quantized data type)
+ * - quantized data type must be i8, u8, i16, or u16
+ * - 'levels' attribute to FakeQuantize must be equal to 256 or 65536
+ * - (output_low, output_high) must match the quantized data type range
* - 'zero_point' and 'scale' must be broadcastable to FakeQuantize's output
+ * - supports mixed precision: quantizer and dequantizer can use different floating-point types (e.g., fp32/fp16)
*/
class ov::pass::ConvertQuantizeDequantize : public ov::pass::MatcherPass {
@@ -36,6 +37,5 @@ public:
ConvertQuantizeDequantize(const ov::element::TypeVector& supported_low_precisions = {ov::element::i8,
ov::element::u8,
ov::element::i16,
- ov::element::u16},
- const ov::element::TypeVector& supported_original_precisions = {ov::element::f32});
+ ov::element::u16});
};
diff --git a/src/common/transformations/src/transformations/common_optimizations/convert_quantize_dequantize.cpp b/src/common/transformations/src/transformations/common_optimizations/convert_quantize_dequantize.cpp
index 9ad6ffc2644..85d8338ba0b 100644
--- a/src/common/transformations/src/transformations/common_optimizations/convert_quantize_dequantize.cpp
+++ b/src/common/transformations/src/transformations/common_optimizations/convert_quantize_dequantize.cpp
@@ -35,6 +35,8 @@ namespace ov::pass {
// be (-128, 127) or (0, 255) or (-32768, 32767) or (0, 65535) (depends on type and depends
// on sign of the quantized data type). Another limitation is that 'zero_point' and 'scale' have to be broadcastable to
// the output of FakeQuantize.
+// Mixed precision is supported: the quantizer (FakeQuantize input) and dequantizer (scale/zero_point)
+// can use different floating-point precisions (e.g., FakeQuantize on fp32, dequantizer scale in fp16).
//
//
// | | | | |
@@ -69,10 +71,12 @@ namespace ov::pass {
// v
//
-ConvertQuantizeDequantize::ConvertQuantizeDequantize(const ov::element::TypeVector& supported_low_precisions,
- const ov::element::TypeVector& supported_original_precisions) {
+ConvertQuantizeDequantize::ConvertQuantizeDequantize(const ov::element::TypeVector& supported_low_precisions) {
MATCHER_SCOPE(ConvertQuantizeDequantize);
- auto data_pattern = pattern::any_input(pattern::type_matches_any(supported_original_precisions));
+ // Allow floating-point input types (fp32, fp16, bf16) for mixed precision support
+ auto data_pattern = pattern::any_input([](const Output<Node>& output) {
+ return output.get_element_type().is_real();
+ });
auto input_low_pattern = pattern::any_input();
auto input_high_pattern = pattern::any_input();
auto output_low_pattern = pattern::wrap_type<v0::Constant>();
@@ -82,9 +86,12 @@ ConvertQuantizeDequantize::ConvertQuantizeDequantize(const ov::element::TypeVect
auto convert1_pattern = pattern::wrap_type<v0::Convert>(
{fq_pattern},
pattern::type_matches_any(supported_low_precisions) && pattern::consumers_count(1));
+ // Allow mixed precision: dequantizer can use fp16 even if quantizer uses fp32
auto convert2_pattern = pattern::wrap_type<v0::Convert>(
{convert1_pattern},
- pattern::type_matches_any(supported_original_precisions) && pattern::consumers_count(1));
+ pattern::consumers_count(1) && [](const Output<Node>& output) {
+ return output.get_element_type().is_real();
+ });
auto zero_point_pattern = pattern::any_input();
auto sub_pattern =
@@ -151,15 +158,35 @@ ConvertQuantizeDequantize::ConvertQuantizeDequantize(const ov::element::TypeVect
return false;
}
+ // Get the element type of the FakeQuantize output bounds (typically f32)
+ const auto& fq_output_type = output_low->get_element_type();
+ const auto& scale_type = scale.get_element_type();
+
+ // Perform arithmetic in FP32 to avoid overflow (e.g., 65535 overflows FP16 max ~65504),
+ // then convert the final result to the scale's precision to preserve dequantizer precision info.
const bool has_zero_point = pattern_map.count(zero_point_pattern);
std::shared_ptr<Node> new_out_low = output_low, new_out_high = output_high;
if (has_zero_point) {
- const auto& zero_point = pattern_map.at(zero_point_pattern);
+ auto zero_point = pattern_map.at(zero_point_pattern);
+ // Ensure zero_point is in FP32 for safe arithmetic
+ if (zero_point.get_element_type() != fq_output_type) {
+ zero_point = std::make_shared<v0::Convert>(zero_point, fq_output_type);
+ }
new_out_low = std::make_shared<v1::Subtract>(new_out_low, zero_point);
new_out_high = std::make_shared<v1::Subtract>(new_out_high, zero_point);
}
- new_out_low = std::make_shared<v1::Multiply>(new_out_low, scale);
- new_out_high = std::make_shared<v1::Multiply>(new_out_high, scale);
+ // Ensure scale is in FP32 for safe multiplication
+ auto scale_fp32 = scale;
+ if (scale_type != fq_output_type) {
+ scale_fp32 = std::make_shared<v0::Convert>(scale, fq_output_type);
+ }
+ new_out_low = std::make_shared<v1::Multiply>(new_out_low, scale_fp32);
+ new_out_high = std::make_shared<v1::Multiply>(new_out_high, scale_fp32);
+ // Convert the result to scale's original precision to preserve dequantizer precision info
+ if (scale_type != fq_output_type) {
+ new_out_low = std::make_shared<v0::Convert>(new_out_low, scale_type);
+ new_out_high = std::make_shared<v0::Convert>(new_out_high, scale_type);
+ }
// check if new_out_low/high shapes are broadcastable to FQ's input
auto data_shape = data.get_partial_shape();
@@ -181,10 +208,23 @@ ConvertQuantizeDequantize::ConvertQuantizeDequantize(const ov::element::TypeVect
auto new_fq =
std::make_shared<v0::FakeQuantize>(data, input_low, input_high, new_out_low, new_out_high, levels);
- new_fq->set_friendly_name(mul->get_friendly_name());
- copy_runtime_info({fq, convert1.get_node_shared_ptr(), convert2.get_node_shared_ptr()}, new_fq);
- replace_node(mul, new_fq);
+ // Preserve the output precision of the dequantizer (e.g., fp16)
+ std::shared_ptr<Node> final_output = new_fq;
+ const auto& mul_output_type = mul->get_output_element_type(0);
+ if (mul_output_type != new_fq->get_output_element_type(0)) {
+ // Convert FakeQuantize output to match the original dequantizer output type
+ auto convert_output = std::make_shared<v0::Convert>(new_fq, mul_output_type);
+ convert_output->set_friendly_name(mul->get_friendly_name());
+ copy_runtime_info({fq, convert1.get_node_shared_ptr(), convert2.get_node_shared_ptr()},
+ {new_fq, convert_output});
+ final_output = convert_output;
+ } else {
+ new_fq->set_friendly_name(mul->get_friendly_name());
+ copy_runtime_info({fq, convert1.get_node_shared_ptr(), convert2.get_node_shared_ptr()}, new_fq);
+ }
+
+ replace_node(mul, final_output);
return true;
};
diff --git a/src/common/transformations/tests/common_optimizations/convert_quantize_dequantize.cpp b/src/common/transformations/tests/common_optimizations/convert_quantize_dequantize.cpp
index fe838fc1846..3556ef47a3a 100644
--- a/src/common/transformations/tests/common_optimizations/convert_quantize_dequantize.cpp
+++ b/src/common/transformations/tests/common_optimizations/convert_quantize_dequantize.cpp
@@ -34,23 +34,25 @@ std::shared_ptr<Model> create_q_dq_function(const Shape& data_shape,
std::vector<T> zero_point_values, /*const bool zero_point_in_f32,*/
const Shape& scale_shape,
std::vector<float> scale_values,
- size_t levels) {
- auto data = std::make_shared<opset1::Parameter>(element::f32, data_shape);
- auto input_low = opset1::Constant::create(element::f32, Shape{}, {in_low});
- auto input_high = opset1::Constant::create(element::f32, Shape{}, {in_high});
- auto output_low = opset1::Constant::create(element::f32, Shape{}, {out_low});
- auto output_high = opset1::Constant::create(element::f32, Shape{}, {out_high});
+ size_t levels,
+ element::Type data_type,
+ element::Type fq_type) {
+ auto data = std::make_shared<opset1::Parameter>(data_type, data_shape);
+ auto input_low = opset1::Constant::create(data_type, Shape{}, {in_low});
+ auto input_high = opset1::Constant::create(data_type, Shape{}, {in_high});
+ auto output_low = opset1::Constant::create(fq_type, Shape{}, {out_low});
+ auto output_high = opset1::Constant::create(fq_type, Shape{}, {out_high});
auto fq = std::make_shared<opset1::FakeQuantize>(data, input_low, input_high, output_low, output_high, levels);
auto convert1 = std::make_shared<opset1::Convert>(fq, ov::element::from<LowPrecision>());
- auto convert2 = std::make_shared<opset1::Convert>(convert1, element::f32);
+ auto convert2 = std::make_shared<opset1::Convert>(convert1, fq_type);
const std::shared_ptr<Node> zero_point =
- ov::element::from<T>() == element::f32
+ ov::element::from<T>() == fq_type
? opset1::Constant::create(ov::element::from<T>(), zero_point_shape, zero_point_values)
: std::dynamic_pointer_cast<Node>(std::make_shared<opset1::Convert>(
opset1::Constant::create(ov::element::from<T>(), zero_point_shape, zero_point_values),
- element::f32));
+ fq_type));
auto sub = std::make_shared<opset1::Subtract>(convert2, zero_point);
- auto scale = opset1::Constant::create(element::f32, scale_shape, scale_values);
+ auto scale = opset1::Constant::create(fq_type, scale_shape, scale_values);
auto mul = std::make_shared<opset1::Multiply>(sub, scale);
return std::make_shared<Model>(OutputVector{mul}, ParameterVector{data});
@@ -66,7 +68,9 @@ void positive_test(const Shape& data_shape,
std::vector<T> zero_point_values,
const Shape& scale_shape,
std::vector<float> scale_values,
- size_t levels) {
+ size_t levels,
+ element::Type data_type,
+ element::Type fq_type) {
std::shared_ptr<Model> f(nullptr), f_ref(nullptr);
{
f = create_q_dq_function<LowPrecision>(data_shape,
@@ -78,7 +82,9 @@ void positive_test(const Shape& data_shape,
zero_point_values,
scale_shape,
scale_values,
- levels);
+ levels,
+ data_type,
+ fq_type);
pass::Manager m;
m.register_pass<ov::pass::InitNodeInfo>();
@@ -90,15 +96,20 @@ void positive_test(const Shape& data_shape,
}
{
- auto data = std::make_shared<opset1::Parameter>(element::f32, data_shape);
- auto input_low = opset1::Constant::create(element::f32, Shape{}, {in_low});
- auto input_high = opset1::Constant::create(element::f32, Shape{}, {in_high});
+ auto data = std::make_shared<opset1::Parameter>(data_type, data_shape);
+ auto input_low = opset1::Constant::create(data_type, Shape{}, {in_low});
+ auto input_high = opset1::Constant::create(data_type, Shape{}, {in_high});
auto output_low =
- opset1::Constant::create(element::f32, Shape{}, {(out_low - zero_point_values[0]) * scale_values[0]});
+ opset1::Constant::create(fq_type, Shape{}, {(out_low - zero_point_values[0]) * scale_values[0]});
auto output_high =
- opset1::Constant::create(element::f32, Shape{}, {(out_high - zero_point_values[0]) * scale_values[0]});
+ opset1::Constant::create(fq_type, Shape{}, {(out_high - zero_point_values[0]) * scale_values[0]});
auto fq = std::make_shared<opset1::FakeQuantize>(data, input_low, input_high, output_low, output_high, levels);
- f_ref = std::make_shared<Model>(OutputVector{fq}, ParameterVector{data});
+ if (data_type == fq_type) {
+ f_ref = std::make_shared<Model>(OutputVector{fq}, ParameterVector{data});
+ } else {
+ auto convert_out = std::make_shared<opset1::Convert>(fq, fq_type);
+ f_ref = std::make_shared<Model>(OutputVector{convert_out}, ParameterVector{data});
+ }
}
auto res = compare_functions(f, f_ref);
@@ -127,7 +138,9 @@ TEST(TransformationTests, ConvertQuantizeDequantizeINT8WithINT8ZeroPoint) {
zero_point_values,
scale_shape,
scale_values,
- levels);
+ levels,
+ element::f32,
+ element::f32);
}
TEST(TransformationTests, ConvertQuantizeDequantizeINT8WithFP32ZeroPoint) {
@@ -152,7 +165,9 @@ TEST(TransformationTests, ConvertQuantizeDequantizeINT8WithFP32ZeroPoint) {
zero_point_values,
scale_shape,
scale_values,
- levels);
+ levels,
+ element::f32,
+ element::f32);
}
TEST(TransformationTests, ConvertQuantizeDequantizeUINT8WithUINT8ZeroPoint) {
@@ -177,7 +192,9 @@ TEST(TransformationTests, ConvertQuantizeDequantizeUINT8WithUINT8ZeroPoint) {
zero_point_values,
scale_shape,
scale_values,
- levels);
+ levels,
+ element::f32,
+ element::f32);
}
TEST(TransformationTests, ConvertQuantizeDequantizeUINT8WithFP32ZeroPoint) {
@@ -202,7 +219,36 @@ TEST(TransformationTests, ConvertQuantizeDequantizeUINT8WithFP32ZeroPoint) {
zero_point_values,
scale_shape,
scale_values,
- levels);
+ levels,
+ element::f32,
+ element::f32);
+}
+
+TEST(TransformationTests, ConvertQuantizeDequantizeINT8WithINT8ZeroPointMixedFP16) {
+ std::shared_ptr<Model> f(nullptr), f_ref(nullptr);
+ Shape data_shape{3, 1, 2};
+ float in_low = 0;
+ float in_high = 5;
+ float out_low = -128;
+ float out_high = 127;
+ Shape zero_point_shape{};
+ std::vector<int8_t> zero_point_values{2};
+ Shape scale_shape{};
+ std::vector<float> scale_values{3};
+ size_t levels = 256;
+
+ positive_test<int8_t>(data_shape,
+ in_low,
+ in_high,
+ out_low,
+ out_high,
+ zero_point_shape,
+ zero_point_values,
+ scale_shape,
+ scale_values,
+ levels,
+ element::f16,
+ element::f32);
}
template <typename LowPrecision, typename T>
@@ -215,7 +261,9 @@ void negative_test(const Shape& data_shape,
std::vector<T> zero_point_values,
const Shape& scale_shape,
std::vector<float> scale_values,
- size_t levels) {
+ size_t levels,
+ element::Type data_type,
+ element::Type fq_type) {
std::shared_ptr<Model> f(nullptr), f_ref(nullptr);
{
f = create_q_dq_function<LowPrecision>(data_shape,
@@ -227,7 +275,9 @@ void negative_test(const Shape& data_shape,
zero_point_values,
scale_shape,
scale_values,
- levels);
+ levels,
+ data_type,
+ fq_type);
pass::Manager m;
m.register_pass<ov::pass::InitNodeInfo>();
m.register_pass<ov::pass::ConvertQuantizeDequantize>();
@@ -246,7 +296,9 @@ void negative_test(const Shape& data_shape,
zero_point_values,
scale_shape,
scale_values,
- levels);
+ levels,
+ data_type,
+ fq_type);
}
auto res = compare_functions(f, f_ref);
ASSERT_TRUE(res.first) << res.second;
@@ -274,7 +326,9 @@ TEST(TransformationTests, ConvertQuantizeDequantizeZeroPointNotBroadcastableWith
zero_point_values,
scale_shape,
scale_values,
- levels);
+ levels,
+ element::f32,
+ element::f32);
}
TEST(TransformationTests, ConvertQuantizeDequantizeZeroPointNotBroadcastableWithFP32ZeroPoint) {
@@ -299,7 +353,9 @@ TEST(TransformationTests, ConvertQuantizeDequantizeZeroPointNotBroadcastableWith
zero_point_values,
scale_shape,
scale_values,
- levels);
+ levels,
+ element::f32,
+ element::f32);
}
TEST(TransformationTests, ConvertQuantizeDequantizeScaleNotBroadcastableWithINT8ZeroPoint) {
@@ -324,7 +380,9 @@ TEST(TransformationTests, ConvertQuantizeDequantizeScaleNotBroadcastableWithINT8
zero_point_values,
scale_shape,
scale_values,
- levels);
+ levels,
+ element::f32,
+ element::f32);
}
TEST(TransformationTests, ConvertQuantizeDequantizeScaleNotBroadcastableWithFP32ZeroPoint) {
@@ -349,7 +407,9 @@ TEST(TransformationTests, ConvertQuantizeDequantizeScaleNotBroadcastableWithFP32
zero_point_values,
scale_shape,
scale_values,
- levels);
+ levels,
+ element::f32,
+ element::f32);
}
TEST(TransformationTests, ConvertQuantizeDequantizeInvalidLevelsWithINT8ZeroPoint) {
@@ -374,7 +434,9 @@ TEST(TransformationTests, ConvertQuantizeDequantizeInvalidLevelsWithINT8ZeroPoin
zero_point_values,
scale_shape,
scale_values,
- levels);
+ levels,
+ element::f32,
+ element::f32);
}
TEST(TransformationTests, ConvertQuantizeDequantizeInvalidLevelsWithFP32ZeroPoint) {
@@ -399,7 +461,9 @@ TEST(TransformationTests, ConvertQuantizeDequantizeInvalidLevelsWithFP32ZeroPoin
zero_point_values,
scale_shape,
scale_values,
- levels);
+ levels,
+ element::f32,
+ element::f32);
}
TEST(TransformationTests, ConvertQuantizeDequantizeInvalidOutLowOutHighWithUINT8ZeroPoint) {
@@ -425,7 +489,9 @@ TEST(TransformationTests, ConvertQuantizeDequantizeInvalidOutLowOutHighWithUINT8
zero_point_values,
scale_shape,
scale_values,
- levels);
+ levels,
+ element::f32,
+ element::f32);
}
TEST(TransformationTests, ConvertQuantizeDequantizeInvalidOutLowOutHighWithFP32ZeroPoint) {
@@ -451,5 +517,7 @@ TEST(TransformationTests, ConvertQuantizeDequantizeInvalidOutLowOutHighWithFP32Z
zero_point_values,
scale_shape,
scale_values,
- levels);
+ levels,
+ element::f32,
+ element::f32);
}
diff --git a/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp b/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp
index 774a78d67a7..a8899d8a635 100644
--- a/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp
+++ b/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp
@@ -521,8 +521,7 @@ void Transformations::PreLpt(const std::vector<ov::element::Type>& defaultPrecis
// 0. Deduplicate identical DQ subgraphs sharing a common Convert node
qdq_stripping_manager.register_pass<ov::pass::SharedOpOptimization>();
// 1. Fuse FQ->Convert->DQ to a single FQ
- qdq_stripping_manager.register_pass<ov::pass::ConvertQuantizeDequantize>(TypeVector{i16, u16},
- TypeVector{f32});
+ qdq_stripping_manager.register_pass<ov::pass::ConvertQuantizeDequantize>(TypeVector{i16, u16});
// 2. Strip FQ layers with unsupported levels
qdq_stripping_manager.register_pass<FQStrippingTransformation>(std::set<size_t>{levels::int16}, false);
qdq_stripping_manager.run_passes(model);
diff --git a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
index 489f6bb4d02..105f24c932b 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
@@ -569,7 +569,7 @@ void TransformationsPipeline::apply(std::shared_ptr<ov::Model> func) {
// 0. Deduplicate identical DQ subgraphs sharing a common Convert node
qdq_stripping_manager.register_pass<ov::pass::SharedOpOptimization>();
// 1. Fuse FQ->Convert->DQ to a single FQ
- qdq_stripping_manager.register_pass<ov::pass::ConvertQuantizeDequantize>(TypeVector{i16, u16}, TypeVector{f32});
+ qdq_stripping_manager.register_pass<ov::pass::ConvertQuantizeDequantize>(TypeVector{i16, u16});
// 2. Strip FQ layers with unsupported levels
const bool need_weights_adjustment = infer_precision == ov::element::f16;
qdq_stripping_manager.register_pass<FQStrippingTransformation>(std::set<size_t>{levels::int16}, need_weights_adjustment);
diff --git a/src/tests/ov_helpers/ov_lpt_models/include/ov_lpt_models/qdq_stripping.hpp b/src/tests/ov_helpers/ov_lpt_models/include/ov_lpt_models/qdq_stripping.hpp
index d6111a25f00..970d14a1283 100644
--- a/src/tests/ov_helpers/ov_lpt_models/include/ov_lpt_models/qdq_stripping.hpp
+++ b/src/tests/ov_helpers/ov_lpt_models/include/ov_lpt_models/qdq_stripping.hpp
@@ -27,11 +27,13 @@ class QDQStrippingFunction {
public:
static ov::Output<ov::Node> build_fq(const ov::Output<ov::Node>& input,
const QuantizationParams& qp,
- size_t levels = 65536);
+ size_t levels = 65536,
+ bool mixed_precision = false);
static ov::Output<ov::Node> build_dq(const ov::Output<ov::Node>& input,
const ov::element::Type& quantization_precision,
const QuantizationParams& qp,
- bool convert_on_zero_point = true);
+ bool convert_on_zero_point = true,
+ const ov::element::Type& float_precision = ov::element::f32);
// Builds a weight-DQ pattern: Constant(quantized_type) -> Convert(f32) -> Subtract(zp) -> Multiply(scale)
// When seed is provided, generates random constant values using make_constant.
@@ -75,6 +77,11 @@ public:
static std::shared_ptr<ov::Model> build_forward_bias_pattern_ref(const ov::PartialShape& input_shape,
bool need_weights_adjustment = true);
+ static std::shared_ptr<ov::Model> build_mixed_precision_pattern(const ov::PartialShape& input_shape,
+ const ov::element::Type& quantization_precision);
+ static std::shared_ptr<ov::Model> build_mixed_precision_pattern_ref(const ov::PartialShape& input_shape,
+ bool need_weights_adjustment = true);
+
private:
// Builds one residual block: MVN → Conv+bias [→ optional FQ] + shortcut → Add.
// Shared by build_residual_block_pattern (source) and build_residual_block_pattern_ref (ref).
diff --git a/src/tests/ov_helpers/ov_lpt_models/src/qdq_stripping.cpp b/src/tests/ov_helpers/ov_lpt_models/src/qdq_stripping.cpp
index c99c0d5a616..0c7f0f0d201 100644
--- a/src/tests/ov_helpers/ov_lpt_models/src/qdq_stripping.cpp
+++ b/src/tests/ov_helpers/ov_lpt_models/src/qdq_stripping.cpp
@@ -28,29 +28,32 @@ namespace builder {
namespace subgraph {
ov::Output<ov::Node> QDQStrippingFunction::build_fq(const ov::Output<ov::Node>& input,
const QuantizationParams& qp,
- size_t levels) {
+ size_t levels,
+ bool mixed_precision) {
+ auto out_precision = mixed_precision ? ov::element::f16 : ov::element::f32;
auto il = ov::op::v0::Constant::create(ov::element::f32, {}, {qp.i_l});
auto ih = ov::op::v0::Constant::create(ov::element::f32, {}, {qp.i_h});
- auto ol = ov::op::v0::Constant::create(ov::element::f32, {}, {qp.o_l});
- auto oh = ov::op::v0::Constant::create(ov::element::f32, {}, {qp.o_h});
+ auto ol = ov::op::v0::Constant::create(out_precision, {}, {qp.o_l});
+ auto oh = ov::op::v0::Constant::create(out_precision, {}, {qp.o_h});
return std::make_shared<ov::op::v0::FakeQuantize>(input, il, ih, ol, oh, levels);
}
ov::Output<ov::Node> QDQStrippingFunction::build_dq(const ov::Output<ov::Node>& input,
const ov::element::Type& quantization_precision,
const QuantizationParams& qp,
- bool convert_on_zero_point) {
+ bool convert_on_zero_point,
+ const ov::element::Type& float_precision) {
std::shared_ptr<ov::Node> sub;
if (convert_on_zero_point) {
auto act_zero_point = ov::op::v0::Constant::create(quantization_precision, {}, {qp.zero_point});
- auto act_zp_convert = std::make_shared<ov::op::v0::Convert>(act_zero_point, ov::element::f32);
+ auto act_zp_convert = std::make_shared<ov::op::v0::Convert>(act_zero_point, float_precision);
sub = std::make_shared<ov::op::v1::Subtract>(input, act_zp_convert);
} else {
- auto act_zero_point = ov::op::v0::Constant::create(ov::element::f32, {}, {qp.zero_point});
+ auto act_zero_point = ov::op::v0::Constant::create(float_precision, {}, {qp.zero_point});
sub = std::make_shared<ov::op::v1::Subtract>(input, act_zero_point);
}
float scale_value = (qp.i_h - qp.i_l) / (qp.o_h - qp.o_l);
- auto act_scale = ov::op::v0::Constant::create(ov::element::f32, {}, {scale_value});
+ auto act_scale = ov::op::v0::Constant::create(float_precision, {}, {scale_value});
return std::make_shared<ov::op::v1::Multiply>(sub, act_scale);
}
@@ -598,6 +601,76 @@ std::shared_ptr<ov::Model> QDQStrippingFunction::build_forward_bias_pattern_ref(
std::make_shared<ov::op::v6::MVN>(matmul2_biased, reduction_axes, true, 1e-3f, ov::op::MVNEpsMode::INSIDE_SQRT);
return std::make_shared<ov::Model>(ov::OutputVector{mvn}, params, "QDQStripping");
}
+
+std::shared_ptr<ov::Model> QDQStrippingFunction::build_mixed_precision_pattern(
+ const ov::PartialShape& input_shape,
+ const ov::element::Type& quantization_precision) {
+ ov::ParameterVector params{std::make_shared<ov::op::v0::Parameter>(ov::element::f32, input_shape)};
+ static const std::unordered_map<ov::element::Type_t, QuantizationParams>
+ quantization_params{
+ {ov::element::Type_t::u8, {0.f, 10.f, 0.f, 255.f, 0}},
+ {ov::element::Type_t::i8, {-5.0000762939453125f, 4.9999237060546875f, -128.f, 127.f, 0}},
+ };
+
+ const auto& q_params = quantization_params.at(quantization_precision);
+ auto input_fq = build_fq(params[0], q_params, 256, true);
+
+ auto input_convert1 = std::make_shared<ov::op::v0::Convert>(input_fq, quantization_precision);
+ auto input_convert2 = std::make_shared<ov::op::v0::Convert>(input_convert1, ov::element::f16);
+
+ size_t seed = 1;
+ auto input_dequantized = build_dq(input_convert2, quantization_precision, q_params, false, ov::element::f16);
+
+ ov::test::utils::InputGenerateData weights_gen_data;
+ weights_gen_data.seed = seed;
+ auto weight_quantized =
+ ov::test::utils::make_constant(ov::element::i8, ov::Shape{32, 3, 3, 3}, weights_gen_data);
+ auto weight_convert = std::make_shared<ov::op::v0::Convert>(weight_quantized, ov::element::f16);
+ auto weight_scale = ov::test::utils::make_constant(ov::element::f16, {}, std::vector<float>{1e-4f});
+ auto weight_dequantized = std::make_shared<ov::op::v1::Multiply>(weight_convert, weight_scale);
+
+ auto conv = std::make_shared<ov::op::v1::Convolution>(input_dequantized,
+ weight_dequantized,
+ ov::Strides{1, 1},
+ ov::CoordinateDiff{1, 1},
+ ov::CoordinateDiff{1, 1},
+ ov::Strides{1, 1});
+
+ ov::test::utils::InputGenerateData bias_gen_data(-2.0, 4, 100, seed++);
+ auto bias_const = ov::test::utils::make_constant(ov::element::f16, ov::Shape{1, 32, 1, 1}, bias_gen_data);
+ auto conv_biased = std::make_shared<ov::op::v1::Add>(conv, bias_const);
+
+ return std::make_shared<ov::Model>(ov::OutputVector{conv_biased}, params, "QDQStripping");
+}
+
+std::shared_ptr<ov::Model> QDQStrippingFunction::build_mixed_precision_pattern_ref(const ov::PartialShape& input_shape,
+ bool need_weights_adjustment) {
+ ov::ParameterVector params{std::make_shared<ov::op::v0::Parameter>(ov::element::f32, input_shape)};
+ auto input_convert = std::make_shared<ov::op::v0::Convert>(params[0], ov::element::f16);
+
+ size_t seed = 1;
+ ov::test::utils::InputGenerateData weights_gen_data;
+ weights_gen_data.seed = seed;
+ auto weight_quantized =
+ ov::test::utils::make_constant(ov::element::i8, ov::Shape{32, 3, 3, 3}, weights_gen_data);
+ auto weight_convert = std::make_shared<ov::op::v0::Convert>(weight_quantized, ov::element::f16);
+ auto weight_scale = ov::test::utils::make_constant(ov::element::f16, {}, std::vector<float>{1e-4f});
+ auto weight_dequantized = std::make_shared<ov::op::v1::Multiply>(weight_convert, weight_scale);
+
+ auto conv = std::make_shared<ov::op::v1::Convolution>(input_convert,
+ weight_dequantized,
+ ov::Strides{1, 1},
+ ov::CoordinateDiff{1, 1},
+ ov::CoordinateDiff{1, 1},
+ ov::Strides{1, 1});
+
+ ov::test::utils::InputGenerateData bias_gen_data(-2.0, 4, 100, seed++);
+ auto bias_const = ov::test::utils::make_constant(ov::element::f16, ov::Shape{1, 32, 1, 1}, bias_gen_data);
+ auto conv_biased = std::make_shared<ov::op::v1::Add>(conv, bias_const);
+
+ return std::make_shared<ov::Model>(ov::OutputVector{conv_biased}, params, "QDQStripping");
+}
+
} // namespace subgraph
} // namespace builder
} // namespace ov