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
Add transformation pipeline to PrePostProcessing (#28852)
Ivan Tikhonov5d0027354f10
diff --git a/src/bindings/python/tests/test_graph/test_preprocess.py b/src/bindings/python/tests/test_graph/test_preprocess.pyindex cb0cc32b3fc..1074c60b973 100644--- a/src/bindings/python/tests/test_graph/test_preprocess.py+++ b/src/bindings/python/tests/test_graph/test_preprocess.py@@ -72,7 +72,8 @@ def test_graph_preprocess_scale_vector():assert list(model.get_output_shape(0)) == [2, 2]assert model.get_output_element_type(0) == Type.f32assert "Constant" in model_operators- assert "Divide" in model_operators+ # Div will be converted to Mul in the transformations+ assert "Multiply" in model_operatorsdef test_graph_preprocess_mean_scale_convert():@@ -95,12 +96,13 @@ def test_graph_preprocess_mean_scale_convert():model = ppp.build()model_operators = [op.get_name().split("_")[0] for op in model.get_ops()]+ # Div will be converted to Mul in the transformationsexpected_ops = ["Parameter","Convert","Constant","Subtract",- "Divide",+ "Multiply","Result","Abs",]@@ -137,12 +139,13 @@ def test_graph_preprocess_input_output_by_name():model = ppp.build()model_operators = [op.get_name().split("_")[0] for op in model.get_ops()]+ # Div will be converted to Mul in the transformationsexpected_ops = ["Parameter","Convert","Constant","Subtract",- "Divide",+ "Multiply","Result","Abs",]@@ -404,7 +407,7 @@ def test_graph_preprocess_steps(algorithm, color_format1, color_format2, is_fail"Gather","Interpolate",]- assert len(model_operators) == 15+ assert len(model_operators) == 12assert model.get_output_size() == 1assert list(model.get_output_shape(0)) == [1, 3, 3, 3]assert model.get_output_element_type(0) == Type.f32@@ -456,10 +459,9 @@ def test_graph_preprocess_postprocess_layout():"Constant","Result","Gather",- "Range","Transpose",]- assert len(model_operators) == 14+ assert len(model_operators) == 11assert model.get_output_size() == 1assert list(model.get_output_shape(0)) == [1, 1, 3, 3]assert model.get_output_element_type(0) == Type.f32@@ -486,9 +488,8 @@ def test_graph_preprocess_reverse_channels():"Constant","Result","Gather",- "Range",]- assert len(model_operators) == 10+ assert len(model_operators) == 7assert model.get_output_size() == 1assert list(model.get_output_shape(0)) == [1, 2, 2, 2]assert model.get_output_element_type(0) == Type.f32@@ -628,6 +629,7 @@ def test_graph_preprocess_model():model = ppp.build()model_operators = [op.get_name().split("_")[0] for op in model.get_ops()]+ # Div will be converted to Mul in the transformationsexpected_ops = ["Parameter","Constant",@@ -636,7 +638,7 @@ def test_graph_preprocess_model():"Convert","Abs","Add",- "Divide",+ "Multiply",]assert len(model_operators) == 13assert model.get_output_size() == 1diff --git a/src/common/transformations/include/transformations/rt_info/dequantization_node.hpp b/src/common/transformations/include/transformations/rt_info/dequantization_node.hppindex 50be44aba3d..a2b9e854e81 100644--- a/src/common/transformations/include/transformations/rt_info/dequantization_node.hpp+++ b/src/common/transformations/include/transformations/rt_info/dequantization_node.hpp@@ -14,6 +14,8 @@ TRANSFORMATIONS_API void mark_as_dequantization_node(const std::shared_ptr<Node>TRANSFORMATIONS_API bool is_dequantization_node(const std::shared_ptr<const Node>& node);+TRANSFORMATIONS_API void unmark_dequantization_node(const std::shared_ptr<Node>& node);+/*** @ingroup ov_runtime_attr_api* @brief DequantizationNode class represents runtime info attribute that marks operationdiff --git a/src/common/transformations/src/transformations/common_optimizations/moc_transformations.cpp b/src/common/transformations/src/transformations/common_optimizations/moc_transformations.cppindex 01a9f4169e7..2ea7f9a02dc 100644--- a/src/common/transformations/src/transformations/common_optimizations/moc_transformations.cpp+++ b/src/common/transformations/src/transformations/common_optimizations/moc_transformations.cpp@@ -131,8 +131,15 @@ bool ov::pass::MOCTransformations::run_on_model(const std::shared_ptr<ov::Model>using namespace ov::pass;REGISTER_PASS(manager, InitNodeInfo)if (m_low_precision_enabled) {- manager.register_pass<ov::pass::MarkDequantization>(- element::TypeVector{ov::element::i8, ov::element::u8, ov::element::i4, ov::element::u4});+ manager.register_pass<ov::pass::MarkDequantization>(element::TypeVector{ov::element::i8,+ ov::element::u8,+ ov::element::i4,+ ov::element::u4,+ ov::element::nf4,+ ov::element::f4e2m1,+ ov::element::f8e4m3,+ ov::element::f8e5m2,+ ov::element::f8e8m0});}if (!m_use_shapes) {manager.register_pass<ov::pass::DisableShapeOfConstantFolding>();diff --git a/src/common/transformations/src/transformations/rt_info/dequantization_node.cpp b/src/common/transformations/src/transformations/rt_info/dequantization_node.cppindex 91ebe224fcc..4e3ef2b3cd9 100644--- a/src/common/transformations/src/transformations/rt_info/dequantization_node.cpp+++ b/src/common/transformations/src/transformations/rt_info/dequantization_node.cpp@@ -9,6 +9,10 @@ void ov::mark_as_dequantization_node(const std::shared_ptr<Node>& node) {rt_info[DequantizationNode::get_type_info_static()] = DequantizationNode();}+void ov::unmark_dequantization_node(const std::shared_ptr<Node>& node) {+ node->get_rt_info().erase(DequantizationNode::get_type_info_static());+}+bool ov::is_dequantization_node(const std::shared_ptr<const Node>& node) {const auto& rt_info = node->get_rt_info();return rt_info.find(DequantizationNode::get_type_info_static()) != rt_info.end();diff --git a/src/common/transformations/tests/common_optimizations/preprocessing_fusion_tests.cpp b/src/common/transformations/tests/common_optimizations/preprocessing_fusion_tests.cppindex 55cc23b730d..df9f3cf0cbc 100644--- a/src/common/transformations/tests/common_optimizations/preprocessing_fusion_tests.cpp+++ b/src/common/transformations/tests/common_optimizations/preprocessing_fusion_tests.cpp@@ -10,6 +10,7 @@#include "common_test_utils/ov_test_utils.hpp"#include "openvino/core/model.hpp"#include "openvino/core/preprocess/pre_post_process.hpp"+#include "openvino/core/validation_util.hpp"#include "openvino/opsets/opset12.hpp"#include "openvino/opsets/opset8.hpp"#include "openvino/pass/constant_folding.hpp"@@ -67,7 +68,7 @@ std::shared_ptr<GroupConvolution> create_group_conv_with_gather(Output<Node> inpConstant::create(element::i64, Shape{order.size()}, order),Constant::create(element::i64, Shape{1}, {0}));return std::make_shared<GroupConvolution>(input,- gather,+ ov::util::get_constant_from_source(gather),ov::Strides{1, 1},ov::CoordinateDiff{0, 0},ov::CoordinateDiff{0, 0},@@ -81,7 +82,7 @@ std::shared_ptr<Convolution> create_conv_with_gather(Output<Node> input,Constant::create(element::i64, Shape{order.size()}, order),Constant::create(element::i64, Shape{1}, {1}));return std::make_shared<Convolution>(input,- gather,+ ov::util::get_constant_from_source(gather),ov::Strides{1, 1},ov::CoordinateDiff{0, 0},ov::CoordinateDiff{0, 0},@@ -132,9 +133,8 @@ TEST_F(TransformationTestsF, RICFusionSimple) {auto conv = create_conv(relu, {6, 3, 3, 3});model = std::make_shared<Model>(NodeVector{conv}, ParameterVector{input});+ // ReverseInputChannelsFusion is expected to be applied inside PrePostProcessingapply_reverse_input_channels(model, {{0, "NCHW"}});-- manager.register_pass<ov::pass::ReverseInputChannelsFusion>();}{@@ -173,9 +173,8 @@ TEST_F(TransformationTestsF, RICFusionHard) {model = std::make_shared<Model>(NodeVector{conv, conv2}, ParameterVector{input, input2});+ // ReverseInputChannelsFusion is expected to be applied inside PrePostProcessingapply_reverse_input_channels(model, {{0, "NCHW"}});-- manager.register_pass<ov::pass::ReverseInputChannelsFusion>();}{auto input = create_param({-1, -1, -1, -1});@@ -232,9 +231,8 @@ TEST_F(TransformationTestsF, RICFusionHardNegativePad12) {model = std::make_shared<Model>(NodeVector{conv, conv2}, ParameterVector{input, input2});+ // ReverseInputChannelsFusion is expected to be applied inside PrePostProcessingapply_reverse_input_channels(model, {{0, "NCHW"}});-- manager.register_pass<ov::pass::ReverseInputChannelsFusion>();}{auto input = create_param({-1, -1, -1, -1});@@ -275,9 +273,8 @@ TEST_F(TransformationTestsF, RICFusionDynamic) {auto conv = create_conv(relu, {6, 3, 3, 3});model = std::make_shared<Model>(NodeVector{conv}, ParameterVector{input});+ // ReverseInputChannelsFusion is expected to be applied inside PrePostProcessingapply_reverse_input_channels(model, {{0, "NCHW"}});-- manager.register_pass<ov::pass::ReverseInputChannelsFusion>();}{@@ -298,15 +295,14 @@ TEST_F(TransformationTestsF, RICFusionEltwise1) {auto conv = create_conv(add, {6, 3, 3, 3});model = std::make_shared<Model>(NodeVector{conv}, ParameterVector{input});+ // ReverseInputChannelsFusion is expected to be applied inside PrePostProcessingapply_reverse_input_channels(model, {{0, "NCHW"}});-- manager.register_pass<ov::pass::ReverseInputChannelsFusion>();}{auto input = create_param({1, 3, 64, 64});auto gather = create_gather(Constant::create(element::f32, Shape{3, 1, 1}, {0.1, 0.2, 0.3}), {2, 1, 0}, 0);- auto add = std::make_shared<Add>(input, gather);+ auto add = std::make_shared<Add>(input, ov::util::get_constant_from_source(gather));auto conv = create_conv_with_gather(add, {6, 3, 3, 3}, {2, 1, 0});model_ref = std::make_shared<Model>(NodeVector{conv}, ParameterVector{input});}@@ -323,9 +319,8 @@ TEST_F(TransformationTestsF, RICFusionEltwise2) {auto conv = create_conv(add, {6, 3, 3, 3});model = std::make_shared<Model>(NodeVector{conv}, ParameterVector{input});+ // ReverseInputChannelsFusion is expected to be applied inside PrePostProcessingapply_reverse_input_channels(model, {{0, "NCHW"}});-- manager.register_pass<ov::pass::ReverseInputChannelsFusion>();}{@@ -347,9 +342,8 @@ TEST_F(TransformationTestsF, RICFusionEltwise3) {auto conv = create_conv(add, {6, 3, 3, 3});model = std::make_shared<Model>(NodeVector{conv}, ParameterVector{input});+ // ReverseInputChannelsFusion is expected to be applied inside PrePostProcessingapply_reverse_input_channels(model, {{0, "NCHW"}});-- manager.register_pass<ov::pass::ReverseInputChannelsFusion>();}{@@ -371,15 +365,14 @@ TEST_F(TransformationTestsF, RICFusionEltwise4) {auto conv = create_conv(add, {6, 3, 3, 3});model = std::make_shared<Model>(NodeVector{conv}, ParameterVector{input});+ // ReverseInputChannelsFusion is expected to be applied inside PrePostProcessingapply_reverse_input_channels(model, {{0, "NCHW"}});-- manager.register_pass<ov::pass::ReverseInputChannelsFusion>();}{auto input = create_param({1, 3, 64, 64});auto gather = create_gather(create_weights({3, 1, 1}), {2, 1, 0}, 0);- auto add = std::make_shared<Add>(gather, input);+ auto add = std::make_shared<Add>(ov::util::get_constant_from_source(gather), input);auto conv = create_conv_with_gather(add, {6, 3, 3, 3}, {2, 1, 0});model_ref = std::make_shared<Model>(NodeVector{conv}, ParameterVector{input});}@@ -396,15 +389,14 @@ TEST_F(TransformationTestsF, RICFusionEltwise5) {auto conv = create_conv(add, {6, 3, 3, 3});model = std::make_shared<Model>(NodeVector{conv}, ParameterVector{input});+ // ReverseInputChannelsFusion is expected to be applied inside PrePostProcessingapply_reverse_input_channels(model, {{0, "NCHW"}});-- manager.register_pass<ov::pass::ReverseInputChannelsFusion>();}{auto input = create_param({1, 3, 64, 64});auto gather = create_gather(create_weights({1, 3, 1, 1}), {2, 1, 0}, 1);- auto add = std::make_shared<Add>(gather, input);+ auto add = std::make_shared<Add>(ov::util::get_constant_from_source(gather), input);auto conv = create_conv_with_gather(add, {6, 3, 3, 3}, {2, 1, 0});model_ref = std::make_shared<Model>(NodeVector{conv}, ParameterVector{input});}@@ -422,9 +414,8 @@ TEST_F(TransformationTestsF, RICFusionEltwiseNegative) {auto conv = create_conv(add, {6, 3, 3, 3});model = std::make_shared<Model>(NodeVector{conv}, ParameterVector{input, input2});+ // ReverseInputChannelsFusion is expected to be applied inside PrePostProcessingapply_reverse_input_channels(model, {{0, "NCHW"}});-- manager.register_pass<ov::pass::ReverseInputChannelsFusion>();}}@@ -436,9 +427,8 @@ TEST_F(TransformationTestsF, RICFusionEltwiseTwoRIC) {auto conv = create_conv(add, {6, 3, 3, 3});model = std::make_shared<Model>(NodeVector{conv}, ParameterVector{input, input2});+ // ReverseInputChannelsFusion is expected to be applied inside PrePostProcessingapply_reverse_input_channels(model, {{0, "NCHW"}, {1, "NCHW"}});-- manager.register_pass<ov::pass::ReverseInputChannelsFusion>();}{auto input = create_param({1, 3, 64, 64});@@ -460,9 +450,8 @@ TEST_F(TransformationTestsF, RICFusionEltwiseNegative3) {auto shapeof = std::make_shared<ShapeOf>(add);model = std::make_shared<Model>(NodeVector{shapeof}, ParameterVector{input});+ // ReverseInputChannelsFusion is expected to be applied inside PrePostProcessingapply_reverse_input_channels(model, {{0, "NCHW"}});-- manager.register_pass<ov::pass::ReverseInputChannelsFusion>();}}@@ -474,9 +463,8 @@ TEST_F(TransformationTestsF, RICFusionGroupConv) {auto conv = create_conv(relu, {3, 6, 3, 3});model = std::make_shared<Model>(NodeVector{conv}, ParameterVector{input});+ // ReverseInputChannelsFusion is expected to be applied inside PrePostProcessingapply_reverse_input_channels(model, {{0, "NCHW"}});-- manager.register_pass<ov::pass::ReverseInputChannelsFusion>();}{auto input = create_param({1, 3, 64, 64});@@ -501,9 +489,8 @@ TEST_F(TransformationTestsF, RICFusionGroupConvNegative) {auto conv = create_conv(relu, {6, 3, 3, 3});model = std::make_shared<Model>(NodeVector{conv}, ParameterVector{input});+ // ReverseInputChannelsFusion is expected to be applied inside PrePostProcessingapply_reverse_input_channels(model, {{0, "NCHW"}});-- manager.register_pass<ov::pass::ReverseInputChannelsFusion>();}}@@ -515,15 +502,14 @@ TEST_F(TransformationTestsF, RICFusionTranspose) {auto conv = create_conv(transpose, {6, 3, 3, 3});model = std::make_shared<Model>(NodeVector{conv}, ParameterVector{input});+ // ReverseInputChannelsFusion is expected to be applied inside PrePostProcessingapply_reverse_input_channels(model, {{0, "NHWC"}});-- manager.register_pass<ov::pass::ReverseInputChannelsFusion>();}{auto input = create_param({1, 64, 64, 3});auto gather = create_gather(create_weights({3}), {2, 1, 0}, 0);- auto add = std::make_shared<Add>(input, gather);+ auto add = std::make_shared<Add>(input, ov::util::get_constant_from_source(gather));auto transpose = std::make_shared<Transpose>(add, Constant::create(element::i64, Shape{4}, {0, 3, 1, 2}));auto conv = create_conv_with_gather(transpose, {6, 3, 3, 3}, {2, 1, 0});model_ref = std::make_shared<Model>(NodeVector{conv}, ParameterVector{input});@@ -541,15 +527,15 @@ TEST_F(TransformationTestsF, RICFusionFQOnTheWay) {auto conv = create_conv(fq, create_fq(create_weights({6, 3, 3, 3})));model = std::make_shared<Model>(NodeVector{conv}, ParameterVector{input});+ // ReverseInputChannelsFusion is expected to be applied inside PrePostProcessingapply_reverse_input_channels(model, {{0, "NCHW"}});-- manager.register_pass<ov::pass::ReverseInputChannelsFusion>();}{auto input = create_param({1, 3, 64, 64});auto fq = create_fq(input);- auto conv = create_conv(fq, create_fq(create_gather(create_weights({6, 3, 3, 3}), {2, 1, 0}, 1)));+ auto weights = ov::util::get_constant_from_source(create_gather(create_weights({6, 3, 3, 3}), {2, 1, 0}, 1));+ auto conv = create_conv(fq, create_fq(weights));model_ref = std::make_shared<Model>(NodeVector{conv}, ParameterVector{input});}@@ -573,21 +559,21 @@ TEST_F(TransformationTestsF, RICFusionFQOnTheWay2) {auto conv = create_conv(fq, fq_weights);model = std::make_shared<Model>(NodeVector{conv}, ParameterVector{input});+ // ReverseInputChannelsFusion is expected to be applied inside PrePostProcessingapply_reverse_input_channels(model, {{0, "NCHW"}});-- manager.register_pass<ov::pass::ReverseInputChannelsFusion>();}{auto input = create_param({1, 3, 64, 64});auto fq = create_fq(input);auto weights_const = create_weights({6, 3, 3, 3});- auto fq_weights = std::make_shared<FakeQuantize>(create_gather(weights_const, {2, 1, 0}, 1),- create_gather(create_weights({1, 3, 1, 1}), {2, 1, 0}, 1),- create_weights({1, 1, 1}),- create_weights({1}),- create_gather(create_weights({3, 1, 1}), {2, 1, 0}, 0),- 255);+ auto fq_weights = std::make_shared<FakeQuantize>(+ ov::util::get_constant_from_source(create_gather(weights_const, {2, 1, 0}, 1)),+ ov::util::get_constant_from_source(create_gather(create_weights({1, 3, 1, 1}), {2, 1, 0}, 1)),+ create_weights({1, 1, 1}),+ create_weights({1}),+ ov::util::get_constant_from_source(create_gather(create_weights({3, 1, 1}), {2, 1, 0}, 0)),+ 255);auto conv = create_conv(fq, fq_weights);model_ref = std::make_shared<Model>(NodeVector{conv}, ParameterVector{input});@@ -613,21 +599,21 @@ TEST_F(TransformationTestsF, RICFusionFQOnTheWay3) {auto conv = create_conv(gconv, {6, 3, 1, 1});model = std::make_shared<Model>(NodeVector{conv}, ParameterVector{input});+ // ReverseInputChannelsFusion is expected to be applied inside PrePostProcessingapply_reverse_input_channels(model, {{0, "NCHW"}});-- manager.register_pass<ov::pass::ReverseInputChannelsFusion>();}{auto input = create_param({1, 3, 64, 64});auto fq = create_fq(input);auto weights_const = create_weights({3, 1, 1, 3, 3});- auto fq_weights = std::make_shared<FakeQuantize>(create_gather(weights_const, {2, 1, 0}, 0),- create_gather(create_weights({3, 1, 1, 1, 1}), {2, 1, 0}, 0),- create_weights({1, 1, 1}),- create_weights({1}),- create_weights({1}),- 255);+ auto fq_weights = std::make_shared<FakeQuantize>(+ ov::util::get_constant_from_source(create_gather(weights_const, {2, 1, 0}, 0)),+ ov::util::get_constant_from_source(create_gather(create_weights({3, 1, 1, 1, 1}), {2, 1, 0}, 0)),+ create_weights({1, 1, 1}),+ create_weights({1}),+ create_weights({1}),+ 255);auto gconv = create_group_conv(fq, fq_weights);auto conv = create_conv_with_gather(gconv, {6, 3, 1, 1}, {2, 1, 0});@@ -646,9 +632,8 @@ TEST_F(TransformationTestsF, RICFusionShapeOf) {auto shape_of = std::make_shared<ShapeOf>(relu);model = std::make_shared<Model>(NodeVector{shape_of}, ParameterVector{input});+ // ReverseInputChannelsFusion is expected to be applied inside PrePostProcessingapply_reverse_input_channels(model, {{0, "NCHW"}});-- manager.register_pass<ov::pass::ReverseInputChannelsFusion>();}{@@ -889,9 +874,9 @@ TEST_F(TransformationTestsF, RICFusionConvertMultiply) {CoordinateDiff{0, 0},Strides{1, 1});model = std::make_shared<ov::Model>(conv, ParameterVector{parameter});+ // ReverseInputChannelsFusion is expected to be applied inside PrePostProcessingapply_reverse_input_channels(model, {{0, "NCHW"}});}- manager.register_pass<ov::pass::ReverseInputChannelsFusion>();{auto parameter = std::make_shared<opset8::Parameter>(element::f32, Shape{1, 3, 14, 14});std::shared_ptr<Node> activations =@@ -912,7 +897,7 @@ TEST_F(TransformationTestsF, RICFusionConvertMultiply) {std::shared_ptr<Node> weights = opset8::Constant::create(element::i8, Shape{4, 3, 1, 1}, {-2});{auto scale = opset8::Constant::create(element::f32, Shape{}, {0.2});- auto gather = create_gather(weights, {2, 1, 0}, 1);+ auto gather = ov::util::get_constant_from_source(create_gather(weights, {2, 1, 0}, 1));auto convert = std::make_shared<opset8::Convert>(gather, element::f32);auto multiply = std::make_shared<opset8::Multiply>(convert, scale);weights = multiply;@@ -947,16 +932,16 @@ TEST_F(TransformationTestsF, RICFusionConvertMultiplyGroupConv) {auto relu = std::make_shared<Relu>(group_conv);auto conv = create_conv(relu, {6, 9, 3, 3});model = std::make_shared<Model>(NodeVector{conv}, ParameterVector{data});+ // ReverseInputChannelsFusion is expected to be applied inside PrePostProcessingapply_reverse_input_channels(model, {{0, "NCHW"}});}- manager.register_pass<ov::pass::ReverseInputChannelsFusion>();{auto data = std::make_shared<opset8::Parameter>(element::f32, data_shape);std::shared_ptr<Node> weights = opset8::Constant::create(element::f32, Shape{3, 3, 1, 4, 4}, {-2});auto gather = create_gather(weights, {2, 1, 0}, 1);auto convert = std::make_shared<opset8::Convert>(gather, element::f32);auto scale = opset8::Constant::create(element::f32, Shape{}, {0.2});- auto multiply = std::make_shared<opset8::Multiply>(convert, scale);+ auto multiply = ov::util::get_constant_from_source(std::make_shared<opset8::Multiply>(convert, scale));auto group_conv = std::make_shared<opset8::GroupConvolution>(data,multiply,@@ -967,7 +952,7 @@ TEST_F(TransformationTestsF, RICFusionConvertMultiplyGroupConv) {op::PadType::EXPLICIT);auto relu = std::make_shared<Relu>(group_conv);std::shared_ptr<Node> weights2 = opset8::Constant::create(element::f32, Shape{6, 9, 3, 3}, {-2});- auto gather2 = create_gather(weights2, {6, 7, 8, 3, 4, 5, 0, 1, 2}, 1);+ auto gather2 = ov::util::get_constant_from_source(create_gather(weights2, {6, 7, 8, 3, 4, 5, 0, 1, 2}, 1));auto conv = std::make_shared<opset8::Convolution>(relu,gather2,ov::Strides{1, 1},@@ -1012,9 +997,9 @@ TEST_F(TransformationTestsF, RICFusionConvertMultiplyNegative1) {CoordinateDiff{0, 0},Strides{1, 1});model = std::make_shared<ov::Model>(conv, ParameterVector{parameter});+ // ReverseInputChannelsFusion is expected to be applied inside PrePostProcessingapply_reverse_input_channels(model, {{0, "NCHW"}});}- manager.register_pass<ov::pass::ReverseInputChannelsFusion>();{auto parameter = std::make_shared<opset8::Parameter>(element::f32, Shape{1, 3, 14, 14});std::shared_ptr<Node> activations =@@ -1034,7 +1019,7 @@ TEST_F(TransformationTestsF, RICFusionConvertMultiplyNegative1) {std::shared_ptr<Node> weights = opset8::Constant::create(element::i8, Shape{4, 3, 1, 1}, {-2});{- auto gather = create_gather(weights, {2, 1, 0}, 1);+ auto gather = ov::util::get_constant_from_source(create_gather(weights, {2, 1, 0}, 1));auto convert = std::make_shared<opset8::Convert>(gather, element::f32);auto scale = opset8::Constant::create(element::f32, Shape{1, 1, 1, 1}, {0.2});auto multiply = std::make_shared<opset8::Multiply>(convert, scale);@@ -1085,9 +1070,9 @@ TEST_F(TransformationTestsF, RICFusionConvertMultiplyNegativeBroadcast) {CoordinateDiff{0, 0},Strides{1, 1});model = std::make_shared<ov::Model>(conv, ParameterVector{parameter});+ // ReverseInputChannelsFusion is expected to be applied inside PrePostProcessingapply_reverse_input_channels(model, {{0, "NCHW"}});}- manager.register_pass<ov::pass::ReverseInputChannelsFusion>();{auto parameter = std::make_shared<opset8::Parameter>(element::f32, Shape{1, 3, 14, 14});std::shared_ptr<Node> activations =@@ -1107,10 +1092,10 @@ TEST_F(TransformationTestsF, RICFusionConvertMultiplyNegativeBroadcast) {std::shared_ptr<Node> weights = opset8::Constant::create(element::i8, Shape{3, 1, 1}, {-2});{- auto gather = create_gather(weights, {2, 1, 0}, 0);+ auto gather = ov::util::get_constant_from_source(create_gather(weights, {2, 1, 0}, 0));auto convert = std::make_shared<opset8::Convert>(gather, element::f32);auto scale = opset8::Constant::create(element::f32, Shape{4, 3, 1, 1}, {0.2});- auto gather2 = create_gather(scale, {2, 1, 0}, 1);+ auto gather2 = ov::util::get_constant_from_source(create_gather(scale, {2, 1, 0}, 1));auto multiply = std::make_shared<opset8::Multiply>(convert, gather2);weights = multiply;}@@ -1184,16 +1169,16 @@ TEST_F(TransformationTestsF, RICFusionConvertMultiplyNonScalarFQInput) {CoordinateDiff{0, 0},Strides{1, 1});model = std::make_shared<ov::Model>(conv, ParameterVector{parameter});+ // ReverseInputChannelsFusion is expected to be applied inside PrePostProcessingapply_reverse_input_channels(model, {{0, "NCHW"}});}- manager.register_pass<ov::pass::ReverseInputChannelsFusion>();{auto parameter = std::make_shared<opset8::Parameter>(element::f32, Shape{1, 3, 14, 14});auto gather =create_gather(std::make_shared<opset8::Constant>(element::f32, Shape{1, 3, 14, 14}), {2, 1, 0}, 1);std::shared_ptr<Node> activations =std::make_shared<opset8::FakeQuantize>(parameter,- gather,+ ov::util::get_constant_from_source(gather),opset8::Constant::create(element::f32, Shape{}, {20}),opset8::Constant::create(element::f32, Shape{}, {0}),opset8::Constant::create(element::f32, Shape{}, {254}),@@ -1209,7 +1194,7 @@ TEST_F(TransformationTestsF, RICFusionConvertMultiplyNonScalarFQInput) {std::shared_ptr<Node> weights = opset8::Constant::create(element::i8, Shape{4, 3, 1, 1}, {-2});{auto scale = opset8::Constant::create(element::f32, Shape{}, {0.2});- gather = create_gather(weights, {2, 1, 0}, 1);+ auto gather = ov::util::get_constant_from_source(create_gather(weights, {2, 1, 0}, 1));auto convert = std::make_shared<opset8::Convert>(gather, element::f32);auto multiply = std::make_shared<opset8::Multiply>(convert, scale);weights = multiply;@@ -1270,9 +1255,8 @@ TEST_F(TransformationTestsF, RICFusionTwoConvolutions) {auto conv1 = create_conv(input, create_weights({3, 3, 1, 1}));auto conv2 = create_conv(conv1, create_weights({3, 3, 1, 1}));model = std::make_shared<Model>(NodeVector{conv2}, ParameterVector{input});+ // ReverseInputChannelsFusion is expected to be applied inside PrePostProcessingapply_reverse_input_channels(model, {{0, "NCHW"}});-- manager.register_pass<ov::pass::ReverseInputChannelsFusion>();}{auto conv1_with_gather = create_conv_with_gather(input, create_weights({3, 3, 1, 1}), {2, 1, 0});@@ -1289,9 +1273,9 @@ TEST_F(TransformationTestsF, RICFusionTwoConvolutionsTheSameWeights) {auto conv1 = create_conv(input, weights);auto conv2 = create_conv(conv1, weights);model = std::make_shared<Model>(NodeVector{conv2}, ParameterVector{input});- apply_reverse_input_channels(model, {{0, "NCHW"}});- manager.register_pass<ov::pass::ReverseInputChannelsFusion>();+ // ReverseInputChannelsFusion is expected to be applied inside PrePostProcessing+ apply_reverse_input_channels(model, {{0, "NCHW"}});}{auto conv1_with_gather = create_conv_with_gather(input, weights, {2, 1, 0});diff --git a/src/core/src/pass/constant_folding.cpp b/src/core/src/pass/constant_folding.cppindex ae8d74e737c..467898e9f6c 100644--- a/src/core/src/pass/constant_folding.cpp+++ b/src/core/src/pass/constant_folding.cpp@@ -14,6 +14,8 @@#include "openvino/op/util/read_value_base.hpp"#include "openvino/op/util/shape_of_base.hpp"#include "openvino/op/util/sub_graph_base.hpp"+#include "transformations/rt_info/decompression.hpp"+#include "transformations/rt_info/dequantization_node.hpp"/*** \brief Check if \ref ov::Output<ov::Node> can be folded base on `can_be_folded` attribute.@@ -51,7 +53,7 @@ const auto friendly_name_from = [](const ov::Node& node, const size_t output_coustatic bool restore_original_input_precision(const std::shared_ptr<ov::Node>& node) {bool restored = false;- if (ov::is_type<ov::op::v0::Convert>(node)) {+ if (ov::is_type<ov::op::v0::Convert>(node) && !is_decompression(node) && !is_dequantization_node(node)) {auto input = node->input(0);ov::util::remove_original_input_precision_attribute(input);return restored;diff --git a/src/core/src/preprocess/pre_post_process.cpp b/src/core/src/preprocess/pre_post_process.cppindex a74eb8bc5ad..32c22eae779 100644--- a/src/core/src/preprocess/pre_post_process.cpp+++ b/src/core/src/preprocess/pre_post_process.cpp@@ -6,9 +6,102 @@#include "color_utils.hpp"#include "function_guard.hpp"+#include "itt.hpp"#include "layout_utils.hpp"#include "openvino/core/model.hpp"+#include "openvino/pass/constant_folding.hpp"+#include "openvino/pass/manager.hpp"+#include "openvino/pass/pattern/op/true.hpp"#include "preprocess_impls.hpp"+#include "transformations/common_optimizations/convolution_to_group_convolution_fusion.hpp"+#include "transformations/common_optimizations/disable_random_uniform_constant_folding.hpp"+#include "transformations/common_optimizations/disable_shapeof_constant_folding.hpp"+#include "transformations/common_optimizations/mul_conv_fusion.hpp"+#include "transformations/common_optimizations/ric_fusion.hpp"+#include "transformations/fp16_compression/mark_decompression_convert_constant_folding.hpp"+#include "transformations/low_precision/mark_dequantization_subgraph.hpp"+#include "transformations/op_conversions/convert_divide.hpp"+#include "transformations/rt_info/dequantization_node.hpp"+#include "transformations/utils/utils.hpp"++namespace {++struct RTInfoCache {+ template <typename Func>+ void traverse(const std::shared_ptr<ov::Model>& model, Func&& func) {+ for (const auto& op : model->get_ordered_ops()) {+ func(op);+ if (const auto& multi_subgraph_op = ov::as_type_ptr<ov::op::util::MultiSubGraphOp>(op)) {+ for (const auto& sub_graph : multi_subgraph_op->get_functions()) {+ if (sub_graph)+ traverse(sub_graph, func);+ }+ }+ }+ }++ void store(const std::shared_ptr<ov::Model>& model) {+ traverse(model, [this](const std::shared_ptr<ov::Node>& op) {+ m_rt_info_cache[op.get()] = op->get_rt_info();+ });+ }++ void restore(const std::shared_ptr<ov::Model>& model) {+ traverse(model, [this](const std::shared_ptr<ov::Node>& op) {+ auto it = m_rt_info_cache.find(op.get());+ if (it != m_rt_info_cache.end()) {+ op->get_rt_info() = it->second;+ } else {+ ov::pass::enable_constant_folding(op);+ ov::unmark_dequantization_node(op);+ ov::unmark_as_decompression(op);+ }+ });+ }++ std::unordered_map<ov::Node*, ov::RTMap> m_rt_info_cache;+};++void transformation_pipeline(std::shared_ptr<ov::Model>& model) {+ using namespace ov;+ using namespace ov::pass;+ using namespace ov::element;++ // 0. Store RT info to not affect plugin compilation+ RTInfoCache rt_info_cache;+ rt_info_cache.store(model);++ Manager manager("pre_post_processing");+ manager.set_per_pass_validation(false);++ // 1. Set "disable_const_folding" attribute+ REGISTER_PASS(manager, MarkDequantization, TypeVector{i8, u8, i4, u4, nf4, f4e2m1, f8e4m3, f8e5m2, f8e8m0});+ REGISTER_PASS(manager, DisableShapeOfConstantFolding, false);+ REGISTER_PASS(manager, DisableRandomUniformConstantFolding)+ // Mark quantized and f16/bf16 compressed constants to prevent CF for them,+ // so that not extra memory is used for intermediate decompressed constants.+ REGISTER_PASS(manager, MarkCompressedFloatConstants);+ REGISTER_PASS(manager, DisableDecompressionConvertConstantFolding);++ // 2. Fusion transformations:+ REGISTER_PASS(manager, ConvertDivideWithConstant)+ auto multiply_fusions = manager.register_pass<GraphRewrite>();+ ADD_MATCHER(multiply_fusions, MultiplyConvolutionFusion)+ ADD_MATCHER(multiply_fusions, MultiplyGroupConvolutionFusion)+ ADD_MATCHER(multiply_fusions, MultiplyConvolutionBackpropDataFusion)+ ADD_MATCHER(multiply_fusions, MultiplyGroupConvolutionBackpropDataFusion)+ multiply_fusions->set_name("ov::pass::MultiplyFusions");+ REGISTER_PASS(manager, ReverseInputChannelsFusion)++ // 3. CF call due to detected perf degradations+ REGISTER_PASS(manager, ConstantFolding)+ manager.run_passes(model);++ // 4. Restore old RT info to not affect plugin compilation+ rt_info_cache.restore(model);+}++} // namespacenamespace ov {namespace preprocess {@@ -201,6 +294,18 @@ std::shared_ptr<Model> PrePostProcessor::build() {function->remove_result(*function->get_results().begin());function->add_results(results);+ // After switching from ModelOptimizer to OVC, the order of+ // applying PrePostProcessing and MOCTransformations has changed:+ //+ // MO path : [fw model conversion -> PrePostProcessing -> MOC] -> nncf+ // OVC path: [fw model conversion -> MOC] -> PrePostProcessing -> nncf+ //+ // Since nncf is applied to a not fully optimized model, extra FQ ops might appear,+ // which can affect both accuracy and performance.+ // PrePostProcessing is not part of OVC, so we have to insert an additional+ // Transformation calls inside PrePostProcessing.+ transformation_pipeline(function);+guard.reset();return function;}diff --git a/src/core/tests/preprocess.cpp b/src/core/tests/preprocess.cppindex 96f23149b6a..bea6be55393 100644--- a/src/core/tests/preprocess.cpp+++ b/src/core/tests/preprocess.cpp@@ -2,6 +2,7 @@// SPDX-License-Identifier: Apache-2.0//+#include "common_test_utils/ov_test_utils.hpp"#include "common_test_utils/test_assertions.hpp"#include "common_test_utils/test_tools.hpp"#include "gtest/gtest.h"@@ -37,6 +38,24 @@ static std::shared_ptr<Model> create_trivial(element::Type type, const PartialShreturn std::make_shared<Model>(ResultVector{res}, ParameterVector{data1});}+static std::shared_ptr<Model> create_conv(element::Type in_type, const PartialShape& shape, element::Type weight_type) {+ auto data1 = std::make_shared<op::v0::Parameter>(in_type, shape);+ data1->set_friendly_name("input1");+ data1->get_output_tensor(0).set_names({"tensor_input1"});++ std::shared_ptr<Node> weights = std::make_shared<op::v0::Constant>(weight_type, ov::Shape{1, 3, 3, 3}, 1);+ if (weight_type == element::f16) {+ // decompression subgraph+ weights = std::make_shared<op::v0::Convert>(weights, element::f32);+ }+ auto conv =+ std::make_shared<op::v1::Convolution>(data1, weights, Strides{}, CoordinateDiff{}, CoordinateDiff{}, Strides{});+ auto res = std::make_shared<op::v0::Result>(conv);+ res->set_friendly_name("Result1");+ res->get_output_tensor(0).set_names({"tensor_output1"});+ return std::make_shared<Model>(ResultVector{res}, ParameterVector{data1});+}+template <int N>static std::shared_ptr<Model> create_n_inputs(element::Type type, const PartialShape& shape) {ResultVector res;@@ -2426,3 +2445,62 @@ TEST(pre_post_process, dump_error) {auto dump = stream.str();EXPECT_TRUE(dump.find("Error occurred:") != std::string::npos) << dump;}++TEST_F(TransformationTestsF, preprocessing_mul_conv_fusion) {+ auto in_shape = Shape{1, 3, 32, 32};+ auto in_type = element::f32;+ auto weight_type = element::f32;+ {+ auto f = create_conv(in_type, in_shape, weight_type);+ auto p = PrePostProcessor(f);++ p.input().tensor().set_layout(Layout("NCHW"));+ p.input().preprocess().reverse_channels();+ p.input().preprocess().scale(255.);+ model = p.build();+ }++ {+ // we expect that MultiplyConvolutionFusion will be applied+ auto input = std::make_shared<op::v0::Parameter>(in_type, in_shape);++ auto weights = op::v0::Constant::create(element::f32, ov::Shape({1, 3, 3, 3}), {0.003922f});+ auto conv = std::make_shared<op::v1::Convolution>(input,+ weights,+ Strides{},+ CoordinateDiff{},+ CoordinateDiff{},+ Strides{});+ auto res = std::make_shared<op::v0::Result>(conv);+ model_ref = std::make_shared<ov::Model>(ResultVector{res}, ParameterVector{input});+ }+}++TEST_F(TransformationTestsF, preprocessing_conv_decompression) {+ auto in_shape = Shape{1, 3, 32, 32};+ auto in_type = element::f32;+ auto weight_type = element::f16;+ {+ auto f = create_conv(in_type, in_shape, weight_type);+ auto p = PrePostProcessor(f);++ p.input().tensor().set_layout(Layout("NCHW"));+ p.input().preprocess().reverse_channels();+ p.input().preprocess().scale(255.);+ model = p.build();+ }++ {+ // we expect that MultiplyConvolutionFusion will be applied+ auto input = std::make_shared<op::v0::Parameter>(in_type, in_shape);++ auto weights = op::v0::Constant::create(weight_type, ov::Shape({1, 3, 3, 3}), {1.f});+ auto convert = std::make_shared<op::v0::Convert>(weights, element::f32);+ auto B = op::v0::Constant::create(in_type, ov::Shape({1}), {0.003922f});+ auto mul = std::make_shared<op::v1::Multiply>(convert, B);+ auto conv =+ std::make_shared<op::v1::Convolution>(input, mul, Strides{}, CoordinateDiff{}, CoordinateDiff{}, Strides{});+ auto res = std::make_shared<op::v0::Result>(conv);+ model_ref = std::make_shared<ov::Model>(ResultVector{res}, ParameterVector{input});+ }+}