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] Fix `TransposeFQ` to only fire when FQ consumer is a Transpose (#36454)
Aleksandr Voron351f38886a47
diff --git a/src/common/transformations/include/transformations/utils/utils.hpp b/src/common/transformations/include/transformations/utils/utils.hppindex 9e8b0a6592d..259ba59c1ff 100644--- a/src/common/transformations/include/transformations/utils/utils.hpp+++ b/src/common/transformations/include/transformations/utils/utils.hpp@@ -156,6 +156,8 @@ TRANSFORMATIONS_API bool get_single_value(const std::shared_ptr<ov::op::v0::Consfloat& value,bool check_value_range = true);+TRANSFORMATIONS_API bool fq_ranges_are_equal(const std::shared_ptr<const ov::Node>& fq);+TRANSFORMATIONS_API std::shared_ptr<Node> normalize_constant(const std::shared_ptr<ov::op::v0::Constant>& constant,const PartialShape& shape);diff --git a/src/common/transformations/src/transformations/common_optimizations/transpose_sinking.cpp b/src/common/transformations/src/transformations/common_optimizations/transpose_sinking.cppindex 60a667a72ac..2c3b3810f97 100644--- a/src/common/transformations/src/transformations/common_optimizations/transpose_sinking.cpp+++ b/src/common/transformations/src/transformations/common_optimizations/transpose_sinking.cpp@@ -100,6 +100,11 @@ ov::pass::TransposeFQ::TransposeFQ() {if (!transpose_order || !fq)return false;+ // Sink only when the FakeQuantize represents both quantize and dequantize:+ // input_low == output_low and input_high == output_high+ if (!op_util::fq_ranges_are_equal(fq))+ return false;+ov::NodeVector new_ops;const auto& reverse_order_constant = get_reversed_order_constant(transpose_order);diff --git a/src/common/transformations/src/transformations/utils/utils.cpp b/src/common/transformations/src/transformations/utils/utils.cppindex 30b0ee8351b..7dcd90d0bec 100644--- a/src/common/transformations/src/transformations/utils/utils.cpp+++ b/src/common/transformations/src/transformations/utils/utils.cpp@@ -12,13 +12,16 @@#include <vector>#include "openvino/core/validation_util.hpp"+#include "openvino/op/abs.hpp"#include "openvino/op/add.hpp"#include "openvino/op/broadcast.hpp"#include "openvino/op/concat.hpp"#include "openvino/op/constant.hpp"#include "openvino/op/convert.hpp"#include "openvino/op/divide.hpp"+#include "openvino/op/fake_quantize.hpp"#include "openvino/op/gather.hpp"+#include "openvino/op/less.hpp"#include "openvino/op/multiply.hpp"#include "openvino/op/paged_attention.hpp"#include "openvino/op/parameter.hpp"@@ -139,6 +142,27 @@ bool get_single_value(const std::shared_ptr<op::v0::Constant>& const_node, float}}+bool fq_ranges_are_equal(const std::shared_ptr<const ov::Node>& fq) {+ if (!ov::is_type<op::v0::FakeQuantize>(fq))+ return false;++ auto equal_with_threshold = [](const ov::Output<ov::Node>& val1, const ov::Output<ov::Node>& val2) {+ auto diff = std::make_shared<op::v1::Subtract>(val1, val2);+ auto abs_diff = std::make_shared<op::v0::Abs>(diff);+ auto eps = op::v0::Constant::create(val1.get_element_type(), {}, {1e-6f});+ auto is_less = ov::util::get_constant_from_source(std::make_shared<op::v1::Less>(abs_diff, eps));+ if (!is_less)+ return false;+ const auto v = is_less->get_vector<bool>();+ return std::all_of(v.begin(), v.end(), [](bool b) {+ return b;+ });+ };++ return equal_with_threshold(fq->input_value(1), fq->input_value(3)) &&+ equal_with_threshold(fq->input_value(2), fq->input_value(4));+}+std::shared_ptr<Node> normalize_constant(const std::shared_ptr<op::v0::Constant>& constant, const PartialShape& shape) {auto const_shape = constant->get_shape();if (static_cast<int64_t>(const_shape.size()) == shape.rank().get_length()) {diff --git a/src/common/transformations/tests/common_optimizations/transpose_sinking_test.cpp b/src/common/transformations/tests/common_optimizations/transpose_sinking_test.cppindex 1189cc2b62c..e53218a153a 100644--- a/src/common/transformations/tests/common_optimizations/transpose_sinking_test.cpp+++ b/src/common/transformations/tests/common_optimizations/transpose_sinking_test.cpp@@ -16,6 +16,7 @@#include "openvino/op/add.hpp"#include "openvino/op/convert.hpp"#include "openvino/op/fake_quantize.hpp"+#include "openvino/op/multiply.hpp"#include "openvino/op/reduce_l1.hpp"#include "openvino/op/reduce_l2.hpp"#include "openvino/op/reduce_logical_and.hpp"@@ -233,6 +234,40 @@ INSTANTIATE_TEST_SUITE_P(TransposeSinkingCommon,TransposeFQTransposeFuseParams{{1, 1, 3}, {1, 3, 1, 1}},TransposeFQTransposeFuseParams{{1, 1, 1, 3}, {1, 3, 1, 1}}));+// TransposeFQ must not fire when the FakeQuantize is the Quantize half of a QDQ pair+TEST(TransposeFQNoSinkingTest, TransposeFQNotAppliedForQuantizeLinear) {+ auto input = std::make_shared<opset6::Parameter>(element::f32, PartialShape{1, 3, 4, 5});++ auto order = std::make_shared<opset6::Constant>(element::i64, Shape{4}, std::vector<int64_t>{0, 2, 3, 1});+ auto transpose = std::make_shared<opset6::Transpose>(input, order);++ auto i_low = std::make_shared<opset6::Constant>(element::f32, Shape{}, std::vector<float>{-3.5f});+ auto i_high = std::make_shared<opset6::Constant>(element::f32, Shape{}, std::vector<float>{6.2f});+ auto o_low = std::make_shared<opset6::Constant>(element::f32, Shape{}, std::vector<float>{0.f});+ auto o_high = std::make_shared<opset6::Constant>(element::f32, Shape{}, std::vector<float>{255.f});+ auto fq = std::make_shared<opset6::FakeQuantize>(transpose, i_low, i_high, o_low, o_high, 256);++ auto convert_to_u8 = std::make_shared<opset6::Convert>(fq, element::u8);+ auto convert_to_f32 = std::make_shared<opset6::Convert>(convert_to_u8, element::f32);+ auto zero_point = std::make_shared<opset6::Constant>(element::f32, Shape{}, std::vector<float>{92.f});+ auto subtract = std::make_shared<opset6::Subtract>(convert_to_f32, zero_point);+ auto scale = std::make_shared<opset6::Constant>(element::f32, Shape{}, std::vector<float>{0.038f});+ auto multiply = std::make_shared<opset6::Multiply>(subtract, scale);++ auto model = std::make_shared<ov::Model>(OutputVector{multiply}, ParameterVector{input});+ auto model_ref = model->clone();++ pass::Manager manager;+ manager.register_pass<ov::pass::InitNodeInfo>();+ manager.register_pass<ov::pass::TransposeFQ>();+ manager.run_passes(model);++ auto fc =+ FunctionsComparator::no_default().enable(FunctionsComparator::NODES).enable(FunctionsComparator::PRECISIONS);+ auto res = fc.compare(model, model_ref);+ ASSERT_TRUE(res.valid) << res.message;+}+struct TransposeReduceParams {// given paramsPartialShape transpose_input_shape;