Skip to content

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

[GPU] Prevent u16/i16 dequantization subgraphs folding during model conversion, allowing Plugins to handle subgraphs on their own (#30453)

Sergey Shlyapnikov710890dddfc2
diff --git a/src/common/low_precision_transformations/tests/mark_dequantization_subgraph_transformation.cpp b/src/common/low_precision_transformations/tests/mark_dequantization_subgraph_transformation.cpp
index 045a7fd8605..fb4b1f68d3b 100644
--- a/src/common/low_precision_transformations/tests/mark_dequantization_subgraph_transformation.cpp
+++ b/src/common/low_precision_transformations/tests/mark_dequantization_subgraph_transformation.cpp
@@ -8,10 +8,12 @@
#include "transformations/rt_info/decompression.hpp"
#include "transformations/rt_info/dequantization_node.hpp"
#include "transformations/rt_info/disable_constant_folding.hpp"
+#include "transformations/rt_info/disable_fp16_compression.hpp"
#include "transformations/rt_info/keep_const_precision.hpp"
#include "common_test_utils/ov_test_utils.hpp"
#include "transformations/convert_precision.hpp"
+#include "openvino/op/add.hpp"
#include "openvino/op/clamp.hpp"
#include "openvino/op/convert.hpp"
#include "openvino/op/convolution.hpp"
@@ -775,3 +777,94 @@ TEST_F(TransformationTestsF, MarkDequantizationTransformationFoldSubConst) {
comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);
comparator.enable(FunctionsComparator::CmpValues::RUNTIME_KEYS);
}
+
+TEST_F(TransformationTestsF, KeepDequantizationPrecisionTransformationMarkup) {
+ // After KeepDequantizationPrecision all Convert, Subtract, and Multiply nodes
+ // are marked with the 'disable_fp16_compression' attribute
+
+ auto quantization_dt = element::u16;
+ auto dequantization_dt = element::f32;
+
+ {
+ auto parameter = std::make_shared<opset10::Parameter>(dequantization_dt, Shape{1});
+ auto weights = opset10::Constant::create(quantization_dt, Shape{4, 16, 1, 1}, {3});
+ auto convert = std::make_shared<opset10::Convert>(weights, dequantization_dt);
+ auto zero_point = opset10::Constant::create(quantization_dt, Shape{}, {127});
+ auto convert_on_zero_point = std::make_shared<opset10::Convert>(zero_point, dequantization_dt);
+ auto subtract = std::make_shared<opset10::Subtract>(convert, convert_on_zero_point);
+ auto scale = opset10::Constant::create(dequantization_dt, Shape{}, {0.2});
+ auto multiply = std::make_shared<opset10::Multiply>(subtract, scale);
+ auto add = std::make_shared<opset10::Add>(parameter, multiply);
+ model = std::make_shared<ov::Model>(ov::OutputVector{add});
+ }
+
+ manager.register_pass<pass::KeepDequantizationPrecision>(element::TypeVector{quantization_dt});
+
+ {
+ auto parameter = std::make_shared<opset10::Parameter>(dequantization_dt, Shape{1});
+ auto weights = opset10::Constant::create(quantization_dt, Shape{4, 16, 1, 1}, {3});
+ auto convert = std::make_shared<opset10::Convert>(weights, dequantization_dt);
+ disable_fp16_compression(convert);
+ auto zero_point = opset10::Constant::create(quantization_dt, Shape{}, {127});
+ auto convert_on_zero_point = std::make_shared<opset10::Convert>(zero_point, dequantization_dt);
+ disable_fp16_compression(convert_on_zero_point);
+ auto subtract = std::make_shared<opset10::Subtract>(convert, convert_on_zero_point);
+ disable_fp16_compression(subtract);
+ auto scale = opset10::Constant::create(dequantization_dt, Shape{}, {0.2});
+ auto multiply = std::make_shared<opset10::Multiply>(subtract, scale);
+ disable_fp16_compression(multiply);
+ auto add = std::make_shared<opset10::Add>(parameter, multiply);
+ model_ref = std::make_shared<ov::Model>(ov::OutputVector{add});
+ }
+
+ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);
+ comparator.enable(FunctionsComparator::CmpValues::RUNTIME_KEYS);
+}
+
+TEST_F(TransformationTests, KeepDequantizationPrecisionTransformationFolding) {
+ // This test uses the legacy TransformationTests class because ConvertPrecision pass
+ // internally inserts additional nodes for data type consistency, which causes the
+ // check_unique_names() function to fail with a "Tensor name: N is missing in ..." error.
+ // Once this issue is fixed, the test should be migrated to the new TransformationTestsF infrastructure.
+
+ // After KeepDequantizationPrecision all Convert, Subtract, and Multiply nodes
+ // are marked with the 'disable_fp16_compression' attribute, and dequantization subgraph is folded
+ // during Constant Folding (called from ConvertPrecision pass), using f32 data type for constants.
+
+ std::shared_ptr<Model> model, model_ref;
+ pass::Manager manager;
+
+ auto quantization_dt = element::u16;
+ auto dequantization_dt = element::f32;
+
+ {
+ auto parameter = std::make_shared<opset10::Parameter>(dequantization_dt, Shape{1});
+ auto weights = opset10::Constant::create(quantization_dt, Shape{4, 16, 1, 1}, {3});
+ auto convert = std::make_shared<opset10::Convert>(weights, dequantization_dt);
+ auto zero_point = opset10::Constant::create(quantization_dt, Shape{}, {127});
+ auto convert_on_zero_point = std::make_shared<opset10::Convert>(zero_point, dequantization_dt);
+ auto subtract = std::make_shared<opset10::Subtract>(convert, convert_on_zero_point);
+ auto scale = opset10::Constant::create(dequantization_dt, Shape{}, {0.2});
+ auto multiply = std::make_shared<opset10::Multiply>(subtract, scale);
+ auto add = std::make_shared<opset10::Add>(parameter, multiply);
+ model = std::make_shared<ov::Model>(ov::OutputVector{add});
+
+ const auto keep_precision_sensitive_in_fp32 = true;
+ manager.register_pass<pass::KeepDequantizationPrecision>(element::TypeVector{quantization_dt});
+ manager.register_pass<pass::ConvertPrecision>(element::f32, element::f16, type_to_fuse_map{}, keep_precision_sensitive_in_fp32);
+ manager.run_passes(model);
+ }
+
+ {
+ auto parameter = std::make_shared<opset10::Parameter>(element::f16, Shape{1});
+ auto input_decompress_to_f32 = std::make_shared<opset10::Convert>(parameter, element::f32);
+ auto weights = opset10::Constant::create(dequantization_dt, Shape{4, 16, 1, 1}, {3});
+ auto add = std::make_shared<opset10::Add>(input_decompress_to_f32, weights);
+ auto add_compressed_to_f16 = std::make_shared<opset10::Convert>(add, element::f16);
+ model_ref = std::make_shared<ov::Model>(ov::OutputVector{add_compressed_to_f16});
+ }
+
+ auto func_comparator = FunctionsComparator::with_default();
+ auto result = func_comparator(model_ref, model);
+ ASSERT_TRUE(result.valid) << result.message;
+}
diff --git a/src/common/transformations/include/transformations/low_precision/mark_dequantization_subgraph.hpp b/src/common/transformations/include/transformations/low_precision/mark_dequantization_subgraph.hpp
index 9fcacdcbb5b..f4103379656 100644
--- a/src/common/transformations/include/transformations/low_precision/mark_dequantization_subgraph.hpp
+++ b/src/common/transformations/include/transformations/low_precision/mark_dequantization_subgraph.hpp
@@ -76,5 +76,38 @@ public:
bool fold_multiply_const = true);
};
+/**
+ * @ingroup ov_transformation_common_api
+ *
+ * @brief KeepDequantizationPrecision matches Dequantization subgraphs and, if precision matches with
+ * specified, Convert, Multiply, Subtract and Reshape nodes might be marked with disable_fp16_compression attribute.
+ * This prevents precision loss when the original precision is lowered during ConvertPrecision execution.
+ *
+ * Example scenario:
+ * Original Dequantization subgraph: Potential transformed subgraph after ConvertPrecision:
+ * Input (i32) Const (i32) Input (i32) Const (i32)
+ * │ │ │ │
+ * ▼ ▼ ▼ ▼
+ * Convert (f32) Convert (f32) Convert (f16) Convert (f16)
+ * │ │ │ │
+ * ▼ ▼ ▼ ▼
+ * Subtract (f32) Subtract (f16)
+ * │ │
+ * │ Scale (f32) │ Scale (f16)
+ * │ │ │ │
+ * ▼ ▼ ▼ ▼
+ * Multiply (f32) Multiply (f16)
+ *
+ * Without KeepDequantizationPrecision, ConvertPrecision transformation may convert
+ * these operations to use fp16 instead of f32, potentially leading to accuracy degradation.
+ * Marking these nodes (KeepDequantizationPrecision) preserves the original dequantization precision (f32).
+ *
+ */
+class TRANSFORMATIONS_API KeepDequantizationPrecision : public ov::pass::MatcherPass {
+public:
+ OPENVINO_MATCHER_PASS_RTTI("KeepDequantizationPrecision");
+ explicit KeepDequantizationPrecision(const element::TypeVector& precisions);
+};
+
} // namespace pass
} // namespace ov
diff --git a/src/common/transformations/src/transformations/common_optimizations/moc_transformations.cpp b/src/common/transformations/src/transformations/common_optimizations/moc_transformations.cpp
index 926ddf85882..8af30f3528f 100644
--- a/src/common/transformations/src/transformations/common_optimizations/moc_transformations.cpp
+++ b/src/common/transformations/src/transformations/common_optimizations/moc_transformations.cpp
@@ -131,7 +131,11 @@ bool ov::pass::MOCTransformations::run_on_model(const std::shared_ptr<ov::Model>
using namespace ov::pass;
REGISTER_PASS(manager, InitNodeInfo)
if (m_low_precision_enabled) {
- manager.register_pass<ov::pass::MarkDequantization>(element::TypeVector{ov::element::i8,
+ manager.register_pass<ov::pass::MarkDequantization>(element::TypeVector{ov::element::i32,
+ ov::element::u32,
+ ov::element::i16,
+ ov::element::u16,
+ ov::element::i8,
ov::element::u8,
ov::element::i4,
ov::element::u4,
diff --git a/src/common/transformations/src/transformations/low_precision/mark_dequantization_subgraph.cpp b/src/common/transformations/src/transformations/low_precision/mark_dequantization_subgraph.cpp
index d3ca5fd99bc..4718a5c2deb 100644
--- a/src/common/transformations/src/transformations/low_precision/mark_dequantization_subgraph.cpp
+++ b/src/common/transformations/src/transformations/low_precision/mark_dequantization_subgraph.cpp
@@ -309,3 +309,51 @@ ov::pass::KeepConstPrecision::KeepConstPrecision(const element::TypeVector& prec
auto m = std::make_shared<Matcher>(multiply_pattern, "KeepConstPrecision");
this->register_matcher(m, callback);
}
+
+ov::pass::KeepDequantizationPrecision::KeepDequantizationPrecision(const element::TypeVector& precisions) {
+ MATCHER_SCOPE(KeepDequantizationPrecision);
+
+ auto input_pattern = any_input(pattern::type_matches_any(precisions));
+ auto convert_pattern = pattern::wrap_type<v0::Convert>({input_pattern}, pattern::consumers_count(1));
+
+ // zero points:
+ auto zp_pattern = any_input();
+ auto zp_convert_pattern = pattern::optional<v0::Convert>(zp_pattern);
+ auto zp_reshape_pattern = pattern::optional<v1::Reshape, v0::Unsqueeze>({zp_convert_pattern, any_input()});
+ auto subtract_pattern = pattern::optional<v1::Subtract>({convert_pattern, zp_reshape_pattern});
+
+ // scale:
+ auto scale_pattern = any_input();
+ auto scale_convert_pattern = pattern::optional<v0::Convert>(scale_pattern);
+ auto scale_reshape_pattern = pattern::optional<v1::Reshape, v0::Unsqueeze>({scale_convert_pattern, any_input()});
+ auto multiply_pattern = pattern::wrap_type<v1::Multiply>({subtract_pattern, scale_reshape_pattern});
+
+ matcher_pass_callback callback = [=](Matcher& m) {
+ const auto& pt_map = m.get_pattern_value_map();
+ auto multiply = m.get_match_root();
+
+ if (transformation_callback(multiply)) {
+ return false;
+ }
+
+ auto nodes_to_mark = {convert_pattern,
+ multiply_pattern,
+ subtract_pattern,
+ zp_convert_pattern,
+ zp_reshape_pattern,
+ scale_convert_pattern,
+ scale_reshape_pattern};
+
+ for (const auto& node_to_mark : nodes_to_mark) {
+ if (pt_map.count(node_to_mark)) {
+ auto node_ptr = pt_map.at(node_to_mark).get_node_shared_ptr();
+ disable_fp16_compression(node_ptr);
+ }
+ }
+
+ return false;
+ };
+
+ auto m = std::make_shared<Matcher>(multiply_pattern, "KeepDequantizationPrecision");
+ this->register_matcher(m, callback);
+}
diff --git a/src/core/src/preprocess/pre_post_process.cpp b/src/core/src/preprocess/pre_post_process.cpp
index ea4dcff859f..14acfd31cc1 100644
--- a/src/core/src/preprocess/pre_post_process.cpp
+++ b/src/core/src/preprocess/pre_post_process.cpp
@@ -81,7 +81,9 @@ void transformation_pipeline(std::shared_ptr<ov::Model>& model) {
REGISTER_PASS(manager, SharedOpOptimization)
// 1. Set "disable_const_folding" attribute
- REGISTER_PASS(manager, MarkDequantization, TypeVector{i8, u8, i4, u4, nf4, f4e2m1, f8e4m3, f8e5m2, f8e8m0});
+ REGISTER_PASS(manager,
+ MarkDequantization,
+ TypeVector{i32, u32, i16, u16, i8, u8, i4, u4, nf4, f4e2m1, f8e4m3, f8e5m2, f8e8m0});
REGISTER_PASS(manager, DisableShapeOfConstantFolding, false);
REGISTER_PASS(manager, DisableRandomUniformConstantFolding)
// Mark quantized and f16/bf16 compressed constants to prevent CF for them,
diff --git a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
index 3d8413b3076..c0950ca09ff 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
@@ -463,6 +463,9 @@ void TransformationsPipeline::apply(std::shared_ptr<ov::Model> func) {
const bool convert_input_output_precision = false;
const bool store_original_precision_as_rt_attribute = true;
+ manager.register_pass<ov::pass::KeepDequantizationPrecision>(
+ ov::element::TypeVector{ov::element::i32, ov::element::u32, ov::element::u16});
+
manager.register_pass<ov::pass::ConvertPrecision>(fp_convert_precision_map,
empty_fuse_map,
keep_precision_sensitive_in_fp32_1,