OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[Transformations] EliminateSequentialFakeQuantize (#36075)
Mikhail Ryzhov5e120a00a656
diff --git a/src/common/transformations/include/transformations/common_optimizations/fq_eliminate_sequential.hpp b/src/common/transformations/include/transformations/common_optimizations/fq_eliminate_sequential.hppnew file mode 100644index 00000000000..cbc8e071b53--- /dev/null+++ b/src/common/transformations/include/transformations/common_optimizations/fq_eliminate_sequential.hpp@@ -0,0 +1,81 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#pragma once++#include "openvino/pass/pass.hpp"+#include "transformations_visibility.hpp"++namespace ov {+namespace pass {++class TRANSFORMATIONS_API FakeQuantizeEliminateSequential;++} // namespace pass+} // namespace ov++/**+ * @ingroup ov_transformation_common_api+ * @brief FakeQuantizeEliminateSequential removes a redundant second FakeQuantize from a sequential+ * pair (FQ1 -> FQ2). The notation below is FQ(in_low, in_high, out_low, out_high, levels).+ *+ * FQ1 and FQ2 may be separated by one or several value-preserving ops.+ * A matched FakeQuantize is applied element-wise (whether per-tensor or per-channel), so it+ * commutes with such ops and the folding stays valid through the chain.+ *+ * FQ2 is removed (FQ1 kept) when it is identical to FQ1, i.e. it has the same levels, auto broadcast,+ * and range constants (input_low/input_high/output_low/output_high). In that case FQ2 only re-applies+ * the quantization already produced by FQ1 and is redundant.+ *+ * Before:+ * ┌─────────┐+ * │ Data │+ * └────┬────┘+ * │+ * ┌────┴────┐+ * │ FQ1 │+ * └────┬────┘+ * │+ * ┌──────┴───────┐+ * │ Reshape / │ (optional, zero or more value-preserving ops)+ * │ Transpose / │+ * │ Squeeze / │+ * │ Unsqueeze │+ * └──────┬───────┘+ * │+ * ┌────┴────┐+ * │ FQ2 │ (identical params to FQ1)+ * └────┬────┘+ * │+ * ┌────┴────┐+ * │Consumer │+ * └─────────┘+ *+ * After:+ * ┌─────────┐+ * │ Data │+ * └────┬────┘+ * │+ * ┌────┴────┐+ * │ FQ1 │+ * └────┬────┘+ * │+ * ┌──────┴───────┐+ * │ Reshape / │ (optional, preserved)+ * │ Transpose / │+ * │ Squeeze / │+ * │ Unsqueeze │+ * └──────┬───────┘+ * │+ * ┌────┴────┐+ * │Consumer │+ * └─────────┘+ *+ * For example: FQ1(-1, 1, -1, 1, 256) -> FQ2(-1, 1, -1, 1, 256) => FQ1(-1, 1, -1, 1, 256)+ */+class ov::pass::FakeQuantizeEliminateSequential : public ov::pass::ModelPass {+public:+ OPENVINO_MODEL_PASS_RTTI("FakeQuantizeEliminateSequential");+ bool run_on_model(const std::shared_ptr<ov::Model>& model) override;+};diff --git a/src/common/transformations/include/transformations/utils/utils.hpp b/src/common/transformations/include/transformations/utils/utils.hppindex 259ba59c1ff..d3a033a719a 100644--- a/src/common/transformations/include/transformations/utils/utils.hpp+++ b/src/common/transformations/include/transformations/utils/utils.hpp@@ -26,6 +26,9 @@namespace ov {namespace op {+namespace v0 {+class FakeQuantize;+} // namespace v0namespace util {template <class T>@@ -223,6 +226,18 @@ TRANSFORMATIONS_API void visit_path_forward(ov::Node* start_node,std::function<void(ov::Node*)> func,std::function<bool(ov::Node*)> skip_node_predicate);+/**+ * \brief Checks whether two FakeQuantize ops share identical quantization parameters: the same number+ * of levels, the same auto-broadcast spec, and equal input_low/input_high/output_low/output_high+ * constants.+ *+ * \param lhs The first FakeQuantize.+ * \param rhs The second FakeQuantize.+ * \return true if both ops are valid and have identical parameters.+ */+TRANSFORMATIONS_API bool have_same_fake_quantize_params(const std::shared_ptr<ov::op::v0::FakeQuantize>& lhs,+ const std::shared_ptr<ov::op::v0::FakeQuantize>& rhs);+/*** \brief Traverses a shapeOf subgraph starting from the node and not including the ShapeOf nodes,* and calls "func" for each ov::Node.diff --git a/src/common/transformations/src/transformations/common_optimizations/common_optimizations.cpp b/src/common/transformations/src/transformations/common_optimizations/common_optimizations.cppindex c3b25f230ce..7d2c6f8533f 100644--- a/src/common/transformations/src/transformations/common_optimizations/common_optimizations.cpp+++ b/src/common/transformations/src/transformations/common_optimizations/common_optimizations.cpp@@ -25,6 +25,7 @@#include "transformations/common_optimizations/dropout_with_random_uniform_replacer.hpp"#include "transformations/common_optimizations/eliminate_unsqueeze_gather.hpp"#include "transformations/common_optimizations/fq_concat_fusion.hpp"+#include "transformations/common_optimizations/fq_eliminate_sequential.hpp"#include "transformations/common_optimizations/fq_mul_fusion.hpp"#include "transformations/common_optimizations/fq_reshape_fusion.hpp"#include "transformations/common_optimizations/fuse_clamp_and_fake_quantize.hpp"@@ -257,6 +258,8 @@ bool ov::pass::CommonOptimizations::run_on_model(const std::shared_ptr<ov::ModelADD_MATCHER(fq_fusions, MulFakeQuantizeFusion)fq_fusions->set_name("ov::pass::FakeQuantizeFusions");+ REGISTER_PASS(manager, FakeQuantizeEliminateSequential)+// Temporary transformation to allow for PyTorch frontend to// partially support bitwise operators with boolean inputs for plugins// that didn't enabled BitwiseOps from opset13 and to allow for constantdiff --git a/src/common/transformations/src/transformations/common_optimizations/fq_concat_fusion.cpp b/src/common/transformations/src/transformations/common_optimizations/fq_concat_fusion.cppindex 41e18feb484..dc894d707f6 100644--- a/src/common/transformations/src/transformations/common_optimizations/fq_concat_fusion.cpp+++ b/src/common/transformations/src/transformations/common_optimizations/fq_concat_fusion.cpp@@ -9,38 +9,18 @@#include "compare.hpp"#include "itt.hpp"-#include "openvino/core/graph_util.hpp"#include "openvino/core/rt_info.hpp"#include "openvino/op/concat.hpp"#include "openvino/op/constant.hpp"#include "openvino/op/fake_quantize.hpp"#include "openvino/pass/pattern/op/wrap_type.hpp"+#include "transformations/utils/utils.hpp"namespace v0 = ov::op::v0;+namespace op_util = ov::op::util;namespace ov::pass {-namespace {--bool have_same_fake_quantize_params(const std::shared_ptr<v0::FakeQuantize>& lhs,- const std::shared_ptr<v0::FakeQuantize>& rhs) {- if (!lhs || !rhs || lhs->get_levels() != rhs->get_levels() ||- lhs->get_auto_broadcast() != rhs->get_auto_broadcast()) {- return false;- }-- for (size_t index = 1; index < lhs->get_input_size(); ++index) {- if (!ov::compare_constants(lhs->input_value(index).get_node_shared_ptr(),- rhs->input_value(index).get_node_shared_ptr())) {- return false;- }- }-- return true;-}--} // namespace-FakeQuantizeConcatFusion::FakeQuantizeConcatFusion() {MATCHER_SCOPE(FakeQuantizeConcatFusion);auto concat_pattern = pattern::wrap_type<v0::Concat>({}, pattern::consumers_count(1));@@ -63,7 +43,7 @@ FakeQuantizeConcatFusion::FakeQuantizeConcatFusion() {for (const auto& concat_input : concat->input_values()) {const auto input_fq = ov::as_type_ptr<v0::FakeQuantize>(concat_input.get_node_shared_ptr());- if (!have_same_fake_quantize_params(input_fq, output_fq)) {+ if (!op_util::have_same_fake_quantize_params(input_fq, output_fq)) {return false;}diff --git a/src/common/transformations/src/transformations/common_optimizations/fq_eliminate_sequential.cpp b/src/common/transformations/src/transformations/common_optimizations/fq_eliminate_sequential.cppnew file mode 100644index 00000000000..80ee387f06c--- /dev/null+++ b/src/common/transformations/src/transformations/common_optimizations/fq_eliminate_sequential.cpp@@ -0,0 +1,75 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "transformations/common_optimizations/fq_eliminate_sequential.hpp"++#include <memory>+#include <unordered_set>+#include <vector>++#include "itt.hpp"+#include "openvino/core/graph_util.hpp"+#include "openvino/core/model.hpp"+#include "openvino/op/fake_quantize.hpp"+#include "openvino/op/reshape.hpp"+#include "openvino/op/transpose.hpp"+#include "openvino/op/unsqueeze.hpp"+#include "openvino/op/util/op_types.hpp"+#include "openvino/op/util/squeeze_base.hpp"+#include "transformations/utils/utils.hpp"++namespace v0 = ov::op::v0;+namespace v1 = ov::op::v1;+namespace op_util = ov::op::util;++namespace ov::pass {++namespace {++bool is_value_preserving(const std::shared_ptr<ov::Node>& node) {+ return ov::is_type_any_of<v1::Reshape, v1::Transpose, op_util::SqueezeBase, v0::Unsqueeze>(node);+}++} // namespace++bool FakeQuantizeEliminateSequential::run_on_model(const std::shared_ptr<ov::Model>& model) {+ RUN_ON_MODEL_SCOPE(FakeQuantizeEliminateSequential);++ bool eliminated = false;+ for (const auto& op : model->get_ordered_ops()) {+ auto fq1 = ov::as_type_ptr<v0::FakeQuantize>(op);+ if (!fq1) {+ continue;+ }++ // Walk forward from FQ1 following its consumers and collect the redundant FakeQuantizes.+ // Traversal continues only through FQ1, value-preserving ops, and redundant FakeQuantizes, so+ // branches into several consumers are handled as well. visit_path_forward queries a node's+ // consumers right after the visitor runs, so the redundant FakeQuantizes are only collected+ // here and detached afterwards to avoid dereferencing a freed node.+ std::vector<std::shared_ptr<v0::FakeQuantize>> redundant_fqs;+ std::unordered_set<ov::Node*> visited;+ auto skip_node = [&](ov::Node* node) {+ auto shared_node = node->shared_from_this();+ if (shared_node == fq1 || is_value_preserving(shared_node)) {+ return false;+ }+ return !op_util::have_same_fake_quantize_params(fq1, ov::as_type_ptr<v0::FakeQuantize>(shared_node));+ };+ auto collect_redundant_fq = [&](ov::Node* node) {+ if (auto fq2 = ov::as_type_ptr<v0::FakeQuantize>(node->shared_from_this()); fq2 && fq2 != fq1) {+ redundant_fqs.push_back(fq2);+ }+ };+ op_util::visit_path_forward(fq1.get(), visited, collect_redundant_fq, skip_node);++ for (const auto& fq2 : redundant_fqs) {+ eliminated = replace_output_update_name(fq2->output(0), fq2->input_value(0)) || eliminated;+ }+ }++ return eliminated;+}++} // namespace ov::passdiff --git a/src/common/transformations/src/transformations/utils/utils.cpp b/src/common/transformations/src/transformations/utils/utils.cppindex 7dcd90d0bec..6068da85c2a 100644--- a/src/common/transformations/src/transformations/utils/utils.cpp+++ b/src/common/transformations/src/transformations/utils/utils.cpp@@ -11,6 +11,7 @@#include <tuple>#include <vector>+#include "openvino/core/graph_util.hpp"#include "openvino/core/validation_util.hpp"#include "openvino/op/abs.hpp"#include "openvino/op/add.hpp"@@ -107,6 +108,26 @@ void visit_path_forward(ov::Node* start_node,}}+bool have_same_fake_quantize_params(const std::shared_ptr<v0::FakeQuantize>& lhs,+ const std::shared_ptr<v0::FakeQuantize>& rhs) {+ if (!lhs || !rhs || lhs->get_levels() != rhs->get_levels() ||+ lhs->get_auto_broadcast() != rhs->get_auto_broadcast()) {+ return false;+ }++ for (size_t index = 1; index < lhs->get_input_size(); ++index) {+ // compare_constants matches flattened values only, so the shapes are compared separately to+ // avoid treating constants that broadcast differently (e.g. {1, 3, 1, 1} vs {3, 1}) as equal.+ if (lhs->get_input_partial_shape(index) != rhs->get_input_partial_shape(index) ||+ !ov::compare_constants(lhs->input_value(index).get_node_shared_ptr(),+ rhs->input_value(index).get_node_shared_ptr())) {+ return false;+ }+ }++ return true;+}+bool get_single_value(const std::shared_ptr<op::v0::Constant>& const_node, float& value, bool check_value_range) {switch (const_node->get_element_type()) {case element::Type_t::f16:diff --git a/src/common/transformations/tests/common_optimizations/fq_eliminate_sequential_test.cpp b/src/common/transformations/tests/common_optimizations/fq_eliminate_sequential_test.cppnew file mode 100644index 00000000000..f02dac94d3b--- /dev/null+++ b/src/common/transformations/tests/common_optimizations/fq_eliminate_sequential_test.cpp@@ -0,0 +1,326 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "transformations/common_optimizations/fq_eliminate_sequential.hpp"++#include <gtest/gtest.h>++#include <functional>+#include <memory>+#include <vector>++#include "common_test_utils/common_utils.hpp"+#include "common_test_utils/node_builders/fake_quantize.hpp"+#include "common_test_utils/ov_test_utils.hpp"+#include "openvino/core/model.hpp"+#include "openvino/op/abs.hpp"+#include "openvino/op/constant.hpp"+#include "openvino/op/parameter.hpp"+#include "openvino/op/reshape.hpp"+#include "openvino/op/squeeze.hpp"+#include "openvino/op/transpose.hpp"+#include "openvino/op/unsqueeze.hpp"+#include "openvino/pass/manager.hpp"++using namespace ov;+using namespace std;++namespace v0 = ov::op::v0;+namespace v1 = ov::op::v1;++using ov::test::utils::make_fake_quantize;++struct SequentialFqParams {+ std::vector<float> fq1;+ size_t fq1_levels;+ std::vector<float> fq2;+ size_t fq2_levels;+ bool expect_eliminate;+ std::string name;+};++class SequentialFakeQuantizeTests : public TransformationTestsF,+ public testing::WithParamInterface<SequentialFqParams> {+public:+ static std::string get_test_case_name(testing::TestParamInfo<SequentialFqParams> info) {+ return info.param.name;+ }+};++TEST_P(SequentialFakeQuantizeTests, CompareFunctions) {+ const auto& p = GetParam();+ {+ auto input = std::make_shared<v0::Parameter>(element::f32, Shape{1, 3, 16, 16});+ auto fq1 =+ make_fake_quantize(input, element::f32, p.fq1_levels, {1}, {p.fq1[0]}, {p.fq1[1]}, {p.fq1[2]}, {p.fq1[3]});+ auto fq2 =+ make_fake_quantize(fq1, element::f32, p.fq2_levels, {1}, {p.fq2[0]}, {p.fq2[1]}, {p.fq2[2]}, {p.fq2[3]});+ auto abs = std::make_shared<v0::Abs>(fq2);+ model = std::make_shared<ov::Model>(OutputVector{abs}, ParameterVector{input});+ }+ {+ auto input = std::make_shared<v0::Parameter>(element::f32, Shape{1, 3, 16, 16});+ auto fq1 =+ make_fake_quantize(input, element::f32, p.fq1_levels, {1}, {p.fq1[0]}, {p.fq1[1]}, {p.fq1[2]}, {p.fq1[3]});+ std::shared_ptr<Node> tail = fq1;+ if (!p.expect_eliminate) {+ tail = make_fake_quantize(fq1,+ element::f32,+ p.fq2_levels,+ {1},+ {p.fq2[0]},+ {p.fq2[1]},+ {p.fq2[2]},+ {p.fq2[3]});+ }+ auto abs = std::make_shared<v0::Abs>(tail);+ model_ref = std::make_shared<ov::Model>(OutputVector{abs}, ParameterVector{input});+ }++ manager.register_pass<ov::pass::FakeQuantizeEliminateSequential>();++ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);+ comparator.enable(FunctionsComparator::CmpValues::ACCURACY);+}++const std::vector<SequentialFqParams> sequential_fq_params = {+ {{-1.0f, 1.0f, -1.0f, 1.0f}, 256, {-1.0f, 1.0f, -1.0f, 1.0f}, 256, true, "identical"},+ {{-1.0f, 1.0f, -1.0f, 1.0f}, 256, {-0.5f, 0.5f, -1.0f, 1.0f}, 256, false, "different_input_range"},+ {{-1.0f, 1.0f, -1.0f, 1.0f}, 256, {-1.0f, 1.0f, -2.0f, 2.0f}, 256, false, "different_output_range"},+ {{-2.0f, 2.0f, -2.0f, 2.0f}, 256, {-2.0f, 2.0f, -2.0f, 2.0f}, 257, false, "different_levels"},+ // Out-of-range scenario from the geekbench_ai model 011: FQ2 range is slightly looser than FQ1,+ // so the ranges differ and FQ2 must be kept.+ {{-17.819f, 4.900f, -17.819f, 4.900f}, 256, {-17.799f, 5.124f, -17.799f, 5.124f}, 256, false, "out_of_range"},+};++INSTANTIATE_TEST_SUITE_P(SequentialFakeQuantize,+ SequentialFakeQuantizeTests,+ testing::ValuesIn(sequential_fq_params),+ SequentialFakeQuantizeTests::get_test_case_name);++// Builds a value-preserving chain of ops on top of "input" and returns the chain tail.+using ChainBuilder = std::function<std::shared_ptr<Node>(const Output<Node>&)>;++struct SequentialFqChainParams {+ ChainBuilder build_chain;+ std::string name;+};++class SequentialFakeQuantizeChainTests : public TransformationTestsF,+ public testing::WithParamInterface<SequentialFqChainParams> {+public:+ static std::string get_test_case_name(testing::TestParamInfo<SequentialFqChainParams> info) {+ return info.param.name;+ }+};++// FQ1 and FQ2 (identical to FQ1) are separated by a value-preserving chain: FQ2 is eliminated while+// the chain is preserved.+TEST_P(SequentialFakeQuantizeChainTests, EliminateThroughChain) {+ const auto& p = GetParam();+ {+ auto input = std::make_shared<v0::Parameter>(element::f32, Shape{1, 3, 16, 16});+ auto fq1 = make_fake_quantize(input, element::f32, 256, {1}, {-1.0f}, {1.0f}, {-1.0f}, {1.0f});+ auto chain = p.build_chain(fq1);+ auto fq2 = make_fake_quantize(chain, element::f32, 256, {1}, {-1.0f}, {1.0f}, {-1.0f}, {1.0f});+ auto abs = std::make_shared<v0::Abs>(fq2);+ model = std::make_shared<ov::Model>(OutputVector{abs}, ParameterVector{input});+ }+ {+ auto input = std::make_shared<v0::Parameter>(element::f32, Shape{1, 3, 16, 16});+ auto fq1 = make_fake_quantize(input, element::f32, 256, {1}, {-1.0f}, {1.0f}, {-1.0f}, {1.0f});+ auto chain = p.build_chain(fq1);+ auto abs = std::make_shared<v0::Abs>(chain);+ model_ref = std::make_shared<ov::Model>(OutputVector{abs}, ParameterVector{input});+ }++ manager.register_pass<ov::pass::FakeQuantizeEliminateSequential>();++ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);+ comparator.enable(FunctionsComparator::CmpValues::ACCURACY);+}++const std::vector<SequentialFqChainParams> sequential_fq_chain_params = {+ {[](const Output<Node>& input) -> std::shared_ptr<Node> {+ auto target_shape = v0::Constant::create(element::i64, Shape{3}, {1, 3, 256});+ return std::make_shared<v1::Reshape>(input, target_shape, false);+ },+ "reshape"},+ {[](const Output<Node>& input) -> std::shared_ptr<Node> {+ auto order = v0::Constant::create(element::i64, Shape{4}, {0, 1, 3, 2});+ return std::make_shared<v1::Transpose>(input, order);+ },+ "transpose"},+ {[](const Output<Node>& input) -> std::shared_ptr<Node> {+ auto target_shape = v0::Constant::create(element::i64, Shape{3}, {1, 3, 256});+ auto reshape = std::make_shared<v1::Reshape>(input, target_shape, false);+ auto order = v0::Constant::create(element::i64, Shape{3}, {0, 2, 1});+ return std::make_shared<v1::Transpose>(reshape, order);+ },+ "reshape_transpose_chain"},+ {[](const Output<Node>& input) -> std::shared_ptr<Node> {+ auto squeeze_axes = v0::Constant::create(element::i64, Shape{1}, {0});+ auto squeeze = std::make_shared<v0::Squeeze>(input, squeeze_axes);+ auto unsqueeze_axes = v0::Constant::create(element::i64, Shape{1}, {0});+ return std::make_shared<v0::Unsqueeze>(squeeze, unsqueeze_axes);+ },+ "squeeze_unsqueeze"},+};++INSTANTIATE_TEST_SUITE_P(SequentialFakeQuantizeChain,+ SequentialFakeQuantizeChainTests,+ testing::ValuesIn(sequential_fq_chain_params),+ SequentialFakeQuantizeChainTests::get_test_case_name);++// The intermediate Reshape feeds more than one consumer. Eliminating FQ2 only rewires FQ2's own+// output, so the other consumer is unaffected and the transformation still fires.+TEST_F(TransformationTestsF, eliminate_when_intermediate_op_has_multiple_consumers) {+ auto target_shape = v0::Constant::create(element::i64, Shape{3}, {1, 3, 256});+ {+ auto input = std::make_shared<v0::Parameter>(element::f32, Shape{1, 3, 16, 16});+ auto fq1 = make_fake_quantize(input, element::f32, 256, {1}, {-1.0f}, {1.0f}, {-1.0f}, {1.0f});+ auto reshape = std::make_shared<v1::Reshape>(fq1, target_shape, false);+ auto fq2 = make_fake_quantize(reshape, element::f32, 256, {1}, {-1.0f}, {1.0f}, {-1.0f}, {1.0f});+ auto abs1 = std::make_shared<v0::Abs>(fq2);+ auto abs2 = std::make_shared<v0::Abs>(reshape);+ model = std::make_shared<ov::Model>(OutputVector{abs1, abs2}, ParameterVector{input});+ }+ {+ auto input = std::make_shared<v0::Parameter>(element::f32, Shape{1, 3, 16, 16});+ auto fq1 = make_fake_quantize(input, element::f32, 256, {1}, {-1.0f}, {1.0f}, {-1.0f}, {1.0f});+ auto reshape = std::make_shared<v1::Reshape>(fq1, target_shape, false);+ auto abs1 = std::make_shared<v0::Abs>(reshape);+ auto abs2 = std::make_shared<v0::Abs>(reshape);+ model_ref = std::make_shared<ov::Model>(OutputVector{abs1, abs2}, ParameterVector{input});+ }++ manager.register_pass<ov::pass::FakeQuantizeEliminateSequential>();++ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);+ comparator.enable(FunctionsComparator::CmpValues::ACCURACY);+}++// FQ1 and FQ2 use range constants with equal flattened values but different shapes (per-channel+// {1, 3, 1, 1} vs spatial {3, 1}), so they broadcast differently and are not interchangeable. FQ2+// must be kept: comparing only the flattened values would wrongly treat the two as identical.+TEST_F(TransformationTestsF, keep_sequential_fake_quantize_with_same_values_different_shapes) {+ const std::vector<float> in_low{-1.0f, -2.0f, -3.0f};+ const std::vector<float> in_high{1.0f, 2.0f, 3.0f};+ {+ auto input = std::make_shared<v0::Parameter>(element::f32, Shape{1, 3, 3, 1});+ auto fq1 = make_fake_quantize(input, element::f32, 256, {1, 3, 1, 1}, in_low, in_high, in_low, in_high);+ auto fq2 = make_fake_quantize(fq1, element::f32, 256, {3, 1}, in_low, in_high, in_low, in_high);+ auto abs = std::make_shared<v0::Abs>(fq2);+ model = std::make_shared<ov::Model>(OutputVector{abs}, ParameterVector{input});+ }++ manager.register_pass<ov::pass::FakeQuantizeEliminateSequential>();++ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);+ comparator.enable(FunctionsComparator::CmpValues::ACCURACY);+}++// FQ1 and FQ2 use per-channel ranges (constants broadcast over the channel axis). FQ2 is identical to+// FQ1, so it re-applies the same quantization and is eliminated.+TEST_F(TransformationTestsF, eliminate_sequential_per_channel_fake_quantize) {+ const Shape per_channel_shape{1, 3, 1, 1};+ const std::vector<float> in_low{-1.0f, -2.0f, -3.0f};+ const std::vector<float> in_high{1.0f, 2.0f, 3.0f};+ {+ auto input = std::make_shared<v0::Parameter>(element::f32, Shape{1, 3, 16, 16});+ auto fq1 = make_fake_quantize(input, element::f32, 256, per_channel_shape, in_low, in_high, in_low, in_high);+ auto fq2 = make_fake_quantize(fq1, element::f32, 256, per_channel_shape, in_low, in_high, in_low, in_high);+ auto abs = std::make_shared<v0::Abs>(fq2);+ model = std::make_shared<ov::Model>(OutputVector{abs}, ParameterVector{input});+ }+ {+ auto input = std::make_shared<v0::Parameter>(element::f32, Shape{1, 3, 16, 16});+ auto fq1 = make_fake_quantize(input, element::f32, 256, per_channel_shape, in_low, in_high, in_low, in_high);+ auto abs = std::make_shared<v0::Abs>(fq1);+ model_ref = std::make_shared<ov::Model>(OutputVector{abs}, ParameterVector{input});+ }++ manager.register_pass<ov::pass::FakeQuantizeEliminateSequential>();++ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);+ comparator.enable(FunctionsComparator::CmpValues::ACCURACY);+}++// FQ1 feeds several consumers that are all identical to it: every one re-applies FQ1's quantization+// and is eliminated, leaving the consumers connected directly to FQ1.+TEST_F(TransformationTestsF, eliminate_when_fq1_has_several_identical_consumers) {+ {+ auto input = std::make_shared<v0::Parameter>(element::f32, Shape{1, 3, 16, 16});+ auto fq1 = make_fake_quantize(input, element::f32, 256, {1}, {-1.0f}, {1.0f}, {-1.0f}, {1.0f});+ auto fq2_a = make_fake_quantize(fq1, element::f32, 256, {1}, {-1.0f}, {1.0f}, {-1.0f}, {1.0f});+ auto fq2_b = make_fake_quantize(fq1, element::f32, 256, {1}, {-1.0f}, {1.0f}, {-1.0f}, {1.0f});+ auto abs_a = std::make_shared<v0::Abs>(fq2_a);+ auto abs_b = std::make_shared<v0::Abs>(fq2_b);+ model = std::make_shared<ov::Model>(OutputVector{abs_a, abs_b}, ParameterVector{input});+ }+ {+ auto input = std::make_shared<v0::Parameter>(element::f32, Shape{1, 3, 16, 16});+ auto fq1 = make_fake_quantize(input, element::f32, 256, {1}, {-1.0f}, {1.0f}, {-1.0f}, {1.0f});+ auto abs_a = std::make_shared<v0::Abs>(fq1);+ auto abs_b = std::make_shared<v0::Abs>(fq1);+ model_ref = std::make_shared<ov::Model>(OutputVector{abs_a, abs_b}, ParameterVector{input});+ }++ manager.register_pass<ov::pass::FakeQuantizeEliminateSequential>();++ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);+ comparator.enable(FunctionsComparator::CmpValues::ACCURACY);+}++// FQ1 branches into an identical FakeQuantize (eliminated) and a different one (kept): only the+// redundant branch is folded.+TEST_F(TransformationTestsF, eliminate_only_identical_consumer_when_fq1_branches) {+ {+ auto input = std::make_shared<v0::Parameter>(element::f32, Shape{1, 3, 16, 16});+ auto fq1 = make_fake_quantize(input, element::f32, 256, {1}, {-1.0f}, {1.0f}, {-1.0f}, {1.0f});+ auto fq_same = make_fake_quantize(fq1, element::f32, 256, {1}, {-1.0f}, {1.0f}, {-1.0f}, {1.0f});+ auto fq_diff = make_fake_quantize(fq1, element::f32, 256, {1}, {-2.0f}, {2.0f}, {-2.0f}, {2.0f});+ auto abs_same = std::make_shared<v0::Abs>(fq_same);+ auto abs_diff = std::make_shared<v0::Abs>(fq_diff);+ model = std::make_shared<ov::Model>(OutputVector{abs_same, abs_diff}, ParameterVector{input});+ }+ {+ auto input = std::make_shared<v0::Parameter>(element::f32, Shape{1, 3, 16, 16});+ auto fq1 = make_fake_quantize(input, element::f32, 256, {1}, {-1.0f}, {1.0f}, {-1.0f}, {1.0f});+ auto fq_diff = make_fake_quantize(fq1, element::f32, 256, {1}, {-2.0f}, {2.0f}, {-2.0f}, {2.0f});+ auto abs_same = std::make_shared<v0::Abs>(fq1);+ auto abs_diff = std::make_shared<v0::Abs>(fq_diff);+ model_ref = std::make_shared<ov::Model>(OutputVector{abs_same, abs_diff}, ParameterVector{input});+ }++ manager.register_pass<ov::pass::FakeQuantizeEliminateSequential>();++ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);+ comparator.enable(FunctionsComparator::CmpValues::ACCURACY);+}++// FQ1 branches into an identical FakeQuantize (eliminated) and a direct non-FakeQuantize consumer+// (left intact).+TEST_F(TransformationTestsF, eliminate_identical_consumer_with_direct_branch) {+ {+ auto input = std::make_shared<v0::Parameter>(element::f32, Shape{1, 3, 16, 16});+ auto fq1 = make_fake_quantize(input, element::f32, 256, {1}, {-1.0f}, {1.0f}, {-1.0f}, {1.0f});+ auto fq2 = make_fake_quantize(fq1, element::f32, 256, {1}, {-1.0f}, {1.0f}, {-1.0f}, {1.0f});+ auto abs_fq = std::make_shared<v0::Abs>(fq2);+ auto abs_direct = std::make_shared<v0::Abs>(fq1);+ model = std::make_shared<ov::Model>(OutputVector{abs_fq, abs_direct}, ParameterVector{input});+ }+ {+ auto input = std::make_shared<v0::Parameter>(element::f32, Shape{1, 3, 16, 16});+ auto fq1 = make_fake_quantize(input, element::f32, 256, {1}, {-1.0f}, {1.0f}, {-1.0f}, {1.0f});+ auto abs_fq = std::make_shared<v0::Abs>(fq1);+ auto abs_direct = std::make_shared<v0::Abs>(fq1);+ model_ref = std::make_shared<ov::Model>(OutputVector{abs_fq, abs_direct}, ParameterVector{input});+ }++ manager.register_pass<ov::pass::FakeQuantizeEliminateSequential>();++ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);+ comparator.enable(FunctionsComparator::CmpValues::ACCURACY);+}