Skip to content

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

[Transformations] FakeConvertDecomposition: adapt upconvert part to match LPT expectations (#31211)

Vladislav Golubevc8bf2475024b
diff --git a/src/common/transformations/src/transformations/op_conversions/fake_convert_decomposition.cpp b/src/common/transformations/src/transformations/op_conversions/fake_convert_decomposition.cpp
index 695aeb80186..3f62828a517 100644
--- a/src/common/transformations/src/transformations/op_conversions/fake_convert_decomposition.cpp
+++ b/src/common/transformations/src/transformations/op_conversions/fake_convert_decomposition.cpp
@@ -7,6 +7,7 @@
#include "itt.hpp"
#include "openvino/core/graph_util.hpp"
#include "openvino/core/rt_info.hpp"
+#include "openvino/core/validation_util.hpp"
#include "openvino/op/add.hpp"
#include "openvino/op/clamp.hpp"
#include "openvino/op/constant.hpp"
@@ -14,75 +15,92 @@
#include "openvino/op/divide.hpp"
#include "openvino/op/fake_convert.hpp"
#include "openvino/op/multiply.hpp"
+#include "openvino/op/negative.hpp"
+#include "openvino/op/power.hpp"
#include "openvino/op/subtract.hpp"
#include "openvino/pass/pattern/op/wrap_type.hpp"
+#include "transformations/utils/utils.hpp"
ov::pass::FakeConvertDecomposition::FakeConvertDecomposition() {
MATCHER_SCOPE(FakeConvertDecomposition);
- auto data = pattern::any_input();
-
- auto fake_convert = ov::pass::pattern::wrap_type<ov::op::v13::FakeConvert>();
+ auto fake_convert_m = ov::pass::pattern::wrap_type<ov::op::v13::FakeConvert>();
matcher_pass_callback callback = [OV_CAPTURE_CPY_AND_THIS](ov::pass::pattern::Matcher& m) {
- auto& pattern_to_output = m.get_pattern_value_map();
- const auto fake_convert_node =
- ov::as_type_ptr<ov::op::v13::FakeConvert>(pattern_to_output.at(fake_convert).get_node_shared_ptr());
+ const auto& pattern_to_output = m.get_pattern_value_map();
+ const auto fake_convert =
+ ov::as_type_ptr<ov::op::v13::FakeConvert>(pattern_to_output.at(fake_convert_m).get_node_shared_ptr());
- if (fake_convert_node == nullptr || transformation_callback(fake_convert_node)) {
+ if (fake_convert == nullptr || transformation_callback(fake_convert)) {
return false;
}
- Output<Node> data{fake_convert_node->input_value(0)};
- const Output<Node> input_scale{fake_convert_node->input_value(1)};
+ Output<Node> data{fake_convert->input_value(0)};
+ const auto input_scale = fake_convert->input_value(1);
auto input_type = data.get_element_type();
ov::pass::NodeRegistry decomp_ops;
if (input_type != input_scale.get_element_type()) {
input_type = input_scale.get_element_type();
- data = std::make_shared<ov::op::v0::Convert>(data, input_type);
- data = decomp_ops.add(data.get_node_shared_ptr());
+ data = decomp_ops.make<ov::op::v0::Convert>(data, input_type);
}
- // Align with clamp behavior of FakeConvert in ngraph reference
- const auto lower_bound = fake_convert_node->get_destination_element_type() == ov::element::f8e4m3
- ? static_cast<float>(std::numeric_limits<ov::float8_e4m3>::lowest())
- : static_cast<float>(std::numeric_limits<ov::float8_e5m2>::lowest());
- const auto upper_bound = fake_convert_node->get_destination_element_type() == ov::element::f8e4m3
- ? static_cast<float>(std::numeric_limits<ov::float8_e4m3>::max())
- : static_cast<float>(std::numeric_limits<ov::float8_e5m2>::max());
-
- std::shared_ptr<Node> result;
- const auto scale = decomp_ops.make<ov::op::v1::Multiply>(data, input_scale);
- if (fake_convert_node->get_input_size() == 2) {
- const auto clamp = decomp_ops.make<ov::op::v0::Clamp>(scale, lower_bound, upper_bound);
- const auto downconvert =
- decomp_ops.make<ov::op::v0::Convert>(clamp, fake_convert_node->get_destination_element_type());
- const auto upconvert = decomp_ops.make<ov::op::v0::Convert>(downconvert, input_type);
+ std::shared_ptr<Node> result = decomp_ops.make<ov::op::v1::Multiply>(data, input_scale);
+ bool with_shift = fake_convert->get_input_size() == 3;
+ if (with_shift) {
+ const auto& shift_const = fake_convert->input_value(2);
+ auto input_shift = decomp_ops.make<ov::op::v1::Subtract>(result, shift_const);
+ if (ov::op::util::can_eliminate_eltwise_node(input_shift, shift_const, result)) {
+ with_shift = false;
+ } else {
+ result = input_shift;
+ }
+ }
- result = decomp_ops.make<ov::op::v1::Divide>(upconvert, input_scale);
+ // Align with clamp behavior of FakeConvert in ngraph reference
+ const auto [lower_bound, upper_bound] = [&]() {
+ switch (fake_convert->get_destination_element_type()) {
+ case ov::element::f8e4m3:
+ return std::make_pair(static_cast<double>(std::numeric_limits<ov::float8_e4m3>::lowest()),
+ static_cast<double>(std::numeric_limits<ov::float8_e4m3>::max()));
+ case ov::element::f8e5m2:
+ return std::make_pair(static_cast<double>(std::numeric_limits<ov::float8_e5m2>::lowest()),
+ static_cast<double>(std::numeric_limits<ov::float8_e5m2>::max()));
+ default:
+ OPENVINO_THROW("Unsupported destination element type: ", fake_convert->get_destination_element_type());
+ }
+ }();
+
+ result = decomp_ops.make<ov::op::v0::Clamp>(result, lower_bound, upper_bound);
+ result = decomp_ops.make<ov::op::v0::Convert>(result, fake_convert->get_destination_element_type());
+ result = decomp_ops.make<ov::op::v0::Convert>(result, input_type);
+ // Note: upconvert part is composed as Subtract->Multiply sequence
+ // in order to match LPT dequantization operations representation
+ if (with_shift) {
+ const auto negative = decomp_ops.make<ov::op::v0::Negative>(fake_convert->input_value(2));
+ if (const auto constant = ov::util::get_constant_from_source(negative)) {
+ result = decomp_ops.make<ov::op::v1::Subtract>(result, constant);
+ } else {
+ result = decomp_ops.make<ov::op::v1::Subtract>(result, negative);
+ }
+ }
+ const auto power_const = decomp_ops.make<ov::op::v0::Constant>(input_type, Shape{}, -1.f);
+ const auto power = decomp_ops.make<ov::op::v1::Power>(input_scale, power_const);
+ if (const auto constant = ov::util::get_constant_from_source(power)) {
+ result = decomp_ops.make<ov::op::v1::Multiply>(result, constant);
} else {
- const Output<Node> input_shift{fake_convert_node->input_value(2)};
- const auto shift = decomp_ops.make<ov::op::v1::Subtract>(scale, input_shift);
-
- const auto clamp = decomp_ops.make<ov::op::v0::Clamp>(shift, lower_bound, upper_bound);
- const auto downconvert =
- decomp_ops.make<ov::op::v0::Convert>(clamp, fake_convert_node->get_destination_element_type());
- const auto upconvert = decomp_ops.make<ov::op::v0::Convert>(downconvert, input_type);
-
- const auto deshift = decomp_ops.make<ov::op::v1::Add>(upconvert, input_shift);
- result = decomp_ops.make<ov::op::v1::Divide>(deshift, input_scale);
+ result = decomp_ops.make<ov::op::v1::Multiply>(result, power);
}
- if (result->get_output_element_type(0) != fake_convert_node->get_output_element_type(0)) {
- result = decomp_ops.make<ov::op::v0::Convert>(result, fake_convert_node->get_output_element_type(0));
+ if (result->get_output_element_type(0) != fake_convert->get_output_element_type(0)) {
+ result = decomp_ops.make<ov::op::v0::Convert>(result, fake_convert->get_output_element_type(0));
}
- result->set_friendly_name(m.get_match_root()->get_friendly_name());
- ov::copy_runtime_info(fake_convert_node, decomp_ops.get());
- ov::replace_node(m.get_match_root(), result);
+ result->set_friendly_name(fake_convert->get_friendly_name());
+ ov::copy_runtime_info(fake_convert, decomp_ops.get());
+ ov::replace_node(fake_convert, result);
return true;
};
- auto m = std::make_shared<ov::pass::pattern::Matcher>(fake_convert, matcher_name);
+ auto m = std::make_shared<ov::pass::pattern::Matcher>(fake_convert_m, matcher_name);
register_matcher(m, callback);
}
diff --git a/src/common/transformations/tests/op_conversions/fake_convert_decomposition_test.cpp b/src/common/transformations/tests/op_conversions/fake_convert_decomposition_test.cpp
index 85f9480d74b..1d4cdf7e20f 100644
--- a/src/common/transformations/tests/op_conversions/fake_convert_decomposition_test.cpp
+++ b/src/common/transformations/tests/op_conversions/fake_convert_decomposition_test.cpp
@@ -20,6 +20,16 @@
using namespace ov;
+class FakeConvertDecompositionTestBase : public TransformationTestsF {
+protected:
+ void SetUp() override {
+ TransformationTestsF::SetUp();
+ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);
+ comparator.enable(FunctionsComparator::CmpValues::ATTRIBUTES);
+ manager.register_pass<ov::pass::FakeConvertDecomposition>();
+ }
+};
+
using FakeConvertDecompositionParams = std::tuple<Shape, // data shape
Shape, // scale shape
Shape, // shift shape
@@ -27,7 +37,7 @@ using FakeConvertDecompositionParams = std::tuple<Shape, // data shap
element::Type_t, // destination precision
bool>; // default shift
-class FakeConvertDecompositionTest : public ov::test::TestsCommon,
+class FakeConvertDecompositionTest : public FakeConvertDecompositionTestBase,
public ::testing::WithParamInterface<FakeConvertDecompositionParams> {
public:
static std::string getTestCaseName(::testing::TestParamInfo<FakeConvertDecompositionParams> obj) {
@@ -50,75 +60,58 @@ public:
result << "defaultShift=false";
return result.str();
}
-};
-
-TEST_P(FakeConvertDecompositionTest, CompareFunctions) {
- FakeConvertDecompositionParams params = this->GetParam();
-
- Shape data_shape, scale_shape, shift_shape;
- element::Type_t data_prec, dst_prec;
- bool default_shift;
- std::tie(data_shape, scale_shape, shift_shape, data_prec, dst_prec, default_shift) = params;
-
- std::shared_ptr<ov::Model> model(nullptr);
- {
- const auto data = std::make_shared<opset1::Parameter>(data_prec, PartialShape(data_shape));
- const auto scale = std::make_shared<opset1::Constant>(data_prec, scale_shape);
- const auto shift = std::make_shared<opset1::Constant>(data_prec, shift_shape);
-
- const auto fake_convert = default_shift ? std::make_shared<opset13::FakeConvert>(data, scale, dst_prec)
- : std::make_shared<opset13::FakeConvert>(data, scale, shift, dst_prec);
- model = std::make_shared<ov::Model>(OutputVector{fake_convert}, ParameterVector{data});
-
- pass::Manager manager;
- manager.register_pass<ov::pass::InitNodeInfo>();
- manager.register_pass<ov::pass::FakeConvertDecomposition>();
- manager.run_passes(model);
-
- OV_ASSERT_NO_THROW(check_rt_info(model));
- }
-
- std::shared_ptr<ov::Model> model_ref(nullptr);
- {
- const auto input_data = std::make_shared<opset1::Parameter>(data_prec, PartialShape(data_shape));
- const auto input_scale = std::make_shared<opset1::Constant>(data_prec, scale_shape);
- const auto input_shift = std::make_shared<opset1::Constant>(data_prec, shift_shape);
- ParameterVector params;
- params.push_back(input_data);
- std::shared_ptr<Node> data = input_data;
-
- const auto lower_bound = dst_prec == ov::element::f8e4m3
- ? static_cast<float>(std::numeric_limits<ov::float8_e4m3>::lowest())
- : static_cast<float>(std::numeric_limits<ov::float8_e5m2>::lowest());
- const auto upper_bound = dst_prec == ov::element::f8e4m3
- ? static_cast<float>(std::numeric_limits<ov::float8_e4m3>::max())
- : static_cast<float>(std::numeric_limits<ov::float8_e5m2>::max());
-
- std::shared_ptr<Node> result;
- const auto scale = std::make_shared<ov::op::v1::Multiply>(data, input_scale);
- if (default_shift) {
- const auto clamp = std::make_shared<ov::op::v0::Clamp>(scale, lower_bound, upper_bound);
- const auto downconvert = std::make_shared<ov::op::v0::Convert>(clamp, dst_prec);
- const auto upconvert = std::make_shared<ov::op::v0::Convert>(downconvert, data_prec);
- result = std::make_shared<ov::op::v1::Divide>(upconvert, input_scale);
- } else {
- const auto shift = std::make_shared<ov::op::v1::Subtract>(scale, input_shift);
-
- const auto clamp = std::make_shared<ov::op::v0::Clamp>(shift, lower_bound, upper_bound);
- const auto downconvert = std::make_shared<ov::op::v0::Convert>(clamp, dst_prec);
- const auto upconvert = std::make_shared<ov::op::v0::Convert>(downconvert, data_prec);
-
- const auto deshift = std::make_shared<ov::op::v1::Add>(upconvert, input_shift);
- result = std::make_shared<ov::op::v1::Divide>(deshift, input_scale);
+protected:
+ void SetUp() override {
+ FakeConvertDecompositionTestBase::SetUp();
+ const auto [data_shape, scale_shape, shift_shape, data_prec, dst_prec, default_shift] = this->GetParam();
+ {
+ const auto data = std::make_shared<opset1::Parameter>(data_prec, PartialShape(data_shape));
+ const auto scale = std::make_shared<opset1::Constant>(data_prec, scale_shape, 4.f);
+ const auto shift = std::make_shared<opset1::Constant>(data_prec, shift_shape, 12.f);
+
+ const auto fake_convert = default_shift
+ ? std::make_shared<opset13::FakeConvert>(data, scale, dst_prec)
+ : std::make_shared<opset13::FakeConvert>(data, scale, shift, dst_prec);
+ model = std::make_shared<ov::Model>(OutputVector{fake_convert}, ParameterVector{data});
}
- model_ref = std::make_shared<ov::Model>(OutputVector{result}, params);
+ {
+ const auto data = std::make_shared<opset1::Parameter>(data_prec, PartialShape(data_shape));
+ const auto input_scale = std::make_shared<opset1::Constant>(data_prec, scale_shape, 4.f);
+ const auto input_shift = std::make_shared<opset1::Constant>(data_prec, shift_shape, 12.f);
+
+ const auto lower_bound = dst_prec == ov::element::f8e4m3
+ ? static_cast<float>(std::numeric_limits<ov::float8_e4m3>::lowest())
+ : static_cast<float>(std::numeric_limits<ov::float8_e5m2>::lowest());
+ const auto upper_bound = dst_prec == ov::element::f8e4m3
+ ? static_cast<float>(std::numeric_limits<ov::float8_e4m3>::max())
+ : static_cast<float>(std::numeric_limits<ov::float8_e5m2>::max());
+
+ std::shared_ptr<Node> result;
+ const auto scale = std::make_shared<ov::op::v1::Multiply>(data, input_scale);
+ if (default_shift) {
+ const auto clamp = std::make_shared<ov::op::v0::Clamp>(scale, lower_bound, upper_bound);
+ const auto downconvert = std::make_shared<ov::op::v0::Convert>(clamp, dst_prec);
+ result = std::make_shared<ov::op::v0::Convert>(downconvert, data_prec);
+ } else {
+ const auto shift = std::make_shared<ov::op::v1::Subtract>(scale, input_shift);
+
+ const auto clamp = std::make_shared<ov::op::v0::Clamp>(shift, lower_bound, upper_bound);
+ const auto downconvert = std::make_shared<ov::op::v0::Convert>(clamp, dst_prec);
+ const auto upconvert = std::make_shared<ov::op::v0::Convert>(downconvert, data_prec);
+
+ const auto output_shift = std::make_shared<ov::op::v0::Constant>(data_prec, shift_shape, -12.f);
+ result = std::make_shared<ov::op::v1::Subtract>(upconvert, output_shift);
+ }
+ const auto output_scale = std::make_shared<opset1::Constant>(data_prec, scale_shape, 1.f / 4.f);
+ result = std::make_shared<ov::op::v1::Multiply>(result, output_scale);
+ model_ref = std::make_shared<ov::Model>(OutputVector{result}, ParameterVector{data});
+ }
}
+};
- const auto res = compare_functions(model, model_ref);
- ASSERT_TRUE(res.first) << res.second;
-}
+TEST_P(FakeConvertDecompositionTest, CompareFunctions) {}
const std::vector<element::Type_t> data_precisions = {element::Type_t::f32,
element::Type_t::f16,
@@ -163,3 +156,33 @@ INSTANTIATE_TEST_SUITE_P(ElementwiseFakeConvert_Decomposition,
FakeConvertDecompositionTest,
elementwise_fake_convert_params,
FakeConvertDecompositionTest::getTestCaseName);
+
+TEST_F(FakeConvertDecompositionTestBase, FakeConvert_Decomposition_TrivialShift) {
+ const ov::element::Type_t data_prec = ov::element::f32;
+ const ov::PartialShape data_shape{2, 3, 4, 5};
+ const ov::element::Type_t dst_prec = ov::element::f8e4m3;
+ {
+ const auto data = std::make_shared<opset1::Parameter>(data_prec, PartialShape(data_shape));
+ const auto scale = std::make_shared<opset1::Constant>(data_prec, Shape{}, 4.f);
+ const auto shift = std::make_shared<opset1::Constant>(data_prec, Shape{}, 0.f);
+
+ const auto fake_convert = std::make_shared<opset13::FakeConvert>(data, scale, shift, dst_prec);
+ model = std::make_shared<ov::Model>(OutputVector{fake_convert}, ParameterVector{data});
+ }
+
+ {
+ const auto data = std::make_shared<opset1::Parameter>(data_prec, PartialShape(data_shape));
+ const auto input_scale = std::make_shared<opset1::Constant>(data_prec, Shape{}, 4.f);
+
+ const auto lower_bound = static_cast<float>(std::numeric_limits<ov::float8_e4m3>::lowest());
+ const auto upper_bound = static_cast<float>(std::numeric_limits<ov::float8_e4m3>::max());
+
+ const auto scale = std::make_shared<ov::op::v1::Multiply>(data, input_scale);
+ const auto clamp = std::make_shared<ov::op::v0::Clamp>(scale, lower_bound, upper_bound);
+ const auto downconvert = std::make_shared<ov::op::v0::Convert>(clamp, dst_prec);
+ const auto upconvert = std::make_shared<ov::op::v0::Convert>(downconvert, data_prec);
+ const auto output_scale = std::make_shared<opset1::Constant>(data_prec, Shape{}, 1.f / 4.f);
+ const auto out_multiply = std::make_shared<ov::op::v1::Multiply>(upconvert, output_scale);
+ model_ref = std::make_shared<ov::Model>(OutputVector{out_multiply}, ParameterVector{data});
+ }
+}