Skip to content

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

[GPU] Keep dequantization subgraph output as inference precision for GPU plugin (#30702)

Sergey Shlyapnikov762eb8ab639d
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 fb4b1f68d3b..72b45bc45e6 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
@@ -868,3 +868,52 @@ TEST_F(TransformationTests, KeepDequantizationPrecisionTransformationFolding) {
auto result = func_comparator(model_ref, model);
ASSERT_TRUE(result.valid) << result.message;
}
+
+TEST_F(TransformationTests, KeepDequantizationPrecisionTransformationFoldingWithConvert) {
+ // 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 and finally
+ // converted to the f16 data type.
+
+ 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;
+ const auto add_precision_sensitive_convert = true;
+ manager.register_pass<pass::KeepDequantizationPrecision>(element::TypeVector{quantization_dt}, add_precision_sensitive_convert);
+ manager.register_pass<pass::ConvertPrecision>(element::f32, element::f16, type_to_fuse_map{}, keep_precision_sensitive_in_fp32);
+ manager.run_passes(model);
+ }
+
+ {
+ auto weights_dt = element::f16;
+ auto parameter = std::make_shared<opset10::Parameter>(element::f16, Shape{1});
+ auto weights = opset10::Constant::create(weights_dt, Shape{4, 16, 1, 1}, {3});
+ auto add = std::make_shared<opset10::Add>(parameter, weights);
+ model_ref = std::make_shared<ov::Model>(ov::OutputVector{add});
+ }
+
+ 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 f4103379656..82b8ba00847 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
@@ -106,7 +106,8 @@ public:
class TRANSFORMATIONS_API KeepDequantizationPrecision : public ov::pass::MatcherPass {
public:
OPENVINO_MATCHER_PASS_RTTI("KeepDequantizationPrecision");
- explicit KeepDequantizationPrecision(const element::TypeVector& precisions);
+ explicit KeepDequantizationPrecision(const element::TypeVector& precisions,
+ bool add_precision_sensitive_convert = false);
};
} // namespace pass
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 4718a5c2deb..21fec907f7a 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
@@ -9,6 +9,7 @@
#include "openvino/op/reshape.hpp"
#include "openvino/op/subtract.hpp"
#include "openvino/op/unsqueeze.hpp"
+#include "openvino/op/util/precision_sensitive_attribute.hpp"
#include "openvino/pass/manager.hpp"
#include "openvino/pass/pattern/op/optional.hpp"
#include "openvino/pass/pattern/op/pattern.hpp"
@@ -309,8 +310,8 @@ 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) {
+ov::pass::KeepDequantizationPrecision::KeepDequantizationPrecision(const element::TypeVector& precisions,
+ bool add_precision_sensitive_convert) {
MATCHER_SCOPE(KeepDequantizationPrecision);
auto input_pattern = any_input(pattern::type_matches_any(precisions));
@@ -351,6 +352,19 @@ ov::pass::KeepDequantizationPrecision::KeepDequantizationPrecision(const element
}
}
+ // Insert Convert to stop disable_fp16_compression attribute propagation in
+ // PropagateUpMarkToKeepInMixedPrecision and PropagateDownMarkToKeepInMixedPrecision passes because this node is
+ // not included to the node propagation list. Marking Convert as precision sensitive to prevent additional
+ // Convert insertion (with disable_const_folding flag) inside AlignMixedFP32FP16Types transformation. Use
+ // Multiply's output data type to ensure data type consistency.
+ if (add_precision_sensitive_convert) {
+ auto convert = std::make_shared<v0::Convert>(multiply, multiply->get_output_element_type(0));
+ multiply->output(0).replace(convert);
+ ov::mark_as_precision_sensitive(convert->input(0));
+
+ return true;
+ }
+
return false;
};
diff --git a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
index 77ab99dafdc..99de7e4d1df 100644
--- a/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
+++ b/src/plugins/intel_gpu/src/plugin/transformations_pipeline.cpp
@@ -465,9 +465,10 @@ void TransformationsPipeline::apply(std::shared_ptr<ov::Model> func) {
const bool keep_precision_sensitive_in_fp32_1 = true;
const bool convert_input_output_precision = false;
const bool store_original_precision_as_rt_attribute = true;
+ const auto add_precision_sensitive_convert = true;
manager.register_pass<ov::pass::KeepDequantizationPrecision>(
- ov::element::TypeVector{ov::element::i32, ov::element::u32, ov::element::u16});
+ ov::element::TypeVector{ov::element::i32, ov::element::u32, ov::element::u16}, add_precision_sensitive_convert);
manager.register_pass<ov::pass::ConvertPrecision>(fp_convert_precision_map,
empty_fuse_map,