Skip to content

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

[CPU]Mark Range as FP32 (#26456)

Zhang Yie8f11674c233
diff --git a/src/common/transformations/include/transformations/fp16_compression/mark_floatpoint_range.hpp b/src/common/transformations/include/transformations/fp16_compression/mark_floatpoint_range.hpp
new file mode 100644
index 00000000000..a61bd270c58
--- /dev/null
+++ b/src/common/transformations/include/transformations/fp16_compression/mark_floatpoint_range.hpp
@@ -0,0 +1,30 @@
+// Copyright (C) 2018-2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include "openvino/pass/graph_rewrite.hpp"
+#include "transformations_visibility.hpp"
+
+namespace ov {
+namespace pass {
+
+/**
+ * @ingroup ov_transformation_common_api
+ * @brief This transformation markups the 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 TRANSFORMATIONS_API MarkFloatingPointRange : public ov::pass::MatcherPass {
+public:
+ OPENVINO_RTTI("MarkFloatingPointRange", "0");
+ MarkFloatingPointRange();
+};
+
+OPENVINO_API void mark_range_path(const std::shared_ptr<Node>& node);
+OPENVINO_API bool is_range_path(const std::shared_ptr<const Node>& node);
+OPENVINO_API void erase_range_path(const std::shared_ptr<Node>& node);
+
+} // namespace pass
+} // namespace ov
\ No newline at end of file
diff --git a/src/common/transformations/src/transformations/fp16_compression/mark_floatpoint_range.cpp b/src/common/transformations/src/transformations/fp16_compression/mark_floatpoint_range.cpp
new file mode 100644
index 00000000000..d0e2ea4d3a5
--- /dev/null
+++ b/src/common/transformations/src/transformations/fp16_compression/mark_floatpoint_range.cpp
@@ -0,0 +1,77 @@
+// Copyright (C) 2018-2024 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "transformations/fp16_compression/mark_floatpoint_range.hpp"
+
+#include "itt.hpp"
+#include "openvino/op/convert.hpp"
+#include "openvino/op/greater.hpp"
+#include "openvino/op/greater_eq.hpp"
+#include "openvino/op/less.hpp"
+#include "openvino/op/less_eq.hpp"
+#include "openvino/op/range.hpp"
+#include "openvino/op/reshape.hpp"
+#include "openvino/op/squeeze.hpp"
+#include "openvino/op/unsqueeze.hpp"
+#include "openvino/pass/pattern/op/wrap_type.hpp"
+#include "transformations/rt_info/disable_fp16_compression.hpp"
+#include "transformations/utils/gen_pattern.hpp"
+#include "transformations/utils/utils.hpp"
+
+void ov::pass::mark_range_path(const std::shared_ptr<Node>& node) {
+ node->get_rt_info().emplace("range_path", true);
+}
+
+bool ov::pass::is_range_path(const std::shared_ptr<const Node>& node) {
+ return node->get_rt_info().count("range_path");
+}
+
+void ov::pass::erase_range_path(const std::shared_ptr<Node>& node) {
+ auto& rt_info = node->get_rt_info();
+ rt_info.erase("range_path");
+}
+
+ov::pass::MarkFloatingPointRange::MarkFloatingPointRange() {
+ MATCHER_SCOPE(MarkFloatingPointRange);
+ using namespace ov::pass::pattern;
+ using namespace ov::gen_pattern;
+ // 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>();
+
+ ov::matcher_pass_callback callback = [=](pattern::Matcher& m) {
+ const auto& node = m.get_match_root();
+ if (!node)
+ return false;
+
+ auto range = ov::as_type_ptr<ov::op::v4::Range>(node);
+ if (range && range->get_output_type().is_real()) {
+ mark_range_path(node);
+ ov::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);
+ ov::disable_fp16_compression(node);
+ is_changed = true;
+ }
+ }
+
+ return is_changed;
+ };
+ auto m = std::make_shared<ov::pass::pattern::Matcher>(range_propagating_nodes, matcher_name);
+ register_matcher(m, callback);
+}
\ No newline at end of file
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 769fa3c7287..2235e87c792 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
@@ -49,6 +49,7 @@
#include "openvino/pass/pattern/op/wrap_type.hpp"
#include "transformations/common_optimizations/mark_precision_sensitive_shapeof_subgraphs.hpp"
#include "transformations/convert_precision.hpp"
+#include "transformations/fp16_compression/mark_floatpoint_range.hpp"
#include "transformations/rt_info/disable_fp16_compression.hpp"
#include "transformations/utils/utils.hpp"
@@ -83,19 +84,6 @@ 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,
@@ -227,57 +215,6 @@ 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");
@@ -495,7 +432,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, ov::pass::MarkFloatingPointRange)
REGISTER_PASS(manager, MarkDivWithEps)
REGISTER_PASS(manager, MarkExpInReduceOpPath)
REGISTER_PASS(manager, PropagateDownDisableSensitivityForQuantized)
@@ -514,7 +451,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);
+ ov::pass::erase_range_path(node);
}
return false; // no need to revalidate
diff --git a/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp b/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp
index fd31d6b2fa5..38649b2906e 100644
--- a/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp
+++ b/src/plugins/intel_cpu/src/transformations/transformation_pipeline.cpp
@@ -39,6 +39,7 @@
#include "transformations/common_optimizations/rms_fusion.hpp"
#include "transformations/control_flow/unroll_tensor_iterator.hpp"
#include "transformations/fp16_compression/mark_decompression_convert_constant_folding.hpp"
+#include "transformations/fp16_compression/mark_floatpoint_range.hpp"
#include "transformations/op_conversions/convert_avgpool_downgrade.hpp"
#include "transformations/op_conversions/convert_batch_to_space.hpp"
#include "transformations/op_conversions/convert_bitwise_to_logical_bool.hpp"
@@ -860,8 +861,10 @@ void Transformations::PostLpt() {
ov::intel_cpu::DecomposeRMSNorm);
// markup Rope Input when BF16/F16 inference.
- if (one_of(inferencePrecision, ov::element::bf16, ov::element::f16))
+ if (one_of(inferencePrecision, ov::element::bf16, ov::element::f16)) {
CPU_REGISTER_PASS_COMMON(postLPTPassManager, ov::pass::MarkRopeInputsToKeepInMixedPrecision);
+ CPU_REGISTER_PASS_COMMON(postLPTPassManager, ov::pass::MarkFloatingPointRange);
+ }
// Should be before Snippets pipeline because Ngram pattern contains eltwise nodes that can be tokenized by Snippets.
auto symbolic_pipeline = CPU_REGISTER_PASS_COMMON(postLPTPassManager, ov::pass::SymbolicOptimizations, false);