Skip to content

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

[GPU] Fix accuracy loss in Range due to high value generation for attention mask (#26093)

Sergey Shlyapnikovdcf9605f7fba
diff --git a/src/common/transformations/src/transformations/convert_precision.cpp b/src/common/transformations/src/transformations/convert_precision.cpp
index 3c819d481ba..ef1e815ab2f 100644
--- a/src/common/transformations/src/transformations/convert_precision.cpp
+++ b/src/common/transformations/src/transformations/convert_precision.cpp
@@ -560,7 +560,7 @@ bool fuse_type_to_range_v4(const std::shared_ptr<ov::Node>& node, const precisio
return false;
const auto& to = it->second;
if (auto range = ov::as_type_ptr<ov::op::v4::Range>(node)) {
- if (to.is_integral_number() || to.is_real()) {
+ if (!fp16_compression_is_disabled(node) && (to.is_integral_number() || to.is_real())) {
range->set_output_type(to);
return true;
}
diff --git a/src/common/transformations/src/transformations/fp16_compression/mark_subgraphs_to_keep_in_mixed_precision.cpp b/src/common/transformations/src/transformations/fp16_compression/mark_subgraphs_to_keep_in_mixed_precision.cpp
index ef367571642..769fa3c7287 100644
--- a/src/common/transformations/src/transformations/fp16_compression/mark_subgraphs_to_keep_in_mixed_precision.cpp
+++ b/src/common/transformations/src/transformations/fp16_compression/mark_subgraphs_to_keep_in_mixed_precision.cpp
@@ -13,13 +13,18 @@
#include "openvino/op/divide.hpp"
#include "openvino/op/exp.hpp"
#include "openvino/op/fake_quantize.hpp"
+#include "openvino/op/greater.hpp"
+#include "openvino/op/greater_eq.hpp"
#include "openvino/op/interpolate.hpp"
+#include "openvino/op/less.hpp"
+#include "openvino/op/less_eq.hpp"
#include "openvino/op/max_pool.hpp"
#include "openvino/op/maximum.hpp"
#include "openvino/op/multiply.hpp"
#include "openvino/op/mvn.hpp"
#include "openvino/op/normalize_l2.hpp"
#include "openvino/op/power.hpp"
+#include "openvino/op/range.hpp"
#include "openvino/op/reduce_max.hpp"
#include "openvino/op/reduce_mean.hpp"
#include "openvino/op/reduce_min.hpp"
@@ -78,6 +83,19 @@ void erase_fq_path(const std::shared_ptr<Node>& node) {
rt_info.erase("fq_path");
}
+void mark_range_path(const std::shared_ptr<Node>& node) {
+ node->get_rt_info().emplace("range_path", true);
+}
+
+bool is_range_path(const std::shared_ptr<const Node>& node) {
+ return node->get_rt_info().count("range_path");
+}
+
+void erase_range_path(const std::shared_ptr<Node>& node) {
+ auto& rt_info = node->get_rt_info();
+ rt_info.erase("range_path");
+}
+
// Marking continues to propagate through these ops.
const std::shared_ptr<Node> propagate_through_ops =
pattern::wrap_type<ov::op::v0::Squeeze,
@@ -209,6 +227,57 @@ public:
}
};
+/* MarkFloatingPointRange marks paths that involve Range operations with floating point output data types,
+ * as well as their users allowed for propagation. This pass is needed to prevent accuracy data loss
+ * in cases of high range generation, which could suffer due to lowered precision.
+ */
+class MarkFloatingPointRange : public pass::MatcherPass {
+public:
+ OPENVINO_RTTI("MarkFloatingPointRange", "0");
+ MarkFloatingPointRange() {
+ MATCHER_SCOPE(MarkFloatingPointRange);
+
+ // through these nodes
+ const auto range_propagating_nodes = pattern::wrap_type<ov::op::v0::Convert,
+ ov::op::v1::Greater,
+ ov::op::v1::GreaterEqual,
+ ov::op::v1::Less,
+ ov::op::v1::LessEqual,
+ ov::op::v1::Reshape,
+ ov::op::v4::Range,
+ ov::op::v0::Squeeze,
+ ov::op::v0::Unsqueeze>();
+
+ matcher_pass_callback callback = [=](pattern::Matcher& m) {
+ const auto& node = m.get_match_root();
+ if (!node)
+ return false;
+
+ auto range = as_type_ptr<ov::op::v4::Range>(node);
+ if (range && range->get_output_type().is_real()) {
+ mark_range_path(node);
+ disable_fp16_compression(node);
+ return true;
+ }
+
+ bool is_changed = false;
+
+ for (const auto& in_node_output : node->input_values()) {
+ auto input_node = in_node_output.get_node_shared_ptr();
+ if (is_range_path(input_node)) {
+ mark_range_path(node);
+ disable_fp16_compression(node);
+ is_changed = true;
+ }
+ }
+
+ return is_changed;
+ };
+ auto m = make_shared<pattern::Matcher>(range_propagating_nodes, matcher_name);
+ register_matcher(m, callback);
+ }
+};
+
class PropagateMarkUpReduceOpPath : public pass::MatcherPass {
public:
OPENVINO_RTTI("PropagateMarkUpReduceOpPath", "0");
@@ -426,6 +495,7 @@ bool MarkSugraphsToKeepInMixedPrecision::run_on_model(const shared_ptr<ov::Model
Manager manager(get_pass_config(), "MarkSugraphsToKeepInMixedPrecision");
manager.set_per_pass_validation(false);
// Mark root of Division with eps pattern to keep in FP32
+ REGISTER_PASS(manager, MarkFloatingPointRange)
REGISTER_PASS(manager, MarkDivWithEps)
REGISTER_PASS(manager, MarkExpInReduceOpPath)
REGISTER_PASS(manager, PropagateDownDisableSensitivityForQuantized)
@@ -444,6 +514,7 @@ bool MarkSugraphsToKeepInMixedPrecision::run_on_model(const shared_ptr<ov::Model
for (auto& node : m->get_ops()) {
erase_reduceop_path(node);
erase_fq_path(node);
+ erase_range_path(node);
}
return false; // no need to revalidate
diff --git a/src/common/transformations/tests/common_optimizations/mark_subgraph_to_keep_in_mixed_precision_test.cpp b/src/common/transformations/tests/common_optimizations/mark_subgraph_to_keep_in_mixed_precision_test.cpp
index 95dfeae604e..669d79ffbf6 100644
--- a/src/common/transformations/tests/common_optimizations/mark_subgraph_to_keep_in_mixed_precision_test.cpp
+++ b/src/common/transformations/tests/common_optimizations/mark_subgraph_to_keep_in_mixed_precision_test.cpp
@@ -1087,6 +1087,78 @@ TEST(TransformationTests, MarkDivWithEpsToKeepInMixedPrecision_MinimalPatternUnc
ASSERT_TRUE(result.valid) << result.message;
}
+TEST(TransformationTests, MarkFloatingPointRange) {
+ shared_ptr<Model> model, model_ref;
+ pass::Manager manager;
+ {
+ auto begin = Constant::create(element::i64, Shape{}, {0});
+ auto step = Constant::create(element::i64, Shape{}, {1});
+
+ auto end = make_shared<Parameter>(element::i64, Shape{});
+
+ auto range_1 = make_shared<op::v4::Range>(begin, end, step, element::f32);
+ auto range_2 = make_shared<op::v4::Range>(begin, end, step, element::f32);
+
+ auto convert_1 = make_shared<Convert>(range_1, element::i64);
+ auto convert_2 = make_shared<Convert>(convert_1, element::f32);
+
+ auto unsqueeze_const = Constant::create(element::i64, Shape{2}, {-1, 1});
+ auto unsqueeze = make_shared<Unsqueeze>(range_2, unsqueeze_const);
+
+ auto greater = make_shared<Greater>(convert_2, unsqueeze);
+ auto convert = make_shared<Convert>(greater, element::f32);
+
+ auto multiply_const = Constant::create(element::f32, Shape{}, {1.f});
+ auto multiply = make_shared<Multiply>(convert, multiply_const);
+
+ model = make_shared<Model>(NodeVector{convert}, ParameterVector{end});
+
+ manager.register_pass<pass::MarkSugraphsToKeepInMixedPrecision>();
+ manager.run_passes(model);
+ }
+
+ {
+ auto begin = Constant::create(element::i64, Shape{}, {0});
+ auto step = Constant::create(element::i64, Shape{}, {1});
+
+ auto end = make_shared<Parameter>(element::i64, Shape{});
+
+ auto range_1 = make_shared<op::v4::Range>(begin, end, step, element::f32);
+ auto range_2 = make_shared<op::v4::Range>(begin, end, step, element::f32);
+
+ auto convert_1 = make_shared<Convert>(range_1, element::i64);
+ auto convert_2 = make_shared<Convert>(convert_1, element::f32);
+
+ auto unsqueeze_const = Constant::create(element::i64, Shape{2}, {-1, 1});
+ auto unsqueeze = make_shared<Unsqueeze>(range_2, unsqueeze_const);
+
+ auto greater = make_shared<Greater>(convert_2, unsqueeze);
+ auto convert = make_shared<Convert>(greater, element::f32);
+
+ auto multiply_const = Constant::create(element::f32, Shape{}, {1.f});
+ auto multiply = make_shared<Multiply>(convert, multiply_const);
+
+ // marking nodes to be kept in fp32 for mixed precision
+ disable_fp16_compression(range_1);
+ disable_fp16_compression(range_2);
+ disable_fp16_compression(convert_1);
+ disable_fp16_compression(convert_2);
+ disable_fp16_compression(unsqueeze);
+ disable_fp16_compression(greater);
+ disable_fp16_compression(convert);
+
+ model_ref = make_shared<Model>(NodeVector{convert}, ParameterVector{end});
+ }
+
+ const FunctionsComparator func_comparator =
+ FunctionsComparator::with_default().enable(FunctionsComparator::RUNTIME_KEYS);
+ // need to compare twice to ensure that no extra nodes are marked
+ FunctionsComparator::Result result = func_comparator(model_ref, model);
+ ASSERT_TRUE(result.valid) << result.message;
+ result = func_comparator(model, model_ref);
+ ASSERT_TRUE(result.valid) << result.message;
+}
+
TEST(TransformationTests, MarkDivWithEpsToKeepInMixedPrecision_InL2NormWithSqrtAndWithMax) {
const float eps_value = 1.e-12f;
shared_ptr<Model> model, model_ref;